| 1 | ############################################################################### | ||
| 2 | # XML::Template::Document | ||
| 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 | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Document; | ||
| 11 | use base (XML::Template::Base); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | |||
| 15 | |||
| 16 | =pod | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | XML::Template::Document - Module to encapsulate a parsed or unparsed XML | ||
| 21 | document. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use XML::Template::Document; | ||
| 26 | |||
| 27 | my $document = XML::Template::Document->new (XML => $xml); | ||
| 28 | $document->code ($code) if ! $document->compiled; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This module defines an object class that represents a parsed or unparsed | ||
| 33 | XML document. Typically, in L<XML::Template::Process>, one in a series of | ||
| 34 | load objects will return a XML::Template::Document object. If the | ||
| 35 | document is not parsed (i.e., no code has been generated), the document is | ||
| 36 | passed to L<XML::Template::Parser> to be parsed. The parsed | ||
| 37 | XML::Template::Document object is then passed to each in a series of put | ||
| 38 | objects to store the document. If document caching is turned on, the | ||
| 39 | first object in the load and put lists is a L<XML::Template::Cache> | ||
| 40 | object. | ||
| 41 | |||
| 42 | =head1 CONSTRUCTOR | ||
| 43 | |||
| 44 | The constructor returns a reference to a new document object or undef if | ||
| 45 | an error occurred. If undef is returned, you can use the method C<error> | ||
| 46 | to retrieve the error. For instance: | ||
| 47 | |||
| 48 | my $document = XML::Templatte::Document->new (%config) | ||
| 49 | || die XML::Template::Document->error; | ||
| 50 | |||
| 51 | A list of named configuration parameters may be passed to the constructor. | ||
| 52 | The following named configuration parameters are supported by this module: | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item XML | ||
| 57 | |||
| 58 | The unparsed XML document. | ||
| 59 | |||
| 60 | =item Code | ||
| 61 | |||
| 62 | The Perl code that will generate the output of the XML document. | ||
| 63 | |||
| 64 | =item Source | ||
| 65 | |||
| 66 | The original source of the document. This is used by the file caching | ||
| 67 | system to check if a document has been updated. The format is | ||
| 68 | |||
| 69 | C<type>:C<source_info> | ||
| 70 | |||
| 71 | Where C<type> can be | ||
| 72 | |||
| 73 | =over 4 | ||
| 74 | |||
| 75 | =item file | ||
| 76 | |||
| 77 | Indicates the original source is a file. C<source_info> is the full | ||
| 78 | filespec of the original document. | ||
| 79 | |||
| 80 | =item source | ||
| 81 | |||
| 82 | Indicates the original document comes from a data source. | ||
| 83 | C<source_info> is | ||
| 84 | |||
| 85 | C<sourcename>:C<data_source_info> | ||
| 86 | |||
| 87 | where C<sourcename> is the name of the document's data source entry and, | ||
| 88 | C<data_source_info> is specific to the type of source. For a DBI | ||
| 89 | source, it is a table. | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub new { | ||
| 96 | my $proto = shift; | ||
| 97 | my %params = @_; | ||
| 98 | |||
| 99 | my $class = ref ($proto) || $proto; | ||
| 100 | my $self = {}; | ||
| 101 | bless ($self, $class); | ||
| 102 | |||
| 103 | $self->{_xml} = $params{XML}; | ||
| 104 | $self->{_code} = $params{Code}; | ||
| 105 | $self->{_source} = $params{Source}; | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head1 PUBLIC METHODS | ||
| 111 | |||
| 112 | =head2 compiled | ||
| 113 | |||
| 114 | my $iscompiled = $document->compiled; | ||
| 115 | |||
| 116 | This method returns 1 if the document is compiled (i.e., code is | ||
| 117 | stored) and 0 if otherwise. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub compiled { | ||
| 122 | my $self = shift; | ||
| 123 | |||
| 124 | return defined $self->{_code}; | ||
| 125 | } | ||
| 126 | |||
| 127 | =pod | ||
| 128 | |||
| 129 | =head2 code | ||
| 130 | |||
| 131 | $document->code ($code); | ||
| 132 | my $code = $document->code; | ||
| 133 | |||
| 134 | This method stores and returns the Perl code that generates the document | ||
| 135 | output. The first parameter is a string containg the Perl code to store. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub code { | ||
| 140 | my $self = shift; | ||
| 141 | my $code = shift; | ||
| 142 | |||
| 143 | if (defined $code) { | ||
| 144 | $self->{_code} = $code; | ||
| 145 | delete $self->{_xml}; | ||
| 146 | } | ||
| 147 | return $self->{_code}; | ||
| 148 | } | ||
| 149 | |||
| 150 | =pod | ||
| 151 | |||
| 152 | =head2 xml | ||
| 153 | |||
| 154 | $document->xml ($xml); | ||
| 155 | my $xml = $document->xml; | ||
| 156 | |||
| 157 | This method stores and returns the unparsed XML document. The first | ||
| 158 | parameter is a string containg the XML. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub xml { | ||
| 163 | my $self = shift; | ||
| 164 | my $xml = shift; | ||
| 165 | |||
| 166 | $self->{_xml} = $xml if defined $xml; | ||
| 167 | return $self->{_xml}; | ||
| 168 | } | ||
| 169 | |||
| 170 | =pod | ||
| 171 | |||
| 172 | =head2 source | ||
| 173 | |||
| 174 | $document->source ($source); | ||
| 175 | my $source = $document->source; | ||
| 176 | |||
| 177 | This method stores and returns the original document source. The first | ||
| 178 | argument is a string containing the document source as described above. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub source { | ||
| 183 | my $self = shift; | ||
| 184 | my $source = shift; | ||
| 185 | |||
| 186 | $self->{_source} = $source if defined $source; | ||
| 187 | return $self->{_source}; | ||
| 188 | } | ||
| 189 | |||
| 190 | =pod | ||
| 191 | |||
| 192 | =head1 AUTHOR | ||
| 193 | |||
| 194 | Jonathan Waxman | ||
| 195 | <jowaxman@bbl.med.upenn.edu> | ||
| 196 | |||
| 197 | =head1 COPYRIGHT | ||
| 198 | |||
| 199 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 200 | All rights reserved. | ||
| 201 | |||
| 202 | This program is free software; you can redistribute it and/or | ||
| 203 | modify it under the same terms as Perl itself. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | |||
| 208 | 1; |