Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Navigator/Builder.pm @ 148:e6447ad85cb4
DOM objects now have a schema and schemaSource properties
RegExp now can launder data
Improved post to DOM transformation (multiple values a now supported)
Added new axes to navigation queries: ancestor and descendant
minor changes and bug fixes
author | wizard |
---|---|
date | Mon, 16 Aug 2010 08:26:44 +0400 |
parents | 7b14e0122b79 |
children | 76515373dac0 |
rev | line source |
---|---|
49 | 1 package IMPL::DOM::Navigator::Builder; |
2 use strict; | |
3 use warnings; | |
4 | |
106 | 5 use base qw(IMPL::DOM::Navigator); |
49 | 6 use IMPL::Class::Property; |
7 use IMPL::Class::Property::Direct; | |
8 require IMPL::DOM::Navigator::SchemaNavigator; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
9 require IMPL::DOM::Schema::ValidationError; |
106 | 10 use IMPL::DOM::Document; |
49 | 11 |
12 BEGIN { | |
13 private _direct property _schemaNavi => prop_all; | |
103 | 14 private _direct property _docClass => prop_all; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
15 public _direct property BuildErrors => prop_get | prop_list; |
49 | 16 public _direct property Document => prop_get | owner_set; |
17 } | |
18 | |
106 | 19 our %CTOR = ( |
112 | 20 'IMPL::DOM::Navigator' => sub { IMPL::DOM::Document->Empty; } |
106 | 21 ); |
22 | |
49 | 23 sub CTOR { |
24 my ($this,$docClass,$schema) = @_; | |
25 | |
26 $this->{$_docClass} = $docClass; | |
27 $this->{$_schemaNavi} = $schema ? IMPL::DOM::Navigator::SchemaNavigator->new($schema) : undef; | |
28 } | |
29 | |
30 sub NavigateCreate { | |
31 my ($this,$nodeName,%props) = @_; | |
32 | |
33 if (my $schemaNode = $this->{$_schemaNavi}->NavigateName($nodeName)) { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
34 my $class = $schemaNode->can('nativeType') ? $schemaNode->nativeType || 'IMPL::DOM::Node' : 'IMPL::DOM::Node'; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
35 |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
36 my $schemaSource = $this->{$_schemaNavi}->SourceSchemaNode; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
37 |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
38 my @errors = $this->inflateProperties($schemaNode,\%props); |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
39 $props{schema} = $schemaNode; |
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
40 $props{schemaSource} = $schemaSource; |
49 | 41 |
42 my $node; | |
43 if (! $this->{$Document}) { | |
44 $node = $this->{$Document} = $this->{$_docClass}->new(nodeName => $nodeName,%props); | |
106 | 45 $this->_initNavigator($node); |
49 | 46 } else { |
106 | 47 die new IMPL::InvalidOperationException('Can\t create a second top level element') unless $this->Current; |
49 | 48 $node = $this->{$Document}->Create($nodeName,$class,\%props); |
106 | 49 $this->Current->appendChild($node); |
50 $this->internalNavigateNodeSet($node); | |
49 | 51 } |
52 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
53 if (@errors) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
54 $this->BuildErrors->Append( |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
55 map { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
56 IMPL::DOM::Schema::ValidationError->new( |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
57 Node => $node, |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
58 Source => $schemaSource, |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
59 Schema => $schemaNode, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
60 Message => $schemaNode->messageInflateError, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
61 Error => $_ |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
62 ) |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
63 } @errors |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
64 ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
65 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
66 |
49 | 67 return $node; |
68 } else { | |
69 die new IMPL::InvalidOperationException("The specified node is undefined", $nodeName); | |
70 } | |
71 } | |
72 | |
103 | 73 sub inflateProperties { |
74 my ($this,$schemaNode,$refProps) = @_; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
75 my @errors; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
76 foreach my $schemaProp ( $schemaNode->selectNodes('Property') ) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
77 next if not exists $refProps->{$schemaProp->name}; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
78 my $result = eval {$schemaProp->inflateValue($refProps->{$schemaProp->name}) }; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
79 if (my $e = $@) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
80 push @errors, $e; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
81 } else { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
82 $refProps->{$schemaProp->name} = $result; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
83 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
84 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
85 return @errors; |
103 | 86 } |
87 | |
88 sub inflateValue { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
89 my ($this,$value,$node) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
90 |
113 | 91 $node ||= $this->Current; |
92 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
93 my $nodeSchema = $this->{$_schemaNavi}->Current; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
94 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
95 my $result = eval { $nodeSchema->inflateValue($value) }; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
96 if (my $e=$@) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
97 $this->BuildErrors->Append(new IMPL::DOM::Schema::ValidationError( |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
98 Schema => $nodeSchema, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
99 Node => $node, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
100 Error => $e, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
101 Message => $nodeSchema->messageInflateError, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
102 Source => $this->{$_schemaNavi}->SourceSchemaNode |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
103 )); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
104 return $value; |
103 | 105 } else { |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
106 return $result; |
103 | 107 } |
108 } | |
109 | |
49 | 110 sub Back { |
111 my ($this) = @_; | |
112 | |
113 $this->{$_schemaNavi}->SchemaBack(); | |
106 | 114 $this->SUPER::Back(); |
49 | 115 } |
116 | |
112 | 117 sub saveState { |
118 my ($this) = @_; | |
119 | |
120 $this->{$_schemaNavi}->saveState; | |
121 $this->SUPER::saveState; | |
122 } | |
123 | |
124 sub restoreState { | |
125 my ($this) = @_; | |
126 | |
127 $this->{$_schemaNavi}->restoreState; | |
128 $this->SUPER::restoreState; | |
129 } | |
130 | |
49 | 131 1; |
132 | |
133 __END__ | |
64 | 134 |
49 | 135 =pod |
136 | |
64 | 137 =head1 NAME |
138 | |
139 C< IMPL::DOM::Navigator::Builder > - Навигатор, строящий документ по указанной схеме. | |
140 | |
49 | 141 =head1 SYNOPSIS |
142 | |
64 | 143 =begin code |
144 | |
49 | 145 my $builder = new IMPL::DOM::Navigator::Builder(new MyApp::Document,$schema); |
146 my $reader = new IMPL::DOM::XMLReader(Navigator => $builder); | |
147 | |
148 $reader->ParseFile("document.xml"); | |
149 | |
150 my @errors = $schema->Validate($builder->Document); | |
151 | |
64 | 152 =end code |
153 | |
49 | 154 =head1 DESCRIPTION |
155 | |
156 Построитель DOM документов по указанной схеме. Обычно используется в связке | |
157 с объектами для чтения такими как C<IMPL::DOM::XMLReader>. | |
158 | |
159 =head1 METHODS | |
160 | |
161 =over | |
162 | |
64 | 163 =item C< CTOR($classDocument,$schema) > |
49 | 164 |
64 | 165 Создает новый объект, принимает на вход класс документа (или фабрику, например |
166 L<IMPL::Object::Factory>) и схему. В процессе процедуры построения документа | |
167 будет создан объект документа. | |
49 | 168 |
64 | 169 =item C< NavigateCreate($nodeName,\%props) > |
49 | 170 |
171 Создает новый узел с указанным именем и переходит в него. В случае если в схеме | |
172 подходящий узел не найден, то вызывается исключение. | |
173 | |
174 При этом по имени узла ищется его схема, после чего определяется класс для | |
64 | 175 создания экземпляра и созданный узел доавляется в документ. При создании |
176 нового узла используется метод документа C<< IMPL::DOM::Document->Create >> | |
49 | 177 |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
178 Свойства узла передаются при создании через параметр C<%props>, но имя создаваемого |
64 | 179 узла НЕ может быть переопределено свойством C<nodeName>, оно будет проигнорировано. |
180 | |
181 =item C< Document > | |
182 | |
148
e6447ad85cb4
DOM objects now have a schema and schemaSource properties
wizard
parents:
113
diff
changeset
|
183 Свойство, которое содержит документ по окончании процедуры построения. |
49 | 184 |
185 =back | |
186 | |
187 =cut |