annotate lib/IMPL/XML/SaxParser.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::XML::SaxParser;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 XMLReader => 'XML::LibXML::Reader',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 base => [ 'IMPL::Object' => undef ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 props => [ _reader => PROP_RW ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 XMLReader->import;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 sub Parse {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 my ( $this, $options ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 my $reader = $this->_reader( XMLReader->new($options) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 if ( $reader->read() > 0) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->ProcessRootNode($reader);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 sub ProcessRootNode {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my ( $this, $reader ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 warn "not implemented";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 sub ReadChildren {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my ( $this, $handler ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my $reader = $this->_reader;
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 if($reader->nodeType == XML_READER_TYPE_ELEMENT && !$reader->isEmptyElement) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 # нужно прочитать все, что ниже, для этого запоминаем текущий уровень
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my $currentLevel = $reader->depth;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 # при чтении и проверке данного условия "съедается" закрывающий теэг текущего узла
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 while($reader->read && $reader->depth > $currentLevel) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 # при обходе дочерних узлов нужно пропустить закрывающие узлы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 $this->$handler($reader)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 if $handler and $reader->nodeType != XML_READER_TYPE_END_ELEMENT;
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 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 sub ReadTextNode {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 my $text = "";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my $handler;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 $handler = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my ( $me, $reader ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 if ( $reader->nodeType == XML_READER_TYPE_TEXT ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 $text .= $reader->value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 $this->ReadChildren($handler);
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $this->ReadChildren($handler);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 return $text;
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 sub ReadComplexContent {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 goto &ReadComplexNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 sub ReadComplexNode {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 my ( $this, $schema ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 if ( ref $schema eq 'HASH' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 my %data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 my ($handlers,$aliases);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 while(my ($selector,$handler) = each %$schema) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 my ($alias,$node) = split /:/, $selector;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 $node ||= $alias;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 $handlers->{$node} = $handler;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $aliases->{$node} = $alias;
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 $this->ReadChildren(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 my ( $me, $node ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 my $name = $node->localName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 my $alias = $aliases->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 if ( my $handler = $handlers->{$name} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 if (ref $handler eq 'ARRAY') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 push @{$data{$alias}}, $me->ReadComplexNode($$handler[0]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 $data{$alias} = $me->ReadComplexNode($handler);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 $me->ReadChildren();
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 return \%data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 elsif ( ref $schema eq 'CODE' or not ref $schema ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 return $this->$schema($this->_reader);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 die ArgException->new( schema => 'An invalid schema is supplied' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 sub attribute {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 shift->_reader->getAttribute(shift);
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head2 ReadComplexNode($schema)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 comments => sub { shift->ReadTextNode },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 data => [ {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 location => sub { $_[1]->getAttribute('href')} ,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 timestamp => 'ReadTextNode'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 } ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 =cut