/usr/share/perl5/Template/Plugin/XML/LibXML.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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | #============================================================= -*-Perl-*-
#
# Template::Plugin::XML::LibXML
#
# DESCRIPTION
# Template Toolkit plugin interfacing to the XML::LibXML.pm module.
#
# AUTHORS
# Mark Fowler <mark@twoshortplanks.com>
# Andy Wardley <abw@cpan.org>
#
# COPYRIGHT
# Copyright (C) 2002-3 Mark Fowler, 2006 Andy Wardley.
# All Rights Reserved.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#============================================================================
package Template::Plugin::XML::LibXML;
require 5.004;
use strict;
use warnings;
use base 'Template::Plugin';
use Template::Plugin::XML;
use XML::LibXML;
# load the recommended (but not manditory) openhandle routine
# for filehandle detection.
BEGIN { eval "use Scalar::Util qw(openhandle)" }
our $VERSION = 2.00;
# these are a list of combatibilty mappings from names that were used
# (or logical extensions of those names for html) in the XML::XPath
# plugin. Though we're using existing names, I want you to be able
# to still use the old names. Very DWIM
use constant TYPE => {
'xml' => 'string',
'text' => 'string',
'filename' => 'file',
'html' => 'html_string',
'html_text' => 'html_string',
'html_file' => 'html_file',
'html_filename' => 'html_file',
};
#------------------------------------------------------------------------
# new($context, \%config)
#
# Constructor method for XML::LibXML plugin. Creates an XML::LibXML
# object and initialises plugin configuration.
#------------------------------------------------------------------------
sub new {
my $class = shift;
my $context = shift;
my $args = ref $_[-1] eq 'HASH' ? pop(@_) : { };
my $type; # how we're going to get out data
my $content; # a ref to the data
local $_;
# work out what data we should process
if (@_) {
# ah, we got positional data.
$content = \$_[0]; # remember where it is
$type = _guess_type($_[0]); # guess what type we're doing
}
else {
# okay, the data must be in the named parameters
# first up we'll just try the method names. You really should
# supply the arguments like this you know.
foreach (qw(string file fh html_string html_file html_fh)) {
if ($args->{ $_ }) {
$content = \$args->{ $_ }; # remember where it is
delete $args->{ $_ }; # don't pass on parameter though
$type = $_; # remember what type we're doing
last; # skip to the end
}
}
unless ($type) {
# last ditch effort. In this case we'll try some of the names
# that the XML::XPath plugin uses. We might strike lucky
foreach (keys %{ &TYPE }) {
if ($args->{ $_ }) {
$content = \$args->{ $_ }; # remember where it is
delete $args->{ $_ }; # don't pass on parameter though
$type = &TYPE->{ $_ }; # remember what type we're doing
last; # skip to the end
}
}
}
}
# return an error if we didn't get a response back
return $class->_throw('no filename, handle or text specified')
unless $type;
# create a parser
my $parser = XML::LibXML->new();
# set the options
foreach my $method (keys %$args) {
# try setting the method
eval { $parser->$method($args->{$method}) };
# if there's a problem throw a Tempalte::Exception
$self->throw("option '$method' not supported") if $@;
}
# parse
my $method = "parse_$type";
return $parser->$method($$content);
}
#------------------------------------------------------------------------
# _guess_type($string)
#
# Guesses what type of data this is
#------------------------------------------------------------------------
sub _guess_type
{
# look for a filehandle
return "fh" if _openhandle($_[0]);
# okay, look for the xml declaration at the start
return "string" if $_[0] =~ m/^\<\?xml/;
# okay, look for the html declaration anywhere in the doc
return "html_string" if $_[0] =~ m/<html>/i;
# okay, does this contain a "<" symbol, and declare it to be
# xml if it's got one, though they should use "<?xml"
return "string" if $_[0] =~ m{\<};
# okay, we've tried everything else, return a filename
return "file";
}
#------------------------------------------------------------------------
# _throw($errmsg)
#
# Raise a Template::Exception of type XML.XPath via die().
#------------------------------------------------------------------------
sub throw {
my $self = shift;
die $Template::Plugin::XML::EXCEPTION->new( 'XML.LibXML' => join('', @_) );
}
#------------------------------------------------------------------------
# _openhandle($scalar)
#
# Determines if this is probably an open filehandle or not.
#
# uses openhandle from Scalar::Util if we have it.
#------------------------------------------------------------------------
sub _openhandle ($)
{
return openhandle($_[0]) if defined(&openhandle);
# poor man's openhandle
return defined(fileno $_[0]);
}
#========================================================================
package XML::LibXML::Node;
#========================================================================
#-----------------------------------------------------------------------
# present($view)
#
# Method to present an node via a view, using the block that has the
# same localname.
#-----------------------------------------------------------------------
# note, should this worry about namespaces? Probably. Hmm.
sub present {
my ($self, $view) = @_;
my $localname = $self->localname();
# convert anything that isn't A-Za-z1-9 to _. All those years
# of working on i18n and this throws it all away. I suck.
$localname =~ s/[^A-Za-z0-9]/_/g;
# render out with the block matching the hacked version of localname
$view->view($localname, $self);
}
#-----------------------------------------------------------------------
# content($view)
#
# Method present the node's children via a view
#-----------------------------------------------------------------------
sub content {
my ($self, $view) = @_;
my $output = '';
foreach my $node ($self->childNodes ) {
$output .= $node->present($view);
}
return $output;
}
#----------------------------------------------------------------------
# starttag(), endtag()
#
# Methods to output the start & end tag, e.g. <bar:foo buzz="baz">
# and </bar:foo>
#----------------------------------------------------------------------
sub starttag {
my ($self) = @_;
my $output = "<". $self->nodeName();
foreach my $attr ($self->attributes)
{
$output .= $attr->toString();
}
$output .= ">";
return $output;
}
sub endtag {
my ($self) = @_;
return "</". $self->nodeName() . ">";
}
#========================================================================
package XML::LibXML::Document;
#========================================================================
#------------------------------------------------------------------------
# present($view)
#
# Method to present a document node via a view.
#------------------------------------------------------------------------
sub present {
my ($self, $view) = @_;
# okay, just start rendering from the first element, ignore the pi
# and all that
$self->documentElement->present($view);
}
#========================================================================
package XML::LibXML::Text;
#========================================================================
#------------------------------------------------------------------------
# present($view)
#
# Method to present a text node via a view.
#------------------------------------------------------------------------
sub present {
my ($self, $view) = @_;
$view->view('text', $self->data); # same as $self->nodeData
}
#========================================================================
package XML::LibXML::NodeList;
#========================================================================
#------------------------------------------------------------------------
# present($view)
#
# Method to present a node list via a view. This is only normally useful
# when you call outside of TT as findnodes will be called in list context
# normally
#------------------------------------------------------------------------
sub present {
my ($self, $view) = @_;
my $output = '';
foreach my $node ($self->get_nodelist ) {
$output .= $node->present($view);
}
return $output;
}
#package debug;
#sub debug
#{
# local $^W;
# my $nodename;
# eval { $nodename = $_[0]->nodeName(); };
# my $methodname = (caller(1))[3];
# $methodname =~ s/.*:://;
#
# print STDERR "${nodename}'s $methodname: ".
# (join ",", (map { ref } @_)) .
# "\n";
#}
1;
__END__
=head1 NAME
Template::Plugin::XML::LibXML - XML::LibXML Template Toolkit Plugin
=head1 SYNOPSIS
[% USE docroot = XML.LibXML("helloworld.xml") %]
The message is: [% docroot.find("/greeting/text") %]
=head1 DESCRIPTION
This module provides a plugin for the XML::LibXML module. It can be
utilised the same as any other Template Toolkit plugin, by using a USE
statement from within a Template. The use statment will return a
reference to root node of the parsed document
=head2 Specifying a Data Source
The plugin is capable of using either a string, a filename or a
filehandle as a source for either XML data, or HTML data which will be
converted to XHTML internally.
The USE statement can take one or more arguments to specify what XML
should be processed. If only one argument is passed then the plugin
will attempt to guess how what it has been passed should be
interpreted.
When it is forced to guess what type of data it is used the routine
will first look for an open filehandle, which if it finds it will
assume it's a filehandle to a file containing XML. Failing this (in
decreasing order) it will look for the chars "<?xml" at the start of
what it was passed (and assume it's an XML string,) look for a
"<html>" tag (and assume it's HTML string,) look for a "<" (and assume
it's XML string without a header,) or assume what it's been passed is
the filename to a file containing XML.
In the interests of being explicit, you may specify the type of
data you are loading using the same names as in the B<XML::LibXML>
documentation:
# value contains the xml string
[% USE docroot = XML.LibXML(string => value) %]
# value contains the filename of the xml
[% USE docroot = XML.LibXML(file => value) %]
# value contains an open filehandle to some xml
[% USE docroot = XML.LibXML(fh => value) %]
# value contains the html string
[% USE docroot = XML.LibXML(html_string => value) %]
# value contains the filename of the html
[% USE docroot = XML.LibXML(html_file => value) %]
# value contains an open filehandle to some html
[% USE docroot = XML.LibXML(html_fh => value) %]
Or, if you want you can use similar names to that the XML.XPath
plugin uses:
# value contains the xml string
[% USE docroot = XML.LibXML(xml => value) %] or
[% USE docroot = XML.LibXML(text => value) %]
# value contains the filename of the xml
[% USE docroot = XML.LibXML(filename => value) %]
# value contains the html string
[% USE docroot = XML.LibXML(html => value) %] or
[% USE docroot = XML.LibXML(html_text => value) %]
# value contains the filename of the html
[% USE docroot = XML.LibXML(html_file => value) %]
[% USE docroot = XML.LibXML(html_filename => value) %]
You can provide extra arguments which will be used to set parser
options. See L<XML::LibXML> for details on these. I will repeat the
following warning however: "LibXML options are global (unfortunately
this is a limitation of the underlying implementation, not this
interface)...Note that even two forked processes will share some of
the same options, so be careful out there!"
# turn off expanding entities
[% USE docroot = XML.LibXML("file.xml",
expand_entities => 0);
=head2 Obtaining Parts of an XML Document
XML::LibXML provides two simple mechanisms for obtaining sections
of the XML document, both of which can be used from within
the Template Toolkit
The first of these is to use a XPath statement. Simple values
can be found with the C<findvalue> routine:
# get the title attribute of the first page node
# (note xpath starts counting from one not zero)
[% docroot.findvalue("/website/section/page[1]/@title"); %]
# get the text contained within a node
[% htmldoc.findvalue("/html/body/h1[1]/text()") %]
Nodes of the xml document can be found with the C<findnodes>
# get all the pages ('pages' is a list of nodes)
[% pages = docroot.findnodes("/website/section/page") %]
# get the first page (as TT folds single elements arrays
# to scalars, 'page1' is the one node that matched)
[% page1 = docroot.findnodes("/website/section/page[1]") %]
Then further xpath commands can then be applied to those
nodes in turn:
# get the title attribute of the first page
[% page1.findvalue("@title") %]
An alternative approach is to use individual method calls to move
around the tree. So the above could be written:
# get the text of the h1 node
[% htmlroot.documentElement
.getElementsByLocalName("body").first
.getElementsByLocalName("h1").first
.textContent %]
# get the title of the first page
[% docroot.documentElement
.getElementsByLocalName("section").first
.getElementsByLocalName("page").first
.getAttribute("title") %]
You should use the technique that makes the most since in the
particular situation. These approaches can even be mixed:
# get the first page title
[% page1 = htmlroot.findnodes("/website/section/page[1]");
page1.getAttribute("title") %]
Much more information can be found in L<XML::LibXML::Node>.
=head2 Rendering XML
The simplest way to use this plugin is simply to extract each value
you want to print by hand
The title of the first page is '[%
docroot.findvalue("/website/section/page[1]/@title") %]'
or
The title of the first page is '[%
docroot.documentElement
.getElementsByLocalName("section").first
.getElementsByLocalName("page").first
.getAttribute("title") %]'
You might want to discard whitespace from text areas. XPath
can remove all leading and following whitespace, and condense
all multiple spaces in the text to single spaces.
<p>[% htmlroot.findvalue("normalize-space(
/html/body/p[1]/text()
)" | html %]</p>
Note that, as above, when we're inserting the values extracted into a
XML or HTML document we have to be careful to re-encode the attributes
we need to escape with something like the html filter. A slightly
more advanced technique is to extract a whole node and use the
toString method call on it to convert it to a string of XML. This is
most useful when you are extracting an existing chunk of XML en mass,
as things like E<lt>bE<gt> and E<lt>iE<gt> tags will be passed thought
correctly and entities will be encoded suitably (for example '"' will
be turned into '"')
# get the second paragraph and insert it here as XML
[% htmlroot.findnodes("/html/body/p[2]").toString %]
The most powerful technique is to use a view (as defined by the VIEW
keyword) to recursively render out the XML. By loading this plugin
C<present> methods will be created in the B<XML::LibXML::Node> classes
and subclasses. Calling C<present> on a node with a VIEW will cause
it to be rendered by the view block matching the local name of that
node (with any non alphanumeric charecters turned to underscores. So
a E<lt>authorE<gt> tag will be rendered by the 'author' block. Text
nodes will call the 'text' block with the text of the node.
As the blocks can refer back to both the node it was called with and
the view they can choose to recursively render out it's children using
the view again. To better facilitate this technique the extra methods
C<starttag> (recreate a string of the starting tag, including
attributes,) C<endtag> (recreate a string of the ending tag) and
C<content> (when called with a view, will render by calling all the
children of that node in turn with that view) have been added.
This is probably best shown with a well commented example:
# create the view
[% VIEW myview notfound => 'passthru' %]
# default tag that will recreate the tag 'as is' meaning
# that unknown tags will 'passed though' by the view
[% BLOCK passthru; item.starttag;
item.content(view);
item.endtag;
END %]
# convert all sections to headed paragraphs
[% BLOCK section %]
<h2>[% item.getAttribute("title") %]</h2>
<p>[% item.content(view) %]</p>
[% END %]
# urls link to themselves
[% BLOCK url %]
<a href="[% item.content(view) %]">[% item.content(view) %]</a>
[% END %]
# email link to themselves with mailtos
[% BLOCK email %]
<a href="mailto:[% item.content(view) %]">[% item.content(view) %]</a>
[% END %]
# make pod links bold
[% BLOCK pod %]
<b>[% item.content(view) %]</b>
[% END %]
# render text, re-encoding the attributes as we go
[% BLOCK text; item | html; END %]
# render arrays out
[% BLOCK list; FOREACH i = item; view.print(i); END ; END %]
[% END %]
# use it to render the paragraphs
[% USE doc = XML.LibXML("mydoc.xml") %]
<html>
<head>
<title>[% doc.findvalue("/doc/page[1]/@title") %]</title>
</head>
<body>
[% sections = doc.findnodes("/doc/page[1]/section");
FOREACH section = sections %]
<!-- next section -->
[% section.present(myview);
END %]
</body>
</html>
=head1 BUGS
In order to detect if a scalar is an open filehandle (which is used if
the USE isn't explicit about it's data source) this plugin uses the
C<openhandle> routine from B<Scalar::Util>. If you do not have
B<Scalar::Util> installed, or the version of B<Scalar::Util> is
sufficiently old that it does not support the C<openhandle> routine
then a much cruder C<defined(fileno $scalar)> check will be employed.
Bugs may be reported either via the CPAN RT at
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Template-Plugin-XML-LibXML
or via the Template Toolkit mailing list:
http://www.template-toolkit.org/mailman/listinfo/templates or direct
to the author
=head1 AUTHOR
Written by Mark Fowler <mark@twoshortplanks.com>
Copyright Mark Fowler 2002-3, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
This module wouldn't have been possible without the wonderful work
that has been put into the libxml library by the gnome team or the
equally wonderful work put in by Matt Sergeant and Christian Glahn in
creating XML::LibXML.
=head1 SEE ALSO
L<Template>, L<Template::Plugin>, L<XML::LibXML>, L<XML::LibXML::Node>.
On a similar note, you may want to see L<Template::Plugin::XML::XPath>.
The t/test directory in the Template-Plugin-XML-LibXML distribution
contains all the example XML files discussed in this documentation.
=cut
|