blib/lib/XML/LibXML/Reader.pm

filter: Statementstmt 89.8% 88 / 98 Branchbran 73.6% 28 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 83.3% 5 / 6 totaltotal 85.7% 138 / 161 CC 20 SCAR 19.4
CC
20
Cov
85%
new
14.1
setParserProp
12.0
BEGIN
2.1
CRAP 6.9
« blib/lib/XML/LibXML/SAX/Builder.pm ↑ Summary blib/lib/XML/LibXML.pm »
1 # $Id: Reader.pm,v 1.1.2.1 2004/04/20 20:09:48 pajas Exp $
2 #
3 # This is free software, you may use it and distribute it under the same terms as
4 # Perl itself.
5 #
6 # Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
7 #
8 #
9 package XML::LibXML::Reader;
10  
11 4 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 $VERSION = "2.0210"; # VERSION TEMPLATE: DO NOT CHANGE
18  
19 4 ▶use 5.008_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 BEGIN {
22 4 ▶ UNIVERSAL::can('XML::LibXML::Reader','_newForFile') or
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can("XML::LibXML::Reader", "_newForFile") or croak("Cannot use XML::LibXML::Reader module - your libxml2 is compiled without reader support!") 0 4 4
23 croak("Cannot use XML::LibXML::Reader module - ".
24 "your libxml2 is compiled without reader support!");
25 }
26  
27 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 use constant {
29 4 XML_READER_TYPE_NONE => 0,
30 XML_READER_TYPE_ELEMENT => 1,
31 XML_READER_TYPE_ATTRIBUTE => 2,
32 XML_READER_TYPE_TEXT => 3,
33 XML_READER_TYPE_CDATA => 4,
34 XML_READER_TYPE_ENTITY_REFERENCE => 5,
35 XML_READER_TYPE_ENTITY => 6,
36 XML_READER_TYPE_PROCESSING_INSTRUCTION => 7,
37 XML_READER_TYPE_COMMENT => 8,
38 XML_READER_TYPE_DOCUMENT => 9,
39 XML_READER_TYPE_DOCUMENT_TYPE => 10,
40 XML_READER_TYPE_DOCUMENT_FRAGMENT => 11,
41 XML_READER_TYPE_NOTATION => 12,
42 XML_READER_TYPE_WHITESPACE => 13,
43 XML_READER_TYPE_SIGNIFICANT_WHITESPACE => 14,
44 XML_READER_TYPE_END_ELEMENT => 15,
45 XML_READER_TYPE_END_ENTITY => 16,
46 XML_READER_TYPE_XML_DECLARATION => 17,
47  
48 XML_READER_NONE => -1,
49 XML_READER_START => 0,
50 XML_READER_ELEMENT => 1,
51 XML_READER_END => 2,
52 XML_READER_EMPTY => 3,
53 XML_READER_BACKTRACK => 4,
54 XML_READER_DONE => 5,
55 XML_READER_ERROR => 6
56 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 0 ▶sub CLONE_SKIP { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
60  
61 BEGIN {
62  
63 4 ▶%EXPORT_TAGS = (
Subroutine BEGIN: called CC=1 SCAR=0.0
64 types =>
65 [qw(
66 XML_READER_TYPE_NONE
67 XML_READER_TYPE_ELEMENT
68 XML_READER_TYPE_ATTRIBUTE
69 XML_READER_TYPE_TEXT
70 XML_READER_TYPE_CDATA
71 XML_READER_TYPE_ENTITY_REFERENCE
72 XML_READER_TYPE_ENTITY
73 XML_READER_TYPE_PROCESSING_INSTRUCTION
74 XML_READER_TYPE_COMMENT
75 XML_READER_TYPE_DOCUMENT
76 XML_READER_TYPE_DOCUMENT_TYPE
77 XML_READER_TYPE_DOCUMENT_FRAGMENT
78 XML_READER_TYPE_NOTATION
79 XML_READER_TYPE_WHITESPACE
80 XML_READER_TYPE_SIGNIFICANT_WHITESPACE
81 XML_READER_TYPE_END_ELEMENT
82 XML_READER_TYPE_END_ENTITY
83 XML_READER_TYPE_XML_DECLARATION
84 )],
85 states =>
86 [qw(
87 XML_READER_NONE
88 XML_READER_START
89 XML_READER_ELEMENT
90 XML_READER_END
91 XML_READER_EMPTY
92 XML_READER_BACKTRACK
93 XML_READER_DONE
94 XML_READER_ERROR
95 )]
96 );
97 4 @EXPORT = (@{$EXPORT_TAGS{types}},@{$EXPORT_TAGS{states}});
98 4 @EXPORT_OK = @EXPORT;
99 4 $EXPORT_TAGS{all}=\@EXPORT_OK;
100 }
101  
102 4 our %_preserve_flag;
103  
104 {
105 4 my %props = (
106 load_ext_dtd => 1, # load the external subset
107 complete_attributes => 2, # default DTD attributes
108 validation => 3, # validate with the DTD
109 expand_entities => 4, # substitute entities
110 );
111 sub getParserProp {
112 1 ▶ my ($self, $name) = @_;
Subroutine getParserProp: called CC=2 SCAR=7.0
Pod documented
113 1 my $prop = $props{$name};
114 1 ▶ return undef unless defined $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop 0 1 1
115 1 return $self->_getParserProp($prop);
116 }
117 sub setParserProp {
118 0 ▶ my $self = shift;
Subroutine setParserProp: not called CC=3 SCAR=24.8
Pod documented
119 0 ▶ my %args = map { ref($_) eq 'HASH' ? (%$_) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 0 0
120 0 my ($key, $value);
121 0 ▶ while (($key,$value) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
122 0 my $prop = $props{ $key };
123 0 $self->_setParserProp($prop,$value);
124 }
125 0 return;
126 }
127  
128 4 my (%string_pool,%rng_pool,%xsd_pool); # used to preserve data passed to the reader
129 sub new {
130 31 ▶ my ($class) = shift;
Subroutine new: called CC=14 SCAR=26.4
Pod undocumented
131 31 ▶ my %args = map { ref($_) eq 'HASH' ? (%$_) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 1 98 99
132 31 my $encoding = $args{encoding};
133 31 my $URI = $args{URI};
134 31 ▶ $URI="$URI" if defined $URI; # stringify in case it is an URI object
Branch: 1 branch
BranchTrueFalseTotal
if defined $URI 4 27 31
135 31 my $options = XML::LibXML->_parser_options(\%args);
136  
137 31 my $self = undef;
138 31 ▶ if ( defined $args{location} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $args{'location'}) { } 9 22 31
elsif (defined $args{'string'}) { } 19 3 22
elsif (defined $args{'IO'}) { } 1 2 3
elsif (defined $args{'DOM'}) { } 1 1 2
elsif (defined $args{'FD'}) { } 1 0 1
139 9 $self = $class->_newForFile( $args{location}, $encoding, $options );
140 }
141 elsif ( defined $args{string} ) {
142 19 $self = $class->_newForString( $args{string}, $URI, $encoding, $options );
143 19 ▶ if (defined($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 19 0 19
144 19 $string_pool{$self} = \$args{string};
145 }
146 }
147 elsif ( defined $args{IO} ) {
148 1 $self = $class->_newForIO( $args{IO}, $URI, $encoding, $options );
149 }
150 elsif ( defined $args{DOM} ) {
151 croak("DOM must be a XML::LibXML::Document node")
152 1 ▶ unless UNIVERSAL::isa($args{DOM}, 'XML::LibXML::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($args{"DOM"}, "XML::LibXML::Document") 0 1 1
153 1 $self = $class->_newForDOM( $args{DOM} );
154 }
155 elsif ( defined $args{FD} ) {
156 1 my $fd = fileno($args{FD});
157 1 $self = $class->_newForFd( $fd, $URI, $encoding, $options );
158 }
159 else {
160 0 croak("XML::LibXML::Reader->new: specify location, string, IO, DOM, or FD");
161 }
162 31 ▶ if ($args{RelaxNG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"RelaxNG"}) 4 27 31
163 4 ▶ if (ref($args{RelaxNG})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'RelaxNG'}) { } 2 2 4
164 2 $rng_pool{$self} = \$args{RelaxNG};
165 2 $self->_setRelaxNG($args{RelaxNG});
166 } else {
167 2 $self->_setRelaxNGFile($args{RelaxNG});
168 }
169 }
170 31 ▶ if ($args{Schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"Schema"}) 4 27 31
171 4 ▶ if (ref($args{Schema})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'Schema'}) { } 2 2 4
172 2 $xsd_pool{$self} = \$args{Schema};
173 2 $self->_setXSD($args{Schema});
174 } else {
175 2 $self->_setXSDFile($args{Schema});
176 }
177 }
178 31 return $self;
179 }
180 sub DESTROY {
181 31 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
182 31 delete $string_pool{$self};
183 31 delete $rng_pool{$self};
184 31 delete $xsd_pool{$self};
185 31 $self->_DESTROY;
186 }
187 }
188 4 sub close {
189 1 ▶ my ($reader) = @_;
Subroutine close: called CC=2 SCAR=7.2
Pod documented
190 # _close return -1 on failure, 0 on success
191 # perl close returns 0 on failure, 1 on success
192 1 ▶ return $reader->_close == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$reader->_close == 0 ? : 1 0 1
193 }
194  
195 sub preservePattern {
196 2 ▶ my $reader=shift;
Subroutine preservePattern: called CC=2 SCAR=6.9
Pod documented
197 2 my ($pattern,$ns_map)=@_;
198 2 ▶ if (ref($ns_map) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ns_map eq 'HASH') { } 1 1 2
199 # translate prefix=>URL hash to a (URL,prefix) list
200 1 $reader->_preservePattern($pattern,[reverse %$ns_map]);
201 } else {
202 1 $reader->_preservePattern(@_);
203 }
204 }
205  
206 sub nodePath {
207 8 ▶ my $reader=shift;
Subroutine nodePath: called CC=1 SCAR=0.0
Pod documented
208 8 my $path = $reader->_nodePath;
209 8 $path=~s/\[\d+\]//g; # make /foo[1]/bar[1] just /foo/bar, since
210 # sibling count in the buffered fragment is
211 # basically random and generally misleading
212 8 return $path;
213 }
214  
215 4 1;
216 __END__
« blib/lib/XML/LibXML/SAX/Builder.pm ↑ Summary blib/lib/XML/LibXML.pm »