Lite/Tree.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 247 CC 19 SCAR 30.1
CC
19
Cov
0%
mark_namespaces
56.0
strip_white
30.0
cleanup
20.0
CRAP 20.3
1 package XML::Parser::Lite::Tree;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 use XML::Parser::LiteCopy;
7  
8 our $VERSION = '0.14';
9  
10 use vars qw( $parser );
11  
12 sub instance {
13 return $parser if $parser;
14 $parser = __PACKAGE__->new;
15 }
16  
17 sub new {
18 my $class = shift;
19 my $self = bless {}, $class;
20  
21 my %opts = (ref $_[0]) ? ((ref $_[0] eq 'HASH') ? %{$_[0]} : () ) : @_;
22 $self->{opts} = \%opts;
23  
24 $self->{__parser} = new XML::Parser::LiteCopy
25 Handlers => {
26 Start => sub { $self->_start_tag(@_); },
27 Char => sub { $self->_do_char(@_); },
28 CData => sub { $self->_do_cdata(@_); },
29 End => sub { $self->_end_tag(@_); },
30 Comment => sub { $self->_do_comment(@_); },
31 PI => sub { $self->_do_pi(@_); },
32 Doctype => sub { $self->_do_doctype(@_); },
33 };
34 $self->{process_ns} = $self->{opts}->{process_ns} || 0;
35 $self->{skip_white} = $self->{opts}->{skip_white} || 0;
36  
37 return $self;
38 }
39  
40 sub parse {
41 my ($self, $content) = @_;
42  
43 my $root = {
44 'type' => 'root',
45 'children' => [],
46 };
47  
48 $self->{tag_stack} = [$root];
49  
50 $self->{__parser}->parse($content);
51  
52 $self->cleanup($root);
53  
54 if ($self->{skip_white}){
55 $self->strip_white($root);
56 }
57  
58 if ($self->{process_ns}){
59 $self->{ns_stack} = {};
60 $self->mark_namespaces($root);
61 }
62  
63 return $root;
64 }
65  
66 sub _start_tag {
67 my $self = shift;
68 shift;
69  
70 my $new_tag = {
71 'type' => 'element',
72 'name' => shift,
73 'attributes' => {},
74 'children' => [],
75 };
76  
77 while (my $a_name = shift @_){
78 my $a_value = shift @_;
79 $new_tag->{attributes}->{$a_name} = $a_value;
80 }
81  
82 push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
83 push @{$self->{tag_stack}}, $new_tag;
84 1;
85 }
86  
87 sub _do_char {
88 my $self = shift;
89 shift;
90  
91 for my $content(@_){
92  
93 my $new_tag = {
94 'type' => 'text',
95 'content' => $content,
96 };
97  
98 push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
99 }
100 1;
101 }
102  
103 sub _do_cdata {
104 my $self = shift;
105 shift;
106  
107 for my $content(@_){
108  
109 my $new_tag = {
110 'type' => 'cdata',
111 'content' => $content,
112 };
113  
114 push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
115 }
116 1;
117 }
118  
119 sub _end_tag {
120 my $self = shift;
121  
122 pop @{$self->{tag_stack}};
123 1;
124 }
125  
126 sub _do_comment {
127 my $self = shift;
128 shift;
129  
130 for my $content(@_){
131  
132 my $new_tag = {
133 'type' => 'comment',
134 'content' => $content,
135 };
136  
137 push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
138 }
139 1;
140 }
141  
142 sub _do_pi {
143 my $self = shift;
144 shift;
145  
146 push @{$self->{tag_stack}->[-1]->{children}}, {
147 'type' => 'pi',
148 'content' => shift,
149 };
150 1;
151 }
152  
153 sub _do_doctype {
154 my $self = shift;
155 shift;
156  
157 push @{$self->{tag_stack}->[-1]->{children}}, {
158 'type' => 'dtd',
159 'content' => shift,
160 };
161 1;
162 }
163  
164 sub mark_namespaces {
165 my ($self, $obj) = @_;
166  
167 my @ns_keys;
168  
169 #
170 # mark
171 #
172  
173 if ($obj->{type} eq 'element'){
174  
175 #
176 # first, add any new NS's to the stack
177 #
178  
179 my @keys = keys %{$obj->{attributes}};
180  
181 for my $k(@keys){
182  
183 if ($k =~ /^xmlns:(.*)$/){
184  
185 push @{$self->{ns_stack}->{$1}}, $obj->{attributes}->{$k};
186 push @ns_keys, $1;
187 delete $obj->{attributes}->{$k};
188 }
189  
190 if ($k eq 'xmlns'){
191  
192 push @{$self->{ns_stack}->{__default__}}, $obj->{attributes}->{$k};
193 push @ns_keys, '__default__';
194 delete $obj->{attributes}->{$k};
195 }
196 }
197  
198  
199 #
200 # now - does this tag have a NS?
201 #
202  
203 if ($obj->{name} =~ /^(.*?):(.*)$/){
204  
205 $obj->{local_name} = $2;
206 $obj->{ns_key} = $1;
207 $obj->{ns} = $self->{ns_stack}->{$1}->[-1];
208 }else{
209 $obj->{local_name} = $obj->{name};
210 $obj->{ns} = $self->{ns_stack}->{__default__}->[-1];
211 }
212  
213  
214 #
215 # finally, add xpath-style namespace nodes
216 #
217  
218 $obj->{namespaces} = {};
219  
220 for my $key (keys %{$self->{ns_stack}}){
221  
222 if (scalar @{$self->{ns_stack}->{$key}}){
223  
224 my $uri = $self->{ns_stack}->{$key}->[-1];
225 $obj->{namespaces}->{$key} = $uri;
226 }
227 }
228 }
229  
230  
231 #
232 # descend
233 #
234  
235 if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
236  
237 for my $child (@{$obj->{children}}){
238  
239 $self->mark_namespaces($child);
240 }
241 }
242  
243  
244 #
245 # pop from stack
246 #
247  
248 for my $k (@ns_keys){
249 pop @{$self->{ns_stack}->{$k}};
250 }
251 }
252  
253 sub strip_white {
254 my ($self, $obj) = @_;
255  
256 if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
257  
258 my $new_kids = [];
259  
260 for my $child (@{$obj->{children}}){
261  
262 if ($child->{type} eq 'text'){
263  
264 if ($child->{content} =~ m/\S/){
265  
266 push @{$new_kids}, $child;
267 }
268  
269 }elsif ($child->{type} eq 'element'){
270  
271 $self->strip_white($child);
272 push @{$new_kids}, $child;
273 }else{
274 push @{$new_kids}, $child;
275 }
276 }
277  
278 $obj->{children} = $new_kids;
279 }
280 }
281  
282 sub cleanup {
283 my ($self, $obj) = @_;
284  
285 #
286 # cleanup PIs
287 #
288  
289 if ($obj->{type} eq 'pi'){
290  
291 my ($x, $y) = split /\s+/, $obj->{content}, 2;
292 $obj->{target} = $x;
293 $obj->{content} = $y;
294 }
295  
296  
297 #
298 # cleanup DTDs
299 #
300  
301 if ($obj->{type} eq 'dtd'){
302  
303 my ($x, $y) = split /\s+/, $obj->{content}, 2;
304 $obj->{name} = $x;
305 $obj->{content} = $y;
306 }
307  
308  
309 #
310 # recurse
311 #
312
313 if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
314  
315 for my $child (@{$obj->{children}}){
316  
317 $self->cleanup($child);
318 }
319 }
320 }
321  
322  
323 1;
324 __END__