SAX/PurePerl.pm

filter: Statementstmt 80.2% 313 / 390 Branchbran 62.5% 110 / 176 Conditioncond 65.3% 17 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 78.0% 39 / 50 Podpod 0.0% 0 / 19 totaltotal 71.6% 484 / 676 CC 100 SCAR 25.6
CC
100
Cov
74%
Reference
57.0
element
25.1
content
15.1
CRAP 12.9
« SAX/PurePerl/Reader/URI.pm ↑ Summary SAX/PurePerl/Reader.pm »
1 # $Id$
2  
3 package XML::SAX::PurePerl;
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 $VERSION = '1.02';
9  
10 14 ▶use XML::SAX::PurePerl::Productions qw($NameChar $SingleChar);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use XML::SAX::PurePerl::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use XML::SAX::PurePerl::EncodingDetect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use XML::SAX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use XML::SAX::PurePerl::DocType ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use XML::SAX::PurePerl::DTDDecls ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use XML::SAX::PurePerl::XMLDecl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use XML::SAX::DocumentLocator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use XML::SAX::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use XML::SAX qw(Namespaces);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use XML::NamespaceSupport ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 14 ▶if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.006) { } 0 14 14
24 0 require XML::SAX::PurePerl::NoUnicodeExt;
25 }
26 else {
27 14 require XML::SAX::PurePerl::UnicodeExt;
28 }
29  
30 14 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 14 @ISA = ('XML::SAX::Base');
32  
33 14 my %int_ents = (
34 amp => '&',
35 lt => '<',
36 gt => '>',
37 quot => '"',
38 apos => "'",
39 );
40  
41 14 my $xmlns_ns = "http://www.w3.org/2000/xmlns/";
42 14 my $xml_ns = "http://www.w3.org/XML/1998/namespace";
43  
44 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 0 ▶sub _parse_characterstream {
Subroutine _parse_characterstream: not called CC=1 SCAR=6.9
46 0 my $self = shift;
47 0 my ($fh) = @_;
48 0 confess("CharacterStream is not yet correctly implemented");
49 0 my $reader = XML::SAX::PurePerl::Reader::Stream->new($fh);
50 0 return $self->_parse($reader);
51 }
52  
53 3 ▶sub _parse_bytestream {
Subroutine _parse_bytestream: called CC=1 SCAR=0.0
54 3 my $self = shift;
55 3 my ($fh) = @_;
56 3 my $reader = XML::SAX::PurePerl::Reader::Stream->new($fh);
57 3 return $self->_parse($reader);
58 }
59  
60 4 ▶sub _parse_string {
Subroutine _parse_string: called CC=1 SCAR=0.0
61 4 my $self = shift;
62 4 my ($str) = @_;
63 4 my $reader = XML::SAX::PurePerl::Reader::String->new($str);
64 4 return $self->_parse($reader);
65 }
66  
67 18 ▶sub _parse_systemid {
Subroutine _parse_systemid: called CC=1 SCAR=0.0
68 18 my $self = shift;
69 18 my ($uri) = @_;
70 18 my $reader = XML::SAX::PurePerl::Reader::URI->new($uri);
71 18 return $self->_parse($reader);
72 }
73  
74 25 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=8.3
75 25 my ($self, $reader) = @_;
76
77 25 $reader->public_id($self->{ParseOptions}{Source}{PublicId});
78 25 $reader->system_id($self->{ParseOptions}{Source}{SystemId});
79  
80 25 $self->{NSHelper} = XML::NamespaceSupport->new({xmlns => 1});
81  
82 $self->set_document_locator(
83 XML::SAX::DocumentLocator->new(
84 0 ▶ sub { $reader->public_id },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 ▶ sub { $reader->system_id },
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 ▶ sub { $reader->line },
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 ▶ sub { $reader->column },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ sub { $reader->get_encoding },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ sub { $reader->get_xml_version },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 25 ),
91 );
92
93 25 $self->start_document({});
94  
95 25 ▶ if (defined $self->{ParseOptions}{Source}{Encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ParseOptions'}{'Source'}{'Encoding'}) { } 0 25 25
96 0 $reader->set_encoding($self->{ParseOptions}{Source}{Encoding});
97 }
98 else {
99 25 $self->encoding_detect($reader);
100 }
101
102 # parse a document
103 25 $self->document($reader);
104
105 21 return $self->end_document({});
106 }
107  
108 3 ▶sub parser_error {
Subroutine parser_error: called CC=1 SCAR=0.0
Pod undocumented
109 3 my $self = shift;
110 3 my ($error, $reader) = @_;
111
112 # warn("parser error: $error from ", $reader->line, " : ", $reader->column, "\n");
113 3 my $exception = XML::SAX::Exception::Parse->new(
114 Message => $error,
115 ColumnNumber => $reader->column,
116 LineNumber => $reader->line,
117 PublicId => $reader->public_id,
118 SystemId => $reader->system_id,
119 );
120  
121 3 $self->fatal_error($exception);
122 3 $exception->throw;
123 }
124  
125 25 ▶sub document {
Subroutine document: called CC=4 SCAR=14.1
Pod undocumented
126 25 my ($self, $reader) = @_;
127
128 # document ::= prolog element Misc*
129
130 25 $self->prolog($reader);
131 22 ▶ $self->element($reader) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->element($reader) or $self->parser_error("Document requires an element", $reader) 21 0 21
132 $self->parser_error("Document requires an element", $reader);
133
134 21 ▶ while(length($reader->data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $reader->data) 18 21 39
135 18 ▶ $self->Misc($reader) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->Misc($reader) or $self->parser_error("Only Comments, PIs and whitespace allowed at end of document", $reader) 18 0 18
136 $self->parser_error("Only Comments, PIs and whitespace allowed at end of document", $reader);
137 }
138 }
139  
140 25 ▶sub prolog {
Subroutine prolog: called CC=5 SCAR=16.1
Pod undocumented
141 25 my ($self, $reader) = @_;
142
143 25 $self->XMLDecl($reader);
144
145 # consume all misc bits
146 23 ▶ 1 while($self->Misc($reader));
Branch: 1 branch
BranchTrueFalseTotal
$self->Misc($reader) and do { '???' } 27 22 49
147
148 22 ▶ if ($self->doctypedecl($reader)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->doctypedecl($reader)) 1 21 22
149 1 ▶ while (length($reader->data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $reader->data) 4 0 4
150 4 ▶ $self->Misc($reader) || last;
Branch: 1 branch
BranchTrueFalseTotal
$self->Misc($reader) or last 3 1 4
151 }
152 }
153 }
154  
155 458 ▶sub element {
Subroutine element: called CC=24 SCAR=32.2
Pod undocumented
156 458 my ($self, $reader) = @_;
157
158 458 ▶ return 0 unless $reader->match('<');
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->match("<") 0 458 458
159
160 458 ▶ my $name = $self->Name($reader) || $self->parser_error("Invalid element name", $reader);
Condition33%1/3
$self->Name($reader) || $self->parser_error('Invalid element name', $reader)
l!l&&r!l&&!r
45800
MC/DC0%0/2
$self->Name($reader) || $self->parser_error('Invalid element name', $reader)
$self->Name($reader) $self->parser_error('Invalid element name', $reader)
Truth table33%1/3
A || B
A= $self->Name($reader)B= $self->parser_error('Invalid element name', $reader)
ABresult
1X1
011
000
161
162 458 my %attribs;
163
164 458 ▶ while( my ($k, $v) = $self->Attribute($reader) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->Attribute($reader)) 2002 458 2460
165 2002 $attribs{$k} = $v;
166 }
167
168 458 my $have_namespaces = $self->get_feature(Namespaces);
169
170 # Namespace processing
171 458 $self->{NSHelper}->push_context;
172 458 my @new_ns;
173 # my %attrs = @attribs;
174 # while (my ($k,$v) = each %attrs) {
175 458 ▶ if ($have_namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_namespaces) 458 0 458
176 458 ▶ while ( my ($k, $v) = each %attribs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attribs) 2002 458 2460
177 2002 ▶ if ($k =~ m/^xmlns(:(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^xmlns(:(.*))?$/) 4 1998 2002
178 4 ▶ my $prefix = $2 || '';
Condition100%2/2
$2 || ''
l!l
22
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
179 4 $self->{NSHelper}->declare_prefix($prefix, $v);
180 4 my $ns =
181 {
182 Prefix => $prefix,
183 NamespaceURI => $v,
184 };
185 4 push @new_ns, $ns;
186 4 $self->SUPER::start_prefix_mapping($ns);
187 }
188 }
189 }
190  
191 # Create element object and fire event
192 458 my %attrib_hash;
193 458 ▶ while (my ($name, $value) = each %attribs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attribs) 2002 458 2460
194 # TODO normalise value here
195 2002 my ($ns, $prefix, $lname);
196 2002 ▶ if ($have_namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_namespaces) 2002 0 2002
197 2002 ($ns, $prefix, $lname) = $self->{NSHelper}->process_attribute_name($name);
198 }
199 2002 ▶ $ns ||= ''; $prefix ||= ''; $lname ||= '';
Condition83%5/6
$ns ||= ""
l!l
31999
$prefix ||= ""
l!l
31999
$lname ||= ""
l!l
20020
MC/DC100%1/1
$ns ||= ""
$ns
MC/DC100%1/1
$prefix ||= ""
$prefix
MC/DC0%0/1
$lname ||= ""
$lname
Truth table100%2/2
A
A= $ns
Aresult
11
00
Truth table100%2/2
A
A= $prefix
Aresult
11
00
Truth table50%1/2
A
A= $lname
Aresult
11
00
200 2002 $attrib_hash{"{$ns}$lname"} = {
201 Name => $name,
202 LocalName => $lname,
203 Prefix => $prefix,
204 NamespaceURI => $ns,
205 Value => $value,
206 };
207 }
208
209 458 %attribs = (); # lose the memory since we recurse deep
210
211 458 my ($ns, $prefix, $lname);
212 458 ▶ if ($self->get_feature(Namespaces)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_feature('http://xml.org/sax/features/namespaces')) { } 458 0 458
213 458 ($ns, $prefix, $lname) = $self->{NSHelper}->process_element_name($name);
214 }
215 else {
216 0 $lname = $name;
217 }
218 457 ▶ $ns ||= ''; $prefix ||= ''; $lname ||= '';
Condition83%5/6
$ns ||= ""
l!l
3454
$prefix ||= ""
l!l
1456
$lname ||= ""
l!l
4570
MC/DC100%1/1
$ns ||= ""
$ns
MC/DC100%1/1
$prefix ||= ""
$prefix
MC/DC0%0/1
$lname ||= ""
$lname
Truth table100%2/2
A
A= $ns
Aresult
11
00
Truth table100%2/2
A
A= $prefix
Aresult
11
00
Truth table50%1/2
A
A= $lname
Aresult
11
00
219  
220 # Process remainder of start_element
221 457 $self->skip_whitespace($reader);
222 457 my $have_content;
223 457 my $data = $reader->data(2);
224 457 ▶ if ($data =~ /^\/>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m[^/>]) { } 12 445 457
225 12 $reader->move_along(2);
226 }
227 else {
228 445 ▶ $data =~ /^>/ or $self->parser_error("No close element tag", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^>/ or $self->parser_error("No close element tag", $reader) 445 0 445
229 445 $reader->move_along(1);
230 445 $have_content++;
231 }
232
233 457 my $el =
234 {
235 Name => $name,
236 LocalName => $lname,
237 Prefix => $prefix,
238 NamespaceURI => $ns,
239 Attributes => \%attrib_hash,
240 };
241 457 $self->start_element($el);
242
243 # warn("($name\n");
244
245 457 ▶ if ($have_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_content) 445 12 457
246 445 $self->content($reader);
247
248 445 my $data = $reader->data(2);
249 445 ▶ $data =~ /^<\// or $self->parser_error("No close tag marker", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$data =~ m[^</] or $self->parser_error("No close tag marker", $reader) 445 0 445
250 445 $reader->move_along(2);
251 445 my $end_name = $self->Name($reader);
252 445 ▶ $end_name eq $name || $self->parser_error("End tag mismatch ($end_name != $name)", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$end_name eq $name or $self->parser_error("End tag mismatch ($end_name != $name)", $reader) 445 0 445
253 445 $self->skip_whitespace($reader);
254 445 ▶ $reader->match('>') or $self->parser_error("No close '>' on end tag", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$reader->match(">") or $self->parser_error("No close '>' on end tag", $reader) 445 0 445
255 }
256
257 457 my %end_el = %$el;
258 457 delete $end_el{Attributes};
259 457 $self->end_element(\%end_el);
260  
261 457 for my $ns (@new_ns) {
262 3 $self->end_prefix_mapping($ns);
263 }
264 457 $self->{NSHelper}->pop_context;
265
266 457 return 1;
267 }
268  
269 445 ▶sub content {
Subroutine content: called CC=11 SCAR=27.1
Pod undocumented
270 445 my ($self, $reader) = @_;
271
272 445 while (1) {
273 946 $self->CharData($reader);
274
275 946 my $data = $reader->data(2);
276
277 946 ▶ if ($data =~ /^<\//) {
Branch: 5 branches
BranchTrueFalseTotal
if ($data =~ m[^</]) { } 445 501 946
elsif ($data =~ /^&/) { } 51 450 501
elsif ($data =~ /^<!/) { } 14 436 450
elsif ($data =~ /^<\?/) { } 0 436 436
elsif ($data =~ /^</) { } 436 0 436
278 445 return 1;
279 }
280 elsif ($data =~ /^&/) {
281 51 ▶ $self->Reference($reader) or $self->parser_error("bare & not allowed in content", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$self->Reference($reader) or $self->parser_error("bare & not allowed in content", $reader) 51 0 51
282 51 next;
283 }
284 elsif ($data =~ /^<!/) {
285 14 ▶ ($self->CDSect($reader)
Branch: 1 branch
BranchTrueFalseTotal
$self->CDSect($reader) || $self->Comment($reader) and next 14 0 14
Condition66%2/3
$self->CDSect($reader) or $self->Comment($reader)
l!l&&r!l&&!r
2120
MC/DC0%0/2
$self->CDSect($reader) or $self->Comment($reader)
$self->CDSect($reader) $self->Comment($reader)
Truth table66%2/3
A or B
A= $self->CDSect($reader)B= $self->Comment($reader)
ABresult
1X1
011
000
286 or
287 $self->Comment($reader))
288 and next;
289 }
290 elsif ($data =~ /^<\?/) {
291 0 ▶ $self->PI($reader) and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->PI($reader) and next 0 0 0
292 }
293 elsif ($data =~ /^</) {
294 436 ▶ $self->element($reader) and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->element($reader) and next 436 0 436
295 }
296 0 last;
297 }
298
299 0 return 1;
300 }
301  
302 14 ▶sub CDSect {
Subroutine CDSect: called CC=4 SCAR=14.2
Pod undocumented
303 14 my ($self, $reader) = @_;
304
305 14 my $data = $reader->data(9);
306 14 ▶ return 0 unless $data =~ /^<!\[CDATA\[/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /^<!\[CDATA\[/ 12 2 14
307 2 $reader->move_along(9);
308
309 2 $self->start_cdata({});
310
311 2 $data = $reader->data;
312 2 while (1) {
313 2 ▶ $self->parser_error("EOF looking for CDATA section end", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 2 2
314 unless length($data);
315
316 2 ▶ if ($data =~ /^(.*?)\]\]>/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^(.*?)\]\]>/s) { } 2 0 2
317 2 my $chars = $1;
318 2 $reader->move_along(length($chars) + 3);
319 2 $self->characters({Data => $chars});
320 2 last;
321 }
322 else {
323 0 $self->characters({Data => $data});
324 0 $reader->move_along(length($data));
325 0 $data = $reader->data;
326 }
327 }
328 2 $self->end_cdata({});
329 2 return 1;
330 }
331  
332 946 ▶sub CharData {
Subroutine CharData: called CC=5 SCAR=16.1
Pod undocumented
333 946 my ($self, $reader) = @_;
334
335 946 my $data = $reader->data;
336
337 946 while (1) {
338 954 ▶ return unless length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 954 954
339
340 954 ▶ if ($data =~ /^([^<&]*)[<&]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^([^<&]*)[<&]/s) { } 946 8 954
341 946 my $chars = $1;
342 946 ▶ $self->parser_error("String ']]>' not allowed in character data", $reader)
Branch: 1 branch
BranchTrueFalseTotal
if $chars =~ /\]\]>/ 0 946 946
343 if $chars =~ /\]\]>/;
344 946 $reader->move_along(length($chars));
345 946 ▶ $self->characters({Data => $chars}) if length($chars);
Branch: 1 branch
BranchTrueFalseTotal
if length $chars 935 11 946
346 946 last;
347 }
348 else {
349 8 $self->characters({Data => $data});
350 8 $reader->move_along(length($data));
351 8 $data = $reader->data;
352 }
353 }
354 }
355  
356 72 ▶sub Misc {
Subroutine Misc: called CC=4 SCAR=13.9
Pod undocumented
357 72 my ($self, $reader) = @_;
358 72 ▶ if ($self->Comment($reader)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->Comment($reader)) { } 4 67 71
elsif ($self->PI($reader)) { } 5 62 67
elsif ($self->skip_whitespace($reader)) { } 39 23 62
359 4 return 1;
360 }
361 elsif ($self->PI($reader)) {
362 5 return 1;
363 }
364 elsif ($self->skip_whitespace($reader)) {
365 39 return 1;
366 }
367
368 23 return 0;
369 }
370  
371 51 ▶sub Reference {
Subroutine Reference: called CC=12 SCAR=40.4
Pod undocumented
372 51 my ($self, $reader) = @_;
373
374 51 ▶ return 0 unless $reader->match('&');
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->match("&") 0 51 51
375
376 51 my $data = $reader->data;
377  
378 # Fetch more data if we have an incomplete numeric reference
379 51 ▶ if ($data =~ /^(#\d*|#x[0-9a-fA-F]*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^(#\d*|#x[0-9a-fA-F]*)$/) 0 51 51
380 0 $data = $reader->data(length($data) + 6);
381 }
382
383 51 ▶ if ($data =~ /^#x([0-9a-fA-F]+);/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^#x([0-9a-fA-F]+);/) { } 0 51 51
elsif ($data =~ /^#([0-9]+);/) { } 0 51 51
384 0 my $ref = $1;
385 0 $reader->move_along(length($ref) + 3);
386 0 my $char = chr_ref(hex($ref));
387 0 ▶ $self->parser_error("Character reference &#$ref; refers to an illegal XML character ($char)", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless $char =~ /$XML::SAX::PurePerl::SingleChar/o 0 0 0
388 unless $char =~ /$SingleChar/o;
389 0 $self->characters({ Data => $char });
390 0 return 1;
391 }
392 elsif ($data =~ /^#([0-9]+);/) {
393 0 my $ref = $1;
394 0 $reader->move_along(length($ref) + 2);
395 0 my $char = chr_ref($ref);
396 0 ▶ $self->parser_error("Character reference &#$ref; refers to an illegal XML character ($char)", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless $char =~ /$XML::SAX::PurePerl::SingleChar/o 0 0 0
397 unless $char =~ /$SingleChar/o;
398 0 $self->characters({ Data => $char });
399 0 return 1;
400 }
401 else {
402 # EntityRef
403 51 ▶ my $name = $self->Name($reader)
Condition33%1/3
$self->Name($reader) || $self->parser_error('Invalid name in entity', $reader)
l!l&&r!l&&!r
5100
MC/DC0%0/2
$self->Name($reader) || $self->parser_error('Invalid name in entity', $reader)
$self->Name($reader) $self->parser_error('Invalid name in entity', $reader)
Truth table33%1/3
A || B
A= $self->Name($reader)B= $self->parser_error('Invalid name in entity', $reader)
ABresult
1X1
011
000
404 || $self->parser_error("Invalid name in entity", $reader);
405 51 ▶ $reader->match(';') or $self->parser_error("No semi-colon found after entity name", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$reader->match(";") or $self->parser_error("No semi-colon found after entity name", $reader) 51 0 51
406
407 # warn("got entity: \&$name;\n");
408
409 # expand it
410 51 ▶ if ($self->_is_entity($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_entity($name)) { } 0 51 51
elsif ($name =~ /^(?:amp|gt|lt|quot|apos)$/) { } 51 0 51
411
412 0 ▶ if ($self->_is_external($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_external($name)) { } 0 0 0
413 0 my $value = $self->_get_entity($name);
414 0 my $ent_reader = XML::SAX::PurePerl::Reader::URI->new($value);
415 0 $self->encoding_detect($ent_reader);
416 0 $self->extParsedEnt($ent_reader);
417 }
418 else {
419 0 my $value = $self->_stringify_entity($name);
420 0 my $ent_reader = XML::SAX::PurePerl::Reader::String->new($value);
421 0 $self->content($ent_reader);
422 }
423 0 return 1;
424 }
425 elsif ($name =~ /^(?:amp|gt|lt|quot|apos)$/) {
426 51 $self->characters({ Data => $int_ents{$name} });
427 51 return 1;
428 }
429 else {
430 0 $self->parser_error("Undeclared entity", $reader);
431 }
432 }
433 }
434  
435 3 ▶sub AttReference {
Subroutine AttReference: called CC=8 SCAR=24.4
Pod undocumented
436 3 my ($self, $name, $reader) = @_;
437 3 ▶ if ($name =~ /^#x([0-9a-fA-F]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^#x([0-9a-fA-F]+)$/) { } 1 2 3
elsif ($name =~ /^#([0-9]+)$/) { } 1 1 2
438 1 my $chr = chr_ref(hex($1));
439 1 ▶ $chr =~ /$SingleChar/o or $self->parser_error("Character reference '&$name;' refers to an illegal XML character", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$chr =~ /$XML::SAX::PurePerl::SingleChar/o or $self->parser_error("Character reference '&$name;' refers to an illegal XML character", $reader) 1 0 1
440 1 return $chr;
441 }
442 elsif ($name =~ /^#([0-9]+)$/) {
443 1 my $chr = chr_ref($1);
444 1 ▶ $chr =~ /$SingleChar/o or $self->parser_error("Character reference '&$name;' refers to an illegal XML character", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$chr =~ /$XML::SAX::PurePerl::SingleChar/o or $self->parser_error("Character reference '&$name;' refers to an illegal XML character", $reader) 1 0 1
445 1 return $chr;
446 }
447 else {
448 1 ▶ if ($self->_is_entity($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_entity($name)) { } 0 1 1
elsif ($name =~ /^(?:amp|lt|gt|quot|apos)$/) { } 1 0 1
449 0 ▶ if ($self->_is_external($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_external($name)) { } 0 0 0
450 0 $self->parser_error("No external entity references allowed in attribute values", $reader);
451 }
452 else {
453 0 my $value = $self->_stringify_entity($name);
454 0 return $value;
455 }
456 }
457 elsif ($name =~ /^(?:amp|lt|gt|quot|apos)$/) {
458 1 return $int_ents{$name};
459 }
460 else {
461 0 $self->parser_error("Undeclared entity '$name'", $reader);
462 }
463 }
464 }
465  
466 0 ▶sub extParsedEnt {
Subroutine extParsedEnt: not called CC=1 SCAR=6.9
Pod undocumented
467 0 my ($self, $reader) = @_;
468
469 0 $self->TextDecl($reader);
470 0 $self->content($reader);
471 }
472  
473 0 ▶sub _is_external {
Subroutine _is_external: not called CC=2 SCAR=17.9
474 0 my ($self, $name) = @_;
475 # TODO: Fix this to use $reader to store the entities perhaps.
476 0 ▶ if ($self->{ParseOptions}{external_entities}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ParseOptions"}{"external_entities"}{$name}) 0 0 0
477 0 return 1;
478 }
479 0 return ;
480 }
481  
482 52 ▶sub _is_entity {
Subroutine _is_entity: called CC=2 SCAR=7.6
483 52 my ($self, $name) = @_;
484 # TODO: ditto above
485 52 ▶ if (exists $self->{ParseOptions}{entities}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"ParseOptions"}{"entities"}{$name}) 0 52 52
486 0 return 1;
487 }
488 52 return 0;
489 }
490  
491 0 ▶sub _stringify_entity {
Subroutine _stringify_entity: not called CC=3 SCAR=24.8
492 0 my ($self, $name) = @_;
493 # TODO: ditto above
494 0 ▶ if (exists $self->{ParseOptions}{expanded_entity}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"ParseOptions"}{"expanded_entity"}{$name}) 0 0 0
495 0 return $self->{ParseOptions}{expanded_entity}{$name};
496 }
497 # expand
498 0 my $reader = XML::SAX::PurePerl::Reader::URI->new($self->{ParseOptions}{entities}{$name});
499 0 my $ent = '';
500 0 while(1) {
501 0 my $data = $reader->data;
502 0 $ent .= $data;
503 0 ▶ $reader->move_along(length($data)) or last;
Branch: 1 branch
BranchTrueFalseTotal
$reader->move_along(length $data) or last 0 0 0
504 }
505 0 return $self->{ParseOptions}{expanded_entity}{$name} = $ent;
506 }
507  
508 0 ▶sub _get_entity {
Subroutine _get_entity: not called CC=1 SCAR=6.9
509 0 my ($self, $name) = @_;
510 # TODO: ditto above
511 0 return $self->{ParseOptions}{entities}{$name};
512 }
513  
514 7465 ▶sub skip_whitespace {
Subroutine skip_whitespace: called CC=3 SCAR=11.0
Pod undocumented
515 7465 my ($self, $reader) = @_;
516
517 7465 my $data = $reader->data;
518
519 7465 my $found = 0;
520 7465 ▶ while ($data =~ s/^([\x20\x0A\x0D\x09]*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/^([\x20\x0A\x0D\x09]*)//) 9534 0 9534
521 9534 ▶ last unless length($1);
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 7465 2069 9534
522 2069 $found++;
523 2069 $reader->move_along(length($1));
524 2069 $data = $reader->data;
525 }
526
527 7465 return $found;
528 }
529  
530 2460 ▶sub Attribute {
Subroutine Attribute: called CC=6 SCAR=19.2
Pod undocumented
531 2460 my ($self, $reader) = @_;
532
533 2460 ▶ $self->skip_whitespace($reader) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->skip_whitespace($reader) or return 2007 453 2460
534
535 2007 my $data = $reader->data(2);
536 2007 ▶ return if $data =~ /^\/?>/;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ m[^/?>] 5 2002 2007
537
538 2002 ▶ if (my $name = $self->Name($reader)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->Name($reader)) 2002 0 2002
539 2002 $self->skip_whitespace($reader);
540 2002 ▶ $reader->match('=') or $self->parser_error("No '=' in Attribute", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$reader->match("=") or $self->parser_error("No '=' in Attribute", $reader) 2002 0 2002
541 2002 $self->skip_whitespace($reader);
542 2002 my $value = $self->AttValue($reader);
543  
544 2002 ▶ if (!$self->cdata_attrib($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cdata_attrib($name)) 0 2002 2002
545 0 $value =~ s/^\x20*//; # discard leading spaces
546 0 $value =~ s/\x20*$//; # discard trailing spaces
547 0 $value =~ s/ {1,}/ /g; # all >1 space to single space
548 }
549
550 2002 return $name, $value;
551 }
552
553 0 return;
554 }
555  
556 2002 ▶sub cdata_attrib {
Subroutine cdata_attrib: called CC=1 SCAR=0.0
Pod undocumented
557 # TODO implement this!
558 2002 return 1;
559 }
560  
561 2002 ▶sub AttValue {
Subroutine AttValue: called CC=4 SCAR=13.9
Pod undocumented
562 2002 my ($self, $reader) = @_;
563
564 2002 my $quote = $self->quote($reader);
565
566 2002 my $value = '';
567
568 2002 while (1) {
569 2007 my $data = $reader->data;
570 2007 ▶ $self->parser_error("EOF found while looking for the end of attribute value", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 2007 2007
571 unless length($data);
572 2007 ▶ if ($data =~ /^([^$quote]*)$quote/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^([^$quote]*)$quote/) { } 2002 5 2007
573 2002 $reader->move_along(length($1) + 1);
574 2002 $value .= $1;
575 2002 last;
576 }
577 else {
578 5 $value .= $data;
579 5 $reader->move_along(length($data));
580 }
581 }
582
583 2002 ▶ if ($value =~ /</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /</) 0 2002 2002
584 0 $self->parser_error("< character not allowed in attribute values", $reader);
585 }
586
587 2002 $value =~ s/[\x09\x0A\x0D]/\x20/g;
588 2002 $value =~ s/&(#(x[0-9a-fA-F]+)|#([0-9]+)|\w+);/$self->AttReference($1, $reader)/geo;
589
590 2002 return $value;
591 }
592  
593 84 ▶sub Comment {
Subroutine Comment: called CC=5 SCAR=16.3
Pod undocumented
594 84 my ($self, $reader) = @_;
595
596 84 my $data = $reader->data(4);
597 84 ▶ if ($data =~ /^<!--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^<!--/) 17 67 84
598 17 $reader->move_along(4);
599 17 my $comment_str = '';
600 17 while (1) {
601 17 my $data = $reader->data;
602 17 ▶ $self->parser_error("End of data seen while looking for close comment marker", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 17 17
603 unless length($data);
604 17 ▶ if ($data =~ /^(.*?)-->/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^(.*?)-->/s) { } 17 0 17
605 17 $comment_str .= $1;
606 17 ▶ $self->parser_error("Invalid comment (dash)", $reader) if $comment_str =~ /-$/;
Branch: 1 branch
BranchTrueFalseTotal
if $comment_str =~ /-$/ 1 16 17
607 16 $reader->move_along(length($1) + 3);
608 16 last;
609 }
610 else {
611 0 $comment_str .= $data;
612 0 $reader->move_along(length($data));
613 }
614 }
615
616 16 $self->comment({ Data => $comment_str });
617
618 16 return 1;
619 }
620 67 return 0;
621 }
622  
623 67 ▶sub PI {
Subroutine PI: called CC=7 SCAR=20.0
Pod undocumented
624 67 my ($self, $reader) = @_;
625
626 67 my $data = $reader->data(2);
627
628 67 ▶ if ($data =~ /^<\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^<\?/) 5 62 67
629 5 $reader->move_along(2);
630 5 my ($target);
631 5 ▶ $target = $self->Name($reader) ||
Condition33%1/3
$self->Name($reader) || $self->parser_error('PI has no target', $reader)
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->Name($reader) || $self->parser_error('PI has no target', $reader)
$self->Name($reader) $self->parser_error('PI has no target', $reader)
Truth table33%1/3
A || B
A= $self->Name($reader)B= $self->parser_error('PI has no target', $reader)
ABresult
1X1
011
000
632 $self->parser_error("PI has no target", $reader);
633
634 5 my $pi_data = '';
635 5 ▶ if ($self->skip_whitespace($reader)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_whitespace($reader)) { } 3 2 5
636 3 while (1) {
637 3 my $data = $reader->data;
638 3 ▶ $self->parser_error("End of data seen while looking for close PI marker", $reader)
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 3 3
639 unless length($data);
640 3 ▶ if ($data =~ /^(.*?)\?>/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^(.*?)\?>/s) { } 3 0 3
641 3 $pi_data .= $1;
642 3 $reader->move_along(length($1) + 2);
643 3 last;
644 }
645 else {
646 0 $pi_data .= $data;
647 0 $reader->move_along(length($data));
648 }
649 }
650 }
651 else {
652 2 my $data = $reader->data(2);
653 2 ▶ $data =~ /^\?>/ or $self->parser_error("PI closing sequence not found", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^\?>/ or $self->parser_error("PI closing sequence not found", $reader) 2 0 2
654 2 $reader->move_along(2);
655 }
656
657 5 $self->processing_instruction({ Target => $target, Data => $pi_data });
658
659 5 return 1;
660 }
661 62 return 0;
662 }
663  
664 2962 ▶sub Name {
Subroutine Name: called CC=6 SCAR=18.2
Pod undocumented
665 2962 my ($self, $reader) = @_;
666
667 2962 my $name = '';
668 2962 while(1) {
669 2966 my $data = $reader->data;
670 2966 ▶ return unless length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 2966 2966
671 2966 ▶ $data =~ /^([^\s>\/&\?;=<\)\(\[\],\%\#\!\*\|]*)/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ m{^([^\s>/&\?;=<\)\(\[\],\%\#\!\*\|]*)} or return 2966 0 2966
672 2966 $name .= $1;
673 2966 my $len = length($1);
674 2966 $reader->move_along($len);
675 2966 ▶ last if ($len != length($data));
Branch: 1 branch
BranchTrueFalseTotal
if $len != length $data 2962 4 2966
676 }
677
678 2962 ▶ return unless length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 2962 2962
679
680 2962 ▶ $name =~ /$NameChar/o or $self->parser_error("Name <$name> does not match NameChar production", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /$XML::SAX::PurePerl::NameChar/o or $self->parser_error("Name <$name> does not match NameChar production", $reader) 2962 0 2962
681  
682 2962 return $name;
683 }
684  
685 2003 ▶sub quote {
Subroutine quote: called CC=2 SCAR=7.0
Pod undocumented
686 2003 my ($self, $reader) = @_;
687
688 2003 my $data = $reader->data;
689
690 2003 ▶ $data =~ /^(['"])/ or $self->parser_error("Invalid quote token", $reader);
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^(['"])/ or $self->parser_error("Invalid quote token", $reader) 2003 0 2003
691 2003 $reader->move_along(1);
692 2003 return $1;
693 }
694  
695 14 1;
696 __END__
« SAX/PurePerl/Reader/URI.pm ↑ Summary SAX/PurePerl/Reader.pm »