annotate lib/IMPL/DOM/Schema/ValidationError.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56: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::Schema::ValidationError;
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 overload
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 '""' => \&toString,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 'fallback' => 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 use IMPL::lang qw(is);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 Label => '-IMPL::DOM::Schema::Label'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 node => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 schemaNode => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 schemaType => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 parent => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 message => PROP_RO | PROP_DIRECT
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 use IMPL::Resources::Format qw(FormatMessage);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my ($this,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 $this->{$node} = $args{node};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 $this->{$schemaNode} = $args{schemaNode} if $args{schemaNode};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $this->{$schemaType} = $args{schemaType} if $args{schemaType};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 if ($args{parent}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $this->{$parent} = $args{parent};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 } elsif ($args{node}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 $this->{$parent} = $args{node}->parentNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 die new IMPL::InvalidArgumentException("A 'parent' or a 'node' parameter is required");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 if ($args{message}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 $this->{$message} = is($args{message},Label) ? $args{message}->Format(\%args) : FormatMessage($args{message}, \%args) ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 sub toString {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 (my $this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 return $this->message;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 C<IMPL::DOM::Schema::ValidationError> - Описывает ошибку в документе.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 При проверке документа на ошибки формирования возвращается массив с объектами
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 C<IMPL::DOM::Schema::ValidationError>, каждая из которых описывает одну ошибку
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 в документе.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 Часть ошибок, таких как проверка содержимого на регулярные выражения, привязаны
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 непосредственно к элементу. Но есть ошибки которые привязываются к родительскому
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 контейнеру, например отсутсвие обязательного элемента. В таком случае ошибка
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 содержит свойство C<parent> и по свойству C<source> можно определить элемент
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 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 =item C<[get] node>
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 узлы, которые не могут присутствоать в данном месте по схеме.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 Данное свойство может быть C<undef>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 =item C<[get] parent>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 Родительский узел в котором произошла ошибка. Используется в случаях, когда C<node>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 не указан, например, если по схеме должен существовать дочерний узел с определенным
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 именем, а в реальном документе его нет.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 Также это свойство может использоваться при формировании сообщения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 =item C<[get] schema>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 Схема для C<Node> или узла который должен присутсвовать если C<Node> не задан.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 =item C<[get] source>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 Схема, проверка которой привела к возникновению ошибки. Поскольку схемы могут
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 использовать ссылки, то данное свойство нужно для получения схемы узла, а не
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 схемы его типа.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 Тоесть проверка схемы узла C<IMPL::DOM::Schema::Node> приводит к проверке схемы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 типа, например, C<IMPL::DOM::Schema::ComplexType>, а свойство C<Source> будет
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 указывать именно на C<IMPL::DOM::Schema::Node>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 =item C<[get] message>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 Возвращает форматированное сообщение об ошибке.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 =item C<toString()>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 Преобразует ошибку к строке, возвращает значение свойства C<Message>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 =head1 REMARKS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 my $doc = IMPL::DOM::XMLReader->LoadDocument('data.xml');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 my $schema = IMPL::DOM::Schema->LoadSchema('schema.xml');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 my @errors = $schema->Validate($doc);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 my $node = $doc->selectSingleNode('user','name');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 # Получаем все ошибки относящиеся к данному узлу
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 my @nodeErrors = grep { ($_->node || $_->parent) == $node } @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 =cut