/usr/share/perl5/Template/Plugin/XML/DOM.pm is in libtemplate-plugin-xml-perl 2.17-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | #============================================================= -*-Perl-*-
#
# Template::Plugin::XML::DOM
#
# DESCRIPTION
# Simple Template Toolkit plugin interfacing to the XML::DOM.pm module.
#
# AUTHORS
# Andy Wardley <abw@cpan.org>
# Simon Matthews <sam@knowledgepool.com>
#
# COPYRIGHT
# Copyright (C) 2000-2006 Andy Wardley, Simon Matthews.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#============================================================================
package Template::Plugin::XML::DOM;
use strict;
use warnings;
use base 'Template::Plugin';
use Template::Plugin::XML;
use XML::DOM;
our $VERSION = 2.70;
our $DEBUG = 0 unless defined $DEBUG;
#------------------------------------------------------------------------
# new($context, \%config)
#
# Constructor method for XML::DOM plugin. Creates an XML::DOM::Parser
# object and initialise plugin configuration.
#------------------------------------------------------------------------
sub new {
my $class = shift;
my $context = shift;
my $args = ref $_[-1] eq 'HASH' ? pop(@_) : { };
my $parser ||= XML::DOM::Parser->new(%$args)
|| return $class->throw("failed to create XML::DOM::Parser\n");
bless {
PARSER => $parser,
DOCS => [ ],
CONTEXT => $context,
}, $class;
}
#------------------------------------------------------------------------
# parse($content, \%named_params)
#
# Parses an XML stream, provided as the first positional argument (assumed
# to be a filename unless it contains a '<' character) or specified in
# the named parameter hash as one of 'text', 'xml' (same as text), 'file'
# or 'filename'.
#------------------------------------------------------------------------
sub parse {
my $self = shift;
my $args = ref $_[-1] eq 'HASH' ? pop(@_) : { };
my $parser = $self->{ PARSER };
my ($content, $about, $method, $doc);
# determine the input source from a positional parameter (may be a
# filename or XML text if it contains a '<' character) or by using
# named parameters which may specify one of 'file', 'filename', 'text'
# or 'xml'
if ($content = shift) {
if ($content =~ /\</) {
$about = 'xml text';
$method = 'parse';
}
else {
$about = "xml file $content";
$method = 'parsefile';
}
}
elsif ($content = $args->{ text } || $args->{ xml }) {
$about = 'xml text';
$method = 'parse';
}
elsif ($content = $args->{ file } || $args->{ filename }) {
$about = "xml file $content";
$method = 'parsefile';
}
else {
return $self->throw('no filename or xml text specified');
}
# parse the input source using the appropriate method determined above
eval { $doc = $parser->$method($content) } and not $@
or return $self->throw("failed to parse $about: $@");
# update XML::DOM::Document _UserData to contain config details
$doc->[ XML::DOM::Node::_UserData ] = {
map { ( $_ => $self->{ $_ } ) }
qw( _CONTEXT ),
};
push(@{ $self->{ _DOCS } }, $doc);
return $doc;
}
#------------------------------------------------------------------------
# throw($errmsg)
#
# Raised a Template::Exception of type XML.DOM via die().
#------------------------------------------------------------------------
sub throw {
my $self = shift;
die $Template::Plugin::XML::EXCEPTION->new( 'XML.DOM' => join('', @_) );
}
#------------------------------------------------------------------------
# DESTROY
#
# Cleanup method which calls dispose() on any and all DOM documents
# created by this object. Also breaks any circular references that
# may exist with the context object.
#------------------------------------------------------------------------
sub DESTROY {
my $self = shift;
# call dispose() on each document produced by this parser
foreach my $doc (@{ $self->{ DOCS } }) {
if (ref $doc) {
undef $doc->[ XML::DOM::Node::_UserData ]->{ CONTEXT };
$doc->dispose();
}
}
delete $self->{ CONTEXT };
delete $self->{ PARSER };
}
#========================================================================
package XML::DOM::Node;
#========================================================================
#------------------------------------------------------------------------
# present($view)
#
# Method to present node via a view (supercedes all that messy toTemplate
# stuff below).
#------------------------------------------------------------------------
sub present {
my ($self, $view) = @_;
if ($self->getNodeType() == XML::DOM::ELEMENT_NODE) {
# it's an element
$view->view($self->getTagName(), $self);
}
else {
my $text = $self->toString();
$view->view('text', $text);
}
}
sub content {
my ($self, $view) = @_;
my $output = '';
foreach my $node (@{ $self->getChildNodes }) {
$output .= $node->present($view);
}
return $output;
}
#========================================================================
package XML::DOM::Element;
#========================================================================
use vars qw( $AUTOLOAD );
sub AUTOLOAD {
my $self = shift;
my $method = $AUTOLOAD;
my $attrib;
$method =~ s/.*:://;
return if $method eq 'DESTROY';
my $doc = $self->getOwnerDocument() || $self;
my $data = $doc->[ XML::DOM::Node::_UserData ];
# call 'content' or 'prune' callbacks, if defined (see _template_node())
return &$attrib()
if ($method =~ /^children|prune$/)
&& defined($attrib = $data->{ "_TT_\U$method" })
&& ref $attrib eq 'CODE';
return $attrib
if defined ($attrib = $self->getAttribute($method));
return '';
}
1;
__END__
=head1 NAME
Template::Plugin::XML::DOM - Plugin interface to XML::DOM
=head1 SYNOPSIS
# load plugin
[% USE dom = XML.DOM %]
# also provide XML::Parser options
[% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %]
# parse an XML file
[% doc = dom.parse(filename) %]
[% doc = dom.parse(file = filename) %]
# parse XML text
[% doc = dom.parse(xmltext) %]
[% doc = dom.parse(text = xmltext) %]
# call any XML::DOM methods on document/element nodes
[% FOREACH node = doc.getElementsByTagName('report') %]
* [% node.getAttribute('title') %] # or [% node.title %]
[% END %]
# define VIEW to present node(s)
[% VIEW report notfound='xmlstring' %]
# handler block for a <report>...</report> element
[% BLOCK report %]
[% item.content(view) %]
[% END %]
# handler block for a <section title="...">...</section> element
[% BLOCK section %]
<h1>[% item.title %]</h1>
[% item.content(view) %]
[% END %]
# default template block converts item to string
[% BLOCK xmlstring; item.toString; END %]
# block to generate simple text
[% BLOCK text; item; END %]
[% END %]
# now present node (and children) via view
[% report.print(node) %]
# or print node content via view
[% node.content(report) %]
# following methods are soon to be deprecated in favour of views
[% node.toTemplate %]
[% node.childrenToTemplate %]
[% node.allChildrenToTemplate %]
=head1 DESCRIPTION
This is a Template Toolkit plugin interfacing to the XML::DOM module.
The plugin loads the XML::DOM module and creates an XML::DOM::Parser
object which is stored internally. The parse() method can then be
called on the plugin to parse an XML stream into a DOM document.
[% USE dom = XML.DOM %]
[% doc = dom.parse('/tmp/myxmlfile') %]
The XML::DOM plugin object (i.e. 'dom' in these examples) acts as a
sentinel for the documents it creates ('doc' and any others). When
the plugin object goes out of scope at the end of the current
template, it will automatically call dispose() on any documents that
it has created. Note that if you dispose of the the plugin object
before the end of the block (i.e. by assigning a new value to the
'dom' variable) then the documents will also be disposed at that point
and should not be used thereafter.
[% USE dom = XML.DOM %]
[% doc = dom.parse('/tmp/myfile') %]
[% dom = 'new value' %] # releases XML.DOM plugin and calls
# dispose() on 'doc', so don't use it!
The plugin constructor will also accept configuration options destined
for the XML::Parser object:
[% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %]
=head1 METHODS
=head2 parse()
The parse() method accepts a positional parameter which contains a filename
or XML string. It is assumed to be a filename unless it contains a E<lt>
character.
[% xmlfile = '/tmp/foo.xml' %]
[% doc = dom.parse(xmlfile) %]
[% xmltext = BLOCK %]
<xml>
<blah><etc/></blah>
...
</xml>
[% END %]
[% doc = dom.parse(xmltext) %]
The named parameters 'file' (or 'filename') and 'text' (or 'xml') can also
be used:
[% doc = dom.parse(file = xmlfile) %]
[% doc = dom.parse(text = xmltext) %]
The parse() method returns an instance of the XML::DOM::Document object
representing the parsed document in DOM form. You can then call any
XML::DOM methods on the document node and other nodes that its methods
may return. See L<XML::DOM> for full details.
[% FOREACH node = doc.getElementsByTagName('CODEBASE') %]
* [% node.getAttribute('href') %]
[% END %]
This plugin also provides an AUTOLOAD method for XML::DOM::Node which
calls getAttribute() for any undefined methods. Thus, you can use the
short form of
[% node.attrib %]
in place of
[% node.getAttribute('attrib') %]
=head1 PRESENTING DOM NODES USING VIEWS
You can define a VIEW to present all or part of a DOM tree by automatically
mapping elements onto templates. Consider a source document like the
following:
<report>
<section title="Introduction">
<p>
Blah blah.
<ul>
<li>Item 1</li>
<li>item 2</li>
</ul>
</p>
</section>
<section title="The Gory Details">
...
</section>
</report>
We can load it up via the XML::DOM plugin and fetch the node for the
E<lt>reportE<gt> element.
[% USE dom = XML.DOM;
doc = dom.parse(file = filename);
report = doc.getElementsByTagName('report')
%]
We can then define a VIEW as follows to present this document fragment in
a particular way. The L<Template::Manual::Views> documentation
contains further details on the VIEW directive and various configuration
options it supports.
[% VIEW report_view notfound='xmlstring' %]
# handler block for a <report>...</report> element
[% BLOCK report %]
[% item.content(view) %]
[% END %]
# handler block for a <section title="...">...</section> element
[% BLOCK section %]
<h1>[% item.title %]</h1>
[% item.content(view) %]
[% END %]
# default template block converts item to string representation
[% BLOCK xmlstring; item.toString; END %]
# block to generate simple text
[% BLOCK text; item; END %]
[% END %]
Each BLOCK defined within the VIEW represents a presentation style for
a particular element or elements. The current node is available via the
'item' variable. Elements that contain other content can generate it
according to the current view by calling [% item.content(view) %].
Elements that don't have a specific template defined are mapped to the
'xmlstring' template via the 'notfound' parameter specified in the VIEW
header. This replicates the node as an XML string, effectively allowing
general XML/XHTML markup to be passed through unmodified.
To present the report node via the view, we simply call:
[% report_view.print(report) %]
The output from the above example would look something like this:
<h1>Introduction</h1>
<p>
Blah blah.
<ul>
<li>Item 1</li>
<li>item 2</li>
</ul>
</p>
<h1>The Gory Details</h1>
...
To print just the content of the report node (i.e. don't process the
'report' template for the report node), you can call:
[% report.content(report_view) %]
=head1 AUTHORS
This plugin module was written by Andy Wardley and Simon Matthews.
The XML::DOM module is by Enno Derksen and Clark Cooper. It extends
the the XML::Parser module, also by Clark Cooper which itself is built
on James Clark's expat library.
=head1 COPYRIGHT
Copyright (C) 2000-2006 Andy Wardley, Simon Matthews.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<Template::Plugin>, L<XML::DOM>, L<XML::Parser>
|