| 1 | ############################################################################### | ||
| 2 | # XML::Template::Element::Block::Load | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 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::Element::Block::Load; | ||
| 11 | use base qw(XML::Template::Base); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | |||
| 15 | |||
| 16 | =pod | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | XML::Template::Element::Block::Load - XML::Template loader module that | ||
| 21 | loads documents from a database table. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | This module implements an XML::Template document loader that loads | ||
| 26 | documents from a database table. | ||
| 27 | |||
| 28 | =head1 CONSTRUCTOR | ||
| 29 | |||
| 30 | A constructor method C<new> is provided by L<XML::Template::Base>. A list | ||
| 31 | of named configuration parameters may be passed to the constructor. The | ||
| 32 | constructor returns a reference to a new block loader object or under if | ||
| 33 | an error occurred. If undef is returned, you can use the method C<error> | ||
| 34 | to retrieve the error. For instance: | ||
| 35 | |||
| 36 | my $parser = XML::Template::Element::Block::Load->new (%config) | ||
| 37 | || die XML::Template::Element::Block::Load->error; | ||
| 38 | |||
| 39 | The following named configuration parameters may be passed to the | ||
| 40 | constructor: | ||
| 41 | |||
| 42 | =over 4 | ||
| 43 | |||
| 44 | =item StripPattern | ||
| 45 | |||
| 46 | A regular expression that matches a substring to remove from the document | ||
| 47 | name. | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =head1 PRIVATE METHODS | ||
| 52 | |||
| 53 | =head2 _init | ||
| 54 | |||
| 55 | This method is the internal initialization function called from | ||
| 56 | L<XML::Template::Base> when a new block loader object is created. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub _init { | ||
| 61 | my $self = shift; | ||
| 62 | my %params = @_; | ||
| 63 | |||
| 64 | print ref ($self) . "->_init\n" if $self->{_debug}; | ||
| 65 | |||
| 66 | $self->{_strip_pattern} = $params{StripPattern} if defined $params{StripPattern}; | ||
| 67 | |||
| 68 | $self->{_enabled} = 1; | ||
| 69 | |||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | =pod | ||
| 74 | |||
| 75 | =head2 load | ||
| 76 | |||
| 77 | my $document = $loader->load ($blockname); | ||
| 78 | |||
| 79 | This method loads an XML document from a database table and returns a new | ||
| 80 | L<XML::Template::Document> object. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub load { | ||
| 85 | my $self = shift; | ||
| 86 | my $blockname = shift; | ||
| 87 | |||
| 88 | print ref ($self) . "->load\n" if $self->{_debug}; | ||
| 89 | |||
| 90 | if (defined $self->{_strip_pattern}) { | ||
| 91 | if ($blockname =~ /$self->{_strip_pattern}/) { | ||
| 92 | $blockname = $1; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | # XXX support multiple keys. | ||
| 97 | |||
| 98 | my $namespace = 'http://syrme.net/xml-template/block/v1'; | ||
| 99 | my $source_mapping_info = $self->get_source_mapping_info (namespace => $namespace); | ||
| 100 | my $db = $self->get_source ($source_mapping_info->{source}); | ||
| 101 | if (defined $db) { | ||
| 102 | my ($xml) = $db->select (Field => 'body', | ||
| 103 | Table => $source_mapping_info->{table}, | ||
| 104 | Where => "$source_mapping_info->{keys}='$blockname'"); | ||
| 105 | |||
| 106 | return XML::Template::Document->new (XML => $xml, | ||
| 107 | Source => "source:$source_mapping_info->{source}:$source_mapping_info->{table}"); | ||
| 108 | } else { | ||
| 109 | return undef; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | =pod | ||
| 114 | |||
| 115 | =head1 AUTHOR | ||
| 116 | |||
| 117 | Jonathan Waxman | ||
| 118 | <jowaxman@bbl.med.upenn.edu> | ||
| 119 | |||
| 120 | =head1 COPYRIGHT | ||
| 121 | |||
| 122 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 123 | All rights reserved. | ||
| 124 | |||
| 125 | This program is free software; you can redistribute it and/or | ||
| 126 | modify it under the same terms as Perl itself. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | |||
| 131 | 1; |