blib/lib/XML/NamespaceSupport.pm

filter: Statementstmt 96.5% 169 / 175 Branchbran 76.4% 52 / 68 Conditioncond 62.9% 17 / 27 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 100% 28 / 28 Podpod 93.3% 14 / 15 totaltotal 86.7% 287 / 331 CC 44 SCAR 22.5
CC
44
Cov
88%
declare_prefix
27.1
new
6.8
undeclare_prefix
5.0
CRAP 9.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_9'; # TRIAL 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 5 ▶sub new {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
28 5 ▶ my $class = ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 5 5
29 5 my $options = shift;
30 5 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 5 ▶ $self->[NSMAP]->[0]->[PREFIX_MAP]->{xmlns} = $NS_XMLNS if $options->{xmlns};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"xmlns"} 5 0 5
42 5 ▶ $self->[FATALS] = $options->{fatal_errors} if defined $options->{fatal_errors};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"fatal_errors"} 5 0 5
43 5 ▶ $self->[AUTO_PREFIX] = $options->{auto_prefix} if defined $options->{auto_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"auto_prefix"} 5 0 5
44 5 ▶ $self->[XMLNS_11] = $options->{xmlns_11} if defined $options->{xmlns_11};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"xmlns_11"} 0 5 5
45 5 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 8 ▶sub push_context {
Subroutine push_context: called CC=1 SCAR=0.0
Pod documented
60 8 my $self = shift;
61 8 push @{$self->[NSMAP]}, [
62 $self->[NSMAP]->[-1]->[DEFAULT],
63 8 { %{$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 18 ▶sub declare_prefix {
Subroutine declare_prefix: called CC=19 SCAR=33.0
Pod documented
81 18 my $self = shift;
82 18 my $prefix = shift;
83 18 my $value = shift;
84  
85 18 ▶ warn <<' EOWARN' unless defined $prefix or $self->[AUTO_PREFIX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix or $self->[3] 0 18 18
Condition66%2/3
defined $prefix or $self->[3]
l!l&&r!l&&!r
1710
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 18 ▶ 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 18 18
elsif ($value eq $XML::NamespaceSupport::NS_XML and $prefix ne 'xml') { } 0 18 18
elsif ($value eq $XML::NamespaceSupport::NS_XML and $prefix eq 'xml') { } 0 36 36
Condition33%1/3
$prefix eq "xml" and $value ne $XML::NamespaceSupport::NS_XML
!ll&&!rl&&r
1800
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
1800
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
1800
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 18 ▶ return 0 if index(lc($prefix), 'xml') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index(lc $prefix, "xml") == 0 1 17 18
103 1 ▶ use warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 17 ▶ if (defined $prefix and $prefix eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix eq '') { } 4 13 17
Condition100%3/3
defined $prefix and $prefix eq ""
!ll&&!rl&&r
1124
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 13 ▶ 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 12 13
Condition100%3/3
$value eq "" and not $self->[4]
!ll&&!rl&&r
1111
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 12 ▶ if (not defined $prefix and $self->[AUTO_PREFIX]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $prefix and $self->[3]) { } 1 11 12
elsif (not defined $prefix || $self->[3]) { } 0 11 11
Condition66%2/3
not defined $prefix and $self->[3]
!ll&&!rl&&r
1101
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
1100
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 12 $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix} = $value;
120 }
121 16 push @{$self->[NSMAP]->[-1]->[DECLARATIONS]}, $prefix;
122 16 return 1;
123 }
124  
125 #-------------------------------------------------------------------#
126 # declare_prefixes() - declare several prefixes in the current scope
127 #-------------------------------------------------------------------#
128 1 ▶sub declare_prefixes {
Subroutine declare_prefixes: called CC=2 SCAR=6.9
Pod documented
129 1 my $self = shift;
130 1 my %prefixes = @_;
131 1 ▶ while (my ($k,$v) = each %prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %prefixes) 2 1 3
132 2 $self->declare_prefix($k,$v);
133 }
134 }
135  
136 #-------------------------------------------------------------------#
137 # undeclare_prefix
138 #-------------------------------------------------------------------#
139 4 ▶sub undeclare_prefix {
Subroutine undeclare_prefix: called CC=5 SCAR=16.1
Pod documented
140 4 my $self = shift;
141 4 my $prefix = shift;
142  
143 4 ▶ return if not defined $prefix or $prefix eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $prefix or $prefix eq "" 2 2 4
Condition100%3/3
not defined $prefix or $prefix eq ""
l!l&&r!l&&!r
112
MC/DC100%2/2
not defined $prefix or $prefix eq ""
not defined $prefix $prefix eq ""
Truth table100%3/3
not(A) or B
A= not defined $prefixB= $prefix eq ""
ABresult
1X1
011
000
144 2 ▶ return unless exists $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[1][-1][1]{$prefix} 1 1 2
145  
146 1 my ( $tfix ) = grep { $_ eq $prefix } @{$self->[NSMAP]->[-1]->[DECLARATIONS]};
147 1 ▶ if ( not defined $tfix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tfix) 0 1 1
148 0 die "prefix $prefix not declared in this context\n";
149 }
150  
151 1 @{$self->[NSMAP]->[-1]->[DECLARATIONS]} = grep { $_ ne $prefix } @{$self->[NSMAP]->[-1]->[DECLARATIONS]};
152 1 delete $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$prefix};
153 }
154  
155 #-------------------------------------------------------------------#
156 # get_prefix() - get a (random) prefix for a given URI
157 #-------------------------------------------------------------------#
158 4 ▶sub get_prefix {
Subroutine get_prefix: called CC=3 SCAR=11.0
Pod documented
159 4 my $self = shift;
160 4 my $uri = shift;
161  
162 # we have to iterate over the whole hash here because if we don't
163 # the iterator isn't reset and the next pass will fail
164 4 my $pref;
165 4 ▶ while (my ($k, $v) = each %{$self->[NSMAP]->[-1]->[PREFIX_MAP]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[1][-1][1];}) 15 4 19
166 15 ▶ $pref = $k if $v eq $uri;
Branch: 1 branch
BranchTrueFalseTotal
if $v eq $uri 5 10 15
167 }
168 4 return $pref;
169 }
170  
171 #-------------------------------------------------------------------#
172 # get_prefixes() - get all the prefixes for a given URI
173 #-------------------------------------------------------------------#
174 8 ▶sub get_prefixes {
Subroutine get_prefixes: called CC=2 SCAR=6.9
Pod documented
175 8 my $self = shift;
176 8 my $uri = shift;
177  
178 8 ▶ return keys %{$self->[NSMAP]->[-1]->[PREFIX_MAP]} unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 5 3 8
179 3 return grep { $self->[NSMAP]->[-1]->[PREFIX_MAP]->{$_} eq $uri } keys %{$self->[NSMAP]->[-1]->[PREFIX_MAP]};
180 }
181  
182 #-------------------------------------------------------------------#
183 # get_declared_prefixes() - get all prefixes declared in the last context
184 #-------------------------------------------------------------------#
185 3 ▶sub get_declared_prefixes {
Subroutine get_declared_prefixes: called CC=2 SCAR=7.0
Pod documented
186 3 my $declarations = $_[0]->[NSMAP]->[-1]->[DECLARATIONS];
187 3 ▶ die "At least one context must be pushed onto stack with push_context()\n",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $declarations 0 3 3
188 "before calling get_declared_prefixes()"
189 if not defined $declarations;
190 3 return @{$_[0]->[NSMAP]->[-1]->[DECLARATIONS]};
191 }
192  
193 #-------------------------------------------------------------------#
194 # get_uri() - get a URI given a prefix
195 #-------------------------------------------------------------------#
196 9 ▶sub get_uri {
Subroutine get_uri: called CC=4 SCAR=13.9
Pod documented
197 9 my $self = shift;
198 9 my $prefix = shift;
199  
200 9 ▶ 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 9 9
201  
202 9 ▶ return $self->[NSMAP]->[-1]->[DEFAULT] if $prefix eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "" 1 8 9
203 8 ▶ 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} 6 2 8
204 2 return undef;
205 }
206  
207 #-------------------------------------------------------------------#
208 # process_name() - provide details on a name
209 #-------------------------------------------------------------------#
210 2 ▶sub process_name {
Subroutine process_name: called CC=2 SCAR=7.1
Pod documented
211 2 my $self = shift;
212 2 my $qname = shift;
213 2 my $aflag = shift;
214  
215 2 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 0 2 2
216 0 return( ($self->_get_ns_details($qname, $aflag))[0,2], $qname );
217 }
218 else {
219 2 eval { return( ($self->_get_ns_details($qname, $aflag))[0,2], $qname ); }
220 }
221 }
222  
223 #-------------------------------------------------------------------#
224 # process_element_name() - provide details on a element's name
225 #-------------------------------------------------------------------#
226 9 ▶sub process_element_name {
Subroutine process_element_name: called CC=2 SCAR=6.9
Pod documented
227 9 my $self = shift;
228 9 my $qname = shift;
229  
230 9 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 4 5 9
231 4 return $self->_get_ns_details($qname, 0);
232 }
233 else {
234 5 eval { return $self->_get_ns_details($qname, 0); }
235 }
236 }
237  
238  
239 #-------------------------------------------------------------------#
240 # process_attribute_name() - provide details on a attribute's name
241 #-------------------------------------------------------------------#
242 8 ▶sub process_attribute_name {
Subroutine process_attribute_name: called CC=2 SCAR=6.9
Pod documented
243 8 my $self = shift;
244 8 my $qname = shift;
245  
246 8 ▶ if ($self->[FATALS]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]) { } 4 4 8
247 4 return $self->_get_ns_details($qname, 1);
248 }
249 else {
250 4 eval { return $self->_get_ns_details($qname, 1); }
251 }
252 }
253  
254  
255 #-------------------------------------------------------------------#
256 # ($ns, $prefix, $lname) = $self->_get_ns_details($qname, $f_attr)
257 # returns ns, prefix, and lname for a given attribute name
258 # >> the $f_attr flag, if set to one, will work for an attribute
259 #-------------------------------------------------------------------#
260 19 ▶sub _get_ns_details {
Subroutine _get_ns_details: called CC=5 SCAR=16.1
261 19 my $self = shift;
262 19 my $qname = shift;
263 19 my $aflag = shift;
264  
265 19 my ($ns, $prefix, $lname);
266 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
267 < 3 or die "Invalid QName: $qname";
268  
269 # no prefix
270 18 my $cur_map = $self->[NSMAP]->[-1];
271 18 ▶ if (not defined($tmp_lname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tmp_lname) { } 5 13 18
272 5 $prefix = undef;
273 5 $lname = $qname;
274 # attr don't have a default namespace
275 5 ▶ $ns = ($aflag) ? undef : $cur_map->[DEFAULT];
Branch: 1 branch
BranchTrueFalseTotal
$aflag ? : 2 3 5
276 }
277  
278 # prefix
279 else {
280 13 ▶ if (exists $cur_map->[PREFIX_MAP]->{$tmp_prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cur_map->[1]{$tmp_prefix}) { } 9 4 13
281 9 $prefix = $tmp_prefix;
282 9 $lname = $tmp_lname;
283 9 $ns = $cur_map->[PREFIX_MAP]->{$prefix}
284 }
285 else { # no ns -> lname == name, all rest undef
286 4 die "Undeclared prefix: $tmp_prefix";
287 }
288 }
289  
290 14 return ($ns, $prefix, $lname);
291 }
292  
293 #-------------------------------------------------------------------#
294 # parse_jclark_notation() - parse the Clarkian notation
295 #-------------------------------------------------------------------#
296 2 ▶sub parse_jclark_notation {
Subroutine parse_jclark_notation: called CC=1 SCAR=0.0
Pod undocumented
297 2 shift;
298 2 my $jc = shift;
299 2 $jc =~ m/^\{(.*)\}([^}]+)$/;
300 2 return $1, $2;
301 }
302  
303  
304 #-------------------------------------------------------------------#
305 # Java names mapping
306 #-------------------------------------------------------------------#
307 1 *XML::NamespaceSupport::pushContext = \&push_context;
308 1 *XML::NamespaceSupport::popContext = \&pop_context;
309 1 *XML::NamespaceSupport::declarePrefix = \&declare_prefix;
310 1 *XML::NamespaceSupport::declarePrefixes = \&declare_prefixes;
311 1 *XML::NamespaceSupport::getPrefix = \&get_prefix;
312 1 *XML::NamespaceSupport::getPrefixes = \&get_prefixes;
313 1 *XML::NamespaceSupport::getDeclaredPrefixes = \&get_declared_prefixes;
314 1 *XML::NamespaceSupport::getURI = \&get_uri;
315 1 *XML::NamespaceSupport::processName = \&process_name;
316 1 *XML::NamespaceSupport::processElementName = \&process_element_name;
317 1 *XML::NamespaceSupport::processAttributeName = \&process_attribute_name;
318 1 *XML::NamespaceSupport::parseJClarkNotation = \&parse_jclark_notation;
319 1 *XML::NamespaceSupport::undeclarePrefix = \&undeclare_prefix;
320  
321  
322 1 1;
323  
324 __END__
↑ Summary