annotate Lib/IMPL/DOM/Navigator/Builder.pm @ 103:c289ed9662ca

Schema beta 2 More strict validation, support for inflating a simple nodes and properties
author wizard
date Fri, 07 May 2010 18:17:40 +0400
parents cf3b6ef2be22
children 196bf443b5e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
1 package IMPL::DOM::Navigator::Builder;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
4
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
5 use base qw(IMPL::Object);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
6 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
7 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
8 require IMPL::DOM::Navigator::SchemaNavigator;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
10 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
11 private _direct property _schemaNavi => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
12 private _direct property _nodesPath => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
13 private _direct property _nodeCurrent => prop_all;
103
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
14 private _direct property _docClass => prop_all;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
15 public _direct property Document => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
16 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
17
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
18 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
19 my ($this,$docClass,$schema) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
20
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
21 $this->{$_docClass} = $docClass;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
22 $this->{$_schemaNavi} = $schema ? IMPL::DOM::Navigator::SchemaNavigator->new($schema) : undef;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
23 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
24
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
25 sub NavigateCreate {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
26 my ($this,$nodeName,%props) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
27
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
28 if (my $schemaNode = $this->{$_schemaNavi}->NavigateName($nodeName)) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
29 my $class = $schemaNode->can('nativeType') ? $schemaNode->nativeType : 'IMPL::DOM::Node';
103
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
30 $this->inflateProperties($schemaNode,\%props);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
31
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
32 my $node;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
33 if (! $this->{$Document}) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
34 $node = $this->{$Document} = $this->{$_docClass}->new(nodeName => $nodeName,%props);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
35 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
36 die new IMPL::InvalidOperationException('Can\t create a second top level element') unless $this->{$_nodeCurrent};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
37 $node = $this->{$Document}->Create($nodeName,$class,\%props);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
38 push @{$this->{$_nodesPath}}, $this->{$_nodeCurrent};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
39 $this->{$_nodeCurrent}->appendChild($node);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
40 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
41
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
42 $this->{$_nodeCurrent} = $node;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
43
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
44 return $node;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
45 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
46 die new IMPL::InvalidOperationException("The specified node is undefined", $nodeName);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
47 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
48 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
49
103
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
50 sub inflateProperties {
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
51 my ($this,$schemaNode,$refProps) = @_;
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
52
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
53 $refProps->{$_->name} = $_->inflator->new($refProps->{$_->name})
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
54 foreach $schemaNode->selectNodes(
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
55 sub {
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
56 $_->nodeName eq 'Property' and exists $refProps->{$_->name} and $_->inflator
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
57 }
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
58 );
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
59 }
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
60
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
61 sub inflateValue {
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
62 my ($this,$value) = @_;
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
63 my $schemaNode = $this->{$_schemaNavi}->Current;
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
64 if ($schemaNode->can('inflator') and my $inflator = $schemaNode->inflator) {
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
65 return $inflator->new($value);
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
66 } else {
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
67 return $value;
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
68 }
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
69 }
c289ed9662ca Schema beta 2
wizard
parents: 102
diff changeset
70
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
71 sub Back {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
72 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
73
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
74 $this->{$_schemaNavi}->SchemaBack();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
75 $this->{$_nodeCurrent} = pop @{$this->{$_nodesPath}};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
76 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
77
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
78 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
79
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
80 __END__
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
81
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
82 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
83
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
84 =head1 NAME
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
85
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
86 C< IMPL::DOM::Navigator::Builder > - Навигатор, строящий документ по указанной схеме.
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
87
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
88 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
89
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
90 =begin code
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
91
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
92 my $builder = new IMPL::DOM::Navigator::Builder(new MyApp::Document,$schema);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
93 my $reader = new IMPL::DOM::XMLReader(Navigator => $builder);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
94
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
95 $reader->ParseFile("document.xml");
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
96
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
97 my @errors = $schema->Validate($builder->Document);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
98
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
99 =end code
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
100
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
101 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
102
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
103 Построитель DOM документов по указанной схеме. Обычно используется в связке
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
104 с объектами для чтения такими как C<IMPL::DOM::XMLReader>.
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
105
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
106 =head1 METHODS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
107
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
108 =over
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
109
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
110 =item C< CTOR($classDocument,$schema) >
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
111
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
112 Создает новый объект, принимает на вход класс документа (или фабрику, например
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
113 L<IMPL::Object::Factory>) и схему. В процессе процедуры построения документа
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
114 будет создан объект документа.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
115
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
116 =item C< NavigateCreate($nodeName,\%props) >
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
117
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
118 Создает новый узел с указанным именем и переходит в него. В случае если в схеме
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
119 подходящий узел не найден, то вызывается исключение.
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
120
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
121 При этом по имени узла ищется его схема, после чего определяется класс для
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
122 создания экземпляра и созданный узел доавляется в документ. При создании
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
123 нового узла используется метод документа C<< IMPL::DOM::Document->Create >>
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
124
64
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
125 Свойства узла передаются при создании через параметр C<props>, но имя создаваемого
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
126 узла НЕ может быть переопределено свойством C<nodeName>, оно будет проигнорировано.
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
127
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
128 =item C< Document >
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
129
259cd3df6e53 Doc generation
wizard
parents: 49
diff changeset
130 Свойство, которое содержит документ по окончании процедурв построения.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
131
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
132 =back
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
133
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 35
diff changeset
134 =cut