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