| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package XML::LibXML::Pipeline; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | XML::LibXML::Pipeline - a pipe of XML::LibXSLT objects | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | my $p = XML::LibXML::Pipeline->new( | ||
| 13 | parse_xslt_file("$XSLDIR/detail.xsl"), | ||
| 14 | Paris::Transformer::Photo->new("$CACHEDIR"), | ||
| 15 | parse_xslt_file("$XSLDIR/page.xsl"), | ||
| 16 | ); | ||
| 17 | |||
| 18 | $res = $p->transform(parse_xml_file("input.xml")); | ||
| 19 | |||
| 20 | $p->output_file($res, "output.xml"); | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This enables easy chaining of multiple XML transformer objects. The | ||
| 25 | objects in the pipeline do not have to be XML::LibXSLT objects; they | ||
| 26 | just need to implement the transform() method. (This method must | ||
| 27 | take, and return, and XML::LibXML::Document.) In addition, the I<last> | ||
| 28 | transformer in the pipeline must support the methods | ||
| 29 | C<output_string($doc)> and C<output_file($doc, $filename)> if you wish | ||
| 30 | to use these methods. | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | =over 4 | ||
| 35 | |||
| 36 | =item my $p = XML::LibXML::Pipeline->new($obj1 [, $obj2]...) | ||
| 37 | |||
| 38 | Constructor. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $proto = shift; | ||
| 44 | my $class = ref($proto) || $proto; | ||
| 45 | |||
| 46 | my $self = { | ||
| 47 | PIPELINE => [ @_ ] | ||
| 48 | }; | ||
| 49 | |||
| 50 | bless $self, $class; | ||
| 51 | } | ||
| 52 | |||
| 53 | =item $res = $p->transform($doc [, %args ]) | ||
| 54 | |||
| 55 | Transforms C<$doc>; that is, each of the objects in the pipeline are | ||
| 56 | called with parameters (C<$doc>, C<%args>). | ||
| 57 | |||
| 58 | All objects are called with the same arguments; there is no way to | ||
| 59 | selectively pass arguments to the transformers. (If you need to do | ||
| 60 | this, assemble the pipeline by hand.) | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub transform { | ||
| 65 | my ($self, $doc, %args) = @_; | ||
| 66 | |||
| 67 | my $tmp = $doc; | ||
| 68 | |||
| 69 | foreach (@{$self->{PIPELINE}}) { | ||
| 70 | $tmp = $_->transform($tmp, %args); | ||
| 71 | } | ||
| 72 | |||
| 73 | return $tmp; | ||
| 74 | } | ||
| 75 | |||
| 76 | =item $s = $p->output_string($doc) | ||
| 77 | |||
| 78 | This runs the C<output_string()> method of the I<last> object in the | ||
| 79 | pipeline on C<$doc>, returning a string. | ||
| 80 | |||
| 81 | Note that C<$doc> itself has C<toString()> and C<toFile()> methods; as | ||
| 82 | L<XML::LibXSLT> explains, however, "I<always> output the document in | ||
| 83 | XML format, and in UTF8, which may not be what you asked for in the | ||
| 84 | xsl:output tag." | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub output_string { | ||
| 89 | my ($self, @args) = @_; | ||
| 90 | |||
| 91 | return $self->{PIPELINE}[-1]->output_string(@args); | ||
| 92 | } | ||
| 93 | |||
| 94 | =item $p->output_file($doc, $filename) | ||
| 95 | |||
| 96 | This runs the C<output_file()> method of the I<last> object in the | ||
| 97 | pipeline on C<$doc>. | ||
| 98 | |||
| 99 | See also L<output_string()>. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub output_file { | ||
| 104 | my ($self, @args) = @_; | ||
| 105 | |||
| 106 | return $self->{PIPELINE}[-1]->output_file(@args); | ||
| 107 | } | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =head1 AUTHOR | ||
| 112 | |||
| 113 | Michael Stillwell <mjs@beebo.org> | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | 1; |