annotate lib/IMPL/DOM/Navigator/Builder.pm @ 410:9335cf010b23 ref20150831

refactoring
author cin
date Mon, 14 Sep 2015 01:11:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::DOM::Navigator::Builder;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use parent qw(IMPL::DOM::Navigator);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 require IMPL::DOM::Navigator::SchemaNavigator;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 require IMPL::DOM::Schema::ValidationError;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 use IMPL::DOM::Document;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 private _direct property _schemaNavi => PROP_RW;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 private _direct property _docClass => PROP_RW;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 public _direct property Document => PROP_RO;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 public _direct property ignoreUndefined => PROP_RO;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 'IMPL::DOM::Navigator' => sub { IMPL::DOM::Document->Empty; }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 my ($this,$docClass,$schema,%opts) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->{$_docClass} = $docClass;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $this->{$_schemaNavi} = $schema ? IMPL::DOM::Navigator::SchemaNavigator->new($schema) : undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $this->{$ignoreUndefined} = $opts{ignoreUndefined} if $opts{ignoreUndefined};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 sub NavigateCreate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my ($this,$nodeName,%props) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 if (my $schemaType = $this->{$_schemaNavi}->NavigateName($nodeName)) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my $class = $schemaType->can('nativeType') ? $schemaType->nativeType || 'IMPL::DOM::Node' : 'IMPL::DOM::Node';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my $schemaNode = $this->{$_schemaNavi}->SourceSchemaNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 $props{schemaType} = $schemaType;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 $props{schemaNode} = $schemaNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my $node;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 if (! $this->{$Document}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 # keep reference to the schema document
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 $props{schemaDocument} = $this->{$_schemaNavi}->schema;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $node = $this->{$Document} = $this->{$_docClass}->new(nodeName => $nodeName,%props);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 $this->_initNavigator($node);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 die new IMPL::InvalidOperationException('Can\'t create a second top level element') unless $this->Current;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 $node = $this->{$Document}->Create($nodeName,$class,\%props);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $this->Current->appendChild($node);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 $this->internalNavigateNodeSet($node);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 return $node;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 die new IMPL::InvalidOperationException("The specified node is undefined", $nodeName)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 if !$this->ignoreUndefined;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 sub Back {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $this->{$_schemaNavi}->SchemaBack();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $this->SUPER::Back();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 sub saveState {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 $this->{$_schemaNavi}->saveState;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 $this->SUPER::saveState;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 sub restoreState {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 $this->{$_schemaNavi}->restoreState;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 $this->SUPER::restoreState;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 sub document {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 goto &Document;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 C< IMPL::DOM::Navigator::Builder > - Навигатор, строящий документ по указанной схеме.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 my $builder = new IMPL::DOM::Navigator::Builder(new MyApp::Document,$schema);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 my $reader = new IMPL::DOM::XMLReader(Navigator => $builder);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 $reader->ParseFile("document.xml");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my @errors = $schema->Validate($builder->Document);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 Построитель DOM документов по указанной схеме. Обычно используется в связке
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 с объектами для чтения такими как C<IMPL::DOM::XMLReader>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 =head2 C< CTOR($classDocument,$schema, %opts) >
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 Создает новый объект, принимает на вход класс документа (или фабрику, например
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 L<IMPL::Object::Factory>) и схему. В процессе процедуры построения документа
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 будет создан объект документа.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 Необязательные именованные параметры
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 =item C<ignoreUndefined>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 C<NavigateCreate> не будет вызывать исключение, если запрашиваемый узел не
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 найден в схеме, но будет возвращать C<undef>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =head2 C< NavigateCreate($nodeName,%props) >
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 Создает новый узел с указанным именем и переходит в него. В случае если в схеме
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 подходящий узел не найден, то вызывается исключение или будет возвращено
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 C<undef> см. C<ignoreUndefined>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 При этом по имени узла ищется его схема, после чего определяется класс для
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 создания экземпляра узла и созданный узел доавляется в документ. При создании
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 нового узла используется метод документа C<< IMPL::DOM::Document->Create >>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 Свойства узла передаются при создании через параметр C<%props>, но имя
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 создаваемого узла НЕ может быть переопределено свойством C<nodeName>, оно будет
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 проигнорировано.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 Свойства узла будут преобразованы при помощи заданных в схеме заполнителей
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 C<inflator>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 =head2 C<[get]document >
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 Свойство, которое содержит документ по окончании процедуры построения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 =head2 C<[get]buildErrors>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 Ошибки, возникшие в процессе построения документа.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 =head2 C<[get]ignoreUndefined>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 Опция, заданная при создании построителя, отвечающая за обработку узлов
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 не найденных в схеме.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 =cut