| 1 | ############################################################################### | ||
| 2 | # XML::Template::Base | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu> | ||
| 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 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use XML::Template::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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<HTTP_HOST>. If this is not set (for instance, if | ||
| 54 | running on the command line), the default is C<localhost>. | ||
| 55 | |||
| 56 | =item Config | ||
| 57 | |||
| 58 | A reference to an XML::GDOME object that contains XML::Template | ||
| 59 | configuration information. See L<XML::Template::Config>. | ||
| 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<error> 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<error> may be called as a package method (e.g., | ||
| 132 | C<XML::Template::Module-E<gt>error ($error);> or as an object method | ||
| 133 | (e.g., C<$xml_template-E<gt>error ($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<XML::Template::Config> 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<basedir> and C<domain>. | ||
| 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<source>, C<table>, C<keys>, and C<relation>. | ||
| 261 | The element C<relation> is a hash of related namespace name/table pairs. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub get_source_mapping_info { | ||
| 266 | my $self = shift; | ||
| 267 | my ($type, $name, @elements) = @_; | ||
| 268 | |||
| 269 | my @telements = qw(source table keys) if ! scalar (@elements); | ||
| 270 | my $source_info = $self->get_info ( | ||
| 271 | "/xml-template/source-mappings/source-mapping[\@$type='$name']", | ||
| 272 | @telements); | ||
| 273 | |||
| 274 | my %elements = map { $_ => 1 } @elements; | ||
| 275 | if (! scalar (@elements) || $elements{relation}) { | ||
| 276 | my @nodes = $self->{_config}->findnodes ( | ||
| 277 | "/xml-template/source-mappings/source-mapping[\@$type='$name']/relation/\@namespace"); | ||
| 278 | foreach my $node (@nodes) { | ||
| 279 | my $namespace = $node->string_value; | ||
| 280 | $source_info->{relation}->{$namespace} = $self->get_info ( | ||
| 281 | "/xml-template/source-mappings/source-mapping[\@$type='$name']/relation[\@namespace='$namespace']", | ||
| 282 | 'table'); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | return $source_info; | ||
| 287 | } | ||
| 288 | |||
| 289 | =pod | ||
| 290 | |||
| 291 | =head2 get_source_info | ||
| 292 | |||
| 293 | my $source_info = $self->get_source_info ($sourcename); | ||
| 294 | my $source_info = $self->get_source_info ($sourcename, 'module'); | ||
| 295 | |||
| 296 | This method returns a hash of name/value pairs of source information from | ||
| 297 | the XML::Template configuration file. The first parameter is the name of | ||
| 298 | the source for which information is desired. The remaining parameters | ||
| 299 | name the configuration elements to include in the hash. If no such | ||
| 300 | parameters are given, all host configuration elements are included. | ||
| 301 | Currently, this includes C<module>, C<dsn>, C<user>, and C<pwdfile>. | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub get_source_info { | ||
| 306 | my $self = shift; | ||
| 307 | my ($name, @elements) = @_; | ||
| 308 | |||
| 309 | @elements = qw(module dsn user pwdfile) if ! scalar (@elements); | ||
| 310 | return $self->get_info ( | ||
| 311 | "/xml-template/sources/source[\@name='$name']", | ||
| 312 | @elements); | ||
| 313 | } | ||
| 314 | |||
| 315 | =pod | ||
| 316 | |||
| 317 | =head2 get_source | ||
| 318 | |||
| 319 | my $source = $self->get_source ($sourcename); | ||
| 320 | |||
| 321 | This method returns the data source named by the parameter from the | ||
| 322 | XML::Template configuration file. | ||
| 323 | |||
| 324 | Data source references are stored in a cache. If a requested data source | ||
| 325 | has already been loaded, the cached reference to it is returned. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub get_source { | ||
| 330 | my $self = shift; | ||
| 331 | my $sourcename = shift; | ||
| 332 | |||
| 333 | my $source; | ||
| 334 | if (defined $sourcename) { | ||
| 335 | # If source already requested, return stored reference. | ||
| 336 | if (defined $self->{_source}->{$sourcename}) { | ||
| 337 | $source = $self->{_source}->{$sourcename}; | ||
| 338 | |||
| 339 | # Create and return a reference to a source object. | ||
| 340 | } else { | ||
| 341 | my $sourceinfo = $self->get_source_info ($sourcename, 'module'); | ||
| 342 | if (defined $sourceinfo) { | ||
| 343 | # Load the source module. | ||
| 344 | my $module = $sourceinfo->{module}; | ||
| 345 | XML::Template::Config->load ($module) | ||
| 346 | || return $self->error (XML::Template::Config->error); | ||
| 347 | |||
| 348 | # Create a new data source object. | ||
| 349 | $source = $module->new ($sourcename) | ||
| 350 | || return $self->error ('Source', scalar ($module->error ())); | ||
| 351 | |||
| 352 | # Cache the data source object. | ||
| 353 | $self->{_source}->{$sourcename} = $source; | ||
| 354 | |||
| 355 | } else { | ||
| 356 | return $self->error ('Source', "Source '$sourcename' not defined."); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | return $source; | ||
| 362 | } | ||
| 363 | |||
| 364 | =pod | ||
| 365 | |||
| 366 | =head2 get_subroutine_info | ||
| 367 | |||
| 368 | my $subroutine_info = $self->get_subroutine_info ($subname); | ||
| 369 | my $subroutine_info = $self->get_subroutine_info ($subname, 'module'); | ||
| 370 | |||
| 371 | This method returns a hash of name/value pairs of subroutine information | ||
| 372 | from the XML::Template configuration file. The first parameter is the | ||
| 373 | name of the subroutine for which information is desired. The remaining | ||
| 374 | parameters name the configuration elements to include in the hash. If no | ||
| 375 | such parameters are given, all host configuration elements are included. | ||
| 376 | Currently, this include C<description> and C<module>. | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | sub get_subroutine_info { | ||
| 381 | my $self = shift; | ||
| 382 | my ($name, @elements) = @_; | ||
| 383 | |||
| 384 | @elements = qw(description module) if ! scalar (@elements); | ||
| 385 | return $self->get_info ( | ||
| 386 | "/xml-template/subroutines/subroutine[\@name='$name']", | ||
| 387 | @elements); | ||
| 388 | } | ||
| 389 | |||
| 390 | =pod | ||
| 391 | |||
| 392 | =head2 get_namespace_info | ||
| 393 | |||
| 394 | my $namespace_info = $self->get_namespace_info ($namespace); | ||
| 395 | my $namespace_info = $self->get_namespace_info ($namespace, 'title'); | ||
| 396 | |||
| 397 | This method returns a hash of name/value pairs of namespace information | ||
| 398 | from the XML::Template configuration file. The first parameter is the | ||
| 399 | name of the namespace for which information is desired. The remaining | ||
| 400 | parameters name the configuration elements to include in the hash. If no | ||
| 401 | such parameters are given, all host configuration elements are included. | ||
| 402 | Currently, this include C<prefix>, C<title>, C<decsritpion>, and | ||
| 403 | C<module>. | ||
| 404 | |||
| 405 | =cut | ||
| 406 | |||
| 407 | sub get_namespace_info { | ||
| 408 | my $self = shift; | ||
| 409 | my ($name, @elements) = @_; | ||
| 410 | |||
| 411 | @elements = qw(prefix title description module) if ! scalar (@elements); | ||
| 412 | return $self->get_info ( | ||
| 413 | "/xml-template/namespaces/namespace[\@name='$name']", | ||
| 414 | @elements); | ||
| 415 | } | ||
| 416 | =pod | ||
| 417 | |||
| 418 | =head2 get_element_info | ||
| 419 | |||
| 420 | my $element_info = $self->get_element_info ($namespace, $element); | ||
| 421 | my $element_info = $self->get_element_info ($namespace, $element, | ||
| 422 | 'content'); | ||
| 423 | |||
| 424 | This method returns a hash of name/value pairs of element information from | ||
| 425 | the XML::Template configuration file. The first two parameters, | ||
| 426 | respectively, are the name of the namespace in which the element resides | ||
| 427 | and the name of the element for which information is desired. The | ||
| 428 | remaining parameters name the configuration elements to include in the | ||
| 429 | hash. If no such parameters are given, all host configuration elements | ||
| 430 | are included. Currently, this include C<content> and C<nestedin>. | ||
| 431 | |||
| 432 | =cut | ||
| 433 | |||
| 434 | sub get_element_info { | ||
| 435 | my $self = shift; | ||
| 436 | my ($namespace, $name, @elements) = @_; | ||
| 437 | |||
| 438 | @elements = qw(content nestedin) if ! scalar (@elements); | ||
| 439 | return $self->get_info ( | ||
| 440 | "/xml-template/namespaces/namespace[\@name='$namespace']/element[\@name='$name']", | ||
| 441 | @elements); | ||
| 442 | } | ||
| 443 | |||
| 444 | =pod | ||
| 445 | |||
| 446 | =head2 get_attribs | ||
| 447 | |||
| 448 | my $attribs = $self->get_attribs ($namespace, $element, $attrib); | ||
| 449 | |||
| 450 | This method returns an array of attribute names for an element. The first | ||
| 451 | parameter specifies the namespace in which the element resides. The | ||
| 452 | second parameter is the name of the element. | ||
| 453 | |||
| 454 | =cut | ||
| 455 | |||
| 456 | sub get_attribs { | ||
| 457 | my $self = shift; | ||
| 458 | my ($namespace, $element) = @_; | ||
| 459 | |||
| 460 | my @attribs; | ||
| 461 | my @nodes = $self->{_config}->findnodes ("/xml-template/namespaces/namespace[\@name='$namespace']/element[\@name='$element']/attrib/\@name"); | ||
| 462 | foreach my $node (@nodes) { | ||
| 463 | push (@attribs, $node->string_value); | ||
| 464 | } | ||
| 465 | return @attribs; | ||
| 466 | |||
| 467 | return undef; | ||
| 468 | } | ||
| 469 | |||
| 470 | =pod | ||
| 471 | |||
| 472 | =head2 get_attrib_info | ||
| 473 | |||
| 474 | my $attrib_info = $self->get_attrib_info ($namespace, $element, $attrib); | ||
| 475 | my $attrib_info = $self->get_attrib_info ($namespace, $element, $attrib, | ||
| 476 | 'parse'); | ||
| 477 | |||
| 478 | This method returns a hash of name/value pairs of attribute information | ||
| 479 | from the XML::Template configuration file. The first three parameters, | ||
| 480 | respectively, are the namespace in which the associated element resides, | ||
| 481 | the attribute's associated element, and the name of the attribute for | ||
| 482 | which information is desired. The remaining parameters name the | ||
| 483 | configuration elements to include in the hash. If no such parameters are | ||
| 484 | given, all host configuration elements are included. Currently, this | ||
| 485 | include C<requires>, C<parse>, C<parser>, and C<type>. | ||
| 486 | |||
| 487 | =cut | ||
| 488 | |||
| 489 | sub get_attrib_info { | ||
| 490 | my $self = shift; | ||
| 491 | my ($namespace, $element, $name, @elements) = @_; | ||
| 492 | |||
| 493 | @elements = qw(required parse parser type) if ! scalar (@elements); | ||
| 494 | return $self->get_info ( | ||
| 495 | "/xml-template/namespaces/namespace[\@name='$namespace']/element[\@name='$element']/attrib[\@name='$name']", | ||
| 496 | @elements); | ||
| 497 | } | ||
| 498 | |||
| 499 | =pod | ||
| 500 | |||
| 501 | =head1 AUTHOR | ||
| 502 | |||
| 503 | Jonathan Waxman | ||
| 504 | <jowaxman@bbl.med.upenn.edu> | ||
| 505 | |||
| 506 | =head1 COPYRIGHT | ||
| 507 | |||
| 508 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 509 | All rights reserved. | ||
| 510 | |||
| 511 | This program is free software; you can redistribute it and/or | ||
| 512 | modify it under the same terms as Perl itself. | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | |||
| 517 | 1; | ||