--- nfo/perl/libs/XML/XUpdate/XSLT.pm 2003/05/01 23:40:32 1.4 +++ nfo/perl/libs/XML/XUpdate/XSLT.pm 2003/05/06 14:24:06 1.5 @@ -4,9 +4,14 @@ # # By Andreas Motl, andreas.motl@ilo.de # -# $Id: XSLT.pm,v 1.4 2003/05/01 23:40:32 joko Exp $ +# $Id: XSLT.pm,v 1.5 2003/05/06 14:24:06 joko Exp $ # # $Log: XSLT.pm,v $ +# Revision 1.5 2003/05/06 14:24:06 joko +# doesn't use Carp any more +# updated pod +# attempt to rewrite xml to make it more human readable using helper modules from CPAN (node indentation, etc.) +# # Revision 1.4 2003/05/01 23:40:32 joko # minor update: commented debugging part # @@ -25,7 +30,7 @@ =head1 NAME -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. =head3 Overview @@ -63,7 +68,8 @@ Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? - Q: Should we follow the CRUD path first? (CRUD means: Create, Retrieve, Update, Delete) + Q: Should we follow the CRUD path first? + (CRUD is the acronym for the datastore action primitives: Create, Retrieve, Update, Delete) S?: Proposal: XML::XUpdate::XSLT::API uses XML::XUpdate::XSLT::CRUD @@ -72,6 +78,8 @@ o What about proper encoding? (ISO-8859-1 or UTF-8) o Is it possible to create the required "xsl_template.xml" at runtime via XSL itself? o Cache contents of external files (*.xml). Performance! + o Can exception / error-code handling be improved somehow? + Esp.: It would be interesting, if the XUpdate payload actually could be applied, or not... =cut @@ -84,10 +92,10 @@ use strict; use warnings; -use XML::XSLT 0.41; -use Carp; -use File::Basename; use Data::Dumper; +use File::Basename; +use XML::XSLT 0.41; +use XML::XUpdate::Rewrite; # Namespace constants @@ -145,7 +153,7 @@ sub open_xupdate { my $self = shift; my $xml = shift; - # FIXME: check for filename, filehandle and URL (etc.) + # FIXME: check for filename, filehandle and U{XML}->{xupdate} = $xml; } @@ -156,7 +164,7 @@ } # First, translate the xupdate payload to xsl. -# FIXME: do DOM only! +# FIXME: do DOM only! (don't use "->toString") sub _calculate { my $self = shift; $self->{XSLT_ENGINE_PREP} = XML::XSLT->new( @@ -190,19 +198,35 @@ sub toString { my $self = shift; - return $self->{XML}->{result}; + + # use rest of argument list as hash of option values + my $options = {@_}; + + # short circuit - just return what we have - don't modify anything + return $self->{XML}->{result} unless $options and $options->{rewrite}; + + # Rewrite the xml document with certain engine to desired style. + # 'engine' defaults to "XMLParser" if not specified. + my $rewrite = XML::XUpdate::Rewrite->new( style => $options->{mode}, engine => $options->{using} ); + $rewrite->set_document( $self->{XML}->{result} ); + # TODO: Implement a configurable fallback here to return the un-rewritten payload if desired. + $rewrite->process() or die(__PACKAGE__ . ": Error while rewriting XML document."); + return $rewrite->get_document(); } - + ###################################################################### # AUXILIARY METHODS -# Argument parsing (with backwards compatibility hook). +# Argument list parsing. +# ... from XML::XUpdate::LibXML (w/o backwards compatibility hook). +# Could this make up a Class::AutoFill::__read_arglist which transparently +# makes object attributes from constructor arguments in a configurable way? sub __parse_args { my $self = shift; my %args; - if(@_ % 2 ) { + if ( @_ % 2 ) { $args{dummy} = shift; %args = (%args, @_); } else { @@ -228,7 +252,7 @@ if (! -e $filename) { $filename = dirname(__FILE__) . "/$filename"; if (! -e $filename) { - croak("File $filename does not exist."); + die("File $filename does not exist."); } }