annotate lib/IMPL/DOM/XMLReader.pm @ 408:5c80e33f1218 ref20150831

added 'coarsen' function
author cin
date Mon, 07 Sep 2015 01:35:25 +0300
parents c6e90e02dd17
children ee36115f6a34
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::XMLReader;
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 parent qw(IMPL::Object IMPL::Object::Autofill);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use XML::Parser;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 Schema => 'IMPL::DOM::Schema', # IMPL::DOM::Schema references IMPL::DOM::XML::Reader
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 Builder => 'IMPL::DOM::Navigator::Builder',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 SimpleBuilder => 'IMPL::DOM::Navigator::SimpleBuilder'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 public _direct property Navigator => prop_get | owner_set;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 public _direct property SkipWhitespace => prop_get | owner_set;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 private _direct property _current => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 private _direct property _text => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 private _direct property _textHistory => prop_all;
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 sub Parse {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my ($this,$in) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my $parser = new XML::Parser(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 Handlers => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 Start => sub {shift; goto &OnStart($this,@_)},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 End => sub {shift; goto &OnEnd($this,@_)},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 Char => sub {shift; goto &OnChar($this,@_)}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 $parser->parse($in);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 sub ParseFile {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my ($this,$in) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my $parser = new XML::Parser(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 Handlers => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 $parser->parsefile($in);
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 sub _OnBegin {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 my ($this,$element,%attrs) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 push @{$this->{$_textHistory}},$this->{$_text};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 $this->{$_text} = "";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 sub _OnEnd {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my ($this,$element) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 $this->{$_current}->nodeValue($this->{$_text}) if length $this->{$_text} and (not $this->{$SkipWhitespace} or $this->{$_text} =~ /\S/);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $this->{$_text} = pop @{$this->{$_textHistory}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 $this->{$_current} = $this->Navigator->Back;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 sub _OnChar {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 my ($this,$val) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $this->{$_text} .= $val;
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 sub LoadDocument {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 my ($self,$file,$schema) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 my $parser;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 if ($schema) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 $parser = $self->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 Navigator => IMPL::DOM::Navigator::Builder->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 'IMPL::DOM::Document',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 $schema
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 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 $parser = $self->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 $parser->ParseFile($file);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 my $doc = $parser->Navigator->Document;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 my @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 if ($schema) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 push @errors, $schema->Validate($doc);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 return $doc,\@errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 return $doc;
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 my $obj = $reader->parsefile("data.xml");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 Простой класс, использующий навигатор для постороения документа. В зависимости от
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 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 =head1 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =item C<CTOR(Naviagtor => $builder)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 Создает новый экземпляр парсера, с указанным навигатором для построения документа
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =item C<$obj->Parse($in)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 Строит документ. На вход получает либо xml строку, либо HANDLE.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =item C<$obj->ParseFile($fileName)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 Строит документ из файла с именем C<$fileName>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 =cut