49
|
1 package IMPL::DOM::XMLReader;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object IMPL::Object::Autofill);
|
|
6 use IMPL::Class::Property;
|
|
7 use IMPL::Class::Property::Direct;
|
|
8 use XML::Parser;
|
|
9
|
|
10 __PACKAGE__->PassThroughArgs;
|
|
11
|
|
12 BEGIN {
|
|
13 public _direct property Navigator => prop_get | owner_set;
|
|
14 private _direct property _current => prop_all;
|
|
15 private _direct property _text => prop_all;
|
|
16 private _direct property _textHistory => prop_all;
|
|
17 }
|
|
18
|
|
19 sub Parse {
|
|
20 my ($this,$in) = @_;
|
|
21
|
|
22 my $parser = new XML::Parser(
|
|
23 Handlers => {
|
|
24 Start => sub {shift; goto &OnStart($this,@_)},
|
|
25 End => sub {shift; goto &OnEnd($this,@_)},
|
|
26 Char => sub {shift; goto &OnChar($this,@_)}
|
|
27 }
|
|
28 );
|
|
29
|
|
30 $parser->parse($in);
|
|
31 }
|
|
32
|
|
33 sub ParseFile {
|
|
34 my ($this,$in) = @_;
|
|
35
|
|
36 my $parser = new XML::Parser(
|
|
37 Handlers => {
|
|
38 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
|
|
39 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
|
|
40 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
|
|
41 }
|
|
42 );
|
|
43
|
|
44 $parser->parsefile($in);
|
|
45 }
|
|
46
|
|
47
|
|
48 sub _OnBegin {
|
|
49 my ($this,$element,%attrs) = @_;
|
|
50
|
|
51 push @{$this->{$_textHistory}},$this->{$_text};
|
|
52 $this->{$_text} = "";
|
|
53 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
|
|
54 }
|
|
55
|
|
56 sub _OnEnd {
|
|
57 my ($this,$element) = @_;
|
|
58
|
|
59 $this->{$_current}->nodeValue($this->{$_text}) if length $this->{$_text};
|
|
60 $this->{$_text} = pop @{$this->{$_textHistory}};
|
|
61 $this->{$_current} = $this->Navigator->Back;
|
|
62 }
|
|
63
|
|
64 sub _OnChar {
|
|
65 my ($this,$val) = @_;
|
|
66 $this->{$_text} .= $val;
|
|
67 }
|
|
68
|
|
69 1;
|
|
70
|
|
71 __END__
|
|
72
|
|
73 =pod
|
|
74
|
|
75 =head1 SYNOPSIS
|
|
76
|
|
77 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
|
|
78 my $obj = $reader->parsefile("data.xml");
|
|
79
|
|
80 =head1 DESCRIPTION
|
|
81
|
|
82 Простой класс, использующий навигатор для постороения документа. В зависимости от
|
|
83 используемого навигатора может быть получен различный результат.
|
|
84
|
|
85 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
|
|
86
|
|
87 =head1 METHODS
|
|
88
|
|
89 =over
|
|
90
|
|
91 =item C<CTOR(Naviagtor => $builder)>
|
|
92
|
|
93 Создает новый экземпляр парсера, с указанным навигатором для построения документа
|
|
94
|
|
95 =item C<$obj->Parse($in)>
|
|
96
|
|
97 Строит документ. На вход получает либо xml строку, либо HANDLE.
|
|
98
|
|
99 =item C<$obj->ParseFile($fileName)>
|
|
100
|
|
101 Строит документ из файла с именем C<$fileName>.
|
|
102
|
|
103 =back
|
|
104
|
|
105 =cut
|