File Coverage

blib/lib/XML/Template/Config.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             ###############################################################################
2             # XML::Template::Config
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
8             # modify it under the same terms as Perl itself.
9             ###############################################################################
10             package XML::Template::Config;
11 2     2   15 use base qw(XML::Template::Base);
  2         34  
  2         214  
12              
13 2     2   11 use strict;
  2         4  
  2         86  
14 2         350 use vars qw($AUTOLOAD $BASEDIR $CONFIGDIR $CONFIGFILE $PROCESS $CACHE
15             $FILE_CACHE $HANDLER $VARS $STRING $SUBROUTINE $HOSTNAME
16 2     2   11 $CACHE_SLOTS $CACHE_DIR_SLOTS $CACHE_DIR $ADMINDIR);
  2         3  
17 2     2   11 use File::Spec;
  2         3  
  2         47  
18 2     2   9 use XML::Template::Base;
  2         3  
  2         42  
19 2     2   4454 use XML::GDOME;
  0            
  0            
20              
21             use constant PACKAGE => 0;
22             use constant VAR => 1;
23              
24              
25             my $CONFIG;
26              
27              
28             =pod
29              
30             =head1 NAME
31              
32             XML::Template::Config - Configuration module for XML::Template modules.
33              
34             =head1 SYNOPSIS
35              
36             use base qw(XML::Template::Base);
37             use XML::Template::Base;
38              
39             =head1 DESCRIPTION
40              
41             This module is the XML::Template configuration module. It contains
42             the default values of many configuration variables used by
43             many XML::Template modules.
44              
45             =head1 CONFIGURATION VARIABLES
46              
47             Configuration variables and their default values are defined at the top of
48             C. The variable name must be all uppercase.
49             Variable values are actually anonymous array tuples. The first element is
50             the type of configuration variable (C or C), and the second
51             element is the value. For instance,
52              
53             $CONFIGFILE = [VAR, '/usr/local/xml-template/xml-template.conf'];
54             $PROCESS = [PACKAGE, 'XML::Template::Process'];
55              
56             A configuration variable value is obtained by a calls to an
57             XML::Template::Config subroutine that has the same name as the
58             configuration variable but is lowercase. For instance, to get the values
59             of the configuration variables above,
60              
61             my $configfile = XML::Template::Config->configfile;
62             my $process = XML::Template::Config->process (%params);
63              
64             For configuration variables of the type VAR, the value is simply returned.
65             If the type is PACKAGE, the module given by the configuration variable
66             value is required, and an object is instantiated and returned.
67             Parameters passed to the XML::Template::Config subroutine are passed to
68             the module constructor.
69              
70             =head2 General Configuration Variables
71              
72             =over 4
73              
74             =item BASEDIR
75              
76             The base installation directory for XML::Template. This directory
77             typically contains the system-wide configuration file, a directory
78             containing the siteadmin templates, and other system-wide templates.
79              
80             =item ADMINDIR
81              
82             The directory containing the siteadmin templates, typically
83             C<$BASEDIR/admin>.
84              
85             =item CONFIGDIR
86              
87             The directory containing the system-wide configuration file,
88             typically C<$BASEDIR>.
89              
90             =item CONFIGFILE
91              
92             The name of the system-wide configuration file, typically
93             C.
94              
95             =item HOSTNAME
96              
97             The default name of the host if none is given.
98              
99             =back
100              
101             =head2 C Configuration Variables
102              
103             =over 4
104              
105             =item PROCESS
106              
107             The default document processing module, typically
108             L.
109              
110             =back
111              
112             =head2 L Configuration Variables
113              
114             =over 4
115              
116             =item HANDLER
117              
118             The name of default the SAX parser handler module, typically
119             L.
120              
121             =item SUBROUTINE
122              
123             The name of the default module that contains methods implementing
124             XML::Template subroutines, typically L.
125              
126             =item CACHE
127              
128             The default document memory caching module, typically
129             L.
130              
131             =item FILE_CACHE
132              
133             The default document file caching module, typically L.
134              
135             =item VARS
136              
137             The default module that implements XML::Template variables, typically
138             L.
139              
140             =back
141              
142             =head2 L Configuration Variables.
143              
144             =over 4
145              
146             =item CACHE_SLOTS
147              
148             The size of the cache array.
149              
150             =back
151              
152             =head2 L Configuration Variables
153              
154             =over 4
155              
156             =item CACHE_DIR
157              
158             The directory where cached template document files are stored, typically
159             C.
160              
161             =back
162              
163             =head2 L Configuration Variables
164              
165             =over 4
166              
167             =item STRING
168              
169             The default module that parses XML::Template strings (attribute values and
170             content).
171              
172             =back
173              
174             =cut
175              
176             my $basedir = '/usr/local/xml-template';
177             $BASEDIR = [VAR, $basedir];
178             $ADMINDIR = [VAR, File::Spec->join ($basedir, 'admin')];
179             $CONFIGDIR = [VAR, $basedir];
180             $CONFIGFILE = [VAR, 'xml-template.conf'];
181             $HOSTNAME = [VAR, 'localhost'];
182              
183             # XML::Template
184             $PROCESS = [PACKAGE, 'XML::Template::Process'];
185              
186             # XML::Template::Process
187             $HANDLER = [VAR, 'XML::Template::Parser'];
188             $SUBROUTINE = [VAR, 'XML::Template::Subroutine'];
189             $CACHE = [PACKAGE, 'XML::Template::Cache'];
190             $FILE_CACHE = [PACKAGE, 'XML::Template::Cache::File'];
191             $VARS = [PACKAGE, 'XML::Template::Vars'];
192              
193             # XML::Template::Cache
194             $CACHE_SLOTS = [VAR, 5];
195              
196             # XML::Template::Cache::File
197             $CACHE_DIR = [VAR, '/tmp/xml-template'];
198              
199             # XML::Template::Parser
200             $STRING = [PACKAGE, 'XML::Template::Parser::String'];
201              
202             =pod
203              
204             =head1 PUBLIC METHODS
205              
206             =head2 config
207              
208             my $config = XML::Template::Config->config ($configfile);
209              
210             This method reference a reference to an XML parser object (currently a
211             GDOME object) for the parsed XML::Template XML cofiguration document. The
212             first call to C loads the config file and stores it in the package
213             global variable C<$CONFIG>. Subsequent calls to C will simply
214             return C<$CONFIG>.
215              
216             An optional parameter may be given naming the config file. If no
217             parameter is given, the system-wide configuration file is named by the
218             configuration variable C<$CONFIGFILE> and is located in the directory
219             specified by C<$CONFIGDIR>.
220              
221             If the element C is present in the host configuration for the
222             current host, a host-specific configuration document is loaded from
223             C and its contents appended to the contents of the system-wide
224             configuration.
225              
226             =cut
227              
228             sub config {
229             my $self = shift;
230             my $configfile = shift;
231              
232             my $config;
233             if (defined $CONFIG) {
234             $config = $CONFIG;
235             } else {
236             $configfile = File::Spec->catfile (XML::Template::Config->configdir,
237             XML::Template::Config->configfile)
238             if ! defined $configfile;
239              
240             my $parser = XML::GDOME->new ();
241             $config = eval { $parser->parse_file ($configfile) };
242             return $self->error ('Template', $@) if $@;
243              
244             # Load user configuration file.
245             my $hostname = $self->hostname;
246             my ($node) = eval { $config->findnodes (qq{/xml-template/hosts/host[\@name="$hostname"]/basedir/text()}) };
247             return $self->error ('Template', $@) if $@;
248             if (defined $node) {
249             my $basedir = $node->toString;
250             $configfile = File::Spec->catfile ($basedir,
251             XML::Template::Config->configfile);
252             my $config2 = eval { $parser->parse_file ($configfile) };
253             if (! $@) {
254             my (@nodes) = $config2->findnodes ('/xml-template/*');
255             my ($root) = $config->findnodes ("/xml-template");
256             foreach my $node (@nodes) {
257             my $node2 = $config->importNode ($node, 1);
258             $root->appendChild ($node2);
259             }
260             }
261             }
262              
263             $CONFIG = $config;
264             }
265              
266             return $config;
267             }
268              
269             =pod
270              
271             =head2 load
272              
273             XML::Template::Config->load ($module)
274             || return $self->error (XML::Template::Config->error);
275              
276             This method requires the module named by the parameter.
277              
278             =cut
279              
280             sub load {
281             my $self = shift;
282             my $module = shift;
283              
284             $module =~ s[::][/]g;
285             $module .= '.pm';
286             eval {require $module};
287              
288             return $@ ? $self->error ('Config', "Could not load module '$module': $@") : 1;
289             }
290              
291             sub AUTOLOAD {
292             my $self = shift;
293             my %params = @_;
294              
295             if ($AUTOLOAD !~ /DESTROY$/) {
296             my $varname = uc ($AUTOLOAD);
297             $varname =~ s/.*:://;
298              
299             no strict 'refs';
300             my $var = $$varname;
301             use strict;
302             if (! defined $var) {
303             $self->error ('Config', "No configuration variable for '$varname'.\n");
304             } else {
305             if ($var->[0] == PACKAGE) {
306             if ($self->load ($var->[1])) {
307             my $package = $var->[1];
308             return $package->new (%params)
309             || $self->error ('Config', "Could not load module '$package': " . $package->error);
310             }
311             } else {
312             return $var->[1];
313             }
314             }
315              
316             return undef;
317             }
318             }
319              
320             =pod
321              
322             =head1 AUTHOR
323              
324             Jonathan Waxman
325            
326              
327             =head1 COPYRIGHT
328              
329             Copyright (c) 2002-2003 Jonathan A. Waxman
330             All rights reserved.
331              
332             This program is free software; you can redistribute it and/or
333             modify it under the same terms as Perl itself.
334              
335             =cut
336              
337              
338             1;