File: Transformer.pm

package info (click to toggle)
libcatmandu-xml-perl 0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 387; xml: 28; makefile: 2; sh: 1
file content (153 lines) | stat: -rw-r--r-- 3,764 bytes parent folder | download
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
package Catmandu::XML::Transformer;

our $VERSION = '0.17';

use Catmandu::Sane;
use Moo;
use Carp;
use XML::LibXML;
use XML::LibXSLT;
use Scalar::Util qw(blessed reftype);
use XML::Struct::Reader;
use XML::Struct::Writer;

has stylesheet => (
    is      => 'ro',
    coerce  => sub {
        ref $_[0] // '' eq 'ARRAY' ? $_[0] : [ split /,/, $_[0] ]
    },
    default => sub { [] }
);

has output_format => (
    is     => 'ro', 
    coerce => sub { defined $_[0] ? lc $_[0] : undef }
);

has process => (
    is      => 'lazy',
    builder => sub {
        [
            map {
                XML::LibXSLT->new()->parse_stylesheet(
                    XML::LibXML->load_xml(location => $_, no_cdata=>1)
                )
            } @{$_[0]->stylesheet} 
        ]
    }
);

sub BUILD {
    if (@{$_[0]->process} and $_[0]->process->[-1]->output_method eq 'text') {
        $_[0]->{output_format} = 'string'; 
    }
}

sub transform {
    my ($self, $xml) = @_;
    my ($format, $result);

    return if !defined $xml;

    if (blessed $xml) {
        if ($xml->isa('XML::LibXML::Document') or $xml->isa('XML::LibXML::Element')) {
            $format = 'dom';
        } else {
            croak "Cannot convert ".ref($xml)." to XML";
        }
    } elsif (ref $xml) {
        if (reftype $xml eq 'ARRAY') {
            $format = 'struct';
            $xml = XML::Struct::Writer->new->write($xml);
        } else {
            $format = 'simple';
            $xml = XML::Struct::Writer->new(simple => 1)->write($xml);
        }
    } else {
        $format = 'string';
        $xml = XML::LibXML->load_xml(string => $xml);
    }

    $format = $self->output_format if $self->output_format;

    if (@{$self->process}) {
        foreach (@{$self->process}) {
            $xml = $_->transform($xml);
        }
    }

    if ($format eq 'string') {
        if ($self->process->[-1]) {
            return $self->process->[-1]->output_as_chars($xml);
        } else {
            return $xml->toString;
        }
    } elsif ($format eq 'struct') {
        return XML::Struct::Reader->new( from => $xml )->readDocument;
    } elsif ($format eq 'simple') {
        my $reader = XML::Struct::Reader->new( from => $xml, simple => 1, root => 1 );
        return $reader->readDocument;
    } else {
        return $xml;
    }
}

1;
__END__

=head1 NAME

Catmandu::XML::Transformer - Utility module for XML/XSLT processing

=cut

=head1 SYNOPISIS

    $transformer = Catamandu::XML::Transformer->new( stylesheet => 'file.xsl' );

    $xml_string = $transformer->transform( $xml_string ); # SCALAR
    $xml_dom    = $transformer->transform( $xml_dom );    # XML::LibXML::Document
    $xml_struct = $transformer->transform( $xml_struct ); # ARRAY reference
    $xml_simple = $transformer->transform( $xml_simple ); # HASH reference

    $transformer = Catamandu::XML::Transformer->new( output_format => 'string' );
    $xml_string  = $transformer->transform( $xml );       # any XML to SCALAR

=head1 CONFIGURATION

=over

=item stylesheet

Zero or more XSLT files given as comma-separated list of files or array
reference with multiple files to apply as transformation pipeline. Files are
parsed once on instantiation of the Catmandu::XML::Transformer object.

=item output_format

Expected output format C<dom>, C<string>, C<struct>, C<simple>. By default the
input format triggers the output format. If the last stylesheet has text output
(C<< <xsl:output method="text"/> >>) then output format is automatically set to
C<string>.

=back

=head1 METHODS

=over

=item stylesheet()

Returns an array reference of XSLT filenames used as transformation pipeline.

=item output_format()

Returns the output format or C<undef>.

=back

=head1 SEE ALSO

L<XML::Struct>

=cut