This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PI.pm
355 lines (254 loc) · 8.21 KB
/
PI.pm
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package PI;
=head1 NAME
PI (pio)
=head1 DESCRIPTION
This class packages code references and parameters for filtering XML
processing instructions <?...?> in XML templates in a generic way.
=head1 VERSION
$Id: PI.pm,v 1.6 2008/06/26 16:40:00 pfarber Exp $
=head1 SYNOPSIS
my $pio = new PI;
$pio->add_PI('SOME_PI', $PI_handler_coderef, [$param_1, $param_2]);
$pio->process_PIs($xml_template_data_ref);
=head1 METHODS
=over 8
=cut
BEGIN
{
if ($ENV{'HT_DEV'})
{
require "strict.pm";
strict::import();
}
}
use Debug::DUtils;
# Regular expression used to match any processing instruction within
# an html page template, of the form: <?Instruction?> or
# <?Instruction parm1="this" parm2="that"?>
my $PI_reg_exp = '<\?(\w+)(-\w+)?((\s+\w+=".*?")*)\s*\?>';
my $comp_PI_reg_exp = qr/$PI_reg_exp/;
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
# ---------------------------------------------------------------------
=item add_PI
Description
=cut
# ---------------------------------------------------------------------
sub add_PI
{
my $self = shift;
my ($ins, $sub_ref, $parm_list_ref) = @_;
$self->{$ins}{'sub'} = $sub_ref;
$self->{$ins}{'parms'} = $parm_list_ref;
}
# ---------------------------------------------------------------------
=item get_PIs
Description
=cut
# ---------------------------------------------------------------------
sub get_PIs
{
my $self = shift;
my $s_ref = shift;
my @PI_arr = ();
while ($$s_ref =~ m,$comp_PI_reg_exp,gs)
{
my $pi_name = $1;
push(@PI_arr, $pi_name);
}
return \@PI_arr;
}
# ---------------------------------------------------------------------
=item PI_DEBUG_trap
Description
=cut
# ---------------------------------------------------------------------
sub PI_DEBUG_trap
{
return;
}
# ---------------------------------------------------------------------
=item process_PIs
Run through a string of XML and farm out the handling of any
processing instructions it encounters to the handlers
First, make a working copy of the input string, and a new string.
Then, running through the string matching for PIs, grab $` (the text
before the match) and concat it to the new string. Then, process the
PI if a subroutine was supplied to handle it, and concat that to the
new string. Then make the working string equal to the post match
string and repeat.
=cut
# ---------------------------------------------------------------------
sub process_PIs
{
my $self = shift;
my $s_ref = shift;
# working copy of input text
my $s = $$s_ref;
# new string to gather up processed text
my $new_s = '';
while ($s =~ m,(.*?)($comp_PI_reg_exp)(.*),gs)
{
my $pre = $1;
my $pi = $2;
my $instruction = $3 . $4;
my $pi_params = $5;
my $post = $7;
$pi_params =~ s,^\s*,,;
# remove trailing spaces, if any
$instruction =~ s,\s*$,,;
# the following splits up the p1=val1 p2=val2 into a hash,
# properly indexed by p's
# my %pi_params = split (/=|\s+/, $pi_params);
# Superseded below by suggestion from Sakaama Heesakkers to
# allow spaces in the parameter value
my %pi_params = split(/="|"[\s>]/, $pi_params);
# remove quotes from values and downcase all keys and values
# so that PI atribute names (not values) can be case
# insensitive
my %lc_params = ();
foreach my $p (keys (%pi_params))
{
my $temp_param = $pi_params{$p};
$temp_param =~ s,\",,g; #"
my $lc_p = lc ($p);
$lc_params{$lc_p} = $temp_param;
}
# grab routine to run for this instruction
my $sub = $self->__get_codref($instruction);
my $parms = $self->__get_parms($instruction);
# add pre-match text to output string
$new_s .= $pre;
DEBUG('pis', qq{Handling PI="<b>$instruction</b>"});
# if there is no subroutine defined, return Processing
# Instruction to the text
if (! (defined ($sub)))
{
$new_s .= $pi;
}
else
{
if (DEBUG($instruction))
{
PI_DEBUG_trap();
}
# if there is a subroutine CODE ref defined, run it and
# tack on results to the new string. If subroutine is a
# method name the first element in the parameter array is
# the object holding the method. Invoke as an method.
my $return_val;
if (ref($sub) eq 'CODE')
{
$return_val = &$sub(@$parms, \%lc_params);
}
else
{
# The param is a string to be bound symbolically to
# current object (which is the first element of the
# paramter array supplied by the caller to add_PI()
my $object = shift @$parms;
$return_val = $object->$sub(@$parms, \%lc_params);
}
if (ref($return_val) eq 'SCALAR')
{ $new_s .= $$return_val; }
else
{ $new_s .= $return_val; }
}
# finally, replace $s so that match starts from here on
$s = $post;
}
# grab last part of incoming text, after the last match.
$new_s .= $s;
$$s_ref = $new_s;
}
# ---------------------------------------------------------------------
=item __get_codref
Description
=cut
# ---------------------------------------------------------------------
sub __get_codref
{
my $self = shift;
my $ins = shift;
if (exists (${ $self}{$ins}))
{
if (defined ($self->{$ins}{'sub'}))
{
return ($self->{$ins}{'sub'});
}
else
{
return \&__simple_substitution; # reference to anon. subroutine
}
}
else
{
return undef;
}
}
# ---------------------------------------------------------------------
=item __simple_substitution
acts as a default subroutine to be called by process_PI
=cut
# ---------------------------------------------------------------------
sub __simple_substitution
{
my $s = shift;
if (ref($s) eq 'SCALAR')
{ return $$s; }
else
{ return $s; }
}
# ---------------------------------------------------------------------
=item __get_parms
Description
=cut
# ---------------------------------------------------------------------
sub __get_parms
{
my $self = shift;
my $ins = shift;
if ((exists ($ {$self}{$ins})) &&
(defined($self->{$ins}{'parms'})))
{
# Note: this is a reference to an anonymous array of
# parameters for this instruction. Return a writable copy of
# this read-only value so we can have our way with the array
# if we encounter more of the same PI in this run of
# process_PIs().
my @return_parms = @{$self->{$ins}{'parms'}};
return \@return_parms;
}
else
{
return undef;
}
}
1;
__END__
=head1 AUTHOR
Phillip Farber, University of Michigan, [email protected]
=head1 COPYRIGHT
Copyright 2007 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut