Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Navigator/SchemaNavigator.pm @ 232:5c82eec23bb6
Fixed degradations due refactoring
| author | sergey | 
|---|---|
| date | Tue, 09 Oct 2012 20:12:47 +0400 | 
| parents | 6d8092d8ce1b | 
| children | 2904da230022 | 
| rev | line source | 
|---|---|
| 49 | 1 package IMPL::DOM::Navigator::SchemaNavigator; | 
| 2 use strict; | |
| 3 use warnings; | |
| 4 | |
| 165 | 5 use parent qw(IMPL::DOM::Navigator); | 
| 49 | 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 ) { | |
| 194 | 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 { | |
| 230 | 89 return; # abort navigation | 
| 49 | 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 { | 
| 194 | 101 my ($this) = @_; | 
| 102 | |
| 103 if ($this->Current->isa('IMPL::DOM::Schema::SimpleType') or | |
| 104 $this->Current->isa('IMPL::DOM::Schema::ComplexType') | |
| 105 ) { | |
| 230 | 106 # we are redirected | 
| 194 | 107 return $this->GetNodeFromHistory(-1); | 
| 108 } else { | |
| 109 return $this->Current; | |
| 110 } | |
| 
104
 
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 | |
| 180 | 120 Помимо стандартных методов навигации позволяет переходить по элементам документа, | 
| 121 который данной схемой описывается. | |
| 49 | 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 | 
| 180 | 129 Переходит на схему узла с указанным именем. Тоесть использует свойство C<name>. | 
| 49 | 130 | 
| 
104
 
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
 
wizard 
parents: 
103 
diff
changeset
 | 
131 =item C<SchemaBack> | 
| 49 | 132 | 
| 180 | 133 Возвращается на позицию до последней операции C<NavigateName>. Данный метод нужен | 
| 134 посокольку операция навигации по элементам описываемым схемой может приводить к | |
| 135 нескольким операциям навигации по самой схеме. | |
| 49 | 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 | 
| 180 | 139 Получает схему узла из которого было выполнено перенаправление, например, C<IMPL::DOM::Schema::Node>. | 
| 140 В остальных случаях совпадает со свойством C<Current>. | |
| 
104
 
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
 
wizard 
parents: 
103 
diff
changeset
 | 
141 | 
| 49 | 142 =back | 
| 143 | |
| 144 =cut | 
