Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Navigator/SchemaNavigator.pm @ 363:d67e45705be5
sync, model metadata
| author | cin |
|---|---|
| date | Fri, 29 Nov 2013 11:04:19 +0400 |
| parents | 70658970af15 |
| children | 010ceafd0c5a |
| 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 |
| 49 | 117 1; |
| 118 __END__ | |
| 119 | |
| 120 =pod | |
| 121 | |
| 122 =head1 DESCRIPTION | |
| 123 | |
| 180 | 124 Помимо стандартных методов навигации позволяет переходить по элементам документа, |
| 125 который данной схемой описывается. | |
| 49 | 126 |
| 127 =head1 METHODS | |
| 128 | |
| 129 =over | |
| 130 | |
|
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
131 =item C<NavigateName($name)> |
| 49 | 132 |
| 180 | 133 Переходит на схему узла с указанным именем. Тоесть использует свойство C<name>. |
| 49 | 134 |
|
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
135 =item C<SchemaBack> |
| 49 | 136 |
| 180 | 137 Возвращается на позицию до последней операции C<NavigateName>. Данный метод нужен |
| 138 посокольку операция навигации по элементам описываемым схемой может приводить к | |
| 139 нескольким операциям навигации по самой схеме. | |
| 49 | 140 |
|
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
141 =item C<SourceSchemaNode> |
|
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
142 |
| 180 | 143 Получает схему узла из которого было выполнено перенаправление, например, C<IMPL::DOM::Schema::Node>. |
| 144 В остальных случаях совпадает со свойством C<Current>. | |
|
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
145 |
| 49 | 146 =back |
| 147 | |
| 148 =cut |
