blib/lib/XML/NamespaceSupport.pm

filter: Statementstmt 84.0% 147 / 175 Branchbran 70.5% 48 / 68 Conditioncond 58.3% 14 / 24 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 89.2% 25 / 28 Podpod 93.3% 14 / 15 totaltotal 77.6% 253 / 326 CC 43 SCAR 23.5
CC
43
Cov
78%
declare_prefix
27.1
undeclare_prefix
20.0
new
6.0
CRAP 10.5
↑ Summary
1 package XML::NamespaceSupport;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '1.12'; # VERSION
5  
6 # ABSTRACT: A simple generic namespace processor
7  
8 1 ▶use constant FATALS => 0; # root object
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant NSMAP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant UNKNOWN_PREF => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant AUTO_PREFIX => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant XMLNS_11 => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant DEFAULT => 0; # maps
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant PREFIX_MAP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant DECLARATIONS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use vars qw($NS_XMLNS $NS_XML);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 $NS_XMLNS = 'http://www.w3.org/2000/xmlns/';
19 1 $NS_XML = 'http://www.w3.org/XML/1998/namespace';
20  
21  
22 # add the ns stuff that baud wants based on Java's xml-writer
23  
24 #-------------------------------------------------------------------#
25 # constructor
26 #-------------------------------------------------------------------#
27 2 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
28 2 ▶ my $class = ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
29 2 my $options = shift;
30 2 my $self = [
31 1, # FATALS
32 [[ # NSMAP
33 undef, # DEFAULT
34 { xml => $NS_XML }, # PREFIX_MAP
35 undef, # DECLARATIONS
36 ]],
37 'aaa', # UNKNOWN_PREF
38 0, # AUTO_PREFIX
39 1, # XML_11
40 ];
41 2 ▶ $self->[NSMAP]->[0]->[PREFIX_MAP]->{xmlns} = $NS_XMLNS if $options->{xmlns};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"xmlns"} 1 1 2
42 2 ▶ $self->[FATALS] = $options->{fatal_errors} if defined $options->{fatal_errors};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"fatal_errors"} 1 1 2
43 2 ▶ $self->[AUTO_PREFIX] = $options->{auto_prefix} if defined $options->{auto_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"auto_prefix"} 1 1 2
44 2 ▶ $self->[XMLNS_11] = $options->{xmlns_11} if defined $options->{xmlns_11};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"xmlns_11"} 0 2 2
45 2 return bless $self, $class;
46 }
47  
48 #-------------------------------------------------------------------#
49 # reset() - return to the original state (for reuse)
50 #-------------------------------------------------------------------#
51 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
52 1 my $self = shift;
53 1 $#{$self->[NSMAP]} = 0;
54 }
55  
56 #-------------------------------------------------------------------#
57 # push_context() - add a new empty context to the stack
58 #-------------------------------------------------------------------#
59 5 ▶sub push_context {
Subroutine push_context: called CC=1 SCAR=0.0
Pod documented
60 5 my $self = shift;
61 5 push @{$self->[NSMAP]}, [
62 $self->[NSMAP]->[-1]->[DEFAULT],
63 5 { %{$self->[NSMAP]->[-1]->[PREFIX_MAP]} },
64 [],
65 ];
66 }
67  
68 #-------------------------------------------------------------------#
69 # pop_context() - remove the topmost context from the stack
70 #-------------------------------------------------------------------#
71 3 ▶sub pop_context {
Subroutine pop_context: called CC=2 SCAR=7.0
Pod documented
72 3 my $self = shift;
73 3 ▶ die 'Trying to pop context without push context' unless @{$self->[NSMAP]} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->[1];} > 1 0 3 3
74 3 pop @{$self->[NSMAP]};
75 }
76  
77 #-------------------------------------------------------------------#
78 # declare_prefix() - declare a prefix in the current scope
79 #-------------------------------------------------------------------#
80 12 ▶sub declare_prefix {
Subroutine declare_prefix: called CC=19 SCAR=33.0
Pod documented
81 12 my $self = shift;
82 12 my $prefix = shift;
83 12 my $value = shift;
84  
85 12 ▶ warn <<' EOWARN' unless defined $prefix or $self->[AUTO_PREFIX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix or $self->[3] 0 12 12
Condition66%2/3
defined $prefix or $self->[3]
l!l&&r!l&&!r
1110
MC/DC0%0/2
defined $prefix or $self->[3]
defined $prefix $self->[3]
Truth table66%2/3
A or B
A= defined $prefixB= $self->[3]
ABresult
1X1
011
000
86 Prefix was undefined.
87 If you wish to set the default namespace, use the empty string ''.
88 If you wish to autogenerate prefixes, set the auto_prefix option
89 to a true value.
90 EOWARN
91  
92 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 12 ▶ if ($prefix eq 'xml' and $value ne $NS_XML) {
Branch: 3 branches
BranchTrueFalseTotal
if ($prefix eq 'xml' and $value ne $XML::NamespaceSupport::NS_XML) { } 0 12 12
elsif ($value eq $XML::NamespaceSupport::NS_XML and $prefix ne 'xml') { } 0 12 12
elsif ($value eq $XML::NamespaceSupport::NS_XML and $prefix eq 'xml') { } 0 24 24
Condition33%1/3
$prefix eq "xml" and $value ne $XML::NamespaceSupport::NS_XML
!ll&&!rl&&r
1200
MC/DC0%0/2
$prefix eq "xml" and $value ne $XML::NamespaceSupport::NS_XML
$prefix eq "xml" $value ne $XML::NamespaceSupport::NS_XML
Truth table33%1/3
A and B
A= $prefix eq "xml"B= $value ne $XML::NamespaceSupport::NS_XML
ABresult
0X0
100
111
94 0 die "The xml prefix can only be bound to the $NS_XML namespace."
95 }
96 ▶ elsif ($value eq $NS_XML and $prefix ne 'xml') {
Condition33%1/3
$value eq $XML::NamespaceSupport::NS_XML and $prefix ne "xml"
!ll&&!rl&&r
1200
MC/DC0%0/2
$value eq $XML::NamespaceSupport::NS_XML and $prefix ne "xml"
$value eq $XML::NamespaceSupport::NS_XML $prefix ne "xml"
Truth table33%1/3
A and B
A= $value eq $XML::NamespaceSupport::NS_XMLB= $prefix ne "xml"
ABresult
0X0
100
111
97 0 die "the $NS_XML namespace can only be bound to the xml prefix.";
98 }
99 ▶ elsif ($value eq $NS_XML and $prefix eq 'xml') {
Condition33%1/3
$value eq $XML::NamespaceSupport::NS_XML and $prefix eq "xml"
!ll&&!rl&&r
1200
MC/DC0%0/2
$value eq $XML::NamespaceSupport::NS_XML and $prefix eq "xml"
$value eq $XML::NamespaceSupport::NS_XML $prefix eq "xml"
Truth table33%1/3
A and B
A= $value eq $XML::NamespaceSupport::NS_XMLB= $prefix eq "xml"
ABresult
0X0
100
111
100 0 return 1;
101 }
102 12 ▶ return 0 if index(lc($prefix), 'xml') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index(lc $prefix, "xml") == 0 1 11 12
103 1 ▶ use warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 11 ▶ if (defined $prefix and $prefix eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix eq '') { } 4 7 11
Condition100%3/3
defined $prefix and $prefix eq ""
!ll&&!rl&&r
164
MC/DC100%2/2
defined $prefix and $prefix eq ""
defined $prefix $prefix eq ""
Truth table100%3/3
A and B
A= defined $prefixB= $prefix eq ""
ABresult
0X0
100
111
106 4 $self->[NSMAP]->[-1]->[DEFAULT] = $value;
107 }
108 else {
109 7 ▶ die "Cannot declare prefix $prefix" if $value eq '' and not $self->[XMLNS_11];
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" and not $self->[4] 1 6 7
Condition100%3/3
$value eq "" and not $self->[4]
!ll&&!rl&&r
511
MC/DC100%2/2
$value eq "" and not $self->[4]
$value eq "" not $self->[4]
Truth table100%3/3
A and not(B)
A= $value eq ""B= not $self->[4]
ABresult
0X0
100
111
110 6 ▶ if (not defined $prefix and $self->[AUTO_PREFIX]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $prefix and $self->[3]) { } 1 5 6
elsif (not defined $prefix || $self->[3]) { } 0 5 5
Condition66%2/3
not defined $prefix and $self->[3]
!ll&&!rl&&r
501
MC/DC50%1/2
not defined $prefix and $self->[3]
not defined $prefix $self->[3]
Truth table66%2/3
not(A) and B
A= not defined $prefixB= $self->[3]
ABresult
0X0
100
111
111 1 while (1) {
112 1 $prefix = $self->[UNKNOWN_PREF]++;
113 1 ▶ last if not exists $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[1][-1][1]{$prefix} 1 0 1
114 }
115 }
116 ▶ elsif (not defined $prefix and not $self->[AUTO_PREFIX]) {
Condition33%1/3
defined $prefix || $self->[3]
l!l&&r!l&&!r
500
MC/DC0%0/2
defined $prefix || $self->[3]
defined $prefix $self->[3]
Truth table33%1/3
A || B
A= defined $prefixB= $self->[3]
ABresult
1X1
011
000
117 0 return 0;
118 }
119 6 $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix} = $value;
120 }
121 10 push @{$self->[NSMAP]->[-1]->[DECLARATIONS]}, $prefix;
122 10 return 1;
123 }
124  
125 #-------------------------------------------------------------------#
126 # declare_prefixes() - declare several prefixes in the current scope
127 #-------------------------------------------------------------------#
128 0 ▶sub declare_prefixes {
Subroutine declare_prefixes: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = shift;
130 0 my %prefixes = @_;
131 0 ▶ while (my ($k,$v) = each %prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %prefixes) 0 0 0
132 0 $self->declare_prefix($k,$v);
133 }
134 }
135  
136 #-------------------------------------------------------------------#
137 # undeclare_prefix
138 #-------------------------------------------------------------------#
139 0 ▶sub undeclare_prefix {
Subroutine undeclare_prefix: not called CC=4 SCAR=30.0
Pod documented
140 0 my $self = shift;
141 0 my $prefix = shift;
142 0 ▶ return if not defined($prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
143 0 ▶ return unless exists $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[1][-1][1]{$prefix} 0 0 0
144  
145 0 my ( $tfix ) = grep { $_ eq $prefix } @{$self->[NSMAP]->[-1]->[DECLARATIONS]};
146 0 ▶ if ( not defined $tfix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tfix) 0 0 0
147 0 die "prefix $prefix not declared in this context\n";
148 }
149  
150 0 @{$self->[NSMAP]->[-1]->[DECLARATIONS]} = grep { $_ ne $prefix } @{$self->[NSMAP]->[-1]->[DECLARATIONS]};
151 0 delete $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
152 }
153  
154 #-------------------------------------------------------------------#
155 # get_prefix() - get a (random) prefix for a given URI
156 #-------------------------------------------------------------------#
157 2 ▶sub get_prefix {
Subroutine get_prefix: called CC=3 SCAR=11.0
Pod documented
158 2 my $self = shift;
159 2 my $uri = shift;
160  
161 # we have to iterate over the whole hash here because if we don't
162 # the iterator isn't reset and the next pass will fail
163 2 my $pref;
164 2 ▶ while (my ($k, $v) = each %{$self->[NSMAP]->[-1]->[PREFIX_MAP]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[1][-1][1];}) 7 2 9
165 7 ▶ $pref = $k if $v eq $uri;
Branch: 1 branch
BranchTrueFalseTotal
if $v eq $uri 3 4 7
166 }
167 2 return $pref;
168 }
169  
170 #-------------------------------------------------------------------#
171 # get_prefixes() - get all the prefixes for a given URI
172 #-------------------------------------------------------------------#
173 8 ▶sub get_prefixes {
Subroutine get_prefixes: called CC=2 SCAR=6.9
Pod documented
174 8 my $self = shift;
175 8 my $uri = shift;
176  
177 8 ▶ return keys %{$self->[NSMAP]->[-1]->[PREFIX_MAP]} unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 5 3 8
178 3 return grep { $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$_} eq $uri } keys %{$self->[NSMAP]->[-1]->[PREFIX_MAP]};
179 }
180  
181 #-------------------------------------------------------------------#
182 # get_declared_prefixes() - get all prefixes declared in the last context
183 #-------------------------------------------------------------------#
184 4 ▶sub get_declared_prefixes {
Subroutine get_declared_prefixes: called CC=2 SCAR=6.9
Pod documented
185 4 my $declarations = $_[0]->[NSMAP]->[-1]->[DECLARATIONS];
186 4 ▶ die "At least one context must be pushed onto stack with push_context()\n",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $declarations 1 3 4
187 "before calling get_declared_prefixes()"
188 if not defined $declarations;
189 3 return @{$_[0]->[NSMAP]->[-1]->[DECLARATIONS]};
190 }
191  
192 #-------------------------------------------------------------------#
193 # get_uri() - get a URI given a prefix
194 #-------------------------------------------------------------------#
195 3 ▶sub get_uri {
Subroutine get_uri: called CC=4 SCAR=14.5
Pod documented
196 3 my $self = shift;
197 3 my $prefix = shift;
198  
199 3 ▶ warn "Prefix must not be undef in get_uri(). The emtpy prefix must be ''" unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 3 3
200  
201 3 ▶ return $self->[NSMAP]->[-1]->[DEFAULT] if $prefix eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "" 1 2 3
202 2 ▶ return $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix} if exists $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[1][-1][1]{$prefix} 2 0 2
203 0 return undef;
204 }
205  
206 #-------------------------------------------------------------------#
207 # process_name() - provide details on a name
208 #-------------------------------------------------------------------#
209 2 ▶sub process_name {
Subroutine process_name: called CC=2 SCAR=7.1
Pod documented
210 2 my $self = shift;
211 2 my $qname = shift;
212 2 my $aflag = shift;
213  
214 2 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 0 2 2
215 0 return( ($self->_get_ns_details($qname, $aflag))[0,2], $qname );
216 }
217 else {
218 2 eval { return( ($self->_get_ns_details($qname, $aflag))[0,2], $qname ); }
219 }
220 }
221  
222 #-------------------------------------------------------------------#
223 # process_element_name() - provide details on a element's name
224 #-------------------------------------------------------------------#
225 9 ▶sub process_element_name {
Subroutine process_element_name: called CC=2 SCAR=6.9
Pod documented
226 9 my $self = shift;
227 9 my $qname = shift;
228  
229 9 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 4 5 9
230 4 return $self->_get_ns_details($qname, 0);
231 }
232 else {
233 5 eval { return $self->_get_ns_details($qname, 0); }
234 }
235 }
236  
237  
238 #-------------------------------------------------------------------#
239 # process_attribute_name() - provide details on a attribute's name
240 #-------------------------------------------------------------------#
241 8 ▶sub process_attribute_name {
Subroutine process_attribute_name: called CC=2 SCAR=6.9
Pod documented
242 8 my $self = shift;
243 8 my $qname = shift;
244  
245 8 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 4 4 8
246 4 return $self->_get_ns_details($qname, 1);
247 }
248 else {
249 4 eval { return $self->_get_ns_details($qname, 1); }
250 }
251 }
252  
253  
254 #-------------------------------------------------------------------#
255 # ($ns, $prefix, $lname) = $self->_get_ns_details($qname, $f_attr)
256 # returns ns, prefix, and lname for a given attribute name
257 # >> the $f_attr flag, if set to one, will work for an attribute
258 #-------------------------------------------------------------------#
259 19 ▶sub _get_ns_details {
Subroutine _get_ns_details: called CC=5 SCAR=16.1
260 19 my $self = shift;
261 19 my $qname = shift;
262 19 my $aflag = shift;
263  
264 19 my ($ns, $prefix, $lname);
265 19 ▶ (my ($tmp_prefix, $tmp_lname) = split /:/, $qname, 3)
Branch: 1 branch
BranchTrueFalseTotal
(() = split(/:/, $qname, 3)) < 3 or die "Invalid QName: $qname" 18 1 19
266 < 3 or die "Invalid QName: $qname";
267  
268 # no prefix
269 18 my $cur_map = $self->[NSMAP]->[-1];
270 18 ▶ if (not defined($tmp_lname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tmp_lname) { } 5 13 18
271 5 $prefix = undef;
272 5 $lname = $qname;
273 # attr don't have a default namespace
274 5 ▶ $ns = ($aflag) ? undef : $cur_map->[DEFAULT];
Branch: 1 branch
BranchTrueFalseTotal
$aflag ? : 2 3 5
275 }
276  
277 # prefix
278 else {
279 13 ▶ if (exists $cur_map->[PREFIX_MAP]->{$tmp_prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cur_map->[1]{$tmp_prefix}) { } 9 4 13
280 9 $prefix = $tmp_prefix;
281 9 $lname = $tmp_lname;
282 9 $ns = $cur_map->[PREFIX_MAP]->{$prefix}
283 }
284 else { # no ns -> lname == name, all rest undef
285 4 die "Undeclared prefix: $tmp_prefix";
286 }
287 }
288  
289 14 return ($ns, $prefix, $lname);
290 }
291  
292 #-------------------------------------------------------------------#
293 # parse_jclark_notation() - parse the Clarkian notation
294 #-------------------------------------------------------------------#
295 0 ▶sub parse_jclark_notation {
Subroutine parse_jclark_notation: not called CC=1 SCAR=6.9
Pod undocumented
296 0 shift;
297 0 my $jc = shift;
298 0 $jc =~ m/^\{(.*)\}([^}]+)$/;
299 0 return $1, $2;
300 }
301  
302  
303 #-------------------------------------------------------------------#
304 # Java names mapping
305 #-------------------------------------------------------------------#
306 1 *XML::NamespaceSupport::pushContext = \&push_context;
307 1 *XML::NamespaceSupport::popContext = \&pop_context;
308 1 *XML::NamespaceSupport::declarePrefix = \&declare_prefix;
309 1 *XML::NamespaceSupport::declarePrefixes = \&declare_prefixes;
310 1 *XML::NamespaceSupport::getPrefix = \&get_prefix;
311 1 *XML::NamespaceSupport::getPrefixes = \&get_prefixes;
312 1 *XML::NamespaceSupport::getDeclaredPrefixes = \&get_declared_prefixes;
313 1 *XML::NamespaceSupport::getURI = \&get_uri;
314 1 *XML::NamespaceSupport::processName = \&process_name;
315 1 *XML::NamespaceSupport::processElementName = \&process_element_name;
316 1 *XML::NamespaceSupport::processAttributeName = \&process_attribute_name;
317 1 *XML::NamespaceSupport::parseJClarkNotation = \&parse_jclark_notation;
318 1 *XML::NamespaceSupport::undeclarePrefix = \&undeclare_prefix;
319  
320  
321 1 1;
322  
323 __END__
↑ Summary