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