XML/Template/Element.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 86 SCAR 47.0
CC
10
Cov
0%
get_attrib
72.0
generate_named_params
12.0
new
2.0
CRAP 110.0
1 ###############################################################################
2 # XML::Template::Element
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Element;
11 use base qw(XML::Template::Base);
12  
13 use strict;
14 use XML::Template::Config;
15  
16  
17 =pod
18  
19 =head1 NAME
20  
21 XML::Template::Element - XML::Template plugin element base class.
22  
23 =head1 SYNOPSIS
24  
25 package XML::Template::Element::MyElement;
26  
27 use base qw(XML::Template::Element);
28 use XML::Template::Element;
29  
30 =head1 DESCRIPTION
31  
32 This module provides base functionality for XML::Template plugin element
33 modules.
34  
35 =head1 CONSTRUCTOR
36  
37 The first parameter of the constructor is hash containing local
38 name/namespace pairs, i.e. the currently loaded namespaces. The second
39 parameter is the current namespace. The remaining named parameters are
40 passed to the L<XML::Template::Base> constructor. The constructor returns
41 a reference to a new element object or undef if an error occurred. If
42 undef is returned, you can use the method C<error> to retrieve the error.
43 For instance:
44  
45 my $element = XML::Template::Element->new ($namespaces, $namespace, %params)
46 || die XML::Template::Element->error;
47  
48 =cut
49  
50 sub new {
51 my $proto = shift;
52 my $namespaces = shift;
53 my $namespace = shift;
54  
55 my $class = ref ($proto) || $proto;
56  
57 my $self = $class->SUPER::new (@_);
58  
59 $self->{_namespaces} = $namespaces;
60 $self->{_namespace} = $namespace;
61  
62 return $self;
63 }
64  
65 =pod
66  
67 =head1 PUBLIC METHODS
68  
69 =head2 namespace
70  
71 my $namespace = $self->namespace;
72  
73 This method returns the current namespace.
74  
75 =cut
76  
77 sub namespace {
78 my $self = shift;
79  
80 return $self->{_namespace};
81 }
82  
83 =pod
84  
85 =head2 generate_named_params
86  
87 my $attribs_named_params = $self->generate_named_params (\%attrbs);
88  
89 This method generates Perl code for a named parameter list. The first
90 parameter is a reference to a hash containing parameter name/value pairs.
91 For instance,
92  
93 my $attribs_named_params = $self->generate_named_params ({
94 type => "'newsletter'",
95 date => "'2002%'",
96 'map.num' => 3});
97  
98 would return the following string
99  
100 'type' => 'newsletter', 'date' => '2002%', 'map.num' => 3
101  
102 =cut
103  
104 sub generate_named_params {
105 my $self = shift;
106 my ($hash, $quotes) = @_;
107  
108 my $named_params;
109 while (my ($key, $val) = each %$hash) {
110 $named_params .= ', ' if defined $named_params;
111 $named_params .= "'$key' => ";
112 if ($quotes) {
113 $named_params .= "'$val'";
114 } else {
115 $named_params .= $val;
116 }
117 }
118  
119 return $named_params;
120 }
121  
122 =pod
123  
124 =head2 get_attrib
125  
126 my $field = $self->get_attrib ($attribs, ['field', 'fields']) || 'undef';
127 my $name = $self->get_attrib ($attribs, 'name'], 0) || '"default"';
128  
129 This method returns an attribute value. The first parameter is a
130 reference to a hash containing attribute/value pairs. The second
131 parameter is a scalar or a reference to an array of attribute names to
132 look for. The value of the first attribute name found is returned.
133  
134 Unless the optional third parameter is true, the attribute is deleted from
135 the attribute hash.
136  
137 =cut
138  
139 sub get_attrib {
140 my $self = shift;
141 my ($attribs, $attribnames, $delete) = @_;
142  
143 $delete = 1 if ! defined $delete;
144 my @attribnames = ref ($attribnames) ? @$attribnames : ($attribnames);
145  
146 my $value;
147  
148 # Look up "{<namespace>}<attribname>" in attrib hash.
149 foreach (@attribnames) {
150 my $attrib = "{$self->{_namespace}}$_";
151 if (exists $attribs->{$attrib}) {
152 $value = $attribs->{$attrib};
153 delete $attribs->{$attrib} if $delete;
154 }
155 }
156  
157 # Look up "<attribname>" in attrib hash.
158 if (! defined $value) {
159 foreach (@attribnames) {
160 if (exists $attribs->{$_}) {
161 $value = $attribs->{$_};
162 delete $attribs->{$_} if $delete;
163 return $value;
164 }
165 }
166 }
167  
168 return $value;
169 }
170  
171 sub strip {
172 my $self = shift;
173 my $value = shift;
174  
175 $value =~ s/^['"]//;
176 $value =~ s/['"]$//;
177  
178 return $value;
179 }
180  
181 =pod
182  
183 =head1 AUTHOR
184  
185 Jonathan Waxman
186 <jowaxman@bbl.med.upenn.edu>
187  
188 =head1 COPYRIGHT
189  
190 Copyright (c) 2002-2003 Jonathan A. Waxman
191 All rights reserved.
192  
193 This program is free software; you can redistribute it and/or
194 modify it under the same terms as Perl itself.
195  
196 =cut
197  
198  
199 1;