DOM2/Parser.pm

filter: Statementstmt 67.0% 57 / 85 Branchbran 56.6% 17 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 64.7% 11 / 17 Podpod 100% 14 / 14 totaltotal 66.0% 103 / 156 CC 20 SCAR 25.7
CC
20
Cov
64%
start_element
28.7
characters
3.1
text
3.0
CRAP 13.1
« DOM2/DOM/Document.pm ↑ Summary DOM2/Element/Document.pm »
1 package XML::DOM2::Parser;
2  
3 =head1 NAME
4  
5 XML::DOM2::Parser - Sax based xml parser for XML::DOM2
6  
7 =head1 DESCRIPTION
8  
9 This parser was constructed using XML::SAX::PurePerl which
10 Was known at the time to lack a number of calls which where
11 important for dealing with things like document type and
12 text formating and xml decls. hopfully in the future this
13 will be fixed and this method will be able to take advantage
14 of those part of an xml page.
15  
16 =cut
17  
18 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head2 $parser->new( %options )
23  
24 Create a new parser object.
25  
26 =cut
27 sub new
28 2 ▶{
Subroutine new: called CC=2 SCAR=7.4
Pod documented
29 2 my ($proto, %opts) = @_;
30 2 $opts{'inline'} = 1;
31 2 ▶ if(not $opts{'document'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"document"}) 0 2 2
32 0 croak "Unable to parse xml without document";
33 }
34 2 return bless \%opts, $proto;
35 }
36  
37 =head2 $parser->document()
38  
39 Return the document object
40  
41 =cut
42 sub document
43 28 ▶{
Subroutine document: called CC=1 SCAR=0.0
Pod documented
44 28 my ($self) = @_;
45 28 return $self->{'document'};
46 }
47  
48 =head2 $parser->start_document( $document )
49  
50 Called at the start of a document.
51  
52 =cut
53 2 ▶sub start_document {
Subroutine start_document: called CC=1 SCAR=0.0
Pod documented
54 2 my ($self, $doc) = @_;
55 2 $self->{'inline'} = 0;
56 }
57  
58 =head2 $parser->end_document()
59  
60 Called at the end of a document.
61  
62 =cut
63 2 ▶sub end_document {
Subroutine end_document: called CC=1 SCAR=0.0
Pod documented
64 2 my ($self) = @_;
65 }
66  
67 =head2 $parser->start_element( $node )
68  
69 Start a new xml element
70  
71 =cut
72 sub start_element
73 20 ▶{
Subroutine start_element: called CC=15 SCAR=33.6
Pod documented
74 20 my ($self, $node) = @_;
75 20 $self->text;
76 # ELEMENT
77 # LocalName - The name of the element minus any namespace prefix it may have come with in the document.
78 # NamespaceURI - The URI of the namespace associated with this element, or the empty string for none.
79 # Attributes - A set of attributes as described below.
80 # Name - The name of the element as it was seen in the document (i.e. including any prefix associated with it)
81 # Prefix - The prefix used to qualify this element’s namespace, or the empty string if none.
82  
83 20 my $element;
84 20 my $parent = $self->{'parent'};
85  
86 20 ▶ if(not $parent and not $self->{'inline'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent or $self->{"inline"}) 2 18 20
Condition66%2/3
$parent or $self->{"inline"}
l!l&&r!l&&!r
1802
MC/DC50%1/2
$parent or $self->{"inline"}
$parent $self->{"inline"}
Truth table66%2/3
A or B
A= $parentB= $self->{"inline"}
ABresult
1X1
011
000
87 2 $self->document->doctype->name($node->{'LocalName'});
88 }
89  
90 20 ▶ if( $node->{'LocalName'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"LocalName"}) 20 0 20
91 20 ▶ if($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) { } 18 2 20
92 # Name spaces
93 18 ▶ my $ns = $self->document->getNamespace( $node->{'Prefix'} ) if $node->{'Prefix'};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"Prefix"} 0 18 18
94 18 ▶ warn "Could not get namespace for node: ".$node->{'Prefix'}."\n" if $node->{'Prefix'} && not defined($ns);
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"Prefix"} and not defined $ns 0 18 18
Condition33%1/3
$node->{"Prefix"} and not defined $ns
!ll&&!rl&&r
1800
MC/DC0%0/2
$node->{"Prefix"} and not defined $ns
$node->{"Prefix"} not defined $ns
Truth table33%1/3
A and not(B)
A= $node->{"Prefix"}B= not defined $ns
ABresult
0X0
100
111
95 18 $element = $parent->createChildElement($node->{'LocalName'},
96 document => $self->document,
97 namespace => $ns,
98 );
99 } else {
100 # This would be a root element (document)
101 2 $self->{'parents'} = [];
102 2 $element = $self->document->createElement( $node->{'LocalName'}, document => $self->document );
103 2 $self->document->documentElement($element);
104 # Name spaces, we do this first so later on we don't try adding attributes
105 # into the document element that have namespaces yet to be added in the hash
106 # order (perl!)
107 2 my $ns = $self->document->getNamespace( 'xmlns' );
108 2 foreach my $a (keys(%{$node->{'Attributes'}})) {
109 0 my $attribute = $node->{'Attributes'}->{$a};
110 0 ▶ if($attribute->{'Name'} eq 'xmlns') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attribute->{'Name'} eq 'xmlns') { } 0 0 0
elsif ($attribute->{'Prefix'} eq 'xmlns') { } 0 0 0
111 # warn "Namespace ".$attribute->{'Prefix'}.':'.$attribute->{'Name'}.'='.$attribute->{'Value'}." to ".$node->{'Name'}."\n";
112 0 $element->setAttribute( $attribute->{'LocalName'}, $attribute->{'Value'} );
113 } elsif($attribute->{'Prefix'} eq 'xmlns') {
114 # warn "NSW ".$attribute->{'Prefix'}.':'.$attribute->{'Name'}.'='.$attribute->{'Value'}." to ".$node->{'Name'}."\n";
115 0 $self->document->createNamespace($attribute->{'LocalName'}, $attribute->{'Value'});
116 } else {
117 0 next;
118 }
119 0 delete($node->{'Attributes'}->{$a});
120 }
121 }
122 }
123  
124 # ATTRIBUTES {}
125 # LocalName - The name of the attribute minus any namespace prefix it may have come with in the document.
126 # NamespaceURI - The URI of the namespace associated with this attribute. If the attribute had no prefix, then this consists of just the empty string.
127 # Name - The attribute’s name as it appeared in the document, including any namespace prefix.
128 # Prefix - The prefix used to qualify this attribute’s namepace, or the empty string if none.
129 # Value - VALUE.
130  
131 20 foreach my $attribute (values(%{$node->{'Attributes'}})) {
132 6 ▶ if($attribute->{'Prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->{'Prefix'}) { } 0 6 6
133 0 my $ns = $self->document->getNamespace( $attribute->{'Prefix'} );
134 0 ▶ if(not $ns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns) 0 0 0
135 0 warn "Could not get namespace for attribute: ".$attribute->{'Prefix'}." (".$attribute->{'NamespaceURI'}.")\n";
136 0 next;
137 }
138 0 $element->setAttributeNS( $ns, $attribute->{'LocalName'}, $attribute->{'Value'} );
139 } else {
140 6 $element->setAttribute( $attribute->{'LocalName'}, $attribute->{'Value'} );
141 }
142 }
143  
144 20 ▶ push(@{$self->{'parents'}}, $self->{'parent'})if $self->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"} 18 2 20
145 20 $self->{'parent'} = $element;
146  
147 }
148  
149 =head2 $parser->end_element( $element )
150  
151 Ends an xml element
152  
153 =cut
154 sub end_element
155 20 ▶{
Subroutine end_element: called CC=1 SCAR=0.0
Pod documented
156 20 my ($self, $element) = @_;
157 20 $self->text;
158 # ELEMENT
159 # LocalName - The name of the element minus any namespace prefix it may have come with in the document.
160 # NamespaceURI - The URI of the namespace associated with this element, or the empty string for none.
161 # Name - The name of the element as it was seen in the document (i.e. including any prefix associated with it)
162 # Prefix - The prefix used to qualify this element’s namespace, or the empty string if none.
163 20 $self->{'parent'} = pop @{$self->{'parents'}};
164 }
165  
166 =head2 $parser->characters()
167  
168 Handle part of a cdata by concatination
169  
170 =cut
171 sub characters
172 38 ▶{
Subroutine characters: called CC=3 SCAR=11.4
Pod documented
173 38 my ($self, $text) = @_;
174  
175 38 ▶ $text = $text->() if ref($text) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $text eq "CODE" 0 38 38
176 # We wish to keep track of text characters, and
177 # and deal with text once other elements are found
178 38 ▶ $self->{'text'} = '' if not defined($self->{'-text'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-text"} 38 0 38
179 38 $self->{'text'} .= $text->{'Data'};
180 }
181  
182 =head2 $parser->text()
183  
184 Handle combined text strings as cdata
185  
186 =cut
187 sub text
188 40 ▶{
Subroutine text: called CC=3 SCAR=11.0
Pod documented
189 40 my ($self) = @_;
190 40 ▶ if($self->{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"text"}) 38 2 40
191 38 my $text = $self->{'text'};
192 38 ▶ if($text =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/) 12 26 38
193 12 $self->{'parent'}->cdata($text);
194 }
195 38 delete($self->{'text'});
196 }
197 }
198  
199 =head2 $parser->comment()
200  
201 WARNING: Comments are currently removed!
202  
203 =cut
204 sub comment
205 0 ▶{
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
206 0 my ($self, $comment) = @_;
207 0 $self->text;
208 # warn "Comment '".$comment->{'Data'}."'\n";
209 # Data
210 }
211  
212 =head2 $parser->start_cdata()
213  
214 Never used by parser.
215  
216 =cut
217 sub start_cdata
218 0 ▶{
Subroutine start_cdata: not called CC=1 SCAR=6.9
Pod documented
219 0 print STDERR "START CDATA\n";
220 }
221  
222 =head2 $parser->end_cdata()
223  
224 Never used by parser.
225  
226 =cut
227 sub end_cdata
228 0 ▶{
Subroutine end_cdata: not called CC=1 SCAR=6.9
Pod documented
229 0 print STDERR "END CDATA\n";
230 }
231  
232 =head2 $parser->processing_instruction()
233  
234 Never used by parser.
235  
236 =cut
237 sub processing_instruction
238 0 ▶{
Subroutine processing_instruction: not called CC=1 SCAR=6.9
Pod documented
239 0 print STDERR "PI\n";
240 }
241  
242 =head2 $parser->doctype_decl( $dtd )
243  
244 We want to store the below details for the document creation
245  
246 =cut
247 sub doctype_decl
248 0 ▶{
Subroutine doctype_decl: not called CC=1 SCAR=6.9
Pod documented
249 0 my ($self, $dtd) = @_;
250 0 my $doc = $self->document;
251 # Name
252 # SystemId
253 # PublicId
254 0 warn "Setting doctype name to ".$dtd->{'Name'}."\n";
255 0 $doc->doctype->name($dtd->{'Name'});
256 0 $doc->doctype->systemId($dtd->{'SystemId'});
257 0 $doc->doctype->publicId($dtd->{'PublicId'});
258 # $self->{'dtd'} = $dtd;
259 }
260  
261 =head2 $parser->xml_decl( $xml )
262  
263 Decode the xml decleration information.
264  
265 =cut
266 sub xml_decl
267 0 ▶{
Subroutine xml_decl: not called CC=1 SCAR=6.9
Pod documented
268 0 my ($self, $xml) = @_;
269 0 my $doc = $self->document;
270 # Version
271 # Encoding
272 # Standalone
273 0 $doc->version($xml->{'Version'});
274 0 $doc->encoding($xml->{'Encoding'});
275 0 $doc->standalone($xml->{'Standalone'});
276 # $self->{'xml'} = $xml;
277 }
278  
279 =head1 COPYRIGHT
280  
281 Martin Owens, doctormo@cpan.org
282  
283 =head1 SEE ALSO
284  
285 L<XML::DOM2>,L<XML::SAX>
286  
287 =cut
288 4 1;
« DOM2/DOM/Document.pm ↑ Summary DOM2/Element/Document.pm »