7 |
# $Id$ |
# $Id$ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
# Revision 1.2 2003/05/01 17:03:52 joko |
# Revision 1.5 2003/05/06 14:24:06 joko |
11 |
# revamped processing: a) _calculate and b) _apply (NEW) |
# doesn't use Carp any more |
12 |
|
# updated pod |
13 |
|
# attempt to rewrite xml to make it more human readable using helper modules from CPAN (node indentation, etc.) |
14 |
|
# |
15 |
|
# Revision 1.4 2003/05/01 23:40:32 joko |
16 |
|
# minor update: commented debugging part |
17 |
|
# |
18 |
|
# Revision 1.3 2003/05/01 20:11:49 joko |
19 |
|
# * added pod from xupdate.pl |
20 |
|
# - extracted xml to external files |
21 |
# |
# |
22 |
# Revision 1.1 2003/04/30 02:36:32 joko |
# Revision 1.1 2003/04/30 02:36:32 joko |
23 |
# initial commit |
# initial commit |
25 |
# |
# |
26 |
############################################################################### |
############################################################################### |
27 |
|
|
28 |
|
=pod |
29 |
|
|
30 |
|
|
31 |
=head1 NAME |
=head1 NAME |
32 |
|
|
33 |
XML::XUpdate::XSLT - A perl module for updating xml documents using XUpdate via XSLT. |
XML::XUpdate::XSLT - A perl module for updating xml documents using XUpdate via XSLT. |
34 |
|
|
35 |
|
|
36 |
|
=head3 Overview |
37 |
|
|
38 |
|
This is not the same xupdate currently available from CPAN at |
39 |
|
http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/xupdate . |
40 |
|
|
41 |
|
Its intention - however - is identical: |
42 |
|
xupdate - Process XUpdate commands against an XML document. |
43 |
|
|
44 |
|
|
45 |
|
=head3 Their implementations differ: |
46 |
|
|
47 |
|
1. xupdate (by Petr Pajas) uses ... |
48 |
|
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
49 |
|
|
50 |
|
... which is based on XML::LibXML which in turn is: |
51 |
|
[...] |
52 |
|
This module is an interface to the gnome libxml2 DOM parser (no SAX parser support yet), |
53 |
|
and the DOM tree. It also provides an XML::XPath-like findnodes() interface, providing |
54 |
|
access to the XPath API in libxml2. |
55 |
|
[...] |
56 |
|
|
57 |
|
2. This xupdate attempts to implement the XUpdate specs using XSLT only. |
58 |
|
|
59 |
|
|
60 |
|
=head3 Yet another xupdate - facts in short: |
61 |
|
|
62 |
|
S: It would be nice to have a pure perl thingy which does (almost) the same stuff.... |
63 |
|
|
64 |
|
Q: Can we achieve compliance with its (XML::XUpdate::LibXML) API? (or just a subset ....) |
65 |
|
|
66 |
|
Q: Can we achieve the processing using CPAN's XML::XSLT? |
67 |
|
S: Proposal: XML::XUpdate::XSLT!? |
68 |
|
|
69 |
|
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
70 |
|
|
71 |
|
Q: Should we follow the CRUD path first? |
72 |
|
(CRUD is the acronym for the datastore action primitives: Create, Retrieve, Update, Delete) |
73 |
|
S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD |
74 |
|
|
75 |
|
|
76 |
|
=head4 Todo |
77 |
|
|
78 |
|
o What about proper encoding? (ISO-8859-1 or UTF-8) |
79 |
|
o Is it possible to create the required "xsl_template.xml" at runtime via XSL itself? |
80 |
|
o Cache contents of external files (*.xml). Performance! |
81 |
|
o Can exception / error-code handling be improved somehow? |
82 |
|
Esp.: It would be interesting, if the XUpdate payload actually could be applied, or not... |
83 |
|
|
84 |
|
|
85 |
=cut |
=cut |
86 |
|
|
92 |
use strict; |
use strict; |
93 |
use warnings; |
use warnings; |
94 |
|
|
|
use XML::XSLT 0.41; |
|
|
use Carp; |
|
95 |
use Data::Dumper; |
use Data::Dumper; |
96 |
|
use File::Basename; |
97 |
|
use XML::XSLT 0.41; |
98 |
|
use XML::XUpdate::Rewrite; |
99 |
|
|
100 |
# Namespace constants |
# Namespace constants |
101 |
|
|
103 |
|
|
104 |
use vars qw ( $VERSION ); |
use vars qw ( $VERSION ); |
105 |
|
|
106 |
$VERSION = '0.02'; |
$VERSION = '0.01'; |
107 |
|
|
108 |
|
|
109 |
###################################################################### |
###################################################################### |
117 |
$self->{DEBUG} = $args->{debug}; |
$self->{DEBUG} = $args->{debug}; |
118 |
$self->{WARNINGS} = $args->{warnings}; |
$self->{WARNINGS} = $args->{warnings}; |
119 |
|
|
120 |
$self->__init_default_macros(); |
$self->__init_default_stylesheets(); |
121 |
|
|
122 |
return $self; |
return $self; |
123 |
|
|
135 |
my $xml = shift; |
my $xml = shift; |
136 |
my $options = shift; |
my $options = shift; |
137 |
if ($options->{encap}) { |
if ($options->{encap}) { |
138 |
$xml = qq(<?xml version="1.0" encoding="ISO-8859-1"?> |
my $template = $self->__slurp_file("xsl_template.xml"); |
139 |
|
# FIXME! What about the quirky? Is there a better one? |
140 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
$template =~ s!<xsl:quirky_placeholder />!$xml!; |
141 |
|
$xml = $template; |
|
<!-- |
|
|
This XML Stylesheet is the result of a translation from |
|
|
XUpdate lingo. It actually applies the payload to the |
|
|
original xml document. (it's the second processing step!) |
|
|
--> |
|
|
|
|
|
<xsl:output method="xml" /> |
|
|
|
|
|
$xml |
|
|
|
|
|
</xsl:stylesheet> |
|
|
); |
|
142 |
} |
} |
143 |
$self->{XML}->{xsl}->{$name} = $xml; |
$self->{XML}->{xsl}->{$name} = $xml; |
144 |
} |
} |
153 |
sub open_xupdate { |
sub open_xupdate { |
154 |
my $self = shift; |
my $self = shift; |
155 |
my $xml = shift; |
my $xml = shift; |
156 |
# FIXME: check for filename, filehandle and URL (etc.) |
# FIXME: check for filename, filehandle and U<RL (etc.) |
157 |
$self->{XML}->{xupdate} = $xml; |
$self->{XML}->{xupdate} = $xml; |
158 |
} |
} |
159 |
|
|
164 |
} |
} |
165 |
|
|
166 |
# First, translate the xupdate payload to xsl. |
# First, translate the xupdate payload to xsl. |
167 |
# FIXME: do DOM only! |
# FIXME: do DOM only! (don't use "->toString") |
168 |
sub _calculate { |
sub _calculate { |
169 |
my $self = shift; |
my $self = shift; |
170 |
$self->{XSLT_ENGINE_PREP} = XML::XSLT->new( |
$self->{XSLT_ENGINE_PREP} = XML::XSLT->new( |
181 |
# FIXME: do DOM only! |
# FIXME: do DOM only! |
182 |
sub _apply { |
sub _apply { |
183 |
my $self = shift; |
my $self = shift; |
184 |
#print $self->get_stylesheet("_worker"), "\n"; |
|
185 |
|
# debug - print the calculated xsl on STDERR |
186 |
|
#print STDERR $self->get_stylesheet("_worker"), "\n"; |
187 |
|
|
188 |
#return; |
#return; |
189 |
$self->{XSLT_ENGINE_LIVE} = XML::XSLT->new( |
$self->{XSLT_ENGINE_LIVE} = XML::XSLT->new( |
190 |
Source => $self->get_stylesheet("_worker"), |
Source => $self->get_stylesheet("_worker"), |
196 |
$self->{XML}->{result} = $self->{XSLT_ENGINE_LIVE}->toString(); |
$self->{XML}->{result} = $self->{XSLT_ENGINE_LIVE}->toString(); |
197 |
} |
} |
198 |
|
|
|
|
|
199 |
sub toString { |
sub toString { |
200 |
my $self = shift; |
my $self = shift; |
201 |
return $self->{XML}->{result}; |
|
202 |
|
# use rest of argument list as hash of option values |
203 |
|
my $options = {@_}; |
204 |
|
|
205 |
|
# short circuit - just return what we have - don't modify anything |
206 |
|
return $self->{XML}->{result} unless $options and $options->{rewrite}; |
207 |
|
|
208 |
|
# Rewrite the xml document with certain engine to desired style. |
209 |
|
# 'engine' defaults to "XMLParser" if not specified. |
210 |
|
my $rewrite = XML::XUpdate::Rewrite->new( style => $options->{mode}, engine => $options->{using} ); |
211 |
|
$rewrite->set_document( $self->{XML}->{result} ); |
212 |
|
# TODO: Implement a configurable fallback here to return the un-rewritten payload if desired. |
213 |
|
$rewrite->process() or die(__PACKAGE__ . ": Error while rewriting XML document."); |
214 |
|
return $rewrite->get_document(); |
215 |
} |
} |
216 |
|
|
217 |
|
|
218 |
###################################################################### |
###################################################################### |
219 |
# AUXILIARY METHODS |
# AUXILIARY METHODS |
220 |
|
|
221 |
# Argument parsing (with backwards compatibility hook). |
# Argument list parsing. |
222 |
|
# ... from XML::XUpdate::LibXML (w/o backwards compatibility hook). |
223 |
|
# Could this make up a Class::AutoFill::__read_arglist which transparently |
224 |
|
# makes object attributes from constructor arguments in a configurable way? |
225 |
sub __parse_args { |
sub __parse_args { |
226 |
my $self = shift; |
my $self = shift; |
227 |
my %args; |
my %args; |
228 |
|
|
229 |
if(@_ % 2 ) { |
if ( @_ % 2 ) { |
230 |
$args{dummy} = shift; |
$args{dummy} = shift; |
231 |
%args = (%args, @_); |
%args = (%args, @_); |
232 |
} else { |
} else { |
237 |
} |
} |
238 |
|
|
239 |
|
|
240 |
sub __init_default_macros { |
sub __init_default_stylesheets { |
241 |
my $self = shift; |
my $self = shift; |
242 |
$self->{XML}->{xsl}->{xupdate2xsl} = qq(<?xml version="1.0" encoding="ISO-8859-1"?> |
if (my $payload = $self->__slurp_file("xupdate2xsl.xml")) { |
243 |
|
$self->{XML}->{xsl}->{xupdate2xsl} = $payload; |
244 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
} |
245 |
|
} |
246 |
|
|
247 |
|
sub __slurp_file { |
248 |
|
my $self = shift; |
249 |
|
my $filename = shift; |
250 |
|
|
251 |
<!-- |
# does file exist? |
252 |
Purpose of this XML Stylesheet is to implement a set of templates |
if (! -e $filename) { |
253 |
to translate XUpdate lingo into an intermediate xslt stylesheet |
$filename = dirname(__FILE__) . "/$filename"; |
254 |
which actually performs the update to the original xml document |
if (! -e $filename) { |
255 |
in a second step. |
die("File $filename does not exist."); |
256 |
--> |
} |
257 |
|
} |
|
<xsl:output method="xml" /> |
|
|
|
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
|
|
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
|
|
<!-- activate this --> |
|
|
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
|
|
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
|
|
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
|
|
|
|
|
<!-- 2. This is the translation part: XUpdate becomes XSLT --> |
|
|
|
|
|
<!-- This node "encapsulates" common infrastructure. --> |
|
|
<xsl:template match="xupdate:modifications"> |
|
|
|
|
|
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
|
|
<!-- in fact this is the xsl from above translated to be able to be generated by xsl itself! --> |
|
|
<xsl:comment> 1. passthru logic </xsl:comment> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="name">passthru</xsl:attribute> |
|
|
<xsl:element name="xsl:copy"><xsl:element name="xsl:apply-templates" /></xsl:element> |
|
|
</xsl:element> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="match">*</xsl:attribute> |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
|
|
</xsl:element> |
|
|
<xsl:element name="xsl:template"> |
|
|
<xsl:attribute name="match">comment()</xsl:attribute> |
|
|
<xsl:element name="xsl:call-template"><xsl:attribute name="name">passthru</xsl:attribute></xsl:element> |
|
|
</xsl:element> |
|
|
|
|
|
<!-- continue with all inline nodes --> |
|
|
<xsl:apply-templates /> |
|
|
|
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node "encapsulates" infrastructure for handling the directives. --> |
|
|
<xsl:template match="xupdate:insert-after"> |
|
|
<xsl:comment> 2. context finder </xsl:comment> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
|
|
<xsl:template match="xupdate:element"> |
|
|
<xsl:comment> 3. rewrite / vivify elements/attributes </xsl:comment> |
|
|
<xsl:element name="xsl:element"> |
|
|
<xsl:copy-of select="@*"/> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:element> |
|
|
</xsl:template> |
|
|
|
|
|
<!-- This node passes through all attributes and childnodes rewriting the tagname only. --> |
|
|
<xsl:template match="xupdate:attribute"> |
|
|
<xsl:element name="xsl:attribute"> |
|
|
<xsl:copy-of select="@*"/> |
|
|
<xsl:apply-templates /> |
|
|
</xsl:element> |
|
|
</xsl:template> |
|
|
|
|
|
</xsl:stylesheet> |
|
258 |
|
|
259 |
); |
open(FH, "<" . $filename); |
260 |
|
my @lines = <FH>; |
261 |
|
my $content = join("", @lines); |
262 |
|
close(FH); |
263 |
|
return $content; |
264 |
} |
} |
265 |
|
|
266 |
1; |
1; |