lib/Template/Plugin/XML/LibXML.pm

filter: Statementstmt 95.7% 90 / 94 Branchbran 91.6% 22 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.0% 19 / 20 Podpod 50.0% 1 / 2 totaltotal 94.2% 132 / 140 CC 19 SCAR 15.4
CC
19
Cov
95%
new
11.0
_guess_type
5.0
_openhandle
2.5
CRAP 4.7
↑ Summary
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::XML::LibXML
4 #
5 # DESCRIPTION
6 #
7 # Template Toolkit plugin interfacing to the XML::LibXML.pm module.
8 #
9 # AUTHOR
10 # Mark Fowler <mark@twoshortplanks.com>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2002-3 Mark Fowler. 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 #----------------------------------------------------------------------------
19 #
20 # CVS tag goes here.
21 #
22 #============================================================================
23  
24 package Template::Plugin::XML::LibXML;
25  
26 2 require 5.004;
27  
28 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Template::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # load the recommended (but not manditory) openhandle routine
34 # for filehandle detection.
35 2 ▶BEGIN { eval "use Scalar::Util qw(openhandle)" }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use base qw( Template::Plugin );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use vars qw( $VERSION $parser );
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 $VERSION = 1.07; #sprintf("%d.%02d", q$Revision: 2.50 $ =~ /(\d+)\.(\d+)/);
41  
42 # these are a list of combatibilty mappings from names that were used
43 # (or logical extensions of those names for html) in the XML::XPath
44 # plugin. Though we're using existing names, I want you to be able
45 # to still use the old names. Very DWIM
46 2 ▶use constant TYPE => { 'xml' => 'string',
Subroutine BEGIN: called CC=1 SCAR=0.0
47 'text' => 'string',
48 'filename' => 'file',
49 'html' => 'html_string',
50 'html_text' => 'html_string',
51 'html_file' => 'html_file',
52 'html_filename' => 'html_file',
53 2 };
54  
55 #------------------------------------------------------------------------
56 # new($context, \%config)
57 #
58 # Constructor method for XML::LibXML plugin. Creates an XML::LibXML
59 # object and initialises plugin configuration.
60 #------------------------------------------------------------------------
61  
62 37 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
63  
64 37 local $_;
65  
66 37 my $class = shift;
67 37 my $context = shift;
68  
69 # get the named arguments if there were any
70 37 ▶ my $args = ref $_[-1] eq 'HASH' ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 19 18 37
71  
72 37 my $type; # how we're going to get out data
73 my $content; # a ref to the data
74  
75 # work out what data we should process
76 37 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 20 17 37
77 {
78 # ah, we got positional data.
79 20 $content = \$_[0]; # remember where it is
80 20 $type = _guess_type($_[0]); # guess what type we're doing
81 }
82 else
83 {
84 # okay, the data must be in the named parameters
85  
86 # first up we'll just try the method names. You really should
87 # supply the arguments like this you know.
88 17 foreach (qw(string file fh html_string html_file html_fh))
89 {
90 86 ▶ if ($args->{ $_ })
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$_}) 7 79 86
91 {
92 7 $content = \$args->{ $_ }; # remember where it is
93 7 delete $args->{ $_ }; # don't pass on parameter though
94 7 $type = $_; # remember what type we're doing
95 7 last; # skip to the end
96 }
97 }
98  
99 17 ▶ unless ($type)
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 10 7 17
100 {
101 # last ditch effort. In this case we'll try some of the names
102 # that the XML::XPath plugin uses. We might strike lucky
103 10 foreach (keys %{ &TYPE })
104 {
105 52 ▶ if ($args->{ $_ })
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$_}) 10 42 52
106 {
107 10 $content = \$args->{ $_ }; # remember where it is
108 10 delete $args->{ $_ }; # don't pass on parameter though
109 10 $type = &TYPE->{ $_ }; # remember what type we're doing
110 10 last; # skip to the end
111 }
112 }
113 }
114 }
115  
116 # return an error if we didn't get a response back
117 37 ▶ return $class->_throw('no filename, handle or text specified')
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 37 37
118 unless $type;
119  
120 # create a parser
121 37 my $parser = XML::LibXML->new();
122  
123 # set the options
124 37 foreach my $method (keys %$args)
125 {
126 # try setting the method
127 3 eval { $parser->$method($args->{$method}) };
128  
129 # if there's a problem throw a Tempalte::Exception
130 3 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
131 {
132 1 die Template::Exception->new("XML.LibXML",
133 "option '$method' not supported");
134 }
135 }
136  
137 # parse
138 36 my $method = "parse_$type";
139 36 return $parser->$method($$content);
140 }
141  
142 #------------------------------------------------------------------------
143 # _guess_type($string)
144 #
145 # Guesses what type of data this is
146 #------------------------------------------------------------------------
147  
148 sub _guess_type
149 20 ▶{
Subroutine _guess_type: called CC=5 SCAR=16.1
150 # look for a filehandle
151 20 ▶ return "fh" if _openhandle($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &_openhandle($_[0]) 1 19 20
152  
153 # okay, look for the xml declaration at the start
154 19 ▶ return "string" if $_[0] =~ m/^\<\?xml/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^\<\?xml/ 2 17 19
155  
156 # okay, look for the html declaration anywhere in the doc
157 17 ▶ return "html_string" if $_[0] =~ m/<html>/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /<html>/i 1 16 17
158  
159 # okay, does this contain a "<" symbol, and declare it to be
160 # xml if it's got one, though they should use "<?xml"
161 16 ▶ return "string" if $_[0] =~ m{\<};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\</ 4 12 16
162  
163 # okay, we've tried everything else, return a filename
164 12 return "file";
165 }
166  
167 #------------------------------------------------------------------------
168 # _throw($errmsg)
169 #
170 # Raise a Template::Exception of type XML.XPath via die().
171 #------------------------------------------------------------------------
172  
173 0 ▶sub _throw {
Subroutine _throw: not called CC=1 SCAR=6.9
174 0 my ($self, $error) = @_;
175 # print STDERR "about to throw $error\n";
176 0 die Template::Exception->new('XML.LibXML', $error);
177 }
178  
179 #------------------------------------------------------------------------
180 # _openhandle($scalar)
181 #
182 # Determines if this is probably an open filehandle or not.
183 #
184 # uses openhandle from Scalar::Util if we have it.
185 #------------------------------------------------------------------------
186  
187 sub _openhandle ($)
188 20 ▶{
Subroutine _openhandle: called CC=2 SCAR=9.2
189 20 ▶ return openhandle($_[0]) if defined(&openhandle);
Branch: 1 branch
BranchTrueFalseTotal
if defined &Template::Plugin::XML::LibXML::openhandle 20 0 20
190  
191 # poor man's openhandle
192 0 return defined(fileno $_[0]);
193 }
194  
195 #========================================================================
196 package XML::LibXML::Node;
197 #========================================================================
198  
199 #-----------------------------------------------------------------------
200 # present($view)
201 #
202 # Method to present an node via a view, using the block that has the
203 # same localname.
204 #-----------------------------------------------------------------------
205  
206 # note, should this worry about namespaces? Probably. Hmm.
207  
208 19 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
209 19 my ($self, $view) = @_;
210 19 my $localname = $self->localname();
211  
212 # convert anything that isn't A-Za-z1-9 to _. All those years
213 # of working on i18n and this throws it all away. I suck.
214 19 $localname =~ s/[^A-Za-z0-9]/_/g;
215  
216 # render out with the block matching the hacked version of localname
217 19 $view->view($localname, $self);
218 }
219  
220 #-----------------------------------------------------------------------
221 # content($view)
222 #
223 # Method present the node's children via a view
224 #-----------------------------------------------------------------------
225  
226 17 ▶sub content {
Subroutine content: called CC=2 SCAR=6.9
227 17 my ($self, $view) = @_;
228 17 my $output = '';
229 17 foreach my $node ($self->childNodes ) {
230 43 $output .= $node->present($view);
231 }
232 17 return $output;
233 }
234  
235 #----------------------------------------------------------------------
236 # starttag(), endtag()
237 #
238 # Methods to output the start & end tag, e.g. <bar:foo buzz="baz">
239 # and </bar:foo>
240 #----------------------------------------------------------------------
241  
242 5 ▶sub starttag {
Subroutine starttag: called CC=2 SCAR=7.0
243 5 my ($self) = @_;
244 5 my $output = "<". $self->nodeName();
245 5 foreach my $attr ($self->attributes)
246 {
247 0 $output .= $attr->toString();
248 }
249 5 $output .= ">";
250 5 return $output;
251 }
252  
253 5 ▶sub endtag {
Subroutine endtag: called CC=1 SCAR=0.0
254 5 my ($self) = @_;
255 5 return "</". $self->nodeName() . ">";
256 }
257  
258 #========================================================================
259 package XML::LibXML::Document;
260 #========================================================================
261  
262 #------------------------------------------------------------------------
263 # present($view)
264 #
265 # Method to present a document node via a view.
266 #------------------------------------------------------------------------
267  
268 2 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
269 2 my ($self, $view) = @_;
270 # okay, just start rendering from the first element, ignore the pi
271 # and all that
272 2 $self->documentElement->present($view);
273 }
274  
275 #========================================================================
276 package XML::LibXML::Text;
277 #========================================================================
278  
279 #------------------------------------------------------------------------
280 # present($view)
281 #
282 # Method to present a text node via a view.
283 #------------------------------------------------------------------------
284  
285 30 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
286 30 my ($self, $view) = @_;
287 30 $view->view('text', $self->data); # same as $self->nodeData
288 }
289  
290 #========================================================================
291 package XML::LibXML::NodeList;
292 #========================================================================
293  
294 #------------------------------------------------------------------------
295 # present($view)
296 #
297 # Method to present a node list via a view. This is only normally useful
298 # when you call outside of TT as findnodes will be called in list context
299 # normally
300 #------------------------------------------------------------------------
301  
302 1 ▶sub present {
Subroutine present: called CC=2 SCAR=6.9
Pod undocumented
303 1 my ($self, $view) = @_;
304 1 my $output = '';
305 1 foreach my $node ($self->get_nodelist ) {
306 1 $output .= $node->present($view);
307 }
308 1 return $output;
309 }
310  
311 #package debug;
312  
313 #sub debug
314 #{
315 # local $^W;
316 # my $nodename;
317 # eval { $nodename = $_[0]->nodeName(); };
318 # my $methodname = (caller(1))[3];
319 # $methodname =~ s/.*:://;
320 #
321 # print STDERR "${nodename}'s $methodname: ".
322 # (join ",", (map { ref } @_)) .
323 # "\n";
324 #}
325  
326 2 1;
327  
328 __END__
↑ Summary