10
|
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
|
14
|
10 __PACKAGE__->PassThroughArgs;
|
|
11
|
10
|
12 BEGIN {
|
|
13 public _direct property Navigator => prop_get | owner_set;
|
|
14 private _direct property _current => prop_all;
|
37
|
15 private _direct property _text => prop_all;
|
|
16 private _direct property _textHistory => prop_all;
|
10
|
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
|
12
|
33 sub ParseFile {
|
|
34 my ($this,$in) = @_;
|
|
35
|
|
36 my $parser = new XML::Parser(
|
|
37 Handlers => {
|
37
|
38 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
|
|
39 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
|
|
40 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
|
12
|
41 }
|
|
42 );
|
|
43
|
|
44 $parser->parsefile($in);
|
|
45 }
|
|
46
|
|
47
|
14
|
48 sub _OnBegin {
|
10
|
49 my ($this,$element,%attrs) = @_;
|
|
50
|
37
|
51 push @{$this->{$_textHistory}},$this->{$_text};
|
|
52 $this->{$_text} = "";
|
11
|
53 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
|
10
|
54 }
|
|
55
|
14
|
56 sub _OnEnd {
|
10
|
57 my ($this,$element) = @_;
|
|
58
|
37
|
59 $this->{$_current}->nodeValue($this->{$_text}) if length $this->{$_text};
|
|
60 $this->{$_text} = pop @{$this->{$_textHistory}};
|
|
61 $this->{$_current} = $this->Navigator->Back;
|
10
|
62 }
|
|
63
|
14
|
64 sub _OnChar {
|
10
|
65 my ($this,$val) = @_;
|
37
|
66 $this->{$_text} .= $val;
|
10
|
67 }
|
|
68
|
|
69 1;
|
12
|
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
|
14
|
80 =head1 DESCRIPTION
|
12
|
81
|
|
82 , .
|
|
83 .
|
|
84
|
|
85 C<NavigateCreate> C<Back>
|
|
86
|
14
|
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
|
12
|
105 =cut
|