18
|
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
|
24
|
9 require IMPL::DOM::Schema::ComplexType;
|
|
10 require IMPL::DOM::Schema::NodeSet;
|
|
11 require IMPL::DOM::Schema::AnyNode;
|
|
12
|
18
|
13 __PACKAGE__->PassThroughArgs;
|
|
14
|
|
15 BEGIN {
|
|
16 public _direct property Schema => prop_get;
|
24
|
17 private _direct property _historySteps => prop_all;
|
18
|
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
|
24
|
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) = @_;
|
18
|
36
|
34
|
37 die new IMPL::InvalidArgumentException('name is required') unless defined $name;
|
|
38
|
24
|
39 # perform a safe navigation
|
35
|
40 #return dosafe $this sub {
|
24
|
41 my $steps = 1;
|
|
42 # navigate to node
|
|
43 if (
|
|
44 my $node = $this->Navigate( sub {
|
|
45 $_->isa('IMPL::DOM::Schema::Node') and (
|
|
46 $_->name eq $name
|
|
47 or
|
|
48 $_->nodeName eq 'AnyNode'
|
|
49 or
|
|
50 ( $_->nodeName eq 'SwitchNode' and $_->selectNodes( sub { $_->name eq $name } ) )
|
|
51 )
|
|
52 })
|
|
53 ) {
|
|
54 if ($node->nodeName eq 'AnyNode') {
|
|
55 # if we navigate to the anynode
|
|
56 # assume it to be ComplexType by default
|
|
57 $node = $node->type ? $this->{$Schema}->resolveType($node->type) : $schemaAnyNode;
|
|
58 } elsif ($node->nodeName eq 'SwitchNode') {
|
|
59 # if we are in the switchnode
|
|
60 # navigate to the target node
|
|
61 $node = $this->Navigate(sub { $_->name eq $name });
|
|
62 $steps ++;
|
|
63 }
|
|
64
|
|
65 if ($node->nodeName eq 'Node') {
|
|
66 # if we navigate to a reference
|
|
67 # resolve it
|
|
68 $node = $this->{$Schema}->resolveType($node->type);
|
|
69 $this->internalNavigateNodeSet($node);
|
|
70 $steps++;
|
|
71 }
|
|
72
|
|
73 # if target node is a complex node
|
|
74 if ($node->isa('IMPL::DOM::Schema::ComplexNode')) {
|
|
75 # navigate to it's content
|
|
76 $this->internalNavigateNodeSet($node->content);
|
|
77 $steps ++;
|
|
78 }
|
|
79
|
|
80 push @{$this->{$_historySteps}},$steps;
|
|
81
|
|
82 # return found node schema
|
|
83 return $node;
|
|
84 } else {
|
|
85 die; # abort navigation
|
18
|
86 }
|
35
|
87 #}
|
18
|
88 }
|
|
89
|
24
|
90 sub SchemaBack {
|
|
91 my ($this) = @_;
|
|
92
|
|
93 $this->Back(pop @{$this->{$_historySteps}}) if $this->{$_historySteps};
|
|
94 }
|
|
95
|
18
|
96 1;
|
|
97 __END__
|
|
98
|
|
99 =pod
|
|
100
|
|
101 =head1 DESCRIPTION
|
|
102
|
24
|
103 ,
|
|
104 .
|
|
105
|
|
106 =head1 METHODS
|
|
107
|
|
108 =over
|
|
109
|
|
110 =item C<< $navi->NavigateName($name) >>
|
|
111
|
34
|
112 . C<name>.
|
|
113 , ,
|
|
114 .
|
24
|
115
|
|
116 =item C<< $navi->SchemaBack >>
|
|
117
|
|
118 C<NavigateName>.
|
|
119
|
|
120 .
|
|
121
|
|
122 =back
|
18
|
123
|
|
124 =cut |