line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::XML::Transformer; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
14386
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
68552
|
|
|
2
|
|
|
|
|
10
|
|
6
|
2
|
|
|
2
|
|
406
|
use Moo; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
9
|
|
7
|
2
|
|
|
2
|
|
452
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
114
|
|
8
|
2
|
|
|
2
|
|
2273
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use XML::LibXSLT; |
10
|
|
|
|
|
|
|
use Scalar::Util qw(blessed reftype); |
11
|
|
|
|
|
|
|
use XML::Struct::Reader; |
12
|
|
|
|
|
|
|
use XML::Struct::Writer; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has stylesheet => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
coerce => sub { |
17
|
|
|
|
|
|
|
ref $_[0] // '' eq 'ARRAY' ? $_[0] : [ split /,/, $_[0] ] |
18
|
|
|
|
|
|
|
}, |
19
|
|
|
|
|
|
|
default => sub { [] } |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has output_format => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
coerce => sub { defined $_[0] ? lc $_[0] : undef } |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has process => ( |
28
|
|
|
|
|
|
|
is => 'lazy', |
29
|
|
|
|
|
|
|
builder => sub { |
30
|
|
|
|
|
|
|
[ |
31
|
|
|
|
|
|
|
map { |
32
|
|
|
|
|
|
|
XML::LibXSLT->new()->parse_stylesheet( |
33
|
|
|
|
|
|
|
XML::LibXML->load_xml(location => $_, no_cdata=>1) |
34
|
|
|
|
|
|
|
) |
35
|
|
|
|
|
|
|
} @{$_[0]->stylesheet} |
36
|
|
|
|
|
|
|
] |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub BUILD { |
41
|
|
|
|
|
|
|
if (@{$_[0]->process} and $_[0]->process->[-1]->output_method eq 'text') { |
42
|
|
|
|
|
|
|
$_[0]->{output_format} = 'string'; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub transform { |
47
|
|
|
|
|
|
|
my ($self, $xml) = @_; |
48
|
|
|
|
|
|
|
my ($format, $result); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
return if !defined $xml; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if (blessed $xml) { |
53
|
|
|
|
|
|
|
if ($xml->isa('XML::LibXML::Document') or $xml->isa('XML::LibXML::Element')) { |
54
|
|
|
|
|
|
|
$format = 'dom'; |
55
|
|
|
|
|
|
|
} else { |
56
|
|
|
|
|
|
|
croak "Cannot convert ".ref($xml)." to XML"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} elsif (ref $xml) { |
59
|
|
|
|
|
|
|
if (reftype $xml eq 'ARRAY') { |
60
|
|
|
|
|
|
|
$format = 'struct'; |
61
|
|
|
|
|
|
|
$xml = XML::Struct::Writer->new->write($xml); |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
$format = 'simple'; |
64
|
|
|
|
|
|
|
$xml = XML::Struct::Writer->new(simple => 1)->write($xml); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} else { |
67
|
|
|
|
|
|
|
$format = 'string'; |
68
|
|
|
|
|
|
|
$xml = XML::LibXML->load_xml(string => $xml); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$format = $self->output_format if $self->output_format; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
if (@{$self->process}) { |
74
|
|
|
|
|
|
|
foreach (@{$self->process}) { |
75
|
|
|
|
|
|
|
$xml = $_->transform($xml); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
if ($format eq 'string') { |
80
|
|
|
|
|
|
|
if ($self->process->[-1]) { |
81
|
|
|
|
|
|
|
return $self->process->[-1]->output_as_chars($xml); |
82
|
|
|
|
|
|
|
} else { |
83
|
|
|
|
|
|
|
return $xml->toString; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} elsif ($format eq 'struct') { |
86
|
|
|
|
|
|
|
return XML::Struct::Reader->new( from => $xml )->readDocument; |
87
|
|
|
|
|
|
|
} elsif ($format eq 'simple') { |
88
|
|
|
|
|
|
|
my $reader = XML::Struct::Reader->new( from => $xml, simple => 1, root => 1 ); |
89
|
|
|
|
|
|
|
return $reader->readDocument; |
90
|
|
|
|
|
|
|
} else { |
91
|
|
|
|
|
|
|
return $xml; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |