Kite/XML/Node.pm

filter: Statementstmt 79.5% 117 / 147 Branchbran 54.2% 38 / 70 Conditioncond 66.6% 12 / 18 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 94.1% 16 / 17 Podpod 20.0% 1 / 5 totaltotal 70.3% 187 / 266 CC 46 SCAR 25.2
CC
46
Cov
71%
_dump
42.0
init
18.0
child
10.2
CRAP 12.4
« Kite/Profile.pm ↑ Summary Kite/Base.pm »
1 #============================================================= -*-perl-*-
2 #
3 # Kite::XML::Node
4 #
5 # DESCRIPTION
6 # Base class for XML node modules which are constructed automatically
7 # by the Kite::XML::Parser. These represent the XML elements.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@kfs.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 # VERSION
19 # $Id: Node.pm,v 1.1 2000/10/17 11:58:16 abw Exp $
20 #
21 #========================================================================
22
23 package Kite::XML::Node;
24  
25 3 require 5.004;
26  
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Kite::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use base qw( Kite::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use vars qw( $VERSION $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
33  
34 # create some aliases for method names
35 3 *attribute = \&attr;
36 3 *element = \&elem;
37 3 *content = \&char;
38  
39  
40 #------------------------------------------------------------------------
41 # init(\%config)
42 #
43 # Initialisation method called by the base class constructor, new().
44 # Copies attributes pass in the $config hash reference into the $self
45 # object, checking that all mandatory attributes are specified.
46 #------------------------------------------------------------------------
47  
48 15 ▶sub init {
Subroutine init: called CC=15 SCAR=28.9
Pod documented
49 15 my ($self, $config) = @_;
50 15 my $class = ref $self;
51 15 my ($attribs, $elems, $default, $key, $val, $mult);
52  
53 {
54 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 15 ▶ $attribs = ${"$class\::ATTRIBUTES"} || { };
Condition50%1/2
${"${class}::ATTRIBUTES";} || {}
l!l
150
MC/DC0%0/1
${"${class}::ATTRIBUTES";} || {}
${"${class}::ATTRIBUTES";}
Truth table50%1/2
A
A= ${"${class}::ATTRIBUTES";}
Aresult
11
00
56 15 ▶ $elems = ${"$class\::ELEMENTS"} || { };
Condition100%2/2
${"${class}::ELEMENTS";} || {}
l!l
96
MC/DC100%1/1
${"${class}::ELEMENTS";} || {}
${"${class}::ELEMENTS";}
Truth table100%2/2
A
A= ${"${class}::ELEMENTS";}
Aresult
11
00
57 }
58  
59 # ugy hack: we must call keys() to reset the iterators on the hashes
60 15 my @dud = (keys(%$attribs), keys(%$elems));
61  
62 # set attributes from the $config hash, where specified in $ATTRIBUTES
63 15 ▶ while (($key, $val) = each %$attribs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attribs) 29 12 41
64 29 ▶ if ($key =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_/) { } 0 29 29
65 # just set default for private keys with leading _UNDERSCORE
66 0 ▶ $self->{ $key } = ref $val eq 'CODE' ? &$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 0 0 0
67 }
68 else {
69 29 ▶ if (defined $config->{ $key }) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $config->{$key}) { } 18 11 29
elsif (defined $val) { } 8 3 11
70 18 $self->{ $key } = $config->{ $key };
71 }
72 elsif (defined $val) {
73 8 $self->{ $key } = $val;
74 }
75 else {
76 3 return $self->error("$key not defined");
77 }
78 }
79 26 delete $config->{ $key };
80 }
81  
82 # set elements from the $config hash, or initialise
83 12 ▶ while (($key, $val) = each %$elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$elems) 15 12 27
84 # value can be an array ref containing [ $pkg, $module ]
85 15 ▶ $val = $val->[0]
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 4 11 15
86 if ref $val eq 'ARRAY';
87  
88 # look for, and strip trailing '+' on package name, then create
89 # array reference for elements with multiplicity
90 15 $mult = ($val =~ s/\+$//);
91 15 ▶ $self->{ $key } = []
Branch: 1 branch
BranchTrueFalseTotal
if $mult 8 7 15
92 if $mult;
93  
94 # copy any config value(s) into the elements
95 15 ▶ if (defined ($val = $config->{ $key })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($val = $config->{$key})) 0 15 15
96 0 ▶ if ($mult) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mult) { } 0 0 0
97 0 ▶ push(@{ $self->{ $key } }, ref $val eq 'ARRAY' ? @$val : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
98 }
99 else {
100 0 $self->{ $key } = $val;
101 }
102 }
103 15 delete $config->{ $key };
104 }
105  
106 # any items remaining in $config are invalid
107 12 foreach $key (keys %$config) {
108 1 return $self->error("invalid attribute '$key'");
109 }
110  
111 11 return $self;
112 }
113  
114  
115 #------------------------------------------------------------------------
116 # attr($name)
117 # attr($name, $value)
118 #
119 # Accessor method to retrieve or update element attributes.
120 #------------------------------------------------------------------------
121  
122 19 ▶sub attr {
Subroutine attr: called CC=4 SCAR=14.0
Pod undocumented
123 19 my $self = shift;
124 19 my $attr = shift;
125 19 my $class = ref $self;
126  
127 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
128 19 ▶ my $attribs = ${"$class\::ATTRIBUTES"} || { };
Condition50%1/2
${"${class}::ATTRIBUTES";} || {}
l!l
190
MC/DC0%0/1
${"${class}::ATTRIBUTES";} || {}
${"${class}::ATTRIBUTES";}
Truth table50%1/2
A
A= ${"${class}::ATTRIBUTES";}
Aresult
11
00
129  
130 # set new or return existing value for valid PARAMS
131 19 ▶ if (exists $attribs->{ $attr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attribs->{$attr}) { } 19 0 19
132 19 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 18 19
133 1 return ($self->{ $attr } = shift);
134 }
135 else {
136 18 return $self->{ $attr };
137 }
138 }
139 else {
140 0 return $self->error("no such attribute '$attr'");
141 }
142 }
143  
144  
145 #------------------------------------------------------------------------
146 # char()
147 # char($text)
148 #
149 # Returns the internal CDATA member when called without any arguments,
150 # which contains the current character content for the node. When called
151 # with an argument, the passed value will be appended to the CDATA member.
152 # The CDATA item must be defined in the $ELEMENTS hash reference in the
153 # subclass's package for character content to be accepted. A call to
154 # char() for a node that don't accept CDATA will be considered an error
155 # and will set the internal ERROR variable and return undef. There is
156 # one caveat to this rule: any node that *doesn't* define CDATA will
157 # accept and silently ignore any $text that contains only whitespace.
158 # This is required to prevent XML nodes that shouldn't define content,
159 # but do contain whitespace, from raising errors.
160 #------------------------------------------------------------------------
161  
162 3 ▶sub char {
Subroutine char: called CC=7 SCAR=22.0
Pod undocumented
163 3 my ($self, $text) = @_;
164 3 my $class = ref $self;
165  
166 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
167 3 ▶ my $elems = ${"$class\::ELEMENTS"} || { };
Condition50%1/2
${"${class}::ELEMENTS";} || {}
l!l
30
MC/DC0%0/1
${"${class}::ELEMENTS";} || {}
${"${class}::ELEMENTS";}
Truth table50%1/2
A
A= ${"${class}::ELEMENTS";}
Aresult
11
00
168  
169 3 ▶ if ($elems->{ CDATA }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elems->{'CDATA'}) { } 3 0 3
elsif (defined $text) { } 0 0 0
170 3 ▶ $self->{ CDATA } = '' unless defined $self->{ CDATA };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"CDATA"} 2 1 3
171 3 ▶ $self->{ CDATA } .= $text if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 2 1 3
172 3 return $self->{ CDATA };
173 }
174 elsif(defined $text) {
175 # complain about character data unless it's just white noise
176 0 ▶ return $self->error("invalid character data")
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /^\s*$/ 0 0 0
177 unless $text =~ /^\s*$/;
178 0 return 1;
179 }
180 else {
181 0 return $self->error("no character data");
182 }
183 }
184  
185  
186 #------------------------------------------------------------------------
187 # elem($name)
188 # elem($name, @args)
189 #
190 # Accessor method to retrieve the element specified by parameter. If
191 # additional arguments are provided then the call is assumed to be a
192 # construction request and is delegated to child($name, @args).
193 #------------------------------------------------------------------------
194  
195 10 ▶sub elem {
Subroutine elem: called CC=4 SCAR=14.0
Pod undocumented
196 10 my $self = shift;
197 10 my $elem = shift;
198 10 my $class = ref $self;
199  
200 # delegate to child() if additional arguments specified!
201 10 ▶ return $self->child($elem, @_)
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 6 10
202 if @_;
203  
204 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
205 6 ▶ my $elems = ${"$class\::ELEMENTS"} || { };
Condition50%1/2
${"${class}::ELEMENTS";} || {}
l!l
60
MC/DC0%0/1
${"${class}::ELEMENTS";} || {}
${"${class}::ELEMENTS";}
Truth table50%1/2
A
A= ${"${class}::ELEMENTS";}
Aresult
11
00
206  
207 # set new or return existing value for valid PARAMS
208 6 ▶ if (exists $elems->{ $elem }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $elems->{$elem}) { } 6 0 6
209 6 return $self->{ $elem };
210 }
211 else {
212 0 return $self->error("no such element '$elem'");
213 }
214 }
215
216  
217 #------------------------------------------------------------------------
218 # child($element, @args)
219 #
220 # Creates a new child element of type denoted by the first parameter.
221 # Examines the $ELEMENTS hash reference in the object's package for
222 # a key matching $element and uses the relevant value as a package
223 # name against which the new() constructor can be called, passing
224 # any additional arguments specified. The package name may be suffixed
225 # by a '+' to indicate that multiple child elements are permitted.
226 #------------------------------------------------------------------------
227  
228 9 ▶sub child {
Subroutine child: called CC=9 SCAR=23.2
Pod undocumented
229 9 my $self = shift;
230 9 my $class = ref $self;
231 9 my $elem = shift;
232 9 my ($pkg, $mod, $mult) = (0) x 3;
233  
234 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
235 9 ▶ my $elems = ${"$class\::ELEMENTS"} || { };
Condition50%1/2
${"${class}::ELEMENTS";} || {}
l!l
90
MC/DC0%0/1
${"${class}::ELEMENTS";} || {}
${"${class}::ELEMENTS";}
Truth table50%1/2
A
A= ${"${class}::ELEMENTS";}
Aresult
11
00
236  
237 9 ▶ if (defined($pkg = $elems->{ $elem })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($pkg = $elems->{$elem})) { } 9 0 9
238 # value can be an array ref containing [ $pkg, $module ]
239 9 ▶ ($pkg, $mod) = @$pkg
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg eq "ARRAY" 0 9 9
240 if ref $pkg eq 'ARRAY';
241  
242 # look for, and strip trailing '+' on package name
243 9 $mult = ($pkg =~ s/\+$//);
244  
245 # use package name to define module name if $mod set to 1
246 9 ▶ if ($mod eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod eq 1) 0 9 9
247 0 $mod = $pkg;
248 0 $mod =~ s/::/\//g;
249 0 $mod .= '.pm';
250 }
251  
252 9 ▶ require $mod if $mod;
Branch: 1 branch
BranchTrueFalseTotal
if $mod 0 9 9
253  
254 9 ▶ my $node = $pkg->new(@_)
Condition100%2/2
$pkg->new(@_) || (return $self->error($pkg->error))
l!l
72
MC/DC100%1/1
$pkg->new(@_) || (return $self->error($pkg->error))
$pkg->new(@_)
Truth table100%2/2
A
A= $pkg->new(@_)
Aresult
11
00
255 || return $self->error($pkg->error());
256  
257 7 ▶ if ($mult) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mult) { } 6 1 7
258 6 push(@{ $self->{ $elem } }, $node);
259 }
260 else {
261 return $self->error("$elem already defined")
262 1 ▶ if defined $self->{ $elem };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$elem} 0 1 1
263 1 $self->{ $elem } = $node;
264 }
265 7 return $node;
266 }
267 else {
268 0 return $self->error("invalid element '$elem'");
269 }
270 }
271  
272  
273 #------------------------------------------------------------------------
274 # AUTOLOAD
275 #
276 # Autoload method.
277 #------------------------------------------------------------------------
278  
279 39 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=7 SCAR=20.0
280 39 my $self = shift;
281 39 my $class = ref $self;
282 39 my $method = $AUTOLOAD;
283
284 39 $method =~ s/.*:://;
285 39 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 15 24 39
286  
287 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
288 24 ▶ my $attribs = ${"$class\::ATTRIBUTES"} || { };
Condition50%1/2
${"${class}::ATTRIBUTES";} || {}
l!l
240
MC/DC0%0/1
${"${class}::ATTRIBUTES";} || {}
${"${class}::ATTRIBUTES";}
Truth table50%1/2
A
A= ${"${class}::ATTRIBUTES";}
Aresult
11
00
289 24 ▶ my $elems = ${"$class\::ELEMENTS"} || { };
Condition100%2/2
${"${class}::ELEMENTS";} || {}
l!l
1410
MC/DC100%1/1
${"${class}::ELEMENTS";} || {}
${"${class}::ELEMENTS";}
Truth table100%2/2
A
A= ${"${class}::ELEMENTS";}
Aresult
11
00
290
291 24 ▶ if ($method =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^_/) 0 24 24
292 0 my ($pkg, $file, $line) = caller();
293 0 die "attempt to access private member $method at $file line $line\n";
294 }
295  
296 24 ▶ if (exists $attribs->{ $method }) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $attribs->{$method}) { } 17 7 24
elsif (exists $elems->{$method}) { } 7 0 7
297 17 return $self->attr($method, @_);
298 }
299 elsif (exists $elems->{ $method }) {
300 7 return $self->elem($method, @_);
301 }
302 else {
303 0 return $self->error("no such attribute '$method'");
304 }
305 }
306  
307  
308 #------------------------------------------------------------------------
309 # _dump()
310 #
311 # Debug method to return a formatted string containing the object data.
312 #------------------------------------------------------------------------
313  
314 0 ▶sub _dump {
Subroutine _dump: not called CC=6 SCAR=37.4
315 0 my $self = shift;
316 0 my $text = "$self:\n";
317 0 local $" = ', ';
318 0 ▶ while (my ($key, $value) = each %$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 0 0 0
319 0 my $v;
320 0 ▶ $value = '<undef>' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
321 0 ▶ $value = [ map { $v = $value->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 0 0
322 0 ▶ $v = '<undef>' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
323 0 "$_ => $v" } keys %$value ]
324 if ref $value eq 'HASH';
325 0 ▶ $value = "[ @$value ]"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
326 if ref $value eq 'ARRAY';
327 0 $text .= sprintf(" %-12s => $value\n", $key);
328 }
329 0 return $text;
330 }
331  
332  
333 3 1;
334  
335 __END__
« Kite/Profile.pm ↑ Summary Kite/Base.pm »