/
Method.pm
325 lines (293 loc) · 10.1 KB
/
Method.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
###############################################################################
#
# This file copyright (c) 2001 by Randy J. Ray, all rights reserved
#
# Copying and distribution are permitted under the terms of the Artistic
# License as distributed with Perl versions 5.005 and later. See
# http://language.perl.com/misc/Artistic.html
#
###############################################################################
#
# $Id: Method.pm,v 1.2 2001/10/04 07:36:00 rjray Exp $
#
# Description: This class abstracts out all the method-related operations
# from the RPC::XML::Server class
#
# Functions: new
# clone
# is_valid
# name \
# code \
# signature \ These are the accessor functions for the
# help / data in the object, though it's visible.
# version /
# hidden /
# add_signature
# delete_signature
# load_XPL_file
#
# Libraries: XML::Parser (used only on demand in load_XPL_file)
# File::Spec
#
# Global Consts: $VERSION
#
# Environment: None.
#
###############################################################################
package RPC::XML::Method;
use 5.005;
use strict;
use vars qw($VERSION);
use subs qw(new check name code signature add_sig del_sig help version hidden
load_XPL_file);
require File::Spec;
$VERSION = do { my @r=(q$Revision: 1.2 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
1;
###############################################################################
#
# Sub Name: new
#
# Description: Create a new object of this class, storing the info on
# regular keys (no obfuscation used here).
#
# Arguments: NAME IN/OUT TYPE DESCRIPTION
# $class in scalar Class to bless into
# @argz in variable Disposition is variable; see
# below
#
# Returns: Success: object ref
# Failure: error string
#
###############################################################################
sub new
{
my $class = shift;
my @argz = @_;
my $data; # This will be a hashref that eventually gets blessed
#
# There are three things that @argz could be:
#
if (ref $argz[0])
{
# 1. A hashref containing all the relevant keys
$data = {};
%$data = %{$argz[0]};
}
elsif (@argz == 1)
{
# 2. Exactly one non-ref element, a file to load
$data = load_XPL_file(undef, $argz[0]);
return $data unless ref $data; # load_XPL_path signalled an error
}
else
{
# 3. If there is more than one arg, it's a sort-of-hash. That is, the
# key 'signature' is allowed to repeat.
my ($key, $val);
$data = {};
$data->{signature} = [];
while (@argz)
{
($key, $val) = splice(@argz, 0, 2);
if ($key eq 'signature')
{
# Since there may be more than one signature, we allow it to
# repeat. Of course, that's also why we can't just take @argz
# directly as a hash. *shrug*
push(@{$data->{signature}},
[ ref($val) ? @$val : split(/ /, $val) ]);
}
elsif (exists $data->{$key})
{
return "${class}::new: Key '$key' may not be repeated";
}
else
{
$data->{$key} = $val;
}
}
}
bless $data, $class;
}
###############################################################################
#
# Sub Name: clone
#
# Description: Create a near-exact copy of the invoking object, save that
# the listref in the "signature" key is a copy, not a ref
# to the same list.
#
# Arguments: NAME IN/OUT TYPE DESCRIPTION
# $self in ref Object of this class
#
# Returns: Success: $new_self
# Failure: error message
#
###############################################################################
sub clone
{
my $self = shift;
my $new_self = {};
for (keys %$self)
{
next if $_ eq 'signature';
$new_self->{$_} = $self->{$_};
}
$new_self->{signature} = [];
@{$new_self->{signature}} = @{$self->{signature}};
bless $new_self, $self;
}
###############################################################################
#
# Sub Name: is_valid
#
# Description: Boolean test to tell if the calling object has sufficient
# data to be used as a server method for RPC::XML::Server or
# Apache::RPC::Server.
#
# Arguments: NAME IN/OUT TYPE DESCRIPTION
# $self in ref Object to test
#
# Returns: Success: 1, valid/complete
# Failure: 0, invalid/incomplete
#
###############################################################################
sub is_valid
{
my $self = shift;
return ((ref($self->{code}) eq 'CODE') and $self->{name} and
(ref($self->{signature}) && scalar(@{$self->{signature}})));
}
#
# These are basic accessor/setting functions for the various attributes
#
sub name { $_[1] and $_[0]->{name} = $_[1]; $_[0]->{name}; }
sub help { $_[1] and $_[0]->{help} = $_[1]; $_[0]->{help}; }
sub version { $_[1] and $_[0]->{version} = $_[1]; $_[0]->{version}; }
sub hidden { $_[1] and $_[0]->{hidden} = $_[1]; $_[0]->{hidden}; }
sub code
{
ref $_[1] eq 'CODE' and $_[0]->{code} = $_[1];
$_[0]->{code};
}
sub signature
{
ref $_[1] eq 'ARRAY' and $_[0]->{signature} = $_[1];
# Return a copy of the array, not the original
[ @{$_[0]->{signature}} ];
}
###############################################################################
#
# Sub Name: add_signature
# delete_signature
#
# Description: This pair of functions may be used to add and remove
# signatures from a method-object.
#
# Arguments: NAME IN/OUT TYPE DESCRIPTION
# $self in ref Object of this class
# @args in list One or more signatures
#
# Returns: Success: $self
# Failure: error string
#
###############################################################################
sub add_signature
{
my $self = shift;
my @args = @_;
my (%sigs, $one_sig, $tmp);
%sigs = map { $_ => 1 } @{$self->{signature}};
for $one_sig (@args)
{
$tmp = (ref $one_sig) ? join(' ', @$one_sig) : $one_sig;
$sigs{$tmp} = 1;
}
$self->{signature} = [ keys %sigs ];
$self;
}
sub delete_signature
{
my $self = shift;
my @args = @_;
my (%sigs, $one_sig, $tmp);
%sigs = map { $_ => 1 } @{$self->{signature}};
for $one_sig (@args)
{
$tmp = (ref $one_sig) ? join(' ', @$one_sig) : $one_sig;
delete $sigs{$tmp};
}
$self->{signature} = [ keys %sigs ];
$self;
}
###############################################################################
#
# Sub Name: load_XPL_file
#
# Description: Load a XML-encoded method description (generally denoted
# by a *.xpl suffix) and return the relevant information.
#
# Arguments: NAME IN/OUT TYPE DESCRIPTION
# $self in ref Object of this class
# $file in scalar File to load
#
# Returns: Success: hashref of values
# Failure: error string
#
###############################################################################
sub load_XPL_file
{
my $self = shift;
my $file = shift;
require XML::Parser;
my ($signature, $code, $codetext, $return, $accum, $P, %attr);
local *F;
$return = {};
# So these don't end up undef, since they're optional elements
$return->{hidden} = 0; $return->{version} = ''; $return->{help} = '';
$return->{signature} = [];
open(F, "< $file");
return "Error opening $file for reading: $!" if ($?);
$P = XML::Parser
->new(Handlers => {Char => sub { $accum .= $_[1] },
Start => sub { %attr = splice(@_, 2) },
End =>
sub {
my $elem = $_[1];
$accum =~ s/^[\s\n]+//;
$accum =~ s/[\s\n]+$//;
if ($elem eq 'signature')
{
push(@{$return->{signature}},
[ split(/ /, $accum) ]);
}
elsif ($elem eq 'code')
{
$return->{$elem} = $accum
unless ($attr{language} and
$attr{language} ne 'perl');
}
else
{
$return->{$elem} = $accum;
}
%attr = ();
$accum = '';
}});
return "Error creating XML::Parser object" unless $P;
# Trap any errors
eval { $P->parse(*F) };
return "Error parsing $file: $@" if $@;
# Try to normalize $codetext before passing it to eval
($codetext = $return->{code}) =~
s/sub[\s\n]+[\w:]+[\s\n]+\{/\$code = sub \{/;
eval "$codetext";
return "Error creating anonymous sub: $@" if $@;
$return->{code} = $code;
# The XML::Parser approach above gave us an empty "methoddef" key
delete $return->{methoddef};
# Add the file's mtime for when we check for stat-based reloading
$return->{mtime} = (stat $file)[9];
$return->{file} = $file;
$return;
}