line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
591
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
2
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package XML::LibXML::Transformer; |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
76
|
|
7
|
1
|
|
|
1
|
|
492
|
use XML::LibXML::Enhanced qw(parse_xml_chunk); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
|
|
|
|
|
|
my ($proto, $namespace) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
croak "error: namespace not defined" unless defined $namespace; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $class = ref($proto) || $proto; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $self = { |
17
|
|
|
|
|
|
|
NAMESPACE => $namespace, |
18
|
|
|
|
|
|
|
}; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
bless $self, $class; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub transform { |
24
|
|
|
|
|
|
|
my ($self, $doc, @args) = @_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# In scalar context, findnodes returns a NodeList object which |
27
|
|
|
|
|
|
|
# provides iterators, etc. This might be worth investigating if |
28
|
|
|
|
|
|
|
# findnodes is returning a lot of nodes, and the following gives |
29
|
|
|
|
|
|
|
# poor performance. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my @n = $doc->findnodes( |
32
|
|
|
|
|
|
|
"descendant-or-self::*[namespace-uri() = '$self->{NAMESPACE}']" |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
foreach my $n (@n) { |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $method = $n->localname; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $r = eval { |
40
|
|
|
|
|
|
|
$self->$method($n->toAttributeHash, $n, @args); |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# TODO check that $method actually exists before this call... |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
if ($@) { |
46
|
|
|
|
|
|
|
croak "error: call to $method failed: $@"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
if (defined $r) { |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# A STRING |
52
|
|
|
|
|
|
|
if (!ref($r)) { |
53
|
|
|
|
|
|
|
$n->replaceNode($doc->adoptNode(parse_xml_chunk($r))); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# A NODE |
57
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($r, "XML::LibXML::Node")) { |
58
|
|
|
|
|
|
|
$n->replaceNode($r); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
else { |
62
|
|
|
|
|
|
|
carp qq{warning: "$method" returned neither a node nor a string}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |