Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Navigator/SchemaNavigator.pm @ 142:4c9849f0f879
minor changes
author | wizard |
---|---|
date | Wed, 07 Jul 2010 17:30:40 +0400 |
parents | 196bf443b5e1 |
children | 76515373dac0 |
rev | line source |
---|---|
49 | 1 package IMPL::DOM::Navigator::SchemaNavigator; |
2 use strict; | |
3 use warnings; | |
4 | |
5 use base qw(IMPL::DOM::Navigator); | |
6 use IMPL::Class::Property; | |
7 use IMPL::Class::Property::Direct; | |
8 | |
9 require IMPL::DOM::Schema::ComplexType; | |
10 require IMPL::DOM::Schema::NodeSet; | |
11 require IMPL::DOM::Schema::AnyNode; | |
12 | |
13 __PACKAGE__->PassThroughArgs; | |
14 | |
15 BEGIN { | |
16 public _direct property Schema => prop_get; | |
17 private _direct property _historySteps => prop_all; | |
18 } | |
19 | |
20 sub CTOR { | |
21 my ($this,$schema) = @_; | |
22 | |
23 $this->{$Schema} = $schema; | |
24 | |
25 die new IMPL::InvalidArgumentException("A schema object is required") unless $schema->isa('IMPL::DOM::Schema'); | |
26 } | |
27 | |
28 my $schemaAnyNode = IMPL::DOM::Schema::ComplexType->new(type => '::AnyNodeType', nativeType => 'IMPL::DOM::ComplexNode')->appendRange( | |
29 IMPL::DOM::Schema::NodeSet->new()->appendRange( | |
30 IMPL::DOM::Schema::AnyNode->new() | |
31 ) | |
32 ); | |
33 | |
34 sub NavigateName { | |
35 my ($this,$name) = @_; | |
36 | |
37 die new IMPL::InvalidArgumentException('name is required') unless defined $name; | |
38 | |
39 # perform a safe navigation | |
40 #return dosafe $this sub { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
41 my $steps = 0; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
42 # if we are currently in a ComplexNode, first go to it's content |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
43 if ($this->Current->isa('IMPL::DOM::Schema::ComplexNode')) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
44 # navigate to it's content |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
45 # ComplexNode |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
46 $this->internalNavigateNodeSet($this->Current->content); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
47 $steps ++; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
48 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
49 |
49 | 50 # navigate to node |
51 if ( | |
52 my $node = $this->Navigate( sub { | |
53 $_->isa('IMPL::DOM::Schema::Node') and ( | |
54 $_->name eq $name | |
55 or | |
56 $_->nodeName eq 'AnyNode' | |
57 or | |
58 ( $_->nodeName eq 'SwitchNode' and $_->selectNodes( sub { $_->name eq $name } ) ) | |
59 ) | |
60 }) | |
61 ) { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
62 $steps ++; |
49 | 63 if ($node->nodeName eq 'AnyNode') { |
64 # if we navigate to the anynode | |
65 # assume it to be ComplexType by default | |
66 $node = $node->type ? $this->{$Schema}->resolveType($node->type) : $schemaAnyNode; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
67 $this->internalNavigateNodeSet($node); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
68 $steps ++; |
49 | 69 } elsif ($node->nodeName eq 'SwitchNode') { |
70 # if we are in the switchnode | |
71 # navigate to the target node | |
72 $node = $this->Navigate(sub { $_->name eq $name }); | |
73 $steps ++; | |
74 } | |
75 | |
76 if ($node->nodeName eq 'Node') { | |
77 # if we navigate to a reference | |
78 # resolve it | |
79 $node = $this->{$Schema}->resolveType($node->type); | |
80 $this->internalNavigateNodeSet($node); | |
81 $steps++; | |
82 } | |
83 | |
84 push @{$this->{$_historySteps}},$steps; | |
85 | |
86 # return found node schema | |
87 return $node; | |
88 } else { | |
89 return undef; # abort navigation | |
90 } | |
91 #} | |
92 } | |
93 | |
94 sub SchemaBack { | |
95 my ($this) = @_; | |
96 | |
97 $this->Back(pop @{$this->{$_historySteps}}) if $this->{$_historySteps}; | |
98 } | |
99 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
100 sub SourceSchemaNode { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
101 my ($this) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
102 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
103 if ($this->Current->isa('IMPL::DOM::Schema::SimpleType') or |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
104 $this->Current->isa('IMPL::DOM::Schema::ComplexType') |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
105 ) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
106 # we a redirected |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
107 return $this->GetNodeFromHistory(-1); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
108 } else { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
109 return $this->Current; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
110 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
111 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
112 |
49 | 113 1; |
114 __END__ | |
115 | |
116 =pod | |
117 | |
118 =head1 DESCRIPTION | |
119 | |
120 Помимо стандартных методов навигации позволяет переходить по элементам документа, | |
121 который данной схемой описывается. | |
122 | |
123 =head1 METHODS | |
124 | |
125 =over | |
126 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
127 =item C<NavigateName($name)> |
49 | 128 |
129 Переходит на схему узла с указанным именем. Тоесть использует свойство C<name>. | |
130 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
131 =item C<SchemaBack> |
49 | 132 |
133 Возвращается на позицию до последней операции C<NavigateName>. Данный метод нужен | |
134 посокольку операция навигации по элементам описываемым схемой может приводить к | |
135 нескольким операциям навигации по самой схеме. | |
136 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
137 =item C<SourceSchemaNode> |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
138 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
139 Получает схему узла из которого было выполнено перенаправление, например, C<IMPL::DOM::Schema::Node>. |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
140 В остальных случаях совпадает со свойством C<Current>. |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
141 |
49 | 142 =back |
143 | |
144 =cut |