blib/lib/Data/Dump/XML/Parser.pm

filter: Statementstmt 94.0% 111 / 118 Branchbran 67.7% 42 / 62 Conditioncond 44.4% 12 / 27 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 9 / 9 Podpod 83.3% 5 / 6 totaltotal 75.7% 181 / 239 CC 44 SCAR 33.3
CC
44
Cov
80%
start_element
42.5
end_element
14.9
characters
2.1
CRAP 27.9
« blib/lib/Data/Dump/XML.pm ↑ Summary XML.xs »
1 package Data::Dump::XML::Parser;
2 #
3 # Once upon a time this file was a part of ACIS software,
4 # http://acis.openlib.org/
5 #
6 # Description:
7 #
8 # Parse an Data::Dump::XML-dumped XML string and recreate the
9 # data structure.
10 #
11 # This module is tightly related to Data::Dump::XML, which is
12 # based on Data::DumpXML, and it is accordingly based on
13 # Data::DumpXML::Parser.
14 #
15 # Copyright 2004-2009 Ivan Baktsheev
16 # Copyright 2003 Ivan Baktsheev, Ivan Kurmanov
17 # Copyright 1998-2003 Gisle Aas.
18 # Copyright 1996-1998 Gurusamy Sarathy.
19 #
20 # XXX use of GNU GPL here is questionable.
21 #
22 # This program is free software; you can redistribute it and/or modify
23 # it under the terms of the GNU General Public License, version 2, as
24 # published by the Free Software Foundation.
25 #
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the Free Software
33 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #
35 # ---
36 # $Id: Parser.pm,v 1.16 2009/06/07 20:22:56 apla Exp $
37 # ---
38  
39 5 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 5 ▶use base qw(XML::LibXML::SAX);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 5 ▶use Data::Dump::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 5 our $VERSION = 1.19;
46  
47 # require XSLoader;
48 # XSLoader::load ('Data::Dump::XML::Parser', $VERSION);
49  
50 #*characters = \&Data::Dump::XML::characters;
51  
52 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
53 3 my($class, %arg) = @_;
54
55 3 ▶ Data::Dump::XML->new
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Data::Dump::XML::INSTANCE 3 0 3
56 unless defined $Data::Dump::XML::INSTANCE;
57
58 3 $arg{defaults} = {%$Data::Dump::XML::defaults};
59
60 3 return bless \%arg, $class;
61 }
62  
63 16 ▶sub start_document {
Subroutine start_document: called CC=1 SCAR=0.0
Pod documented
64 16 my $p = shift;
65
66 # real restored data
67 16 $p->{data} = undef;
68
69 # data stack. used for control child/parent axe
70 16 $p->{stack} = [];
71
72 16 push @{$p->{stack}}, \$p->{data};
73
74 # current depth
75 16 $p->{depth} = 0;
76 }
77  
78 3937 ▶sub start_element {
Subroutine start_element: called CC=29 SCAR=37.5
Pod documented
79 3937 my ($p, $element) = @_;
80
81 3937 my $d = $p->{defaults};
82
83 485 my %attr = map {$_->{LocalName} => $_->{Value}}
84 3937 values %{$element->{Attributes}};
85 3937 my $tag = $element->{LocalName};
86
87 3937 my $depth = \$p->{depth};
88 3937 $$depth++;
89
90 3937 $p->{max_depth} = $$depth;
91
92 3937 ▶ if ($$depth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$depth == 1) 16 3921 3937
93
94 16 $d->{'root_name'} = $tag;
95
96 16 foreach (qw(ref_element hash_element array_element empty_array
97 empty_hash undef key_as_hash_element at_key_as_attribute hash_element_attribute_name)
98 ) {
99 $d->{$_} = delete $attr{"_$_"}
100 144 ▶ if exists $attr{"_$_"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"_$_"} 0 144 144
101 }
102
103 }
104
105 3937 my $key_as_hash_element = $d->{'key_as_hash_element'};
106 3937 my $root_name = $d->{'root_name'};
107 3937 my $ref_element = $d->{'ref_element'};
108 3937 my $array_element = $d->{'array_element'};
109 3937 my $hash_element = $d->{'hash_element'};
110 3937 my $empty_array = $d->{'empty_array'};
111 3937 my $undef = $d->{'undef'};
112 3937 my $empty_hash = $d->{'empty_hash'};
113
114 3937 my $blesser;
115 $blesser = $p->{Blesser}
116 3937 ▶ if (exists $p->{Blesser} and ref($blesser) eq "CODE");
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"Blesser"} and ref $blesser eq "CODE" 0 3937 3937
Condition33%1/3
exists $p->{"Blesser"} and ref $blesser eq "CODE"
!ll&&!rl&&r
393700
MC/DC0%0/2
exists $p->{"Blesser"} and ref $blesser eq "CODE"
exists $p->{"Blesser"} ref $blesser eq "CODE"
Truth table33%1/3
A and B
A= exists $p->{"Blesser"}B= ref $blesser eq "CODE"
ABresult
0X0
100
111
117
118 3937 my $parent_attr = $p->{attr}->[-1];
119 3937 my $parent_class = delete $parent_attr->{_class};
120 3937 my $parent_id = delete $parent_attr->{_id};
121
122 3937 my $ref = $p->{'stack'}->[-1];
123
124 #my $defined_parent = 0;
125 #$defined_parent = 1
126 # if ref $p->{'stack'}->[-1] eq 'SCALAR'
127 # and not defined ${$p->{'stack'}->[-1]};
128
129 3937 push (@{$p->{'attr'}}, \%attr);
130
131 3937 ▶ if ($$depth == 1) {# and not defined $ref) {
Branch: 7 branches
BranchTrueFalseTotal
if ($$depth == 1) { } 16 3921 3937
elsif ($tag eq $array_element) { } 455 3466 3921
elsif ($tag eq $ref_element) { } 0 3466 3466
elsif ($tag eq $undef) { } 48 3418 3466
elsif ($tag eq $empty_hash) { } 48 3370 3418
elsif ($tag eq $empty_array) { } 60 3310 3370
elsif ($key_as_hash_element or $tag eq $hash_element and exists $attr{$d->{'hash_element_attribute_name'}}) { } 3310 0 3310
132 # root element
133 } elsif ($tag eq $array_element) {
134 #$$ref = []
135 # if $defined_parent;
136
137 ### check the data type
138 455 ▶ die "'$tag' elements only appear in list elements"
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ref and &Data::Dump::XML::reftype($$ref) ne "ARRAY" 0 455 455
Condition66%2/3
defined $$ref and &Data::Dump::XML::reftype($$ref) ne "ARRAY"
!ll&&!rl&&r
633920
MC/DC0%0/2
defined $$ref and &Data::Dump::XML::reftype($$ref) ne "ARRAY"
defined $$ref &Data::Dump::XML::reftype($$ref) ne "ARRAY"
Truth table66%2/3
A and B
A= defined $$refB= &Data::Dump::XML::reftype($$ref) ne "ARRAY"
ABresult
0X0
100
111
139 if defined $$ref and Data::Dump::XML::reftype ($$ref) ne 'ARRAY';
140
141 455 push @{$$ref}, undef;
142 455 push @{$p->{'stack'}}, \($$ref->[-1]);
143
144 455 ▶ $blesser ? &$blesser ($$ref, $parent_class) : bless ($$ref, $parent_class)
Branch: 2 branches
BranchTrueFalseTotal
if defined $parent_class 0 455 455
$blesser ? : 0 0 0
145 if defined $parent_class;
146  
147
148 } elsif ($tag eq $ref_element) {
149 0 my $value = undef;
150 0 $$ref = \$value;
151
152 0 $$ref = ${$p->{'id'}->[$attr{'to'}]}
153 0 ▶ if (defined $attr{'to'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $attr{"to"} 0 0 0
154
155 0 push @{$p->{'stack'}}, $$ref;
156
157 } elsif ($tag eq $undef) {
158
159 48 $$ref = undef;
160 48 push @{$p->{'stack'}}, undef;
161
162 } elsif ($tag eq $empty_hash) {
163
164 48 $$ref = {};
165 48 push @{$p->{'stack'}}, undef;
166
167 } elsif ($tag eq $empty_array) {
168
169 60 $$ref = [];
170 60 push @{$p->{'stack'}}, undef;
171
172 ▶ } elsif ($key_as_hash_element or ($tag eq $hash_element and exists $attr{$d->{hash_element_attribute_name}})) {
Condition16%1/6
$key_as_hash_element or $tag eq $hash_element and exists $attr{$d->{"hash_element_attribute_name"}}
l!l&&r!l&&!r
331000
$tag eq $hash_element and exists $attr{$d->{"hash_element_attribute_name"}}
!ll&&!rl&&r
000
MC/DC0%0/3
$key_as_hash_element or $tag eq $hash_element and exists $attr{$d->{"hash_element_attribute_name"}}
$key_as_hash_element $tag eq $hash_element exists $attr{$d->{"hash_element_attribute_name"}}
Truth table25%1/4
A or B and C
A= $key_as_hash_elementB= $tag eq $hash_elementC= exists $attr{$d->{"hash_element_attribute_name"}}
ABCresult
1XX1
0111
00X0
0100
173 #$$ref = {}
174 # if $defined_parent;
175
176 3310 my $key = $tag;
177 $key = delete $attr{$d->{hash_element_attribute_name}}
178 3310 ▶ if exists $attr{$d->{hash_element_attribute_name}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{$d->{"hash_element_attribute_name"}} 0 3310 3310
179
180 3310 ▶ die "hash element '$key' must appear in hash context"
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ref and &Data::Dump::XML::reftype($$ref) ne "HASH" 0 3310 3310
Condition66%2/3
defined $$ref and &Data::Dump::XML::reftype($$ref) ne "HASH"
!ll&&!rl&&r
60527050
MC/DC0%0/2
defined $$ref and &Data::Dump::XML::reftype($$ref) ne "HASH"
defined $$ref &Data::Dump::XML::reftype($$ref) ne "HASH"
Truth table66%2/3
A and B
A= defined $$refB= &Data::Dump::XML::reftype($$ref) ne "HASH"
ABresult
0X0
100
111
181 if defined $$ref and Data::Dump::XML::reftype ($$ref) ne 'HASH';
182
183 3310 ▶ unless (defined $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$ref) 605 2705 3310
184 # copy all attributes except _*
185 605 foreach my $k (keys %$parent_attr) {
186 # next if substr ($k, 0, 1) eq '_';
187 469 $$ref->{"\@$k"} = $parent_attr->{$k};
188 }
189 }
190
191 die "hash element '$key' already present"
192 3310 ▶ if exists $$ref->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ref->{$key} 0 3310 3310
193 3310 $$ref->{$key} = undef;
194
195 3310 push @{$p->{stack}}, \(${$ref}->{$key});
196
197 3310 ▶ $blesser ? &$blesser ($$ref, $parent_class) : bless ($$ref, $parent_class)
Branch: 2 branches
BranchTrueFalseTotal
if defined $parent_class 1 3309 3310
$blesser ? : 0 1 1
198 if defined $parent_class;
199 } else {
200 0 warn "found unknown element $tag";
201 }
202
203 # mix of pcdata and elements not allowed, ignore chars
204 3937 $p->{char} = '';
205
206 3937 ▶ $p->{id}->[$parent_id] = $ref
Branch: 1 branch
BranchTrueFalseTotal
if $parent_id 0 3937 3937
207 if ($parent_id);
208 }
209  
210 7673 ▶sub characters {
Subroutine characters: called CC=2 SCAR=7.2
Pod documented
211 7673 my ($p, $str) = @_;
212 $p->{'char'} .= $str->{'Data'}
213 7673 ▶ if defined $str->{'Data'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $str->{"Data"} 7673 0 7673
214 }
215  
216 3937 ▶sub end_element {
Subroutine end_element: called CC=14 SCAR=27.0
Pod documented
217 3937 my ($p, $element) = @_;
218
219 3937 my $d = $p->{defaults};
220
221 3937 my $tag = $element->{'LocalName'};
222
223 3937 my $depth = \$p->{depth};
224 3937 my $str = $p->{'char'};
225 3937 my $ref = pop @{$p->{'stack'}};
226
227 3937 $p->{'char'} = '';
228
229 3937 my $attr = pop @{$p->{'attr'}};
230 3937 my $attributed_keys = {map {$_ => $attr->{$_}} grep {!/^_/} keys %$attr};
231
232 3937 my $has_attrs = scalar keys %$attributed_keys;
233
234 3937 ▶ if( $$depth < $p->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$depth < $p->{'max_depth'}) { } 824 3113 3937
235 #print ' 'x $$depth, "- this element had children\n";
236 } else {
237 # here processing for empty tags
238
239 #my $key = $tag;
240 #$key = delete $attr{$d->{hash_element_attribute_name}}
241 # if exists $attr{$d->{hash_element_attribute_name}};
242  
243  
244 3113 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($has_attrs) { } 14 3099 3113
elsif ($tag ne $d->{'undef'}) { } 3051 48 3099
245 # ($tag eq $d->{array_element} or $tag eq $d->{hash_element}) and
246 $has_attrs
247 ) {
248 14 ▶ $$ref->{'#text'} = $str
Branch: 1 branch
BranchTrueFalseTotal
if defined $str and $str ne "" 12 2 14
Condition66%2/3
defined $str and $str ne ""
!ll&&!rl&&r
0212
MC/DC50%1/2
defined $str and $str ne ""
defined $str $str ne ""
Truth table66%2/3
A and B
A= defined $strB= $str ne ""
ABresult
0X0
100
111
249 if defined $str and $str ne '';
250
251 14 foreach my $k (keys %$attributed_keys) {
252 14 $$ref->{"\@$k"} = $attributed_keys->{$k};
253 }
254
255 } elsif ($tag ne $d->{'undef'}) {
256 3051 ▶ if ($tag eq $d->{ref_element} and $attr->{'to'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq $d->{'ref_element'} and $attr->{'to'}) { } 0 3051 3051
Condition33%1/3
$tag eq $d->{"ref_element"} and $attr->{"to"}
!ll&&!rl&&r
305100
MC/DC0%0/2
$tag eq $d->{"ref_element"} and $attr->{"to"}
$tag eq $d->{"ref_element"} $attr->{"to"}
Truth table33%1/3
A and B
A= $tag eq $d->{"ref_element"}B= $attr->{"to"}
ABresult
0X0
100
111
257 # print "'", $p->{'attr'}->[0]->{'to'}, "'\n";
258 # my $place = $p->{'attr'}->[0]->{'to'};
259 #
260 # $$ref = ${$p->{'id'}->[$place]}
261 # if (defined $place);
262
263 } else {
264 #print ' 'x $$depth, "element '$tag' holds a string value ('$str')\n";
265 3051 $$ref = $str;
266 }
267 }
268  
269 3113 my $class = $attr->{_class};
270
271 3113 my $blesser;
272 $blesser = $p->{Blesser}
273 3113 ▶ if (exists $p->{Blesser} and ref ($blesser) eq "CODE");
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"Blesser"} and ref $blesser eq "CODE" 0 3113 3113
Condition33%1/3
exists $p->{"Blesser"} and ref $blesser eq "CODE"
!ll&&!rl&&r
311300
MC/DC0%0/2
exists $p->{"Blesser"} and ref $blesser eq "CODE"
exists $p->{"Blesser"} ref $blesser eq "CODE"
Truth table33%1/3
A and B
A= exists $p->{"Blesser"}B= ref $blesser eq "CODE"
ABresult
0X0
100
111
274  
275 3113 ▶ $blesser ? &$blesser ($$ref, $class) : bless ($$ref, $class)
Branch: 2 branches
BranchTrueFalseTotal
if defined $class and ref $$ref 1 3112 3113
$blesser ? : 0 1 1
Condition66%2/3
defined $class and ref $$ref
!ll&&!rl&&r
311201
MC/DC50%1/2
defined $class and ref $$ref
defined $class ref $$ref
Truth table66%2/3
A and B
A= defined $classB= ref $$ref
ABresult
0X0
100
111
276 if defined $class and ref $$ref;
277  
278 }
279
280 3937 $$depth--;
281 }
282  
283 16 ▶sub end_document {
Subroutine end_document: called CC=1 SCAR=0.0
Pod documented
284 16 my $p = shift;
285 16 my $data = $p->{'data'};
286
287 16 return $data;
288 }
289  
290 5 1;
291  
292 __END__
« blib/lib/Data/Dump/XML.pm ↑ Summary XML.xs »