7 |
# $Id$ |
# $Id$ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
|
# Revision 1.5 2003/05/06 14:24:06 joko |
11 |
|
# 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 |
# Revision 1.3 2003/05/01 20:11:49 joko |
19 |
# * added pod from xupdate.pl |
# * added pod from xupdate.pl |
20 |
# - extracted xml to external files |
# - extracted xml to external files |
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 |
=head3 Overview |
68 |
|
|
69 |
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
70 |
|
|
71 |
Q: Should we follow the CRUD path first? (CRUD means: Create, Retrieve, Update, Delete) |
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 |
S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD |
74 |
|
|
75 |
|
|
78 |
o What about proper encoding? (ISO-8859-1 or UTF-8) |
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? |
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! |
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 |
92 |
use strict; |
use strict; |
93 |
use warnings; |
use warnings; |
94 |
|
|
|
use XML::XSLT 0.41; |
|
|
use Carp; |
|
|
use File::Basename; |
|
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 |
|
|
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"), |
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 { |
252 |
if (! -e $filename) { |
if (! -e $filename) { |
253 |
$filename = dirname(__FILE__) . "/$filename"; |
$filename = dirname(__FILE__) . "/$filename"; |
254 |
if (! -e $filename) { |
if (! -e $filename) { |
255 |
croak("File $filename does not exist."); |
die("File $filename does not exist."); |
256 |
} |
} |
257 |
} |
} |
258 |
|
|