| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XML::XPathScript; |
|
2
|
|
|
|
|
|
|
|
|
3
|
22
|
|
|
22
|
|
355790
|
use strict; |
|
|
22
|
|
|
|
|
54
|
|
|
|
22
|
|
|
|
|
944
|
|
|
4
|
22
|
|
|
22
|
|
117
|
use warnings; |
|
|
22
|
|
|
|
|
37
|
|
|
|
22
|
|
|
|
|
682
|
|
|
5
|
22
|
|
|
22
|
|
110
|
use Carp; |
|
|
22
|
|
|
|
|
37
|
|
|
|
22
|
|
|
|
|
8622
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# $Revision$ - $Date$ |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=pod |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
XML::XPathScript - a Perl framework for XML stylesheets |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use XML::XPathScript; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# the short way |
|
20
|
|
|
|
|
|
|
my $xps = XML::XPathScript->new; |
|
21
|
|
|
|
|
|
|
my $transformed = $xps->transform( $xml, $stylesheet ); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# having the output piped to STDOUT directly |
|
24
|
|
|
|
|
|
|
my $xps = XML::XPathScript->new( xml => $xml, stylesheet => $stylesheet ); |
|
25
|
|
|
|
|
|
|
$xps->process; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# caching the compiled stylesheet for reuse and |
|
28
|
|
|
|
|
|
|
# outputting to multiple files |
|
29
|
|
|
|
|
|
|
my $xps = XML::XPathScript->new( stylesheetfile => $filename ) |
|
30
|
|
|
|
|
|
|
foreach my $xml (@xmlfiles) { |
|
31
|
|
|
|
|
|
|
my $transformed = $xps->transform( $xml ); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# do stuff with $transformed ... |
|
34
|
|
|
|
|
|
|
}; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Making extra variables available to the stylesheet dialect: |
|
37
|
|
|
|
|
|
|
my $xps = XML::XPathScript->new; |
|
38
|
|
|
|
|
|
|
$xps->compile( qw/ $foo $bar / ); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# in stylesheet, $foo will be set to 'a' |
|
41
|
|
|
|
|
|
|
# and $bar to 'b' |
|
42
|
|
|
|
|
|
|
$xps->transform( $xml, $stylesheet, [ 'a', 'b' ] ); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
XPathScript is a stylesheet language similar in many ways to XSLT (in |
|
47
|
|
|
|
|
|
|
concept, not in appearance), for transforming XML from one format to |
|
48
|
|
|
|
|
|
|
another (possibly HTML, but XPathScript also shines for non-XML-like |
|
49
|
|
|
|
|
|
|
output). |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Like XSLT, XPathScript offers a dialect to mix verbatim portions of |
|
52
|
|
|
|
|
|
|
documents and code. Also like XSLT, it leverages the powerful |
|
53
|
|
|
|
|
|
|
``templates/apply-templates'' and ``cascading stylesheets'' design |
|
54
|
|
|
|
|
|
|
patterns, that greatly simplify the design of stylesheets for |
|
55
|
|
|
|
|
|
|
programmers. The availability of the I query language inside |
|
56
|
|
|
|
|
|
|
stylesheets promotes the use of a purely document-dependent, |
|
57
|
|
|
|
|
|
|
side-effect-free coding style. But unlike XSLT which uses its own |
|
58
|
|
|
|
|
|
|
dedicated control language with an XML-compliant syntax, XPathScript |
|
59
|
|
|
|
|
|
|
uses Perl which is terse and highly extendable. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The result of the merge is an extremely powerful tool for rendering |
|
62
|
|
|
|
|
|
|
complex XML documents into other formats. Stylesheets written in |
|
63
|
|
|
|
|
|
|
XPathScript are very easy to create, extend and reuse, even if they |
|
64
|
|
|
|
|
|
|
manage hundreds of different XML tags. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 STYLESHEET WRITER DOCUMENTATION |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
If you are interested to write stylesheets, refers to the |
|
69
|
|
|
|
|
|
|
B manpage. You might also want |
|
70
|
|
|
|
|
|
|
to take a peek at the manpage of B, a program |
|
71
|
|
|
|
|
|
|
bundled with this module to perform XPathScript transformations |
|
72
|
|
|
|
|
|
|
via the command line. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 STYLESHEET UTILITY METHODS |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Those methods are meants to be used from within a stylesheet. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 current |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$xps = XML::XPathScript->current |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
This class method returns |
|
83
|
|
|
|
|
|
|
the stylesheet object currently being applied. This can be called from |
|
84
|
|
|
|
|
|
|
anywhere within the stylesheet, except a BEGIN or END block or |
|
85
|
|
|
|
|
|
|
similar. B that using the return value for altering (as |
|
86
|
|
|
|
|
|
|
opposed to reading) stuff from anywhere except the stylesheet's top |
|
87
|
|
|
|
|
|
|
level is unwise. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub current { |
|
92
|
0
|
0
|
|
0
|
|
0
|
croak 'Wrong context for calling current()' |
|
93
|
|
|
|
|
|
|
unless defined $XML::XPathScript::current; |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
return $XML::XPathScript::current; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 interpolation |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$interpolate = $XML::XPathScript::current->interpolation |
|
101
|
|
|
|
|
|
|
$interpolate = $XML::XPathScript::current->interpolation( $boolean ) |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Gets (first call form) or sets (second form) the XPath interpolation |
|
104
|
|
|
|
|
|
|
boolean flag. If true, values set in C< pre > and C< post > |
|
105
|
|
|
|
|
|
|
may contain expressions within curly braces, that will be |
|
106
|
|
|
|
|
|
|
interpreted as XPath expressions and substituted in place. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
For example, when interpolation is on, the following code |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$template->set( link => { pre => '', |
|
111
|
|
|
|
|
|
|
post => '' } ); |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
is enough for rendering a C<< >> element as an HTML hyperlink. |
|
114
|
|
|
|
|
|
|
The interpolation-less version is slightly more complex as it requires a |
|
115
|
|
|
|
|
|
|
C: |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub link_testcode { |
|
118
|
|
|
|
|
|
|
my ($node, $t) = @_; |
|
119
|
|
|
|
|
|
|
my $url = $node->findvalue('@url'); |
|
120
|
|
|
|
|
|
|
$t->set({ pre => "", |
|
121
|
|
|
|
|
|
|
post => "" }); |
|
122
|
|
|
|
|
|
|
return DO_SELF_AND_KIDS(); |
|
123
|
|
|
|
|
|
|
}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Interpolation is on by default. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=cut |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub interpolation { |
|
130
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
131
|
0
|
|
|
|
|
0
|
return $self->interpolating( @_ ); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub interpolating { |
|
135
|
0
|
|
|
0
|
|
0
|
my $self=shift; |
|
136
|
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
0
|
if ( @_ ) { |
|
138
|
0
|
|
|
|
|
0
|
$self->processor->set_interpolation( |
|
139
|
|
|
|
|
|
|
$self->{interpolating} = shift |
|
140
|
|
|
|
|
|
|
); |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
0
|
|
0
|
|
|
0
|
return $self->{interpolating} || 0; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 interpolation_regex |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
$regex = $XML::XPathScript::current->interpolation_regex |
|
149
|
|
|
|
|
|
|
$XML::XPathScript::current->interpolation_regex( $regex ) |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Gets or sets the regex to use for interpolation. The value to be |
|
152
|
|
|
|
|
|
|
interpolated must be capture by $1. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
By default, the interpolation regex is qr/{(.*?)}/. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Example: |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
$XML::XPathScript::current->interpolation_regex( qr#\|(.*?)\|# ); |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$template->set( bird => { pre => '|@name| |@gender| |@type|' } ); |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub interpolation_regex { |
|
165
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
166
|
|
|
|
|
|
|
|
|
167
|
0
|
0
|
|
|
|
0
|
if ( my $regex = shift ) { |
|
168
|
0
|
|
|
|
|
0
|
$self->processor->set_interpolation_regex( |
|
169
|
|
|
|
|
|
|
$self->{interpolation_regex} = $regex |
|
170
|
|
|
|
|
|
|
) |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
0
|
return $self->{interpolation_regex}; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 binmode |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Declares that the stylesheet output is B in UTF-8, but instead in |
|
180
|
|
|
|
|
|
|
an (unspecified) character encoding embedded in the stylesheet source |
|
181
|
|
|
|
|
|
|
that neither Perl nor XPathScript should have any business dealing |
|
182
|
|
|
|
|
|
|
with. Calling C<< XML::XPathScript->current()->binmode() >> is an |
|
183
|
|
|
|
|
|
|
B operation with the consequences outlined in L
|
|
184
|
|
|
|
|
|
|
Unicode mess>. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut " |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub binmode { |
|
189
|
0
|
|
|
0
|
|
0
|
my ($self)=@_; |
|
190
|
0
|
|
|
|
|
0
|
$self->{binmode}=1; |
|
191
|
0
|
|
|
|
|
0
|
$self->{processor}->enable_binmode; |
|
192
|
0
|
0
|
|
|
|
0
|
binmode ORIGINAL_STDOUT if (! defined $self->{printer}); |
|
193
|
0
|
|
|
|
|
0
|
return; |
|
194
|
|
|
|
|
|
|
} |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=pod " |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 TECHNICAL DOCUMENTATION |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
The rest of this POD documentation is B useful to programmers who |
|
201
|
|
|
|
|
|
|
just want to write stylesheets; it is of use only to people wanting to |
|
202
|
|
|
|
|
|
|
call existing stylesheets or more generally embed the XPathScript |
|
203
|
|
|
|
|
|
|
engine into some wider framework. |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
I is an object-oriented class with the following features: |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=over |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item * |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
an I that allows the merging of the stylesheet |
|
212
|
|
|
|
|
|
|
code with snippets of the output document. Don't be afraid, this is |
|
213
|
|
|
|
|
|
|
exactly the same kind of stuff as in I, I |
|
214
|
|
|
|
|
|
|
or other similar packages: instead of having text inside Perl (that |
|
215
|
|
|
|
|
|
|
one Is), we have Perl inside text, with a special escaping |
|
216
|
|
|
|
|
|
|
form that a preprocessor interprets and extracts. For XPathScript, |
|
217
|
|
|
|
|
|
|
this preprocessor is embodied by the I shell tool (see |
|
218
|
|
|
|
|
|
|
L) and also available through this package's |
|
219
|
|
|
|
|
|
|
API; |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item * |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
a I, that does the apply-templates loop, starting |
|
224
|
|
|
|
|
|
|
from the top XML node and applying templates to it and its subnodes as |
|
225
|
|
|
|
|
|
|
directed by the stylesheet. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=back |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
When run, the stylesheet is expected to fill in the I |
|
230
|
|
|
|
|
|
|
$template, which is a lexically-scoped variable made available to it at |
|
231
|
|
|
|
|
|
|
preprocess time. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=cut " |
|
234
|
|
|
|
|
|
|
|
|
235
|
22
|
|
|
22
|
|
139
|
use vars qw( $XML_parser $debug_level ); |
|
|
22
|
|
|
|
|
48
|
|
|
|
22
|
|
|
|
|
1438
|
|
|
236
|
|
|
|
|
|
|
|
|
237
|
22
|
|
|
22
|
|
20812
|
use Symbol; |
|
|
22
|
|
|
|
|
20364
|
|
|
|
22
|
|
|
|
|
1510
|
|
|
238
|
22
|
|
|
22
|
|
139
|
use File::Basename; |
|
|
22
|
|
|
|
|
44
|
|
|
|
22
|
|
|
|
|
2211
|
|
|
239
|
22
|
|
|
22
|
|
17007
|
use XML::XPathScript::Processor; |
|
|
22
|
|
|
|
|
87
|
|
|
|
22
|
|
|
|
|
3874
|
|
|
240
|
22
|
|
|
22
|
|
137
|
use XML::XPathScript::Template; |
|
|
22
|
|
|
|
|
34
|
|
|
|
22
|
|
|
|
|
38774
|
|
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
our $VERSION = '1.54'; |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
$XML_parser = 'XML::LibXML'; |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
my %use_parser = ( |
|
247
|
|
|
|
|
|
|
'XML::LibXML' => 'use XML::LibXML', |
|
248
|
|
|
|
|
|
|
'XML::XPath' => <<'END_USE', |
|
249
|
|
|
|
|
|
|
use XML::XPath 1.0; |
|
250
|
|
|
|
|
|
|
use XML::XPath::XMLParser; |
|
251
|
|
|
|
|
|
|
use XML::XPath::Node; |
|
252
|
|
|
|
|
|
|
use XML::XPath::NodeSet; |
|
253
|
|
|
|
|
|
|
use XML::Parser; |
|
254
|
|
|
|
|
|
|
END_USE |
|
255
|
|
|
|
|
|
|
); |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
die "parser $XML_parser unknown\n" unless $use_parser{$XML_parser}; |
|
258
|
22
|
|
|
22
|
|
41164
|
eval $use_parser{$XML_parser}.";1" |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
259
|
|
|
|
|
|
|
or die "couldn't import $XML_parser"; |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# internal variable for debugging information. |
|
262
|
|
|
|
|
|
|
# 0 is total silence and 10 is complete verbiage |
|
263
|
|
|
|
|
|
|
$debug_level = 0; |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub import |
|
266
|
|
|
|
|
|
|
{ |
|
267
|
0
|
|
|
0
|
|
0
|
my $self = shift @_; |
|
268
|
|
|
|
|
|
|
|
|
269
|
0
|
0
|
|
|
|
0
|
if ( grep { $_ eq 'XML::XPath' } @_ ) { |
|
|
0
|
0
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
270
|
0
|
|
|
|
|
0
|
$XML::XPathScript::XML_parser = 'XML::XPath'; |
|
271
|
|
|
|
|
|
|
} |
|
272
|
|
|
|
|
|
|
elsif ( grep { $_ eq 'XML::LibXML' } @_ ) { |
|
273
|
0
|
|
|
|
|
0
|
$XML::XPathScript::XML_parser = 'XML::LibXML'; |
|
274
|
|
|
|
|
|
|
} |
|
275
|
0
|
|
|
|
|
0
|
return; |
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=pod " |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 METHODS |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 new |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
$xps = XML::XPathScript->new( %arguments ) |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Creates a new XPathScript translator. The recognized named arguments are |
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=over |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=item xml => $xml |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
$xml is a scalar containing XML text, or a reference to a filehandle |
|
293
|
|
|
|
|
|
|
from which XML input is available, or an I or |
|
294
|
|
|
|
|
|
|
I object. |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
An XML::XPathscript object without an I argument |
|
297
|
|
|
|
|
|
|
to the constructor is only able to compile stylesheets (see |
|
298
|
|
|
|
|
|
|
L). |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=item stylesheet => $stylesheet |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
$stylesheet is a scalar containing the stylesheet text, or a reference |
|
303
|
|
|
|
|
|
|
to a filehandle from which the stylesheet text is available. The |
|
304
|
|
|
|
|
|
|
stylesheet text may contain unresolved C<< >> |
|
305
|
|
|
|
|
|
|
constructs, which will be resolved relative to ".". |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item stylesheetfile => $filename |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
Same as I but let I do the loading |
|
310
|
|
|
|
|
|
|
itself. Using this form, relative C<< >>s in the |
|
311
|
|
|
|
|
|
|
stylesheet file will be honored with respect to the dirname of |
|
312
|
|
|
|
|
|
|
$filename instead of "."; this provides SGML-style behaviour for |
|
313
|
|
|
|
|
|
|
inclusion (it does not depend on the current directory), which is |
|
314
|
|
|
|
|
|
|
usually what you want. |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=item compiledstylesheet => $function |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Re-uses a previous return value of I (see L and |
|
319
|
|
|
|
|
|
|
L), typically to apply the same stylesheet to several XML |
|
320
|
|
|
|
|
|
|
documents in a row. |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=item interpolation_regex => $regex |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Sets the interpolation regex. Whatever is |
|
325
|
|
|
|
|
|
|
captured in $1 will be used as the xpath expression. |
|
326
|
|
|
|
|
|
|
Defaults to qr/{(.*?)}/. |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=back |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=cut " |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
sub new { |
|
333
|
2
|
|
|
2
|
|
1244
|
my $class = shift; |
|
334
|
2
|
50
|
|
|
|
19
|
die "Invalid hash call to new" if @_ % 2; |
|
335
|
2
|
|
|
|
|
11
|
my %params = @_; |
|
336
|
2
|
|
|
|
|
7
|
my $self = \%params; |
|
337
|
2
|
|
|
|
|
7
|
bless $self, $class; |
|
338
|
2
|
|
|
|
|
18
|
$self->{processor} = XML::XPathScript::Processor->new; |
|
339
|
2
|
50
|
|
|
|
15
|
$self->set_xml( $params{xml} ) if $params{xml}; |
|
340
|
|
|
|
|
|
|
|
|
341
|
0
|
0
|
|
|
|
0
|
$self->interpolation( exists $params{interpolation} |
|
342
|
|
|
|
|
|
|
? $params{interpolation} : 1 ); |
|
343
|
|
|
|
|
|
|
|
|
344
|
0
|
|
0
|
|
|
0
|
$self->interpolation_regex( $params{interpolation_regex} |
|
345
|
|
|
|
|
|
|
|| qr/{(.*?)}/ ); |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
|
|
349
|
0
|
0
|
|
|
|
0
|
if ( $XML::XPathScript::XML_parser eq 'XML::XPath' ) { |
|
350
|
0
|
|
|
|
|
0
|
require XML::XPath; |
|
351
|
0
|
|
|
|
|
0
|
require XML::XPath::XMLParser; |
|
352
|
0
|
|
|
|
|
0
|
require XML::XPath::Node; |
|
353
|
0
|
|
|
|
|
0
|
require XML::XPath::NodeSet; |
|
354
|
0
|
|
|
|
|
0
|
require XML::Parser; |
|
355
|
|
|
|
|
|
|
} |
|
356
|
|
|
|
|
|
|
else { |
|
357
|
0
|
|
|
|
|
0
|
require XML::LibXML; |
|
358
|
|
|
|
|
|
|
} |
|
359
|
|
|
|
|
|
|
|
|
360
|
0
|
0
|
|
|
|
0
|
croak $@ if $@; |
|
361
|
|
|
|
|
|
|
|
|
362
|
0
|
|
|
|
|
0
|
return $self; |
|
363
|
|
|
|
|
|
|
} |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
=head2 transform |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
$xps->transform( $xml, $stylesheet, \@args ) |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
Transforms the document $xml with the $stylesheet (optionally passing to |
|
370
|
|
|
|
|
|
|
the stylesheet the argument array @args) and returns the result. |
|
371
|
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
If the passed $xml or $stylesheet is undefined, the previously loaded xml |
|
373
|
|
|
|
|
|
|
document or stylesheet is used. |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
E.g., |
|
376
|
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
# vanilla-flavored transformation |
|
378
|
|
|
|
|
|
|
my $xml = '...'; |
|
379
|
|
|
|
|
|
|
my $stylesheet = '<% ... %>'; |
|
380
|
|
|
|
|
|
|
my $transformed = $xps->transform( $xml, $stylesheet ); |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
# transform many documents |
|
383
|
|
|
|
|
|
|
$xps->set_stylesheet( $stylesheet ); |
|
384
|
|
|
|
|
|
|
for my $xml ( @xml_documents ) { |
|
385
|
|
|
|
|
|
|
my $transformed = $xps->transform( $xml ); |
|
386
|
|
|
|
|
|
|
# do stuff with $transformed ... |
|
387
|
|
|
|
|
|
|
} |
|
388
|
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
# do many transformation of a document |
|
390
|
|
|
|
|
|
|
$xps->set_xml( $xml ); |
|
391
|
|
|
|
|
|
|
for my $stylesheet ( @stylesheets ) { |
|
392
|
|
|
|
|
|
|
my $transformed = $xps->transform( undef, $stylesheet ); |
|
393
|
|
|
|
|
|
|
# do stuff with $transformed ... |
|
394
|
|
|
|
|
|
|
} |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=cut |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
sub transform { |
|
399
|
0
|
|
|
0
|
|
0
|
my( $self, $xml, $stylesheet, $args ) = @_; |
|
400
|
0
|
|
|
|
|
0
|
my $output; |
|
401
|
|
|
|
|
|
|
|
|
402
|
0
|
0
|
|
|
|
0
|
$self->set_xml( $xml ) if $xml; |
|
403
|
|
|
|
|
|
|
|
|
404
|
0
|
0
|
|
|
|
0
|
if ( $stylesheet ) { |
|
405
|
0
|
|
|
|
|
0
|
$self->{compiledstylesheet} = undef; |
|
406
|
0
|
|
|
|
|
0
|
$self->{stylesheet} = $stylesheet; |
|
407
|
|
|
|
|
|
|
} |
|
408
|
|
|
|
|
|
|
|
|
409
|
0
|
0
|
|
|
|
0
|
$self->process( \$output, $args ? @$args : () ); |
|
410
|
|
|
|
|
|
|
|
|
411
|
0
|
|
|
|
|
0
|
return $output; |
|
412
|
|
|
|
|
|
|
} |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
=head2 set_dom |
|
415
|
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
$xps->set_dom( $dom ) |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
Set the DOM of the document to process. I<$dom> |
|
419
|
|
|
|
|
|
|
must be a node object of one of the supported |
|
420
|
|
|
|
|
|
|
parsers (XML::LibXML, XML::XPath, B::XPath). |
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
=cut |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
sub set_dom { |
|
425
|
0
|
|
|
0
|
|
0
|
my( $self, $dom ) = @_; |
|
426
|
0
|
|
|
|
|
0
|
$self->{dom} = $dom; |
|
427
|
0
|
|
|
|
|
0
|
$self->{processor}->set_dom( $dom ); |
|
428
|
0
|
|
|
|
|
0
|
return $self; |
|
429
|
|
|
|
|
|
|
} |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
=head2 set_xml |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
$xps->set_xml( $xml ) |
|
434
|
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
Sets the xml document to $xml. $xml can be a file, a file handler |
|
436
|
|
|
|
|
|
|
reference, a string, or a XML::LibXML or XML::XPath node. |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=cut |
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
sub set_xml { |
|
441
|
2
|
|
|
2
|
|
5
|
my( $self, $xml ) = @_; |
|
442
|
|
|
|
|
|
|
|
|
443
|
2
|
|
|
|
|
5
|
$self->{xml} = $xml; |
|
444
|
|
|
|
|
|
|
|
|
445
|
2
|
50
|
|
|
|
15
|
my $retval = ref $xml ? $self->_set_xml_ref() |
|
446
|
|
|
|
|
|
|
: $self->_set_xml_scalar() |
|
447
|
|
|
|
|
|
|
; |
|
448
|
|
|
|
|
|
|
|
|
449
|
0
|
|
|
|
|
0
|
$self->{processor}->set_dom( $self->{dom} ); |
|
450
|
|
|
|
|
|
|
|
|
451
|
0
|
|
|
|
|
0
|
return $retval; |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
# FIXME |
|
454
|
|
|
|
|
|
|
|
|
455
|
0
|
|
|
|
|
0
|
my $xpath; |
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
# a third option should be auto, for which we |
|
459
|
|
|
|
|
|
|
# would use the already-defined object |
|
460
|
0
|
0
|
|
|
|
0
|
if( $XML_parser eq 'auto' ) |
|
461
|
|
|
|
|
|
|
{ |
|
462
|
0
|
0
|
|
|
|
0
|
if (UNIVERSAL::isa($self->{xml},"XML::XPath")) |
|
|
|
0
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
{ |
|
464
|
0
|
|
|
|
|
0
|
$xpath=$self->{xml}; |
|
465
|
0
|
|
|
|
|
0
|
$XML_parser = 'XML::XPath'; |
|
466
|
|
|
|
|
|
|
} |
|
467
|
|
|
|
|
|
|
elsif(UNIVERSAL::isa($self->{xml},"XML::LibXML" )) |
|
468
|
|
|
|
|
|
|
{ |
|
469
|
0
|
|
|
|
|
0
|
$xpath=$self->{xml}; |
|
470
|
0
|
|
|
|
|
0
|
$XML_parser = 'XML::LibXML'; |
|
471
|
|
|
|
|
|
|
} |
|
472
|
|
|
|
|
|
|
} |
|
473
|
|
|
|
|
|
|
|
|
474
|
0
|
0
|
|
|
|
0
|
if (UNIVERSAL::isa($self->{xml},"XML::XPath")) |
|
|
|
0
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
{ |
|
476
|
0
|
0
|
0
|
|
|
0
|
if( $XML_parser eq 'XML::XPath' or $XML_parser eq 'auto' ) |
|
477
|
|
|
|
|
|
|
{ |
|
478
|
0
|
|
|
|
|
0
|
$xpath=$self->{xml}; |
|
479
|
0
|
|
|
|
|
0
|
$XML_parser = 'XML::XPath'; |
|
480
|
|
|
|
|
|
|
} |
|
481
|
|
|
|
|
|
|
else # parser if XML::LibXML |
|
482
|
|
|
|
|
|
|
{ |
|
483
|
0
|
|
|
|
|
0
|
$xpath = XML::LibXML->parse_string( $self->{xml}->toString )->documentElement; |
|
484
|
|
|
|
|
|
|
} |
|
485
|
|
|
|
|
|
|
} |
|
486
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($self->{xml},"XML::libXML")) |
|
487
|
|
|
|
|
|
|
{ |
|
488
|
0
|
0
|
0
|
|
|
0
|
if( $XML_parser eq 'XML::LibXML' or $XML_parser eq 'auto' ) |
|
489
|
|
|
|
|
|
|
{ |
|
490
|
0
|
|
|
|
|
0
|
$xpath=$self->{xml}; |
|
491
|
0
|
|
|
|
|
0
|
$XML_parser = 'XML::LibXML'; |
|
492
|
|
|
|
|
|
|
} |
|
493
|
|
|
|
|
|
|
else # parser if xpath |
|
494
|
|
|
|
|
|
|
{ |
|
495
|
0
|
|
|
|
|
0
|
$xpath = new XML::XPath( xml => $self->{xml}->toString ); |
|
496
|
|
|
|
|
|
|
} |
|
497
|
|
|
|
|
|
|
} |
|
498
|
|
|
|
|
|
|
else |
|
499
|
|
|
|
|
|
|
{ |
|
500
|
0
|
0
|
|
|
|
0
|
$XML_parser = 'XML::LibXML' if $XML_parser eq 'auto'; |
|
501
|
|
|
|
|
|
|
|
|
502
|
0
|
0
|
|
|
|
0
|
if (ref($self->{xml})) |
|
503
|
|
|
|
|
|
|
{ |
|
504
|
0
|
0
|
|
|
|
0
|
$xpath= ( $XML_parser eq 'XML::LibXML' ) ? |
|
505
|
|
|
|
|
|
|
XML::LibXML->new->parse_fh( $self->{xml} )->documentElement : |
|
506
|
|
|
|
|
|
|
XML::XPath->new(ioref => $self->{xml}) |
|
507
|
|
|
|
|
|
|
} |
|
508
|
|
|
|
|
|
|
} |
|
509
|
|
|
|
|
|
|
|
|
510
|
0
|
|
|
|
|
0
|
$self->{dom} = $xpath; |
|
511
|
|
|
|
|
|
|
} |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
sub _set_xml_ref { |
|
514
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
515
|
0
|
|
|
|
|
0
|
my $xml = $self->{xml}; |
|
516
|
|
|
|
|
|
|
|
|
517
|
0
|
0
|
|
|
|
0
|
if ( $XML_parser eq 'XML::LibXML' ) { |
|
518
|
0
|
0
|
|
|
|
0
|
if ( $xml->isa( 'XML::LibXML::Document' ) ) { |
|
519
|
0
|
|
|
|
|
0
|
$self->{dom} = $xml; |
|
520
|
0
|
|
|
|
|
0
|
return; |
|
521
|
|
|
|
|
|
|
} |
|
522
|
|
|
|
|
|
|
|
|
523
|
0
|
0
|
|
|
|
0
|
if ( $xml->isa( 'XML::LibXML::Node' ) ) { |
|
524
|
0
|
|
|
|
|
0
|
my $dom = XML::LibXML::Document->new; |
|
525
|
0
|
|
|
|
|
0
|
$dom->setDocumentElement( $xml ); |
|
526
|
0
|
|
|
|
|
0
|
$self->{dom} = $dom; |
|
527
|
0
|
|
|
|
|
0
|
return; |
|
528
|
|
|
|
|
|
|
} |
|
529
|
|
|
|
|
|
|
} |
|
530
|
|
|
|
|
|
|
else { # XML::XPath |
|
531
|
0
|
0
|
|
|
|
0
|
if ( $xml->isa( 'XML::XPath' ) ) { |
|
532
|
0
|
|
|
|
|
0
|
$self->{dom} = $xml; |
|
533
|
0
|
|
|
|
|
0
|
return; |
|
534
|
|
|
|
|
|
|
} |
|
535
|
|
|
|
|
|
|
|
|
536
|
0
|
0
|
|
|
|
0
|
if( $xml->isa( 'XML::XPath::Node' ) ) { |
|
537
|
|
|
|
|
|
|
# evil hack |
|
538
|
0
|
|
|
|
|
0
|
my $dom = XML::XPath->new( xml => $xml->toString ); |
|
539
|
0
|
|
|
|
|
0
|
$self->{dom} = $dom; |
|
540
|
0
|
|
|
|
|
0
|
return; |
|
541
|
|
|
|
|
|
|
} |
|
542
|
|
|
|
|
|
|
} |
|
543
|
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
# try to read it as an io |
|
545
|
0
|
0
|
|
|
|
0
|
$self->{dom} = $XML_parser eq 'XML::LibXML' |
|
546
|
|
|
|
|
|
|
? XML::LibXML->new->parse_fh( $xml )->documentElement |
|
547
|
|
|
|
|
|
|
: XML::XPath->new(ioref => $xml) |
|
548
|
|
|
|
|
|
|
; |
|
549
|
|
|
|
|
|
|
|
|
550
|
0
|
|
|
|
|
0
|
return; |
|
551
|
|
|
|
|
|
|
} |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
sub _set_xml_scalar { |
|
554
|
2
|
|
|
2
|
|
5
|
my $self = shift; |
|
555
|
2
|
|
|
|
|
4
|
my $xml = $self->{xml}; |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# is it a file? |
|
558
|
2
|
0
|
66
|
|
|
100
|
if( index( $xml, "\n" ) == -1 and # quick'n'dirty checks |
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
559
|
|
|
|
|
|
|
index( $xml, '<' ) == -1 and # for non-filename characters |
|
560
|
|
|
|
|
|
|
index( $xml, '>' ) == -1 and -f $xml ) { |
|
561
|
0
|
0
|
|
|
|
0
|
open my $fh, '<', $xml or croak "couldn't open xml file $xml: $!"; |
|
562
|
|
|
|
|
|
|
|
|
563
|
0
|
0
|
|
|
|
0
|
$self->{dom} = $XML_parser eq 'XML::LibXML' |
|
564
|
|
|
|
|
|
|
? XML::LibXML->new->parse_file( $xml )->documentElement |
|
565
|
|
|
|
|
|
|
: XML::XPath->new( filename => $xml ) |
|
566
|
|
|
|
|
|
|
; |
|
567
|
|
|
|
|
|
|
|
|
568
|
0
|
|
|
|
|
0
|
return; |
|
569
|
|
|
|
|
|
|
} |
|
570
|
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
# then it must be a string |
|
572
|
|
|
|
|
|
|
|
|
573
|
2
|
50
|
|
|
|
423
|
$self->{dom} = $XML_parser eq 'XML::LibXML' |
|
574
|
|
|
|
|
|
|
? XML::LibXML->new->parse_string( $xml )->documentElement |
|
575
|
|
|
|
|
|
|
: XML::XPath->new( xml => $xml ); |
|
576
|
|
|
|
|
|
|
|
|
577
|
0
|
|
|
|
|
|
return; |
|
578
|
|
|
|
|
|
|
} |
|
579
|
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
=head2 set_stylesheet |
|
581
|
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
$xps->set_stylesheet( $stylesheet ) |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
Sets the processor's stylesheet to $stylesheet. |
|
585
|
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
=cut |
|
587
|
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
sub set_stylesheet { |
|
589
|
0
|
|
|
0
|
|
|
my ( $self, $stylesheet ) = @_; |
|
590
|
|
|
|
|
|
|
|
|
591
|
0
|
|
|
|
|
|
$self->{compiledstylesheet} = undef; |
|
592
|
0
|
|
|
|
|
|
$self->{stylesheet} = $stylesheet; |
|
593
|
|
|
|
|
|
|
|
|
594
|
0
|
0
|
|
|
|
|
$self->compile if $self->{stylesheet}; |
|
595
|
|
|
|
|
|
|
} |
|
596
|
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
=pod " |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=head2 process |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
$xps->process |
|
602
|
|
|
|
|
|
|
$xps->process( $printer ) |
|
603
|
|
|
|
|
|
|
$xps->process( $printer, @varvalues ) |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
Processes the document and stylesheet set at construction time, and |
|
606
|
|
|
|
|
|
|
prints the result to STDOUT by default. If $printer is set, it must be |
|
607
|
|
|
|
|
|
|
either a reference to a filehandle open for output, or a reference to |
|
608
|
|
|
|
|
|
|
a string, or a reference to a subroutine which does the output, as in |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
open my $fh, '>', 'transformed.txt' |
|
611
|
|
|
|
|
|
|
or die "can't open file transformed.txt: $!"; |
|
612
|
|
|
|
|
|
|
$xps->process( $fh ); |
|
613
|
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
my $transformed; |
|
615
|
|
|
|
|
|
|
$xps->process( \$transformed ); |
|
616
|
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
$xps->process( sub { |
|
618
|
|
|
|
|
|
|
my $output = shift; |
|
619
|
|
|
|
|
|
|
$output =~ y/<>/%%/; |
|
620
|
|
|
|
|
|
|
print $output; |
|
621
|
|
|
|
|
|
|
} ); |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
If the stylesheet was Id with extra Is, then the |
|
624
|
|
|
|
|
|
|
calling code should call I with a corresponding number of |
|
625
|
|
|
|
|
|
|
@varvalues. The corresponding lexical variables will be set |
|
626
|
|
|
|
|
|
|
accordingly, so that the stylesheet code can get at them (looking at |
|
627
|
|
|
|
|
|
|
L) is the easiest way of getting the meaning of this |
|
628
|
|
|
|
|
|
|
sentence). |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
=cut " |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
sub process { |
|
633
|
0
|
|
|
0
|
|
|
my ($self, $printer, @extravars) = @_; |
|
634
|
|
|
|
|
|
|
|
|
635
|
0
|
0
|
|
|
|
|
do { $$printer="" } if (UNIVERSAL::isa($printer, "SCALAR")); |
|
|
0
|
|
|
|
|
|
|
|
636
|
0
|
0
|
|
|
|
|
$self->{printer}=$printer if $printer; |
|
637
|
|
|
|
|
|
|
|
|
638
|
0
|
0
|
|
|
|
|
croak "xml document not defined" unless $self->{dom}; |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
# FIXME |
|
641
|
0
|
0
|
|
|
|
|
eval { $self->{dom}->ownerDocument->setEncoding( "UTF-8" ) } |
|
|
0
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
if $XML_parser eq 'XML::LibXML'; |
|
643
|
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
{ |
|
645
|
0
|
|
|
|
|
|
local *ORIGINAL_STDOUT; |
|
|
0
|
|
|
|
|
|
|
|
646
|
0
|
|
|
|
|
|
*ORIGINAL_STDOUT = *STDOUT; |
|
647
|
0
|
|
|
|
|
|
local *STDOUT; |
|
648
|
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
# Perl 5.6.1 dislikes closed but tied descriptors (causes SEGVage) |
|
650
|
0
|
0
|
|
|
|
|
*STDOUT = *ORIGINAL_STDOUT if $^V lt v5.7.0; |
|
651
|
|
|
|
|
|
|
|
|
652
|
0
|
|
|
|
|
|
tie *STDOUT, __PACKAGE__; |
|
653
|
0
|
0
|
|
|
|
|
$self->compile unless $self->{compiledstylesheet}; |
|
654
|
0
|
|
|
|
|
|
my $retval = $self->{compiledstylesheet}->( $self, @extravars ); |
|
655
|
0
|
|
|
|
|
|
untie *STDOUT; |
|
656
|
0
|
|
|
|
|
|
return $retval; |
|
657
|
|
|
|
|
|
|
} |
|
658
|
|
|
|
|
|
|
} |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
=head2 extract |
|
661
|
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
$xps->extract( $stylesheet ) |
|
663
|
|
|
|
|
|
|
$xps->extract( $stylesheet, $filename ) |
|
664
|
|
|
|
|
|
|
$xps->extract( $stylesheet, @includestack ) # from include_file() only |
|
665
|
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
The embedded dialect parser. Given $stylesheet, which is either a |
|
667
|
|
|
|
|
|
|
filehandle reference or a string, returns a string that holds all the |
|
668
|
|
|
|
|
|
|
code in real Perl. Unquoted text and C<< <%= stuff %> >> constructs in |
|
669
|
|
|
|
|
|
|
the stylesheet dialect are converted into invocations of I<< |
|
670
|
|
|
|
|
|
|
XML::XPathScript->current()->print() >>, while C<< <% stuff %> >> |
|
671
|
|
|
|
|
|
|
constructs are transcripted verbatim. |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
C<< >> constructs are expanded by passing their |
|
674
|
|
|
|
|
|
|
filename argument to L along with @includestack (if any) |
|
675
|
|
|
|
|
|
|
like this: |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
$self->include_file($includefilename,@includestack); |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
@includestack is not interpreted by I (except for the first |
|
680
|
|
|
|
|
|
|
entry, to create line tags for the debugger). It is only a bandaid for |
|
681
|
|
|
|
|
|
|
I to pass the inclusion stack to itself across the |
|
682
|
|
|
|
|
|
|
mutual recursion existing between the two methods (see |
|
683
|
|
|
|
|
|
|
L). If I is invoked from outside |
|
684
|
|
|
|
|
|
|
I, the last invocation form should not be used. |
|
685
|
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
This method does a purely syntactic job. No special framework |
|
687
|
|
|
|
|
|
|
declaration is prepended for isolating the code in its own package, |
|
688
|
|
|
|
|
|
|
defining $t or the like (L does that). It may be overriden |
|
689
|
|
|
|
|
|
|
in subclasses to provide different escape forms in the stylesheet |
|
690
|
|
|
|
|
|
|
dialect. |
|
691
|
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
=cut " |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
sub extract { |
|
695
|
0
|
|
|
0
|
|
|
my ($self,$stylesheet,@includestack) = @_; |
|
696
|
|
|
|
|
|
|
|
|
697
|
0
|
|
0
|
|
|
|
my $filename = $self->{stylesheet_dependencies}[0] || "stylesheet"; |
|
698
|
|
|
|
|
|
|
|
|
699
|
0
|
|
|
|
|
|
my $contents = $self->read_stylesheet( $stylesheet ); |
|
700
|
|
|
|
|
|
|
|
|
701
|
0
|
|
|
|
|
|
my @tokens = split /(<%[-=~#@]*|-?%>)/, $contents; |
|
702
|
|
|
|
|
|
|
|
|
703
|
22
|
|
|
22
|
|
179
|
no warnings qw/ uninitialized /; |
|
|
22
|
|
|
|
|
48
|
|
|
|
22
|
|
|
|
|
23171
|
|
|
704
|
|
|
|
|
|
|
|
|
705
|
0
|
|
|
|
|
|
my $script; |
|
706
|
0
|
|
|
|
|
|
my $line = 1; |
|
707
|
|
|
|
|
|
|
TOKEN: |
|
708
|
0
|
|
|
|
|
|
while ( @tokens ) { |
|
709
|
0
|
|
|
|
|
|
my $token = shift @tokens; |
|
710
|
|
|
|
|
|
|
|
|
711
|
0
|
0
|
|
|
|
|
if ( -1 == index $token, '<%' ) { |
|
712
|
0
|
|
|
|
|
|
$line += $token =~ tr/\n//; |
|
713
|
0
|
0
|
0
|
|
|
|
$token =~ s/\s+$// if -1 < index $tokens[0], '<%' |
|
714
|
|
|
|
|
|
|
and -1 < index $tokens[0], '-'; |
|
715
|
0
|
|
|
|
|
|
$token =~ s/\|/\\\|/g; |
|
716
|
|
|
|
|
|
|
# check for include |
|
717
|
0
|
|
|
|
|
|
$token =~ s{} |
|
718
|
0
|
|
|
|
|
|
{ '|);' |
|
719
|
|
|
|
|
|
|
. $self->include_file( $2, @includestack) |
|
720
|
|
|
|
|
|
|
. 'print(q|'}seg; |
|
721
|
0
|
0
|
|
|
|
|
$script .= 'print(q|'.$token.'|);' if length $token; |
|
722
|
|
|
|
|
|
|
|
|
723
|
0
|
|
|
|
|
|
next TOKEN; |
|
724
|
|
|
|
|
|
|
} |
|
725
|
|
|
|
|
|
|
|
|
726
|
0
|
|
|
|
|
|
$script .= "\n#line $line $filename\n"; |
|
727
|
|
|
|
|
|
|
|
|
728
|
0
|
|
|
|
|
|
my $opening_tag = $token; |
|
729
|
0
|
|
|
|
|
|
my $code; |
|
730
|
|
|
|
|
|
|
my $closing_tag; |
|
731
|
0
|
|
|
|
|
|
my $level = 1; |
|
732
|
0
|
|
|
|
|
|
while( @tokens ) { |
|
733
|
0
|
|
|
|
|
|
my $t = shift @tokens; |
|
734
|
0
|
0
|
|
|
|
|
$level++ if -1 < index $t, '<%'; |
|
735
|
0
|
0
|
|
|
|
|
$level-- if -1 < index $t, '%>'; |
|
736
|
0
|
0
|
|
|
|
|
if ( $level == 0 ) { |
|
737
|
0
|
|
|
|
|
|
$closing_tag = $t; |
|
738
|
0
|
|
|
|
|
|
last; |
|
739
|
|
|
|
|
|
|
} |
|
740
|
0
|
|
|
|
|
|
$code .= $t; |
|
741
|
|
|
|
|
|
|
} |
|
742
|
|
|
|
|
|
|
|
|
743
|
0
|
0
|
|
|
|
|
die "stylesheet <% %>s are unbalanced: $opening_tag$code\n" |
|
744
|
|
|
|
|
|
|
unless $closing_tag; |
|
745
|
|
|
|
|
|
|
|
|
746
|
0
|
|
|
|
|
|
$line += $code =~ tr/\n//; |
|
747
|
|
|
|
|
|
|
|
|
748
|
0
|
0
|
|
|
|
|
if ( -1 < index $opening_tag, '=' ) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
749
|
0
|
|
|
|
|
|
$script .= 'print( '.$code.' );'; |
|
750
|
|
|
|
|
|
|
} |
|
751
|
|
|
|
|
|
|
elsif ( -1 < index $opening_tag, '~' ) { |
|
752
|
0
|
|
|
|
|
|
$code =~ s/^\s+//; |
|
753
|
0
|
|
|
|
|
|
$code =~ s/\s+$//; |
|
754
|
0
|
|
|
|
|
|
$script .= 'print $processor->apply_templates( qq<'. $code .'> );'; |
|
755
|
|
|
|
|
|
|
} |
|
756
|
|
|
|
|
|
|
elsif( -1 < index $opening_tag, '#' ) { |
|
757
|
|
|
|
|
|
|
# do nothing |
|
758
|
|
|
|
|
|
|
} |
|
759
|
|
|
|
|
|
|
elsif( -1 < index $opening_tag, '@' ) { |
|
760
|
0
|
|
|
|
|
|
$code =~ s/^\s+(\S+).*?\n//; # strip first line |
|
761
|
0
|
0
|
|
|
|
|
my $tag = $1 |
|
762
|
|
|
|
|
|
|
or die "tag name missing in <%\@ %> at line $line\n"; |
|
763
|
|
|
|
|
|
|
|
|
764
|
0
|
|
|
|
|
|
my $here_delimiter = 'END_TAG'; |
|
765
|
0
|
|
|
|
|
|
while ( $code =~ /$here_delimiter/ ) { |
|
766
|
0
|
|
|
|
|
|
$here_delimiter .= 'x'; |
|
767
|
|
|
|
|
|
|
} |
|
768
|
0
|
|
|
|
|
|
$script .= <
|
|
769
|
|
|
|
|
|
|
\$template->set( $tag => { content => <<'$here_delimiter' } ); |
|
770
|
|
|
|
|
|
|
$code |
|
771
|
|
|
|
|
|
|
$here_delimiter |
|
772
|
|
|
|
|
|
|
END_SNIPPET |
|
773
|
|
|
|
|
|
|
} |
|
774
|
|
|
|
|
|
|
else { |
|
775
|
|
|
|
|
|
|
# always add a ';', just in case |
|
776
|
0
|
|
|
|
|
|
$script .= $code . ';'; |
|
777
|
|
|
|
|
|
|
} |
|
778
|
|
|
|
|
|
|
|
|
779
|
0
|
0
|
|
|
|
|
if ( -1 < index $closing_tag, '-' ) { |
|
780
|
0
|
|
|
|
|
|
$tokens[0] =~ s/^\s*//; |
|
781
|
0
|
|
|
|
|
|
my $temp = $&; |
|
782
|
0
|
|
|
|
|
|
$line += $temp =~ tr/\n//; |
|
783
|
|
|
|
|
|
|
} |
|
784
|
|
|
|
|
|
|
} |
|
785
|
|
|
|
|
|
|
|
|
786
|
0
|
|
|
|
|
|
return $script; |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
# FIXME not needed anymore |
|
789
|
|
|
|
|
|
|
# <%- -%> magic |
|
790
|
0
|
|
|
|
|
|
$contents =~ s#(\s+)<%-([=~]?)#<%$2$1#gs; |
|
791
|
0
|
|
|
|
|
|
$contents =~ s#-%>(\s+)#$1%>#gs; |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
# <%~ %> magic |
|
794
|
0
|
|
|
|
|
|
$contents =~ s#<%~\s+(\S+)\s+%>#<%= apply_templates( qq<$1> ) %>#gs; |
|
795
|
|
|
|
|
|
|
|
|
796
|
0
|
|
|
|
|
|
$script="#line 1 $filename\n", |
|
797
|
|
|
|
|
|
|
$line = 1; |
|
798
|
|
|
|
|
|
|
|
|
799
|
0
|
|
|
|
|
|
while ($contents =~ /\G(.*?)()/gcs) { |
|
814
|
0
|
0
|
|
|
|
|
last if $1 eq '-->'; |
|
815
|
0
|
0
|
|
|
|
|
$params{$2} = $4 if (defined $2); |
|
816
|
|
|
|
|
|
|
} |
|
817
|
|
|
|
|
|
|
|
|
818
|
0
|
0
|
|
|
|
|
die "No matching file attribute in #include at line $line" |
|
819
|
|
|
|
|
|
|
unless $params{file}; |
|
820
|
|
|
|
|
|
|
|
|
821
|
22
|
|
|
22
|
|
146
|
no warnings qw/ uninitialized /; |
|
|
22
|
|
|
|
|
52
|
|
|
|
22
|
|
|
|
|
20582
|
|
|
822
|
0
|
|
|
|
|
|
$script .= $self->include_file($params{file},@includestack); |
|
823
|
|
|
|
|
|
|
} |
|
824
|
|
|
|
|
|
|
else { |
|
825
|
0
|
0
|
|
|
|
|
$contents =~ /\G(.*?)%>/gcs || die "No terminating '%>' after line $line"; |
|
826
|
0
|
|
|
|
|
|
my $perl = $1; |
|
827
|
0
|
0
|
|
|
|
|
if( $type ne '<%#' ) { |
|
828
|
0
|
|
|
|
|
|
$perl =~ s/;?$/;/s; # add on ; if its missing. As in <% $foo = 'Hello' %> |
|
829
|
0
|
|
|
|
|
|
$script .= $perl; |
|
830
|
|
|
|
|
|
|
} |
|
831
|
0
|
|
|
|
|
|
$line += $perl =~ tr/\n//; |
|
832
|
|
|
|
|
|
|
} |
|
833
|
|
|
|
|
|
|
} |
|
834
|
|
|
|
|
|
|
|
|
835
|
0
|
0
|
|
|
|
|
if ($contents =~ /\G(.+)/gcs) { |
|
836
|
0
|
|
|
|
|
|
my $text = $1; |
|
837
|
0
|
|
|
|
|
|
$text =~ s/\|/\\\|/g; |
|
838
|
0
|
|
|
|
|
|
$script .= "print(q|$text|);"; |
|
839
|
|
|
|
|
|
|
} |
|
840
|
|
|
|
|
|
|
|
|
841
|
0
|
|
|
|
|
|
return $script; |
|
842
|
|
|
|
|
|
|
} |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=head2 read_stylesheet |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
$string = $xps->read_stylesheet( $stylesheet ) |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
Read the $stylesheet (which can be a filehandler or a string). |
|
849
|
|
|
|
|
|
|
Used by I and exists such that it can be overloaded in |
|
850
|
|
|
|
|
|
|
I. |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=cut |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
sub read_stylesheet |
|
855
|
|
|
|
|
|
|
{ |
|
856
|
0
|
|
|
0
|
|
|
my( $self, $stylesheet ) = @_; |
|
857
|
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
# $stylesheet can be a filehandler |
|
859
|
|
|
|
|
|
|
# or a string |
|
860
|
0
|
0
|
|
|
|
|
if( ref($stylesheet) ) { |
|
861
|
0
|
|
|
|
|
|
local $/; |
|
862
|
0
|
|
|
|
|
|
return <$stylesheet>; |
|
863
|
|
|
|
|
|
|
} |
|
864
|
|
|
|
|
|
|
else { |
|
865
|
0
|
|
|
|
|
|
return $stylesheet; |
|
866
|
|
|
|
|
|
|
} |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
} |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
=head2 include_file |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
$xps->include_file( $filename ) |
|
873
|
|
|
|
|
|
|
$xps->include_file( $filename, @includestack ) |
|
874
|
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
Resolves a C<< >> directive on behalf of |
|
876
|
|
|
|
|
|
|
I, that is, returns the script contents of |
|
877
|
|
|
|
|
|
|
I<$filename>. The return value must be de-embedded too, which means |
|
878
|
|
|
|
|
|
|
that I has to be called recursively to expand the contents |
|
879
|
|
|
|
|
|
|
of $filename (which may contain more C<< >>s etc.) |
|
880
|
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
$filename has to be slash-separated, whatever OS it is you are using |
|
882
|
|
|
|
|
|
|
(this is the XML way of things). If $filename is relative (i.e. does |
|
883
|
|
|
|
|
|
|
not begin with "/" or "./"), it is resolved according to the basename |
|
884
|
|
|
|
|
|
|
of the stylesheet that includes it (that is, $includestack[0], see |
|
885
|
|
|
|
|
|
|
below) or "." if we are in the topmost stylesheet. Filenames beginning |
|
886
|
|
|
|
|
|
|
with "./" are considered absolute; this gives stylesheet writers a way |
|
887
|
|
|
|
|
|
|
to specify that they really really want a stylesheet that lies in the |
|
888
|
|
|
|
|
|
|
system's current working directory. |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
@includestack is the include stack currently in use, made up of all |
|
891
|
|
|
|
|
|
|
values of $filename through the stack, lastly added (innermost) |
|
892
|
|
|
|
|
|
|
entries first. The toplevel stylesheet is not in @includestack |
|
893
|
|
|
|
|
|
|
(that is, the outermost call does not specify an @includestack). |
|
894
|
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
This method may be overridden in subclasses to provide support for |
|
896
|
|
|
|
|
|
|
alternate namespaces (e.g. ``axkit://'' URIs). |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
=cut " |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
sub include_file { |
|
901
|
0
|
|
|
0
|
|
|
my ($self, $filename, @includestack) = @_; |
|
902
|
|
|
|
|
|
|
|
|
903
|
0
|
0
|
|
|
|
|
if ( $filename !~ m#^\.?/# ) { |
|
904
|
|
|
|
|
|
|
# We guarantee that all values we insert into @includestack begin |
|
905
|
|
|
|
|
|
|
# either with "/" or "./". This allows us to do the relative |
|
906
|
|
|
|
|
|
|
# directory thing, and at the same time we get to safely ignore |
|
907
|
|
|
|
|
|
|
# bizarre URIs inserted by inheriting classes. |
|
908
|
|
|
|
|
|
|
|
|
909
|
0
|
0
|
0
|
|
|
|
my $reldir = $includestack[0] && $includestack[0] =~ m#^\.?/# |
|
910
|
|
|
|
|
|
|
? dirname($includestack[0]) |
|
911
|
|
|
|
|
|
|
: '.' |
|
912
|
|
|
|
|
|
|
; |
|
913
|
|
|
|
|
|
|
|
|
914
|
0
|
|
|
|
|
|
$filename = "$reldir/$filename"; |
|
915
|
|
|
|
|
|
|
} |
|
916
|
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
# are we going recursive? |
|
918
|
0
|
0
|
|
|
|
|
if ( grep { $_ eq $filename } @includestack ) { |
|
|
0
|
|
|
|
|
|
|
|
919
|
0
|
|
|
|
|
|
warn 'loop detected in stylesheet include chain: ', |
|
920
|
|
|
|
|
|
|
join( ' => ', reverse(@includestack), $filename ), "\n"; |
|
921
|
0
|
|
|
|
|
|
return undef; |
|
922
|
|
|
|
|
|
|
} |
|
923
|
|
|
|
|
|
|
|
|
924
|
0
|
|
|
|
|
|
my $stylesheet; |
|
925
|
0
|
0
|
|
|
|
|
unless ( $stylesheet = $self->{stylesheet_cache}{$filename} ) { |
|
926
|
0
|
0
|
|
|
|
|
open my $fh, '<', $filename |
|
927
|
|
|
|
|
|
|
or Carp::croak "Can't read include file '$filename': $!"; |
|
928
|
0
|
|
|
|
|
|
$stylesheet = $self->{stylesheet_cache}{$filename} |
|
929
|
|
|
|
|
|
|
= $self->read_stylesheet( $fh ); |
|
930
|
|
|
|
|
|
|
} |
|
931
|
|
|
|
|
|
|
|
|
932
|
0
|
|
|
|
|
|
return $self->extract($stylesheet, $filename, @includestack); |
|
933
|
|
|
|
|
|
|
} |
|
934
|
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
=pod " |
|
937
|
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
=head2 I |
|
939
|
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
=head2 I |
|
941
|
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
Compiles the stylesheet set at I time and returns an anonymous |
|
943
|
|
|
|
|
|
|
CODE reference. |
|
944
|
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
I, I, etc. are extraneous arguments that will be |
|
946
|
|
|
|
|
|
|
made available to the stylesheet dialect as lexically scoped |
|
947
|
|
|
|
|
|
|
variables. L shows how to use this feature to pass variables |
|
948
|
|
|
|
|
|
|
to AxKit XPathScript stylesheets, which explains this |
|
949
|
|
|
|
|
|
|
feature better than a lengthy paragraph would do. |
|
950
|
|
|
|
|
|
|
|
|
951
|
|
|
|
|
|
|
The return value is an opaque token that encapsulates a compiled |
|
952
|
|
|
|
|
|
|
stylesheet. It should not be used, except as the |
|
953
|
|
|
|
|
|
|
I argument to I to initiate new objects and |
|
954
|
|
|
|
|
|
|
amortize the compilation time. Subclasses may alter the type of the |
|
955
|
|
|
|
|
|
|
return value, but will need to overload I accordingly of |
|
956
|
|
|
|
|
|
|
course. |
|
957
|
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
The I method is idempotent. Subsequent calls to it will |
|
959
|
|
|
|
|
|
|
return the very same token, and calls to it when a |
|
960
|
|
|
|
|
|
|
I argument was set at I time will return |
|
961
|
|
|
|
|
|
|
said argument. |
|
962
|
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
=cut " |
|
964
|
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
# Internal documentation: the return value is an anonymous sub whose |
|
966
|
|
|
|
|
|
|
# prototype is |
|
967
|
|
|
|
|
|
|
# &$compiledfunc($xpathscriptobj, $val1, $val2,...); |
|
968
|
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
sub compile { |
|
970
|
0
|
|
|
0
|
|
|
my ($self,@extravars) = @_; |
|
971
|
|
|
|
|
|
|
|
|
972
|
0
|
|
|
|
|
|
$self->{compiledstylesheet} = undef; |
|
973
|
|
|
|
|
|
|
|
|
974
|
0
|
|
|
|
|
|
my $stylesheet; |
|
975
|
0
|
|
|
|
|
|
$self->{stylesheet_cache} = {}; |
|
976
|
|
|
|
|
|
|
|
|
977
|
0
|
0
|
|
|
|
|
if (exists $self->{stylesheet}) { |
|
|
|
0
|
|
|
|
|
|
|
978
|
0
|
|
|
|
|
|
$stylesheet=$self->{stylesheet}; |
|
979
|
|
|
|
|
|
|
} |
|
980
|
|
|
|
|
|
|
elsif (exists $self->{stylesheetfile}) { |
|
981
|
|
|
|
|
|
|
# This hack fails if $self->{stylesheetfile} contains |
|
982
|
|
|
|
|
|
|
# double quotes. I think we can ignore this and get |
|
983
|
|
|
|
|
|
|
# away. |
|
984
|
0
|
|
|
|
|
|
$stylesheet=qq::; |
|
985
|
|
|
|
|
|
|
} |
|
986
|
|
|
|
|
|
|
else { |
|
987
|
0
|
|
|
|
|
|
die "Cannot compile without a stylesheet\n"; |
|
988
|
|
|
|
|
|
|
}; |
|
989
|
|
|
|
|
|
|
|
|
990
|
0
|
|
|
|
|
|
my $script = $self->extract($stylesheet); |
|
991
|
|
|
|
|
|
|
|
|
992
|
0
|
|
|
|
|
|
my $package=gen_package_name(); |
|
993
|
|
|
|
|
|
|
|
|
994
|
0
|
|
|
|
|
|
my $extravars = join ',', @extravars; |
|
995
|
|
|
|
|
|
|
|
|
996
|
0
|
|
|
|
|
|
my $processor = $self->{processor}; |
|
997
|
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
# needs to be eval'ed first for the constants |
|
999
|
|
|
|
|
|
|
# to be seen |
|
1000
|
0
|
|
|
|
|
|
eval "package $package;" |
|
1001
|
|
|
|
|
|
|
."\$processor->import_functional();"; |
|
1002
|
|
|
|
|
|
|
|
|
1003
|
0
|
|
|
|
|
|
my $eval = <
|
|
1004
|
|
|
|
|
|
|
package $package; |
|
1005
|
|
|
|
|
|
|
no strict; # Don't moan on sloppyly |
|
1006
|
|
|
|
|
|
|
no warnings; # written stylesheets |
|
1007
|
|
|
|
|
|
|
|
|
1008
|
|
|
|
|
|
|
use $XML_parser; |
|
1009
|
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
sub { |
|
1011
|
|
|
|
|
|
|
my (\$self, $extravars ) = \@_; |
|
1012
|
|
|
|
|
|
|
my \$processor = processor(); |
|
1013
|
|
|
|
|
|
|
local \$XML::XPathScript::current=\$self; |
|
1014
|
|
|
|
|
|
|
my \$t = \$processor->{template} |
|
1015
|
|
|
|
|
|
|
= XML::XPathScript::Template->new(); |
|
1016
|
|
|
|
|
|
|
my \$template = \$t; |
|
1017
|
|
|
|
|
|
|
local \$XML::XPathScript::trans = \$t; |
|
1018
|
|
|
|
|
|
|
#\$processor->{doc} = \$self->{dom}; |
|
1019
|
|
|
|
|
|
|
#\$processor->{parser} = '$XML_parser'; |
|
1020
|
|
|
|
|
|
|
#\$processor->{binmode} = \$self->{binmode}; |
|
1021
|
|
|
|
|
|
|
#\$processor->{is_interpolating} = \$self->interpolation; |
|
1022
|
|
|
|
|
|
|
#\$processor->{interpolation_regex} = \$self->interpolation_regex; |
|
1023
|
|
|
|
|
|
|
|
|
1024
|
|
|
|
|
|
|
$script |
|
1025
|
|
|
|
|
|
|
} |
|
1026
|
|
|
|
|
|
|
EOT |
|
1027
|
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
#warn "script ready for compil: $eval"; |
|
1029
|
0
|
|
|
|
|
|
local $^W; |
|
1030
|
0
|
|
|
|
|
|
$self->debug( 10, "Compiling code:\n $eval" ); |
|
1031
|
0
|
|
|
|
|
|
my $retval = eval $eval; |
|
1032
|
0
|
0
|
|
|
|
|
die $@ unless defined $retval; |
|
1033
|
|
|
|
|
|
|
|
|
1034
|
0
|
|
|
|
|
|
return $self->{compiledstylesheet} = $retval; |
|
1035
|
|
|
|
|
|
|
} |
|
1036
|
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
=head2 print |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
$xps->print($text) |
|
1041
|
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
Outputs a chunk of text on behalf of the stylesheet. The default |
|
1043
|
|
|
|
|
|
|
implementation is to use the second argument to L. |
|
1044
|
|
|
|
|
|
|
Overloading this |
|
1045
|
|
|
|
|
|
|
method in a subclass provides yet another method to redirect output. |
|
1046
|
|
|
|
|
|
|
|
|
1047
|
|
|
|
|
|
|
=cut " |
|
1048
|
|
|
|
|
|
|
|
|
1049
|
|
|
|
|
|
|
sub print { |
|
1050
|
22
|
|
|
22
|
|
139
|
no warnings qw/ uninitialized /; |
|
|
22
|
|
|
|
|
45
|
|
|
|
22
|
|
|
|
|
18955
|
|
|
1051
|
0
|
|
|
0
|
|
|
my ($self, @text)=@_; |
|
1052
|
0
|
|
|
|
|
|
my $printer=$self->{printer}; |
|
1053
|
|
|
|
|
|
|
|
|
1054
|
0
|
0
|
|
|
|
|
if (!defined $printer) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1055
|
0
|
|
|
|
|
|
print ORIGINAL_STDOUT @text; |
|
1056
|
|
|
|
|
|
|
} elsif (ref($printer) eq 'CODE') { |
|
1057
|
0
|
|
|
|
|
|
$printer->(@text); |
|
1058
|
|
|
|
|
|
|
} elsif (UNIVERSAL::isa($printer, 'SCALAR')) { |
|
1059
|
0
|
|
|
|
|
|
$$printer.= join '', @text; |
|
1060
|
|
|
|
|
|
|
} else { |
|
1061
|
0
|
|
|
|
|
|
local $\=undef; |
|
1062
|
0
|
|
|
|
|
|
print $printer @text; |
|
1063
|
|
|
|
|
|
|
}; |
|
1064
|
|
|
|
|
|
|
|
|
1065
|
0
|
|
|
|
|
|
return; |
|
1066
|
|
|
|
|
|
|
} |
|
1067
|
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
|
|
1069
|
|
|
|
|
|
|
# $self->debug( $level, $message ) |
|
1070
|
|
|
|
|
|
|
# Display debugging information |
|
1071
|
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
sub debug { |
|
1073
|
0
|
0
|
|
0
|
|
|
warn $_[2] if $_[1] <= $debug_level; |
|
1074
|
|
|
|
|
|
|
} |
|
1075
|
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
=head2 get_stylesheet_dependencies |
|
1077
|
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
@files = $xps->get_stylesheet_dependencies |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
Returns the files the loaded stylesheet depends on (i.e., has been |
|
1081
|
|
|
|
|
|
|
included by the stylesheet or one of its includes). The order in which |
|
1082
|
|
|
|
|
|
|
files are returned by the function has no special signification. |
|
1083
|
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
=cut |
|
1085
|
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
sub get_stylesheet_dependencies { |
|
1087
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
1088
|
0
|
0
|
|
|
|
|
$self->compile unless $self->{compiledstylesheet}; |
|
1089
|
0
|
|
|
|
|
|
return sort keys %{$self->{stylesheet_cache}}; |
|
|
0
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
} |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
=head2 processor |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
$processor = $xps->processor |
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
Returns the processor object associated with I<$xps>. |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
=cut |
|
1099
|
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
sub processor { |
|
1101
|
0
|
|
|
0
|
|
|
return $_[0]->{processor}; |
|
1102
|
|
|
|
|
|
|
} |
|
1103
|
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
1105
|
|
|
|
|
|
|
|
|
1106
|
|
|
|
|
|
|
#=head2 gen_package_name |
|
1107
|
|
|
|
|
|
|
# |
|
1108
|
|
|
|
|
|
|
#Generates a fresh package name in which we would compile a new |
|
1109
|
|
|
|
|
|
|
#stylesheet. Never returns twice the same name. |
|
1110
|
|
|
|
|
|
|
|
|
1111
|
|
|
|
|
|
|
=cut " |
|
1112
|
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
do { |
|
1114
|
|
|
|
|
|
|
my $uniquifier; |
|
1115
|
|
|
|
|
|
|
sub gen_package_name { |
|
1116
|
0
|
|
|
0
|
|
|
$uniquifier++; |
|
1117
|
0
|
|
|
|
|
|
return "XML::XPathScript::STYLESHEET$uniquifier"; |
|
1118
|
|
|
|
|
|
|
} |
|
1119
|
|
|
|
|
|
|
}; |
|
1120
|
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
=head2 document |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
$nodeset = $xps->document( $uri ) |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
Reads XML given in $uri, parses it and returns it in a nodeset. |
|
1126
|
|
|
|
|
|
|
|
|
1127
|
|
|
|
|
|
|
=cut |
|
1128
|
|
|
|
|
|
|
|
|
1129
|
|
|
|
|
|
|
sub document { |
|
1130
|
|
|
|
|
|
|
# warn "Document function called\n"; |
|
1131
|
0
|
|
|
0
|
|
|
my( $self, $uri ) = @_; |
|
1132
|
|
|
|
|
|
|
|
|
1133
|
0
|
|
|
|
|
|
my( $results, $parser ); |
|
1134
|
0
|
0
|
|
|
|
|
if( $XML_parser eq 'XML::XPath' ) { |
|
|
|
0
|
|
|
|
|
|
|
1135
|
0
|
0
|
|
|
|
|
my $xml_parser = XML::Parser->new( |
|
1136
|
|
|
|
|
|
|
ErrorContext => 2, |
|
1137
|
|
|
|
|
|
|
Namespaces => $XML::XPath::VERSION < 1.07 ? 1 : 0, |
|
1138
|
|
|
|
|
|
|
# ParseParamEnt => 1, |
|
1139
|
|
|
|
|
|
|
); |
|
1140
|
|
|
|
|
|
|
|
|
1141
|
0
|
|
|
|
|
|
$parser = XML::XPath::XMLParser->new(parser => $xml_parser); |
|
1142
|
0
|
|
|
|
|
|
$results = XML::XPath::NodeSet->new(); |
|
1143
|
|
|
|
|
|
|
} |
|
1144
|
|
|
|
|
|
|
elsif ( $XML_parser eq 'XML::LibXML' ) { |
|
1145
|
0
|
|
|
|
|
|
$parser = XML::LibXML->new; |
|
1146
|
0
|
|
|
|
|
|
$results = XML::LibXML::Document->new; |
|
1147
|
|
|
|
|
|
|
} |
|
1148
|
|
|
|
|
|
|
else { |
|
1149
|
0
|
|
|
|
|
|
$self->die( "xml parser not valid: $XML_parser" ); |
|
1150
|
|
|
|
|
|
|
} |
|
1151
|
|
|
|
|
|
|
|
|
1152
|
|
|
|
|
|
|
|
|
1153
|
0
|
|
|
|
|
|
my $newdoc; |
|
1154
|
|
|
|
|
|
|
# TODO: must handle axkit: scheme a little more cleverly |
|
1155
|
0
|
0
|
0
|
|
|
|
if ($uri =~ /^\w\w+:/ and $uri !~ /^axkit:/ ) { # assume it's scheme://foo uri |
|
1156
|
0
|
|
|
|
|
|
eval { |
|
1157
|
0
|
|
|
|
|
|
$self->debug( 5, "trying to parse $uri" ); |
|
1158
|
0
|
|
|
|
|
|
eval "use LWP::Simple"; |
|
1159
|
0
|
|
|
|
|
|
$newdoc = $parser->parse_string( LWP::Simple::get( $uri ) ); |
|
1160
|
0
|
|
|
|
|
|
$self->debug( 5, "Parsed OK into $newdoc\n" ); |
|
1161
|
|
|
|
|
|
|
}; |
|
1162
|
0
|
0
|
|
|
|
|
if (my $E = $@) { |
|
1163
|
0
|
|
|
|
|
|
$self->debug("Parse of '$uri' failed: $E" ); |
|
1164
|
|
|
|
|
|
|
} |
|
1165
|
|
|
|
|
|
|
} |
|
1166
|
|
|
|
|
|
|
else { |
|
1167
|
0
|
|
|
|
|
|
$self->debug(3, "Parsing local: $uri\n"); |
|
1168
|
0
|
0
|
|
|
|
|
if( $XML_parser eq 'XML::LibXML' ) { |
|
|
|
0
|
|
|
|
|
|
|
1169
|
0
|
|
|
|
|
|
$newdoc = $parser->parse_file( $uri ); |
|
1170
|
|
|
|
|
|
|
} elsif( $XML_parser eq 'XML::XPath' ) { |
|
1171
|
0
|
|
|
|
|
|
$newdoc = XML::XPath->new( filename => $uri ); |
|
1172
|
|
|
|
|
|
|
} |
|
1173
|
0
|
|
|
|
|
|
else { die "invalid parser: $XML_parser\n"; } |
|
1174
|
|
|
|
|
|
|
} |
|
1175
|
|
|
|
|
|
|
|
|
1176
|
0
|
0
|
|
|
|
|
if( $newdoc ) { |
|
1177
|
0
|
0
|
|
|
|
|
if( $XML_parser eq 'XML::LibXML' ) { |
|
|
|
0
|
|
|
|
|
|
|
1178
|
0
|
|
|
|
|
|
$results = $newdoc->documentElement(); |
|
1179
|
|
|
|
|
|
|
} |
|
1180
|
|
|
|
|
|
|
elsif( $XML_parser eq 'XML::XPath' ) { |
|
1181
|
0
|
|
|
|
|
|
$results = $newdoc->findnodes('/')->[0]->getChildNodes->[0]; |
|
1182
|
|
|
|
|
|
|
} |
|
1183
|
|
|
|
|
|
|
} |
|
1184
|
|
|
|
|
|
|
|
|
1185
|
0
|
|
|
|
|
|
$self->debug(8, "XPathScript: document() returning"); |
|
1186
|
0
|
|
|
|
|
|
return $results; |
|
1187
|
|
|
|
|
|
|
} |
|
1188
|
|
|
|
|
|
|
|
|
1189
|
0
|
|
|
0
|
|
|
sub TIEHANDLE { my $self = ''; bless \$self, $_[0] } |
|
|
0
|
|
|
|
|
|
|
|
1190
|
|
|
|
|
|
|
sub PRINT { |
|
1191
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
1192
|
0
|
|
|
|
|
|
return XML::XPathScript::current()->print( @_ ); |
|
1193
|
|
|
|
|
|
|
} |
|
1194
|
|
|
|
|
|
|
sub BINMODE { |
|
1195
|
0
|
|
|
0
|
|
|
return XML::XPathScript::current()->binmode( @_ ); |
|
1196
|
|
|
|
|
|
|
} |
|
1197
|
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
1; |
|
1199
|
|
|
|
|
|
|
|
|
1200
|
|
|
|
|
|
|
__END__ |