annotate Lib/IMPL/XML/SaxParser.pm @ 378:2eed076cb944

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