blib/lib/XML/CompactTree.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 72.4% 42 / 58 Conditioncond 49.3% 40 / 81 MC/DCmcdc 23.6% 9 / 38 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 68.8% 214 / 311 CC 58 SCAR 47.8
CC
58
Cov
74%
_readSubtreeToPerl
141.8
readSubtreeToPerl
2.0
readLevelToPerl
2.0
CRAP 118.8
↑ Summary
1 package XML::CompactTree;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 XML::CompactTree - builder of compact tree structures from XML documents
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14 =cut
15  
16 4 our $VERSION = '0.03';
17  
18 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # XCT_USE_QNAMES /* not yet implemented */
23 # XCT_TEXT_AS_STRING /* not yet implemented */
24 # XCT_PRESERVE_PARENT /* not yet implemented */
25 # XCT_MERGE_TEXT_NODES /* not yet implemented */
26  
27 4 ▶use constant do {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 my @flags = (qw(
29 XCT_IGNORE_WS
30 XCT_IGNORE_SIGNIFICANT_WS
31 XCT_IGNORE_PROCESSING_INSTRUCTIONS
32 XCT_IGNORE_COMMENTS
33 XCT_USE_QNAMES
34 XCT_KEEP_NS_DECLS
35 XCT_TEXT_AS_STRING
36 XCT_ATTRIBUTE_ARRAY
37 XCT_PRESERVE_PARENT
38 XCT_MERGE_TEXT_NODES
39 XCT_LINE_NUMBERS
40 XCT_DOCUMENT_ROOT
41 ));
42 4 $EXPORT_TAGS{flags} = \@flags;
43 4 my %c = map { ($flags[$_] => (1 << $_)) } 0..$#flags;
44 4 \%c
45 4 };
46  
47 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 @EXPORT = (map @$_, values %EXPORT_TAGS);
49 4 @EXPORT_OK = @EXPORT;
50 4 $EXPORT_TAGS{all}=\@EXPORT_OK;
51 }
52  
53 =head1 SYNOPSIS
54  
55 use XML::CompactTree;
56 use XML::LibXML::Reader;
57  
58 my $reader = XML::LibXML::Reader->new(location => $url);
59 ...
60 my $tree = XML::CompactTree::readSubtreeToPerl($reader);
61 ...
62  
63 =head1 DESCRIPTION
64  
65 This module provides functions that use XML::LibXML::Reader to parse
66 an XML document into a parse tree formed of nested arrays (and hashes).
67  
68 It aims to be fast in doing that and to presreve all relevant
69 information from the XML (including namespaces, document order, mixed
70 content, etc.). It sacrifices user friendliness for speed.
71  
72 IMPORTANT: There is an even more efficient XS implementation of this
73 module called XML::CompactTree::XS with 100% equivalent functionality.
74  
75 =head1 PURPOSE
76  
77 I wrote this module because I noticed that repeated calls to methods
78 implemented in C (XS) were very expensive in Perl.
79  
80 Therefore traversing a large DOM tree using XML::LibXML or iterating
81 over an XML stream using XML::LibXML::Reader was much slower than
82 traversing similarly large and structured native Perl data
83 structures.
84  
85 This module allows the user to build a document parse tree consisting
86 of native Perl data structures (arrays and optionally hashes) using
87 XML::LibXML::Reader with minimal number of XS calls.
88  
89 (Note that there XML::CompactTree::XS is 100% equivalent of this
90 module that manages the same with just one XS call.)
91  
92 It does not provide full DOM navigation but attempts to provide
93 maximum amount of information. Its memory footprint should be
94 somewhat smaller than that of a corresponding XML::LibXML DOM tree.
95  
96 =head1 EXPORT
97  
98 By default, the following constants are exported (C<:flags> export
99 tag) to be used as flags for the tree builder:
100  
101 XCT_IGNORE_WS
102 XCT_IGNORE_SIGNIFICANT_WS
103 XCT_IGNORE_PROCESSING_INSTRUCTIONS
104 XCT_IGNORE_COMMENTS
105 XCT_USE_QNAMES /* not yet implemented */
106 XCT_KEEP_NS_DECLS
107 XCT_TEXT_AS_STRING /* not yet implemented */
108 XCT_ATTRIBUTE_ARRAY
109 XCT_PRESERVE_PARENT /* not yet implemented */
110 XCT_MERGE_TEXT_NODES /* not yet implemented */
111 XCT_DOCUMENT_ROOT
112  
113 =head1 FUNCTIONS
114  
115 =head2 readSubtreeToPerl( $reader, $flags, \my %ns )
116  
117 Uses a given XML::LibXML::Reader parser objects to parse a subtree at
118 the current reader position to build a tree formed of nested arrays
119 (see L<OUTPUT FORMAT>).
120  
121 =over 4
122  
123 =item reader
124  
125 A XML::LibXML::Reader object to use as the reader. While building the
126 tree, the reader moves to the next node on the current or higher
127 level.
128  
129 =item flags
130  
131 An integer consisting of 1 bit flags (see constants in the EXPORT section).
132 Use binary or (|) to combine individual flags.
133  
134 The following flags are NOT implemented yet:
135  
136 XCT_USE_QNAMES, XCT_TEXT_AS_STRING, XCT_PRESERVE_PARENT, XCT_MERGE_TEXT_NODES
137  
138 =item ns
139  
140 You may pass an empty hash reference that will be populated by a
141 namespace_uri to namespace_index map, that can be used to decode
142 namespace indexes in the resulting data structure (see L<OUTPUT
143 FORMAT>).
144  
145  
146 =back
147  
148 =cut
149  
150 8 ▶sub readSubtreeToPerl {
Subroutine readSubtreeToPerl: called CC=2 SCAR=7.0
Pod documented
151 8 my ($reader,$flags,$ns)=@_;
152 8 ▶ $ns||={};
Condition50%1/2
$ns ||= {}
l!l
80
MC/DC0%0/1
$ns ||= {}
$ns
Truth table50%1/2
A
A= $ns
Aresult
11
00
153 8 $ns->{''}=0;
154 8 my $ret = _readSubtreeToPerl($reader,$flags,$ns,1,0);
155 8 return $ret->[0];
156 }
157  
158 =head2 readLevelToPerl( $reader, $flags, $ns )
159  
160 Like C<readSubtreeToPerl>, but reads the subtree
161 at the current reader position and all its following siblings.
162 It returns an array reference of representations of these subtrees
163 as in the format described in L<OUTPUT FORMAT>.
164  
165 =cut
166  
167 4 ▶sub readLevelToPerl {
Subroutine readLevelToPerl: called CC=2 SCAR=7.0
Pod documented
168 4 my ($reader,$flags,$ns)=@_;
169 4 ▶ $ns||={};
Condition50%1/2
$ns ||= {}
l!l
40
MC/DC0%0/1
$ns ||= {}
$ns
Truth table50%1/2
A
A= $ns
Aresult
11
00
170 4 $ns->{''}=0;
171 4 my $ret = _readSubtreeToPerl($reader,$flags,$ns,1,1);
172 4 return $ret;
173 }
174  
175 12 ▶sub _readSubtreeToPerl {
Subroutine _readSubtreeToPerl: called CC=56 SCAR=49.5
176 12 my ($reader, $flags, $ns_map, $free_ns_index, $read_siblings) = @_;
177 12 my @parents;
178 12 my ($av,$prev,$kids,$ret,$type,$name);
179 12 my $cur_depth=$reader->depth();
180 12 my $start_depth = $cur_depth;
181 12 my $prev_depth = $start_depth;
182 12 my $top = [];
183 12 ▶ if ($reader->nodeType()==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->nodeType == 0) 0 12 12
184 0 ▶ return if $reader->read()!=1;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->read != 1 0 0 0
185 0 ▶ if ($flags & XCT_DOCUMENT_ROOT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 2048) 0 0 0
186 0 $prev = [ XML_READER_TYPE_DOCUMENT,
187 $reader->encoding,
188 ];
189 0 $start_depth --;
190 0 $prev_depth --;
191 0 push @$top, $prev;
192 0 push @parents, $prev;
193 }
194 }
195 12 ▶ do {{
Branch: 2 branches
BranchTrueFalseTotal
$ret == 1 and ($cur_depth = $reader->depth) > $start_depth - ($read_siblings ? 1 : 0) and do { do { { $type = $reader->nodeType; if ($type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6 or $type == 16 or $type == 17) { $ret = $reader->read; } else { if ($flags & 3 and $type == 13 or $flags & 2 and $type == 14 or $flags & 8 and $type == 8 or $flags & 4 and $type == 7) { $ret = $reader->read; } else { my(@av) = (); $av = \@av; push @av, $type; if ($type == 1) { push @av, $reader->localName; $name = $reader->namespaceURI; if ($name) { if (exists $ns_map->{$name}) { push @av, $ns_map->{$name} || 0; } else { push @av, $free_ns_index; $ns_map->{$name} = $free_ns_index; ++$free_ns_index; } ; } else { push @av, 0; } ; if ($reader->hasAttributes and $reader->moveToFirstAttribute == 1) { if ($flags & 128) { my @attrs; do { $name = $reader->name; if ($flags & 32 or substr($name, 0, 5) ne "xmlns") { push @attrs, $name; push @attrs, $reader->value; } } while $reader->moveToNextAttribute == 1; push @av, \@attrs; } else { my %attrs; do { $name = $reader->name; if ($flags & 32 or substr($name, 0, 5) ne "xmlns") { $attrs{$name} = $reader->value; } } while $reader->moveToNextAttribute == 1; $reader->moveToElement; push @av, \%attrs; } ; } else { push @av, undef; } ; if ($flags & 1024) { push @av, $reader->lineNumber; } ; } elsif ($type == 3 or $type == 4 or $type == 8 or $type == 13 or $type == 14) { push @av, $reader->value; } elsif ($type == 5 or $type == 7 or $type == 12) { push @av, $reader->localName; push @av, $reader->value; } elsif ($type == 9 or $type == 11) { push @av, $reader->encoding; } ; if ($cur_depth == $start_depth) { push @$top, $av; $prev_depth = $cur_depth; $kids = undef; } elsif ($cur_depth > $prev_depth) { $kids = []; push @$prev, $kids; push @$kids, $av; push @parents, $prev; $prev_depth = $cur_depth; } elsif ($cur_depth == $prev_depth) { push @$kids, $av if $kids; } else { do { --$prev_depth; pop @parents } while $cur_depth < $prev_depth; my $p = $parents[-1]; if ($p) { $prev = $p; $p = $prev->[-1]; if ($p) { $kids = $p; push @$kids, $av; } ; } ; } ; $prev = $av; $ret = $reader->read; } ; } ; } } } 76 12 88
$read_siblings ? : 32 56 88
Condition66%2/3
$ret == 1 and ($cur_depth = $reader->depth) > $start_depth - ($read_siblings ? 1 : 0)
!ll&&!rl&&r
01276
MC/DC50%1/2
$ret == 1 and ($cur_depth = $reader->depth) > $start_depth - ($read_siblings ? 1 : 0)
$ret == 1 ($cur_depth = $reader->depth) > $start_depth - ($read_siblings ? 1 : 0)
Truth table66%2/3
A and B
A= $ret == 1B= ($cur_depth = $reader->depth) > $start_depth - ($read_siblings ? 1 : 0)
ABresult
0X0
100
111
196 88 $type = $reader->nodeType();
197 # warn("$type, $cur_depth, ".$reader->name."\n");
198 88 ▶ if ($type == XML_READER_TYPE_NONE
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6 or $type == 16 or $type == 17) { } 8 80 88
Condition55%10/18
$type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6 or $type == 16 or $type == 17
l!l&&r!l&&!r
8080
$type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6 or $type == 16
l!l&&r!l&&!r
8080
$type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6
l!l&&r!l&&!r
8080
$type == 0 or $type == 2 or $type == 10 or $type == 15
l!l&&r!l&&!r
0880
$type == 0 or $type == 2 or $type == 10
l!l&&r!l&&!r
0088
$type == 0 or $type == 2
l!l&&r!l&&!r
0088
MC/DC14%1/7
$type == 0 or $type == 2 or $type == 10 or $type == 15 or $type == 6 or $type == 16 or $type == 17
$type == 0 $type == 2 $type == 10 $type == 15 $type == 6 $type == 16 $type == 17
Truth table25%2/8
A or B or C or D or E or F or G
A= $type == 0B= $type == 2C= $type == 10D= $type == 15E= $type == 6F= $type == 16G= $type == 17
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
199 or $type == XML_READER_TYPE_ATTRIBUTE
200 or $type == XML_READER_TYPE_DOCUMENT_TYPE
201 or $type == XML_READER_TYPE_END_ELEMENT
202 or $type == XML_READER_TYPE_ENTITY
203 or $type == XML_READER_TYPE_END_ENTITY
204 or $type == XML_READER_TYPE_XML_DECLARATION) {
205 8 $ret = $reader->read();
206 } else {
207 80 ▶ if (($flags & (XCT_IGNORE_WS|XCT_IGNORE_SIGNIFICANT_WS))
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 3 and $type == 13 or $flags & 2 and $type == 14 or $flags & 8 and $type == 8 or $flags & 4 and $type == 7) { } 20 60 80
Condition52%11/21
$flags & 3 and $type == 13 or $flags & 2 and $type == 14 or $flags & 8 and $type == 8 or $flags & 4 and $type == 7
l!l&&r!l&&!r
20060
$flags & 3 and $type == 13 or $flags & 2 and $type == 14 or $flags & 8 and $type == 8
l!l&&r!l&&!r
20060
$flags & 3 and $type == 13 or $flags & 2 and $type == 14
l!l&&r!l&&!r
02060
$flags & 3 and $type == 13
!ll&&!rl&&r
0800
$flags & 2 and $type == 14
!ll&&!rl&&r
06020
$flags & 8 and $type == 8
!ll&&!rl&&r
6000
$flags & 4 and $type == 7
!ll&&!rl&&r
6000
MC/DC12%1/8
$flags & 3 and $type == 13 or $flags & 2 and $type == 14 or $flags & 8 and $type == 8 or $flags & 4 and $type == 7
$flags & 3 $type == 13 $flags & 2 $type == 14 $flags & 8 $type == 8 $flags & 4 $type == 7
Truth table6%2/31
A and B or C and D or E and F or G and H
A= $flags & 3B= $type == 13C= $flags & 2D= $type == 14E= $flags & 8F= $type == 8G= $flags & 4H= $type == 7
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
208 and $type == XML_READER_TYPE_WHITESPACE
209 or
210 ($flags & XCT_IGNORE_SIGNIFICANT_WS)
211 and $type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE
212 or
213 ($flags & XCT_IGNORE_COMMENTS)
214 and $type == XML_READER_TYPE_COMMENT
215 or
216 ($flags & XCT_IGNORE_PROCESSING_INSTRUCTIONS
217 and $type == XML_READER_TYPE_PROCESSING_INSTRUCTION)) {
218 20 $ret = $reader->read();
219 } else {
220 60 my @av=();
221 60 $av=\@av;
222 60 push @av, $type;
223 60 ▶ if ($type == XML_READER_TYPE_ELEMENT) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 1) { } 36 24 60
elsif ($type == 3 or $type == 4 or $type == 8 or $type == 13 or $type == 14) { } 24 0 24
elsif ($type == 5 or $type == 7 or $type == 12) { } 0 0 0
elsif ($type == 9 or $type == 11) { } 0 0 0
224 # warn(" element\n");
225 36 push @av, $reader->localName();
226 36 $name = $reader->namespaceURI();
227 36 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 36 0 36
228 36 ▶ if (exists($ns_map->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ns_map->{$name}) { } 16 20 36
229 16 ▶ push(@av, $ns_map->{$name} || 0);
Condition50%1/2
$ns_map->{$name} || 0
l!l
160
MC/DC0%0/1
$ns_map->{$name} || 0
$ns_map->{$name}
Truth table50%1/2
A
A= $ns_map->{$name}
Aresult
11
00
230 } else {
231 # warn("storing namespace $name as $free_ns_index)";
232 20 push(@av, $free_ns_index);
233 20 $ns_map->{$name}=$free_ns_index;
234 20 $free_ns_index++;
235 }
236 } else {
237 0 push(@av, 0); # no namespace
238 }
239 36 ▶ if ($reader->hasAttributes() && $reader->moveToFirstAttribute()==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->hasAttributes and $reader->moveToFirstAttribute == 1) { } 16 20 36
Condition66%2/3
$reader->hasAttributes and $reader->moveToFirstAttribute == 1
!ll&&!rl&&r
20016
MC/DC50%1/2
$reader->hasAttributes and $reader->moveToFirstAttribute == 1
$reader->hasAttributes $reader->moveToFirstAttribute == 1
Truth table66%2/3
A and B
A= $reader->hasAttributesB= $reader->moveToFirstAttribute == 1
ABresult
0X0
100
111
240 16 ▶ if ($flags & XCT_ATTRIBUTE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) { } 8 8 16
241 8 my @attrs;
242 8 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$reader->moveToNextAttribute == 1 and do { do { $name = $reader->name; if ($flags & 32 or substr($name, 0, 5) ne "xmlns") { push @attrs, $name; push @attrs, $reader->value; } } } 4 8 12
243 12 $name = $reader->name();
244 12 ▶ if (($flags & XCT_KEEP_NS_DECLS) || substr($name,0,5) ne 'xmlns' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 32 or substr($name, 0, 5) ne "xmlns") 10 2 12
Condition100%3/3
$flags & 32 or substr($name, 0, 5) ne "xmlns"
l!l&&r!l&&!r
642
MC/DC100%2/2
$flags & 32 or substr($name, 0, 5) ne "xmlns"
$flags & 32 substr($name, 0, 5) ne "xmlns"
Truth table100%3/3
A or B
A= $flags & 32B= substr($name, 0, 5) ne "xmlns"
ABresult
1X1
011
000
245 10 push(@attrs, $name);
246 10 push(@attrs, $reader->value());
247 }
248 } while ($reader->moveToNextAttribute()==1);
249 # $reader->moveToElement();
250 8 push(@av, \@attrs);
251 } else {
252 8 my %attrs;
253 8 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$reader->moveToNextAttribute == 1 and do { do { $name = $reader->name; if ($flags & 32 or substr($name, 0, 5) ne "xmlns") { $attrs{$name} = $reader->value; } } } 4 8 12
254 12 $name = $reader->name();
255 12 ▶ if (($flags & XCT_KEEP_NS_DECLS) || substr($name,0,5) ne 'xmlns' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 32 or substr($name, 0, 5) ne "xmlns") 10 2 12
Condition100%3/3
$flags & 32 or substr($name, 0, 5) ne "xmlns"
l!l&&r!l&&!r
642
MC/DC100%2/2
$flags & 32 or substr($name, 0, 5) ne "xmlns"
$flags & 32 substr($name, 0, 5) ne "xmlns"
Truth table100%3/3
A or B
A= $flags & 32B= substr($name, 0, 5) ne "xmlns"
ABresult
1X1
011
000
256 10 $attrs{$name}=$reader->value();
257 }
258 } while ($reader->moveToNextAttribute()==1);
259 8 $reader->moveToElement();
260 8 push(@av, \%attrs);
261 }
262 } else {
263 20 push(@av, undef); # no attributes
264 }
265 36 ▶ if ($flags & XCT_LINE_NUMBERS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1024) 2 34 36
266 2 push(@av, $reader->lineNumber());
267 }
268 ▶ } elsif ($type == XML_READER_TYPE_TEXT or
Condition33%4/12
$type == 3 or $type == 4 or $type == 8 or $type == 13 or $type == 14
l!l&&r!l&&!r
2400
$type == 3 or $type == 4 or $type == 8 or $type == 13
l!l&&r!l&&!r
2400
$type == 3 or $type == 4 or $type == 8
l!l&&r!l&&!r
2400
$type == 3 or $type == 4
l!l&&r!l&&!r
2400
MC/DC0%0/5
$type == 3 or $type == 4 or $type == 8 or $type == 13 or $type == 14
$type == 3 $type == 4 $type == 8 $type == 13 $type == 14
Truth table16%1/6
A or B or C or D or E
A= $type == 3B= $type == 4C= $type == 8D= $type == 13E= $type == 14
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
269 $type == XML_READER_TYPE_CDATA or
270 $type == XML_READER_TYPE_COMMENT or
271 $type == XML_READER_TYPE_WHITESPACE or
272 $type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE) {
273 24 push(@av, $reader->value());
274 ▶ } elsif ($type == XML_READER_TYPE_ENTITY_REFERENCE or
Condition0%0/6
$type == 5 or $type == 7 or $type == 12
l!l&&r!l&&!r
000
$type == 5 or $type == 7
l!l&&r!l&&!r
000
MC/DC0%0/3
$type == 5 or $type == 7 or $type == 12
$type == 5 $type == 7 $type == 12
Truth table0%0/4
A or B or C
A= $type == 5B= $type == 7C= $type == 12
ABCresult
1XX1
01X1
0011
0000
275 $type == XML_READER_TYPE_PROCESSING_INSTRUCTION or
276 $type == XML_READER_TYPE_NOTATION) {
277 0 push(@av, $reader->localName());
278 0 push(@av, $reader->value());
279 ▶ } elsif ($type == XML_READER_TYPE_DOCUMENT or
Condition0%0/3
$type == 9 or $type == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 9 or $type == 11
$type == 9 $type == 11
Truth table0%0/3
A or B
A= $type == 9B= $type == 11
ABresult
1X1
011
000
280 $type == XML_READER_TYPE_DOCUMENT_FRAGMENT) {
281 0 push(@av, $reader->encoding());
282 }
283 60 ▶ if ($cur_depth==$start_depth) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cur_depth == $start_depth) { } 16 44 60
elsif ($cur_depth > $prev_depth) { } 16 28 44
elsif ($cur_depth == $prev_depth) { } 24 4 28
284 16 push(@$top, $av);
285 16 $prev_depth = $cur_depth;
286 16 $kids = undef;
287 } elsif ($cur_depth > $prev_depth) {
288 16 $kids=[];
289 16 push(@$prev, $kids);
290 16 push(@$kids, $av);
291 16 push(@parents, $prev);
292 16 $prev_depth = $cur_depth;
293 } elsif ($cur_depth == $prev_depth) {
294 24 ▶ push(@$kids, $av) if $kids;
Branch: 1 branch
BranchTrueFalseTotal
if $kids 24 0 24
295 } else {
296 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cur_depth < $prev_depth and do { do { --$prev_depth; pop @parents } } 0 4 4
297 4 $prev_depth--;
298 4 pop(@parents);
299 } while ($cur_depth < $prev_depth);
300 4 my $p = $parents[-1];
301 4 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 4 0 4
302 4 $prev = $p;
303 4 $p = $prev->[-1];
304 4 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 4 0 4
305 4 $kids = $p;
306 4 push(@$kids, $av);
307 }
308 }
309 }
310 60 $prev = $av;
311 60 $ret = $reader->read();
312 }
313 }
314 # print STDERR "$cur_depth, ",$reader->depth(),"\n";
315 }} while ($ret == 1 && ($cur_depth = $reader->depth()) > ($start_depth - ($read_siblings ? 1 : 0)));
316 12 ▶ if ($ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == 1) 12 0 12
317 12 ▶ if ($reader->depth() == $start_depth &&
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->depth == $start_depth and $reader->nodeType == 15) 8 4 12
Condition66%2/3
$reader->depth == $start_depth and $reader->nodeType == 15
!ll&&!rl&&r
408
MC/DC50%1/2
$reader->depth == $start_depth and $reader->nodeType == 15
$reader->depth == $start_depth $reader->nodeType == 15
Truth table66%2/3
A and B
A= $reader->depth == $start_depthB= $reader->nodeType == 15
ABresult
0X0
100
111
318 $reader->nodeType() == XML_READER_TYPE_END_ELEMENT) {
319 8 $reader->read();
320 }
321 }
322 12 return $top;
323 }
324  
325 =head1 OUTPUT FORMAT
326  
327 The result of parsing a subtree is a Perl array reference C<$node>
328 contains a node type followed by node data whose interpretation on
329 further positions in $node depends on the node type, as described
330 below:
331  
332 =head2 Any Node
333  
334 =over 5
335  
336 =item *
337  
338 $node->[0] is an integer representing the node type. Use
339 XML::LibXML::Reader node-tye constants, e.g. XML_READER_TYPE_ELEMENT
340 for an element node, XML_READER_TYPE_TEXT for text node, etc.
341  
342 =back
343  
344 =head2 Document or Document Fragment Nodes
345  
346 =over 5
347  
348 =item *
349  
350 $node->[1] contains the document encoding
351  
352 =item *
353  
354 $node->[2] is an array reference containing similar represention of
355 all the child nodes of the document (fragment).
356  
357 =back
358  
359 Note: XML::LibXML::Reader does not document node by default, which
360 means that calling readSubtreeToPerl on a reader object in its initial
361 state only parses the first node in the document (which can be the
362 root element, but also a comment or a processing instruction). Use
363 XCT_DOCUMENT_ROOT flag to force creating a document node in such case.
364  
365 =head2 Element nodes
366  
367 =over 5
368  
369 =item *
370  
371 $node->[1] is the local name (UTF-8 encoded character string)
372  
373 =item *
374  
375 $node->[2] is the namespace index (see L<NAMESPACES> below)
376  
377 =item *
378  
379 $node->[3] is undef if the element has no attributes. Otherwise if
380 XCT_ATTRIBUTE_ARRAY flag was used, $node->[3] is an array reference of
381 the form C<[ name1, value1, name2, value2, ....]> of attribute names and
382 corresponding values. If XCT_ATTRIBUTE_ARRAY flag was not used, then
383 $node->[3] is a hash reference mapping attribute names to the
384 corresponding attribute values C<{ name1=>value1, name2=>value2...}>
385  
386 The flag XCT_KEEP_NS_DECLS controls whether namespace declarations
387 (xmlns=... or xmlns:prefix=...) are included along with normal
388 attributes or not.
389  
390 Note: there is no support for namespaced attributes yet, but the
391 attribute names are stored as QNames, so one can always use
392 XCT_KEEP_NS_DECLS to keep track of namespace prefix declarations and
393 do the resolving manually. Support for namespaced attributes is
394 planned.
395  
396 =item *
397  
398 If XTC_LINE_NUMBERS flag was used, $node->[4] contains the line number
399 of the element and $node->[5] contains an array reference containing
400 similar representions of the child nodes of the current node.
401  
402 =item *
403  
404 If XTC_LINE_NUMBERS flag was NOT used, $node->[4] contains an array
405 reference of similar representations of the child nodes of the current
406 node.
407  
408 =back
409  
410 =head2 Text, CDATA, Comment and White-Space Nodes
411  
412 =over 5
413  
414 =item *
415  
416 $node->[1] contains the node value (UTF-8 encoded character string)
417  
418 =back
419  
420 =head2 Unparsed Entity, Processing-Instruction, and Notation Nodes
421  
422 =over 5
423  
424 =item *
425  
426 $node->[1] contains the local name (there is no support for
427 namespaces on these types of nodes yet)
428  
429 =item *
430  
431 $node->[2] contains the node value
432  
433 =back
434  
435 =head2 Skipping Less-Significant Nodes
436  
437 White-space (non-significant or significant), processing-instruction
438 and comment nodes can be completely skipped, using the following
439 flags:
440  
441 XCT_IGNORE_WS
442 XCT_IGNORE_SIGNIFICANT_WS
443 XCT_IGNORE_PROCESSING_INSTRUCTIONS
444 XCT_IGNORE_COMMENTS
445  
446 =head1 NAMESPACES
447  
448 Namespaces of element nodes are stored in the element node as an
449 integer. 0 always represents nodes without namespace, all other
450 namespaces are assigned unique numbers in an increasing order as they
451 appear. You can pass an empty hash reference to the parsing functions
452 to obtain the mapping.
453  
454 =head2 Example
455  
456 use XML::CompactTree;
457 use XML::LibXML::Reader;
458  
459 my $reader = XML::LibXML::Reader->new(location => $ARGV[0]);
460 my %ns;
461 my $data = XML::CompactTree::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT, \%ns );
462 $ns_map[$ns{$_}]=$_ for keys %ns;
463 my @nodes = ($data);
464 while (@nodes) {
465 my $node = shift @nodes;
466 my $type = $node->[0];
467 if ($type == XML_READER_TYPE_ELEMENT) {
468 print "element $node->[1] is from ns $node->[2] '$ns_map[$node->[2]]'\n";
469 push @nodes, @{$node->[4]}; # queue children
470 } elsif ($type == XML_READER_TYPE_DOCUMENT) {
471 push @nodes, @{$node->[2]}; # queue children
472 }
473 }
474  
475 =head1 PLANNED FEATURES
476  
477 Planned flags:
478  
479 XCT_USE_QNAMES - use QNames instead of local names for all nodes
480 XCT_TEXT_AS_STRING - put text nodes into the tree as plain scalars
481 XCT_PRESERVE_PARENT - add a slot with a weak reference to the parent node
482 XCT_MERGE_TEXT_NODES - merge adjacent text/cdata nodes together
483  
484 Features: allow blessing the array refs to default or user-specified
485 classes; the default classes would provide a very small subset of DOM
486 methods to retrieve node information, manipulate the tree, and
487 possibly serialize the parse tree back to XML.
488  
489 =head1 AUTHOR
490  
491 Petr Pajas, C<< <pajas@matfyz.cz> >>
492  
493 =head1 BUGS
494  
495 Please report any bugs or feature requests to
496 C<bug-xml-compacttree-xs@rt.cpan.org>, or through the web interface at
497 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-CompactTree-XS>.
498 I will be notified, and then you'll automatically be notified of progress on
499 your bug as I make changes.
500  
501 =head1 COPYRIGHT & LICENSE
502  
503 Copyright 2008-2009 Petr Pajas, All Rights Reserved.
504  
505 This program is free software; you can redistribute it and/or modify it
506 under the same terms as Perl itself.
507  
508 =head1 SEE ALSO
509  
510 XML::CompactTree::XS
511  
512 XML::LibXML::Reader
513  
514 =cut
515  
516  
517 4 1; # End of XML::CompactTree
↑ Summary