| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XML::XMLWriter - Module for creating a XML document object oriented | ||
| 4 | with on the fly validating towards the given DTD. | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | ###################################################################### | ||
| 9 | |||
| 10 | package XML::XMLWriter; | ||
| 11 | require 5.8.0; | ||
| 12 | |||
| 13 | # Copyright (c) 2003, Moritz Sinn. This module is free software; | ||
| 14 | # you can redistribute it and/or modify it under the terms of the | ||
| 15 | # GNU GENERAL PUBLIC LICENSE, see COPYING for more information. | ||
| 16 | |||
| 17 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | $VERSION = '0.1'; | ||
| 20 | |||
| 21 | ###################################################################### | ||
| 22 | |||
| 23 | =head1 DEPENDENCIES | ||
| 24 | |||
| 25 | =head2 Perl Version | ||
| 26 | |||
| 27 | 5.004 | ||
| 28 | |||
| 29 | =head2 Standard Modules | ||
| 30 | |||
| 31 | Carp 1.01 | ||
| 32 | Encode 1.98 | ||
| 33 | |||
| 34 | =head2 Nonstandard Modules | ||
| 35 | |||
| 36 | XML::ParseDTD 0.1.3 | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | ###################################################################### | ||
| 41 | |||
| 42 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 1 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 0 | ▶ | use XML::ParseDTD; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 45 | use XML::XMLWriter::Element; | ||
| 46 | |||
| 47 | ###################################################################### | ||
| 48 | |||
| 49 | =head1 SYNOPSIS | ||
| 50 | |||
| 51 | =head2 Example Code | ||
| 52 | |||
| 53 | #!/usr/bin/perl | ||
| 54 | |||
| 55 | use XML::XMLWriter; | ||
| 56 | |||
| 57 | my @data=(['Name', 'Adress', 'Email', 'Sex'], | ||
| 58 | ['Herbert', 'BeerAvenue 45', 'herbert@names.org', 'Male'], | ||
| 59 | ['Anelise', 'SchmidtStreet 21', 'foo@bar.com', 'Female'], | ||
| 60 | ['XYZ', 'ZYX', 'ZY', 'XZ'], | ||
| 61 | ['etc...']); | ||
| 62 | |||
| 63 | my $doc = new XML::XMLWriter(system => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd', | ||
| 64 | public => '-//W3C//DTD XHTML 1.0 Transitional//EN'); | ||
| 65 | |||
| 66 | my $html = $doc->createRoot; | ||
| 67 | $html->head->title->_pcdata('A Table'); | ||
| 68 | my $body = $html->body; | ||
| 69 | $body->h1->_pcdata('Here is a table!'); | ||
| 70 | my $table = $body->table({align => 'center', cellspacing => 1, cellpadding => 2, border => 1}); | ||
| 71 | for(my $i=0; $i<@data; $i++) { | ||
| 72 | my $tr = $table->tr; | ||
| 73 | foreach $_ (@{$data[$i]}) { | ||
| 74 | $i==0 ? $tr->th->_pcdata($_) : $tr->td->_pcdata($_); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | $body->b->_pcdata("that's it!"); | ||
| 78 | $doc->print(); | ||
| 79 | |||
| 80 | =head2 Example Output | ||
| 81 | |||
| 82 | <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?> | ||
| 83 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 84 | <html><head><title>A Table</title></head><body><h1>Here is a table!</h1><table align="center" cellspacing="1" cellpadding="2" border="1"><tr><th>Name</th><th>Adress</th><th>Email</th><th>Sex</th></tr><tr><td>Herbert</td><td>BeerAvenue 45</td><td>herbert@names.org</td><td>Male</td></tr><tr><td>Anelise</td><td>SchmidtStree 21</td><td>foo@bar.com</td><td>Female</td></tr><tr><td>XYZ</td><td>ZYX</td><td>ZY</td><td>XZ</td></tr><tr><td>etc...</td></tr></table><b>that's it!</b></body></html> | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | XMLWriter is a Perl 5 object class, its purpose is to make writing XML | ||
| 89 | documents easier, cleaner, safer and standard conform. Its easier | ||
| 90 | because of the object oriented way XML documents are written with | ||
| 91 | XMLWriter. Its cleaner because of the simple but logical API and its | ||
| 92 | safe and standard conform because of the automatically done checking | ||
| 93 | against the the DTD. | ||
| 94 | |||
| 95 | But still: it might be a matter of taste whether one finds XMLWriter | ||
| 96 | usefull or not and it probably has some bugs (i would appreciate a lot | ||
| 97 | if you report them to me), many usefull features are missing, not | ||
| 98 | implemented or not even thought of and perhaps the API with all its | ||
| 99 | simpleness might be confusing though. So please tell me your opinion | ||
| 100 | and tell me the way how you would make XMLWriter better. Its not so | ||
| 101 | easy to develop a good API for this matter. | ||
| 102 | |||
| 103 | XMLWriter contains 3 packages: XMLWriter.pm which gives you the | ||
| 104 | document object, Element.pm which provides the element/tag objects and | ||
| 105 | PCData.pm which represents the parsed character data the document | ||
| 106 | contains. There'll probably come more objects in feature releases. | ||
| 107 | The most interesting class is Element.pm. It provides some methods you | ||
| 108 | can call on every document element, but besides those methods it uses | ||
| 109 | the I<AUTOLOAD> feature of perl to expect every not known method name | ||
| 110 | to be the name of a tag that should be added to the list of child tags | ||
| 111 | of the element the method is called on. So calling C<$html-E<gt>head> | ||
| 112 | will simply add a new element (the head element) to the list of child | ||
| 113 | tags of the html element. The head object is returned. Have a look at | ||
| 114 | the examples for better understanding. You should also read the POD of | ||
| 115 | Element.pm and PCdata.pm. | ||
| 116 | |||
| 117 | =head1 USING XML::XMLWriter | ||
| 118 | |||
| 119 | =head2 Encoding | ||
| 120 | |||
| 121 | All methods expect the data you pass them to be encoded in UTF8. So if | ||
| 122 | you take data with diffrent encoding call | ||
| 123 | I<Encode::decode(yourencoding,$data)> to make it UTF8. Finally the | ||
| 124 | whole document will be encoded in the document encoding (default: | ||
| 125 | UTF8) before being returned. | ||
| 126 | |||
| 127 | =head2 The Constructor | ||
| 128 | |||
| 129 | =head3 new ([ %conf ]) | ||
| 130 | |||
| 131 | The only argument can be a hash setting some configuration options: | ||
| 132 | |||
| 133 | =over | ||
| 134 | |||
| 135 | =item | ||
| 136 | |||
| 137 | version - XML version, defaults to I<1.0>, shouldn't be changed. | ||
| 138 | |||
| 139 | =item | ||
| 140 | |||
| 141 | encoding - Character encoding. Defaults to I<UTF8>. | ||
| 142 | |||
| 143 | =item | ||
| 144 | |||
| 145 | public - PUBLIC IDENTIFIER. Defaults to I<-//W3C//DTD XHTML 1.0 | ||
| 146 | Strict//EN>. | ||
| 147 | |||
| 148 | =item | ||
| 149 | |||
| 150 | system - SYSTEM IDENTIFIER (must always be a path/url pointing | ||
| 151 | to a valid dtd). Defaults to | ||
| 152 | I<http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>. | ||
| 153 | |||
| 154 | =item | ||
| 155 | |||
| 156 | standalone - sets the I<standalone> option of the XML | ||
| 157 | declaration. Defaults to I<no>, the only other possible value is | ||
| 158 | I<yes>. | ||
| 159 | |||
| 160 | =item | ||
| 161 | |||
| 162 | root - sets the name of the root element. Defaults to I<html>. | ||
| 163 | |||
| 164 | =item | ||
| 165 | |||
| 166 | rootArgs - sets the list of arguments and their values for the | ||
| 167 | root element. Defaults to I<{}> (empty hash reference). | ||
| 168 | |||
| 169 | =item | ||
| 170 | |||
| 171 | intend - sets whether intentation should be done and if so how | ||
| 172 | many spaces per level. Defaults to 0, is not implemented yet, that | ||
| 173 | means setting it won't have any effect. | ||
| 174 | |||
| 175 | =item | ||
| 176 | |||
| 177 | checklm - the value of this option is passed to XML::ParseDTD | ||
| 178 | for setting its I<checklm> parameter. Please the the documentation of | ||
| 179 | XML::ParseDTD for more information. Defaults to I<-1>. | ||
| 180 | |||
| 181 | =back | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | ###################################################################### | ||
| 186 | |||
| 187 | sub new { | ||
| 188 | my ($class, %conf) = @_; | ||
| 189 | my $self = bless({}, ref($class) || $class); | ||
| 190 | $self->{Conf} = { | ||
| 191 | version => '1.0', | ||
| 192 | encoding => 'UTF8', | ||
| 193 | #encoding => 'ISO-8859-15', | ||
| 194 | public => '-//W3C//DTD XHTML 1.0 Strict//EN', | ||
| 195 | system => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd', | ||
| 196 | standalone => 'no', | ||
| 197 | root => 'html', | ||
| 198 | rootArgs => {}, | ||
| 199 | #intend => 2, | ||
| 200 | intend => 0, | ||
| 201 | checklm => -1, | ||
| 202 | }; | ||
| 203 | local $_; | ||
| 204 | foreach $_ (keys(%conf)) { | ||
| 205 | $self->{Conf}->{$_} = $conf{$_}; | ||
| 206 | } | ||
| 207 | return $self; | ||
| 208 | } | ||
| 209 | |||
| 210 | ###################################################################### | ||
| 211 | |||
| 212 | =head2 Methods | ||
| 213 | |||
| 214 | =head3 createRoot ([$rootelem, %arguments, $character_data]) | ||
| 215 | |||
| 216 | Creates the root element and returns a I<XML::XMLWriter::Element> | ||
| 217 | object representing it. | ||
| 218 | |||
| 219 | Possible parameters: | ||
| 220 | |||
| 221 | =over | ||
| 222 | |||
| 223 | =item | ||
| 224 | |||
| 225 | 1. The root elements name. Defaults to the value of the I<root> | ||
| 226 | configuration option (see the C<new> method). | ||
| 227 | |||
| 228 | =item | ||
| 229 | |||
| 230 | 2. A hash of arguments for the root element and their | ||
| 231 | values. Defaults to the value of the I<rootArgs> configuration option | ||
| 232 | (see the C<new> method). | ||
| 233 | |||
| 234 | =item | ||
| 235 | |||
| 236 | 3. A string of character data which will be appended right after | ||
| 237 | the start tag. Defaults to undef, which means no data will be added. | ||
| 238 | |||
| 239 | =back | ||
| 240 | |||
| 241 | Instead of passing the third argument you can also just do a | ||
| 242 | C<$root-E<gt>_pcdata(yourdata)>, its exactly the same. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | ###################################################################### | ||
| 247 | |||
| 248 | sub createRoot { | ||
| 249 | my ($self,$root,%args,$text) = @_; | ||
| 250 | $self->{dtd} = XML::ParseDTD->new($self->{Conf}->{system},checklm => $self->{Conf}->{checklm}) if($self->{Conf}->{system}); | ||
| 251 | $self->{root} = XML::XMLWriter::Element->new(($root||$self->{Conf}->{root}),$self->{dtd},(\%args||$self->{Conf}->{rootArgs}),$text); | ||
| 252 | return $self->{root}; | ||
| 253 | } | ||
| 254 | |||
| 255 | ###################################################################### | ||
| 256 | |||
| 257 | =head3 get () | ||
| 258 | |||
| 259 | Returns the XML document as a string. Every elements child list is | ||
| 260 | checked and a warning is produce if its not allowed by the DTD. | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | ###################################################################### | ||
| 265 | |||
| 266 | sub get { | ||
| 267 | my ($self) = @_; | ||
| 268 | local $_ = '<?xml version="' . $self->{Conf}->{version} . '" encoding="' . $self->{Conf}->{encoding} . '" standalone="' . $self->{Conf}->{standalone} . '"?>' . "\n"; | ||
| 269 | $_ .= '<!DOCTYPE ' . $self->{Conf}->{root} . ($self->{Conf}->{public} ? ' PUBLIC "' . $self->{Conf}->{public} . '"' : '') . ($self->{Conf}->{system} ? ' SYSTEM "' . $self->{Conf}->{system} . '"' : '') . ">\n"; | ||
| 270 | return encode($self->{Conf}->{encoding},$_ . $self->{root}->_get()); | ||
| 271 | } | ||
| 272 | |||
| 273 | ###################################################################### | ||
| 274 | |||
| 275 | =head3 print () | ||
| 276 | |||
| 277 | Prints the XML document to STDOUT. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | ###################################################################### | ||
| 282 | |||
| 283 | sub print { | ||
| 284 | my ($self,$html) = @_; | ||
| 285 | print $self->get($html); | ||
| 286 | } | ||
| 287 | |||
| 288 | ###################################################################### | ||
| 289 | |||
| 290 | =head3 get_dtd () | ||
| 291 | |||
| 292 | Returns the internally used C<XML::ParseDTD> object. | ||
| 293 | |||
| 294 | =cut | ||
| 295 | |||
| 296 | ###################################################################### | ||
| 297 | |||
| 298 | sub get_dtd { | ||
| 299 | my($self) = @_; | ||
| 300 | return $self->{dtd}; | ||
| 301 | } | ||
| 302 | |||
| 303 | return 1; | ||
| 304 | |||
| 305 | __END__ | ||