blib/lib/XML/Parser/Lite.pm

filter: Statementstmt 93.1% 108 / 116 Branchbran 75.0% 33 / 44 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 92.5% 25 / 27 Podpod 60.0% 3 / 5 totaltotal 87.5% 175 / 200 SCAR 32.6
CC
25
Cov
88%
end
12.0
__ANON__
6.0
setHandlers
5.0
CRAP 26.1
↑ Summary
1 # ======================================================================
2 #
3 # Copyright (C) 2000-2007 Paul Kulchenko (paulclinger@yahoo.com)
4 # Copyright (C) 2008 Martin Kutter (martin.kutter@fen-net.de)
5 # XML::Parser::Lite is free software; you can redistribute it
6 # and/or modify it under the same terms as Perl itself.
7 #
8 # ======================================================================
9  
10 package XML::Parser::Lite;
11  
12 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.721';
17  
18 sub new {
19 6 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
20  
21 6 ▶ return $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 5 6
22 5 my $self = bless {} => $class;
23  
24 5 my %parameters = @_;
25 5 $self->setHandlers(); # clear first
26 5 ▶ $self->setHandlers(%{$parameters{Handlers} || {}});
Branch: 1 branch
BranchTrueFalseTotal
$parameters{"Handlers"} or {} 3 2 5
27  
28 5 return $self;
29 }
30  
31 sub setHandlers {
32 17 ▶ my $self = shift;
Subroutine setHandlers: called CC=5 SCAR=16.1
Pod documented
33  
34 # allow symbolic refs, avoid "subroutine redefined" warnings
35 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 # clear all handlers if called without parameters
38 17 ▶ if (not @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 11 6 17
39 11 for (qw(Start End Char Final Init Comment Doctype XMLDecl)) {
40 ▶ *$_ = sub {}
Subroutine __ANON__: called CC=1 SCAR=0.0
41 88 }
42 }
43  
44 # we could use each here, too...
45 17 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 17 33
46 16 my($name, $func) = splice(@_, 0, 2);
47 *$name = defined $func
48 ? $func
49 ▶ : sub {}
Subroutine __ANON__: called CC=1 SCAR=0.0
50 16 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
defined $func ? : 14 2 16
51 17 return $self;
52 }
53  
54 sub _regexp {
55 2 ▶ my $patch = shift || '';
Subroutine _regexp: called CC=2 SCAR=6.9
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56 2 my $package = __PACKAGE__;
57  
58 # This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html
59  
60 # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions",
61 # Technical Report TR 1998-17, School of Computing Science, Simon Fraser University, November, 1998.
62 # Copyright (c) 1998, Robert D. Cameron.
63 # The following code may be freely used and distributed provided that
64 # this copyright and citation notice remains intact and that modifications
65 # or additions are clearly identified.
66  
67 # Modifications may be tracked on SOAP::Lite's SVN at
68 # https://soaplite.svn.sourceforge.net/svnroot/soaplite/
69 #
70 2 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 my $TextSE = "[^<]+";
72 2 my $UntilHyphen = "[^-]*-";
73 2 my $Until2Hyphens = "([^-]*)-(?:[^-]$[^-]*-)*-";
74 #my $CommentCE = "$Until2Hyphens(?{${package}::comment(\$2)})>?";
75 2 my $CommentCE = "(.+)--(?{${package}::comment(\$2)})>?";
76 # my $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-";
77 # my $CommentCE = "$Until2Hyphens>?";
78 2 my $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+";
79 2 my $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>";
80 2 my $S = "[ \\n\\t\\r]+";
81 2 my $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
82 2 my $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
83 2 my $Name = "(?:$NameStrt)(?:$NameChar)*";
84 2 my $QuoteSE = "\"[^\"]*\"|'[^']*'";
85 2 my $DT_IdentSE = "$Name(?:$S(?:$Name|$QuoteSE))*";
86 # my $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*";
87 2 my $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>";
88 2 my $S1 = "[\\n\\r\\t ]";
89 2 my $UntilQMs = "[^?]*\\?";
90 2 my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*";
91 2 my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail>))|%$Name;|$S";
92 2 my $DocTypeCE = "$S($DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?)>(?{${package}::_doctype(\$3)})";
93 # my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>";
94 # my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S";
95 # my $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?";
96 2 my $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?";
97 # my $PI_CE = "$Name(?:$PI_Tail)?";
98 2 my $PI_CE = "($Name(?:$PI_Tail))>(?{${package}::_xmldecl(\$5)})";
99 # these expressions were modified for backtracking and events
100 # my $EndTagCE = "($Name)(?{${package}::_end(\$2)})(?:$S)?>";
101 2 my $EndTagCE = "($Name)(?{${package}::_end(\$6)})(?:$S)?>";
102 2 my $AttValSE = "\"([^<\"]*)\"|'([^<']*)'";
103 # my $ElemTagCE = "($Name)(?:$S($Name)(?:$S)?=(?:$S)?(?:$AttValSE)(?{[\@{\$^R||[]},\$4=>defined\$5?\$5:\$6]}))*(?:$S)?(/)?>(?{${package}::_start( \$3,\@{\$^R||[]})})(?{\${7} and ${package}::_end(\$3)})";
104 2 my $ElemTagCE = "($Name)"
105 . "(?:$S($Name)(?:$S)?=(?:$S)?(?:$AttValSE)"
106 . "(?{[\@{\$^R||[]},\$8=>defined\$9?\$9:\$10]}))*(?:$S)?(/)?>"
107 . "(?{${package}::_start(\$7,\@{\$^R||[]})})(?{\$11 and ${package}::_end(\$7)})";
108  
109 2 my $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|/(?:$EndTagCE)?|(?:$ElemTagCE)?)";
110  
111 # Next expression is under "black magic".
112 # Ideally it should be '($TextSE)(?{${package}::char(\$1)})|$MarkupSPE',
113 # but it doesn't work under Perl 5.005 and only magic with
114 # (?:....)?? solved the problem.
115 # I would appreciate if someone let me know what is the right thing to do
116 # and what's the reason for all this magic.
117 # Seems like a problem related to (?:....)? rather than to ?{} feature.
118 # Tests are in t/31-xmlparserlite.t if you decide to play with it.
119 #"(?{[]})(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE";
120 2 "(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE";
121 }
122  
123 2 setHandlers();
124  
125 # Try 5.6 and 5.10 regex first
126 2 my $REGEXP = _regexp('??');
127  
128 sub _parse_re {
129 2 ▶ use re "eval";
Subroutine BEGIN: called CC=1 SCAR=0.0
130 25 ▶ undef $^R;
Subroutine _parse_re: called CC=2 SCAR=6.9
131 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 25 ▶ 1 while $_[0] =~ m{$REGEXP}go
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$REGEXP/go and do { 1 } 736 16 752
133 };
134  
135 # fixup regex if it does not work...
136 {
137 2 ▶ if (not eval { _parse_re('<soap:foo xmlns:soap="foo">bar</soap:foo>'); 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _parse_re("<soap:foo xmlns:soap=\"foo\">bar</soap:foo>"); 1 } }) 0 2 2
138 0 $REGEXP = _regexp();
139 0 local $^W;
140 *_parse_re = sub {
141 2 ▶ use re "eval";
Subroutine BEGIN: called CC=1 SCAR=0.0
142 0 ▶ undef $^R;
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 ▶ 1 while $_[0] =~ m{$REGEXP}go
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$REGEXP/go and do { 1 } 0 0 0
144 0 };
145 }
146 }
147  
148 sub parse {
149 23 ▶ _init();
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
150 23 _parse_re($_[1]);
151 14 _final();
152 }
153  
154 2 my(@stack, $level);
155  
156 sub _init {
157 23 ▶ @stack = ();
Subroutine _init: called CC=1 SCAR=0.0
158 23 $level = 0;
159 23 Init(__PACKAGE__, @_);
160 }
161  
162 sub _final {
163 14 ▶ die "not properly closed tag '$stack[-1]'\n" if @stack;
Subroutine _final: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if @stack 3 11 14
164 11 ▶ die "no element found\n" unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 3 8 11
165 8 Final(__PACKAGE__, @_)
166 }
167  
168 sub _start {
169 106 ▶ die "multiple roots, wrong element '$_[0]'\n" if $level++ && !@stack;
Subroutine _start: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if $level++ and not @stack 2 104 106
Condition100%3/3
$level++ and not @stack
!ll&&!rl&&r
19852
MC/DC100%2/2
$level++ and not @stack
$level++ not @stack
Truth table100%3/3
A and not(B)
A= $level++B= not @stack
ABresult
0X0
100
111
170 104 push(@stack, $_[0]);
171 104 my $r=Start(__PACKAGE__, @_);
172 104 ▶ return ref($r) eq 'ARRAY' ? $r : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 104 104
173 }
174  
175 sub _char {
176 170 ▶ Char(__PACKAGE__, $_[0]), return if @stack;
Subroutine _char: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if @stack 159 11 170
177  
178 # check for junk before or after element
179 # can't use split or regexp due to limitations in ?{} implementation,
180 # will iterate with loop, but we'll do it no more than two times, so
181 # it shouldn't affect performance
182 11 ▶ for (my $i=0; $i < length $_[0]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $_[0]) 21 7 28
183 21 ▶ die "junk '$_[0]' @{[$level ? 'after' : 'before']} XML element\n"
Branch: 2 branches
BranchTrueFalseTotal
if index("\n\r\t ", substr($_[0], $i, 1)) == -1 4 17 21
$level ? : 2 2 4
184 if index("\n\r\t ", substr($_[0],$i,1)) < 0; # or should '< $[' be there
185 }
186 }
187  
188 sub _end {
189 2 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
190 100 ▶ pop(@stack) eq $_[0] or die "mismatched tag '$_[0]'\n";
Subroutine _end: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
pop @stack eq $_[0] or die "mismatched tag '$_[0]'\n" 3 97 100
191 97 my $r=End(__PACKAGE__, $_[0]);
192 97 ▶ return ref($r) eq 'ARRAY' ? $r : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 97 97
193 }
194  
195 sub comment {
196 2 ▶ my $r=Comment(__PACKAGE__, $_[0]);
Subroutine comment: called CC=2 SCAR=7.2
Pod undocumented
197 2 ▶ return ref($r) eq 'ARRAY' ? $r : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 2 2
198 }
199  
200 sub end {
201 0 ▶ pop(@stack) eq $_[0] or die "mismatched tag '$_[0]'\n";
Subroutine end: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
pop @stack eq $_[0] or die "mismatched tag '$_[0]'\n" 0 0 0
202 0 my $r=End(__PACKAGE__, $_[0]);
203 0 ▶ return ref($r) eq 'ARRAY' ? $r : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 0 0
204 }
205  
206 sub _doctype {
207 1 ▶ my $r=Doctype(__PACKAGE__, $_[0]);
Subroutine _doctype: called CC=2 SCAR=7.2
208 1 ▶ return ref($r) eq 'ARRAY' ? $r : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'ARRAY' ? : 0 1 1
209 }
210  
211 sub _xmldecl {
212 2 ▶ XMLDecl(__PACKAGE__, $_[0]);
Subroutine _xmldecl: called CC=1 SCAR=0.0
213 }
214  
215  
216  
217 # ======================================================================
218 2 1;
219  
220 __END__
↑ Summary