line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################### |
2
|
|
|
|
|
|
|
# XML::Template::Base |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright (c) 2002-2003 Jonathan A. Waxman |
5
|
|
|
|
|
|
|
# All rights reserved. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify it |
8
|
|
|
|
|
|
|
# under the same terms as Perl itself. |
9
|
|
|
|
|
|
|
############################################################################### |
10
|
|
|
|
|
|
|
package XML::Template::Base; |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
79
|
|
13
|
2
|
|
|
2
|
|
1402
|
use XML::Template::Config; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $SOURCE = {}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=pod |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
XML::Template::Base - Base class for XML::Template modules. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use base qw(XML::Template::Base); |
27
|
|
|
|
|
|
|
use XML::Template::Base; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module is the XML::Template base class. It implements common |
32
|
|
|
|
|
|
|
functionality for many other XML::Template modules, including construction |
33
|
|
|
|
|
|
|
and error handling. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
XML::Template::Base provides a common constructor for XML::Template |
38
|
|
|
|
|
|
|
modules. The constructor creates a new self and calls an initialization |
39
|
|
|
|
|
|
|
method. If the derived class does not have its own initialization |
40
|
|
|
|
|
|
|
subroutine, XML::Template::Base provides one that simply returns true. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The following named configuration parameters are supported: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=over 4 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item Debug |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Set to true to turn on printing debug information. Not really supported. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item HTTPHost |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
The hostname of the requesting server. The default value is set to the |
53
|
|
|
|
|
|
|
environment variable C. If this is not set (for instance, if |
54
|
|
|
|
|
|
|
running on the command line), the default is C. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item Config |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
A reference to an XML::GDOME object that contains XML::Template |
59
|
|
|
|
|
|
|
configuration information. See L. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=back 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new { |
66
|
|
|
|
|
|
|
# Due to the force of karma, we take rebirth. |
67
|
|
|
|
|
|
|
my $proto = shift; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Due to ignorance, a self is fabricated. |
70
|
|
|
|
|
|
|
my $self = {}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Due to karmic imprints on our consciousness, name and form arise. |
73
|
|
|
|
|
|
|
my $class = ref ($proto) || $proto; |
74
|
|
|
|
|
|
|
bless ($self, $class); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# There is contact between the sense organs and consciousness. |
77
|
|
|
|
|
|
|
my %params = @_; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Contact is the basis for feelings. |
80
|
|
|
|
|
|
|
$self->{_debug} = $params{Debug} if defined $params{Debug}; |
81
|
|
|
|
|
|
|
$self->{_hostname} = $params{HTTPHost} |
82
|
|
|
|
|
|
|
|| $ENV{HTTP_HOST} |
83
|
|
|
|
|
|
|
|| XML::Template::Config->hostname |
84
|
|
|
|
|
|
|
|| return $proto->error (XML::Template::Config->error); |
85
|
|
|
|
|
|
|
$self->{_config} = $params{Config} |
86
|
|
|
|
|
|
|
|| XML::Template::Config->config |
87
|
|
|
|
|
|
|
|| return $proto->error (XML::Template::Config->error); |
88
|
|
|
|
|
|
|
$self->{_source} = $SOURCE; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# We become attached to what feels good; attachment and craving lead |
91
|
|
|
|
|
|
|
# to becoming and birth. |
92
|
|
|
|
|
|
|
return $self->_init (%params) ? $self |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Eventually we grow old and die. |
95
|
|
|
|
|
|
|
: $proto->error ($self->error); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=pod |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 PRIVATE METHODS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 _init |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
XML::Template::Base provides an initialization function that simply |
105
|
|
|
|
|
|
|
returns 1. This is required for modules that use XML::Template::Base as a |
106
|
|
|
|
|
|
|
base class, but do not require an initialization function. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _init { |
111
|
|
|
|
|
|
|
my $self = shift; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=pod |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 error |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
return $self->error ($type, $error); |
123
|
|
|
|
|
|
|
my ($type, $error) = $self->error; |
124
|
|
|
|
|
|
|
print $self->error; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
XML::Template::Base provides the method C to do simple error |
127
|
|
|
|
|
|
|
handling. If no parameters are given, the currently stored error type and |
128
|
|
|
|
|
|
|
message are returned. If parameters for the error type and message are |
129
|
|
|
|
|
|
|
given, they are stored as the current error. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
C may be called as a package method (e.g., |
132
|
|
|
|
|
|
|
Cerror ($error);> or as an object method |
133
|
|
|
|
|
|
|
(e.g., C<$xml_template-Eerror ($error);>. If it is called as a |
134
|
|
|
|
|
|
|
package method, the error is stored as a package variable. If it is |
135
|
|
|
|
|
|
|
called as an object method, the error is stored as a private variable. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub error { |
140
|
|
|
|
|
|
|
my $self = shift; |
141
|
|
|
|
|
|
|
my ($type, $error) = @_; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# If an error given, set it in the object or package. |
144
|
|
|
|
|
|
|
# Otherwise, return the error from the object or package. |
145
|
|
|
|
|
|
|
if (defined $type) { |
146
|
|
|
|
|
|
|
ref ($self) ? $self->{_error} = [$type, $error] |
147
|
|
|
|
|
|
|
: $self::_error = [$type, $error]; |
148
|
|
|
|
|
|
|
return undef; |
149
|
|
|
|
|
|
|
} else { |
150
|
|
|
|
|
|
|
if (wantarray) { |
151
|
|
|
|
|
|
|
return ref ($self) ? @{$self->{_error}} : @{$self::_error}; |
152
|
|
|
|
|
|
|
} else { |
153
|
|
|
|
|
|
|
if (ref ($self)) { |
154
|
|
|
|
|
|
|
return defined $self->{_error}->[0] |
155
|
|
|
|
|
|
|
? "$self->{_error}->[0]: $self->{_error}->[1]" |
156
|
|
|
|
|
|
|
: undef; |
157
|
|
|
|
|
|
|
} else { |
158
|
|
|
|
|
|
|
return defined $self::_error->[0] |
159
|
|
|
|
|
|
|
? "$self::_error->[0]: $self::_error->[1]" |
160
|
|
|
|
|
|
|
: undef; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=pod |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 get_info |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $host_info = $self->get_info ("/xml-template/hosts/host[\@name='$name']", |
171
|
|
|
|
|
|
|
'basedir', 'domain'); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This method returns a hash containing name/value pairs from the |
174
|
|
|
|
|
|
|
XML::Template configuration file. The first parameter is an XPath query |
175
|
|
|
|
|
|
|
that returns the XML subtree containing the desired configuration |
176
|
|
|
|
|
|
|
information. The remaining parameters name the child elements of the |
177
|
|
|
|
|
|
|
configuration subtree whose values you wish returned in the hash. This |
178
|
|
|
|
|
|
|
method is wrapped by more specific subroutines, for instance, |
179
|
|
|
|
|
|
|
get_host_info, get_subroutine_info, get_namespace_info, etc. See |
180
|
|
|
|
|
|
|
L for more details on the XML::Template |
181
|
|
|
|
|
|
|
configuration file. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub get_info { |
186
|
|
|
|
|
|
|
my $self = shift; |
187
|
|
|
|
|
|
|
my ($query, @elements) = @_; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
my ($node) = $self->{_config}->findnodes ($query); |
190
|
|
|
|
|
|
|
if (defined $node) { |
191
|
|
|
|
|
|
|
my (%info, @info); |
192
|
|
|
|
|
|
|
foreach my $el (@elements) { |
193
|
|
|
|
|
|
|
my @nodes = $self->{_config}->findnodes ("$query/$el"); |
194
|
|
|
|
|
|
|
if (scalar (@nodes) == 1) { |
195
|
|
|
|
|
|
|
if (wantarray) { |
196
|
|
|
|
|
|
|
push (@info, $nodes[0]->string_value); |
197
|
|
|
|
|
|
|
} else { |
198
|
|
|
|
|
|
|
$info{$el} = $nodes[0]->string_value; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} elsif (scalar (@nodes) > 1) { |
201
|
|
|
|
|
|
|
my @values; |
202
|
|
|
|
|
|
|
foreach my $node (@nodes) { |
203
|
|
|
|
|
|
|
push (@values, $node->string_value); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
if (wantarray) { |
206
|
|
|
|
|
|
|
push (@info, \@values); |
207
|
|
|
|
|
|
|
} else { |
208
|
|
|
|
|
|
|
push (@{$info{$el}}, \@values); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
return wantarray ? @info : \%info; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
return undef; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=pod |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 get_host_info |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
my $host_info = $self->get_host_info ($hostname); |
224
|
|
|
|
|
|
|
my $host_info = $self->get_host_info ($hostname, 'domain'); |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This method returns a hash of name/value pairs of hostname information |
227
|
|
|
|
|
|
|
from the XML::Template configuration file. The first parameter is the |
228
|
|
|
|
|
|
|
name of the host for which information is desired. The remaining |
229
|
|
|
|
|
|
|
parameters name the configuration elements to include in the hash. If no |
230
|
|
|
|
|
|
|
such parameters are given, all host configuration elements are included. |
231
|
|
|
|
|
|
|
Currently, this includes C and C. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=cut |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub get_host_info { |
236
|
|
|
|
|
|
|
my $self = shift; |
237
|
|
|
|
|
|
|
my ($name, @elements) = @_; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
@elements = qw(basedir domain) if ! scalar (@elements); |
240
|
|
|
|
|
|
|
return $self->get_info ( |
241
|
|
|
|
|
|
|
"/xml-template/hosts/host[\@name='$name']", |
242
|
|
|
|
|
|
|
@elements); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=pod |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 get_source_mapping_info |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
my $get_source_mapping_info = $self->get_source_mapping_info ( |
250
|
|
|
|
|
|
|
namespace => $namespace); |
251
|
|
|
|
|
|
|
my $get_source_mapping_info = $self->get_source_mapping_info ( |
252
|
|
|
|
|
|
|
namespace => $namespace, |
253
|
|
|
|
|
|
|
'source'); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
This method returns a hash of name/value pairs of source mapping |
256
|
|
|
|
|
|
|
two parameters give the resource type and name of the source mapping for |
257
|
|
|
|
|
|
|
which information is desired. The remaining parameters name the |
258
|
|
|
|
|
|
|
configuration elements to include in the hash. If no such parameters are |
259
|
|
|
|
|
|
|
given, all source mapping configuration elements are included. |
260
|
|
|
|
|
|
|
Currently, this includes C |