blib/lib/XML/Parser/Lite.pm

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