lib/XML/DOM/Lite/Parser.pm

filter: Statementstmt 83.7% 139 / 166 Branchbran 70.3% 38 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 3 totaltotal 80.0% 192 / 240 SCAR 35.6
CC
29
Cov
80%
parseFile
12.0
_mk_gen_node
8.1
parse
6.0
CRAP 35.3
« blib/lib/XML/DOM/Lite/NodeList.pm ↑ Summary blib/lib/XML/DOM/Lite/Document.pm »
1 package XML::DOM::Lite::Parser;
2  
3 8 ▶use XML::DOM::Lite::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use XML::DOM::Lite::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use XML::DOM::Lite::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #========================================================================
8 # These regular expressions have been gratefully borrowed from:
9 #
10 # REX/Perl 1.0
11 # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions",
12 # Technical Report TR 1998-17, School of Computing Science, Simon Fraser
13 # University, November, 1998.
14 # Copyright (c) 1998, Robert D. Cameron.
15 # The following code may be freely used and distributed provided that
16 # this copyright and citation notice remains intact and that modifications
17 # or additions are clearly identified.
18  
19 8 our $TextSE = "[^<]+";
20 8 our $UntilHyphen = "[^-]*-";
21 8 our $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-";
22 8 our $CommentCE = "$Until2Hyphens>?";
23 8 our $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+";
24 8 our $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>";
25 8 our $S = "[ \\n\\t\\r]+";
26 8 our $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
27 8 our $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
28 8 our $Name = "(?:$NameStrt)(?:$NameChar)*";
29 8 our $QuoteSE = "\"[^\"]*\"|'[^']*'";
30 8 our $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*";
31 8 our $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>";
32 8 our $S1 = "[\\n\\r\\t ]";
33 8 our $UntilQMs = "[^?]*\\?+";
34 8 our $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>";
35 8 our $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S";
36 8 our $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?";
37 8 our $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?";
38 8 our $PI_CE = "$Name(?:$PI_Tail)?";
39 8 our $EndTagCE = "$Name(?:$S)?>?";
40 8 our $AttValSE = "\"[^<\"]*\"|'[^<']*'";
41 8 our $ElemTagCE = "$Name(?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*(?:$S)?/?>?";
42 8 our $ElementCE = "/(?:$EndTagCE)?|(?:$ElemTagCE)?";
43 8 our $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|(?:$ElementCE)?)";
44 8 our $XML_SPE = "$TextSE|$MarkupSPE";
45  
46 #========================================================================
47  
48 # these have captures for parsing the attributes
49 8 our $AttValSE2 = "\"([^<\"]*)\"|'([^<']*)'";
50 8 our $ElemTagCE2 = "(?:($Name)(?:$S)?=(?:$S)?($AttValSE2))+(?:$S)?/?>?";
51  
52 sub new {
53 11 ▶ my ($class, %options) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
54 11 my $self = bless {
55 stack => [ ],
56 options => \%options,
57 }, $class;
58 11 return $self;
59 }
60  
61 sub parse {
62 12 ▶ my ($self, $XML) = (shift, shift);
Subroutine parse: called CC=6 SCAR=17.9
Pod undocumented
63 12 ▶ unless (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 12 12
64 0 $self = __PACKAGE__->new(@_);
65 }
66 12 my @nodes = $self->_shallow_parse($XML);
67  
68 12 $self->{document} = XML::DOM::Lite::Document->new();
69 12 push @{$self->{stack}}, $self->{document};
70  
71 12 STEP : foreach my $n ( @nodes ) {
72 103 ▶ substr($n, 0, 1) eq '<' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($n, 0, 1) eq "<" and do { substr($n, 1, 1) eq "!" and do { $self->_handle_decl_node($n); next STEP }; substr($n, 1, 1) eq "?" and do { $self->_handle_pi_node($n); next STEP }; $self->_handle_element_node($n); next STEP } 76 27 103
73 76 ▶ substr($n, 1, 1) eq '!' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($n, 1, 1) eq "!" and do { $self->_handle_decl_node($n); next STEP } 1 75 76
74 1 $self->_handle_decl_node($n);
75 1 next STEP;
76 };
77 75 ▶ substr($n, 1, 1) eq '?' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($n, 1, 1) eq "?" and do { $self->_handle_pi_node($n); next STEP } 1 74 75
78 1 $self->_handle_pi_node($n);
79 1 next STEP;
80 };
81 74 $self->_handle_element_node($n);
82 74 next STEP;
83 };
84 27 $self->_handle_text_node($n);
85 }
86  
87 12 return $self->{document};
88 }
89  
90 sub parseFile {
91 0 ▶ my ($self, $filename) = @_;
Subroutine parseFile: not called CC=3 SCAR=24.8
Pod undocumented
92 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
93 0 $self = __PACKAGE__->new;
94 }
95 0 my $stream;
96 {
97 0 ▶ open FH, $filename or
Branch: 1 branch
BranchTrueFalseTotal
open FH, $filename or die "can't open file $filename for reading " . $! 0 0 0
98 die "can't open file $filename for reading ".$!;
99 0 local $/ = undef;
100 0 $stream = <FH>;
101 0 close FH;
102 }
103 0 return $self->parse($stream);
104 }
105  
106 sub _shallow_parse {
107 12 ▶ my ($self, $XML) = @_;
Subroutine _shallow_parse: called CC=4 SCAR=13.9
108  
109 # Check the options.
110 12 my %options = %{$self->{options}};
111 12 ▶ if (defined($options{'whitespace'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"whitespace"}) 11 1 12
112 11 my $mode = $options{'whitespace'};
113 11 ▶ if (index($mode, 'strip') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($mode, "strip") != -1) 9 2 11
114 9 $XML =~ s/>$S/>/sg;
115 9 $XML =~ s/$S</</sg;
116 }
117 11 ▶ if (index($mode, 'normalize') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($mode, "normalize") != -1) 2 9 11
118 2 $XML =~ s/$S/ /sg
119 }
120 }
121  
122 12 return $XML =~ /$XML_SPE/go;
123 }
124  
125 sub _handle_decl_node {
126 1 ▶ my ($self, $decl) = @_;
Subroutine _handle_decl_node: called CC=4 SCAR=15.6
127 1 my $kind;
128 1 my $length = length($decl);
129 1 my $start = 1;
130 1 $parent = $self->{stack}->[$#{$self->{stack}}];
131 1 ▶ substr($decl, 0, 4) eq '<!--' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 4) eq "<!--" and do { $start = 4; $length = $length - $start - 3; $kind = 8 } 1 0 1
132 1 $start = 4;
133 1 $length = $length - $start - 3;
134 1 $kind = COMMENT_NODE;
135 };
136 1 ▶ substr($decl, 0, 9) eq '<![CDATA[' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 9) eq "<![CDATA[" and do { $start = 9; $length = $length - $start - 3; $kind = 4 } 0 1 1
137 0 $start = 9;
138 0 $length = $length - $start - 3;
139 0 $kind = CDATA_SECTION_NODE;
140 };
141 1 ▶ substr($decl, 0, 9) eq '<!DOCTYPE' && do { # I'm cheating here, should be a separate node!
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 9) eq "<!DOCTYPE" and do { $start = 9; $length = $length - $start - 1; $kind = 10 } 0 1 1
142 0 $start = 9;
143 0 $length = $length - $start - 1;
144 0 $kind = DOCUMENT_TYPE_NODE;
145 };
146 1 return $self->_mk_gen_node(substr($decl, $start, $length), $parent, $kind);
147 }
148  
149 sub _handle_pi_node {
150 1 ▶ my ($self, $pi) = @_;
Subroutine _handle_pi_node: called CC=1 SCAR=0.0
151 1 $pi =~ s/^<\?\S+//o;
152 1 $pi =~ s/\?>$//so;
153 1 $parent = $self->{stack}->[$#{$self->{stack}}];
154 1 return $self->_mk_gen_node($pi, $parent, PROCESSING_INSTRUCTION_NODE);
155 }
156  
157 sub _handle_text_node {
158 27 ▶ my ($self, $text) = @_;
Subroutine _handle_text_node: called CC=2 SCAR=7.0
159 27 $parent = $self->{stack}->[$#{$self->{stack}}];
160 27 ▶ $text =~ s/^\n//so; return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 27 27
161 27 return $self->_mk_gen_node($text, $parent, TEXT_NODE);
162 }
163  
164 sub _handle_element_node {
165 74 ▶ my ($self, $elmnt) = @_;
Subroutine _handle_element_node: called CC=3 SCAR=11.0
166 74 ▶ if ($elmnt =~ /^<\/($EndTagCE)/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elmnt =~ m[^</($EndTagCE)]o) { } 34 40 74
elsif ($elmnt =~ /($ElemTagCE)>$/o) { } 40 0 40
167 34 $self->_handle_element_node_end($1);
168 }
169 elsif ($elmnt =~ /($ElemTagCE)>$/o) {
170 40 $self->_handle_element_node_start($1);
171 }
172 }
173  
174 sub _handle_element_node_start {
175 40 ▶ my ($self, $elmnt) = @_;
Subroutine _handle_element_node_start: called CC=3 SCAR=11.0
176 # this node is a child of the last opened node (top of stack)
177 40 my $parent = $self->{stack}->[$#{$self->{stack}}];
178 40 my $node = $self->_mk_element_node($elmnt, $parent);
179  
180 # last opened node to the top of the stack
181 40 push @{$self->{stack}}, $node;
182  
183 # deal with XML style empty tags
184 40 ▶ if ($elmnt =~ /\/$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elmnt =~ m[/$]) 6 34 40
185 6 $node = $self->_handle_element_node_end($elmnt);
186 }
187 40 ▶ if (defined $node->getAttribute('id')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->getAttribute("id")) 7 33 40
188 7 $self->{document}->setElementById($node->getAttribute("id"), $node);
189 }
190  
191 40 return $node;
192 }
193  
194 sub _handle_element_node_end {
195 40 ▶ my ($self, $elmnt) = @_;
Subroutine _handle_element_node_end: called CC=1 SCAR=0.0
196  
197 # node is now closed, pop it off the stack
198 40 pop @{$self->{stack}};
199  
200 # parentNode is now at the top of the stack
201 40 return $self->{stack}->[$#{$self->{stack}}];
202 }
203  
204 sub _mk_gen_node {
205 29 ▶ my ($self, $str, $parent, $type) = @_;
Subroutine _mk_gen_node: called CC=7 SCAR=20.9
206 29 ▶ $parent = $self->{stack}->[$#{$self->{stack}}] unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 29 29
207 29 my $node = XML::DOM::Lite::Node->new({
208 nodeType => $type,
209 nodeValue => $str,
210 });
211  
212 29 $parent->appendChild($node);
213 29 $node->ownerDocument($self->{document});
214  
215 29 ▶ if ($type == DOCUMENT_TYPE_NODE) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type == 10) { } 0 29 29
elsif ($type == 7) { } 1 28 29
elsif ($type == 3) { } 27 1 28
elsif ($type == 4) { } 0 1 1
elsif ($type == 8) { } 1 0 1
216 0 $node->{nodeName} = '#document-type';
217 } elsif ($type == PROCESSING_INSTRUCTION_NODE) {
218 1 $node->{nodeName} = '#processing-instruction';
219 } elsif ($type == TEXT_NODE) {
220 27 $node->{nodeName} = '#text';
221 } elsif ($type == CDATA_SECTION_NODE) {
222 0 $node->{nodeName} = '#cdata';
223 } elsif ($type == COMMENT_NODE) {
224 1 $node->{nodeName} = '#comment';
225 }
226 29 return $node;
227 }
228  
229 sub _mk_text_node {
230 0 ▶ my ($self, $str, $parent) = @_;
Subroutine _mk_text_node: not called CC=2 SCAR=17.9
231 0 ▶ $parent = $self->{stack}->[$#{$self->{stack}}] unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
232  
233 0 my $node = XML::DOM::Lite::Node->new({
234 nodeName => '#text',
235 nodeType => TEXT_NODE,
236 nodeValue => $str,
237 });
238  
239 0 $parent->appendChild($node);
240 0 $node->ownerDocument($self->{document});
241  
242 0 return $node;
243 }
244  
245 sub _mk_element_node {
246 40 ▶ my ($self, $elmnt, $parent) = @_;
Subroutine _mk_element_node: called CC=1 SCAR=0.0
247  
248 40 ($tagName, $elmnt) = split(/\s+/, $elmnt, 2);
249 40 $tagName =~ s/\/$//;
250 40 my $attrs = $self->_parse_attributes($elmnt);
251 40 my $node = XML::DOM::Lite::Node->new({
252 nodeType => ELEMENT_NODE,
253 attributes => $attrs,
254 nodeName => $tagName,
255 tagName => $tagName,
256 });
257 40 $parent->appendChild($node);
258 40 $node->ownerDocument($self->{document});
259  
260 40 return $node;
261 }
262  
263 sub _parse_attributes {
264 40 ▶ my ($self, $elmnt) = @_;
Subroutine _parse_attributes: called CC=4 SCAR=13.9
265  
266 40 my $attrs = XML::DOM::Lite::NodeList->new([ ]);
267 40 ▶ return $attrs unless $elmnt;
Branch: 1 branch
BranchTrueFalseTotal
unless $elmnt 12 28 40
268  
269 28 ▶ while ($elmnt =~ s/$ElemTagCE2//o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elmnt =~ s/$ElemTagCE2//o) 32 28 60
270 push @$attrs, XML::DOM::Lite::Node->new({
271 nodeType => ATTRIBUTE_NODE,
272 nodeName => $1,
273 nodeValue => defined($3) ? $3 : $4,
274 ownerDocument => $self->{document}
275 32 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 30 2 32
276 }
277  
278 28 return $attrs;
279 }
280  
281 8 1;
282  
283  
284 __END__
« blib/lib/XML/DOM/Lite/NodeList.pm ↑ Summary blib/lib/XML/DOM/Lite/Document.pm »