| 1 | package XML::Generator::DOM; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | XML::Generator::DOM - XML::Generator subclass for producing DOM trees instead of strings. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use XML::Generator::DOM; | ||
| 10 | |||
| 11 | my $dg = XML::Generator::DOM->new(); | ||
| 12 | my $doc = $dg->xml($dg->xmlcmnt("Test document."), | ||
| 13 | $dg->foo({'baz' => 'bam'}, 42)); | ||
| 14 | print $doc->toString; | ||
| 15 | |||
| 16 | yields: | ||
| 17 | |||
| 18 | <?xml version="1.0" standalone="yes"?> | ||
| 19 | <!--Test document--> | ||
| 20 | <foo baz="bam">42</foo> | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | XML::Generator::DOM subclasses XML::Generator in order to produce DOM | ||
| 25 | trees instead of strings (see L<XML::Generator> and L<XML::DOM>). This | ||
| 26 | module is still experimental and its semantics might change. | ||
| 27 | |||
| 28 | Essentially, tag methods return XML::DOM::DocumentFragment objects, | ||
| 29 | constructed either from a DOM document passed into the constructor or | ||
| 30 | a default document that XML::Generator::DOM will automatically construct. | ||
| 31 | |||
| 32 | Calling the xml() method will return this automatically constructed | ||
| 33 | document and cause a fresh one to be constructed for future tag method | ||
| 34 | calls. If you passed in your own document, you may not call the xml() | ||
| 35 | method. | ||
| 36 | |||
| 37 | Below, we just note the remaining differences in semantics between | ||
| 38 | XML::Generator methods and XML::Generator::DOM methods. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use strict; | ||
| 43 | use Carp; | ||
| 44 | use XML::Generator (); | ||
| 45 | use base 'XML::Generator'; | ||
| 46 | use XML::DOM; | ||
| 47 | |||
| 48 | use vars qw( $AUTOLOAD $VERSION ); | ||
| 49 | |||
| 50 | $VERSION = '0.2'; | ||
| 51 | |||
| 52 | =head1 CONSTRUCTOR | ||
| 53 | |||
| 54 | These configuration options are accepted but have no effect on the | ||
| 55 | semantics of the returned object: escape, pretty, conformance and | ||
| 56 | empty. | ||
| 57 | |||
| 58 | =head1 TAG METHODS | ||
| 59 | |||
| 60 | Subsequently, tag method semantics are somewhat different for | ||
| 61 | this module compared to XML::Generator. The primary difference is | ||
| 62 | that tag method return XML::DOM::DocumentFragment objects. Namespace | ||
| 63 | and attribute processing remains the same, but remaining arguments to | ||
| 64 | tag methods must either be text or other XML::DOM::DocumentFragment | ||
| 65 | objects. No escape processing, syntax checking, or output control is | ||
| 66 | done; this is all left up to XML::DOM. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub new { | ||
| 71 | my $class = shift; | ||
| 72 | |||
| 73 | my $dom; | ||
| 74 | for (my $i = 0; $i < $#_; $i+=2) { | ||
| 75 | if ($_[$i] eq 'dom_document') { | ||
| 76 | $dom = $_[$i+1]; | ||
| 77 | unless (UNIVERSAL::isa($dom, 'XML::DOM::Document')) { | ||
| 78 | croak "argument to 'dom' option not an XML::DOM::Document object"; | ||
| 79 | } | ||
| 80 | splice @_, $i, 2; | ||
| 81 | last; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | if (ref $class) { | ||
| 86 | $AUTOLOAD = 'new'; | ||
| 87 | return $class->AUTOLOAD(@_); | ||
| 88 | } | ||
| 89 | |||
| 90 | my $this = $class->SUPER::new(@_); | ||
| 91 | |||
| 92 | $this->{'dom'} = $dom || XML::Generator::DOM::util::new_dom_root(); | ||
| 93 | return $this; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head1 SPECIAL TAGS | ||
| 97 | |||
| 98 | All special tags are available by default with XML::Generator::DOM; you don't | ||
| 99 | need to use 'conformance' => 'strict'. | ||
| 100 | |||
| 101 | =head2 xmlpi(@args) | ||
| 102 | |||
| 103 | Arguments will simply be concatenated and passed as the data to | ||
| 104 | the XML::DOM::ProcessingInstruction object that is returned. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub xmlpi { | ||
| 109 | my $this = shift; | ||
| 110 | my $root = $this->{dom}; | ||
| 111 | my $tgt = shift; | ||
| 112 | return $root->createProcessingInstruction($tgt, join '', @_); | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 xmlcmnt | ||
| 116 | |||
| 117 | Escaping of '--' is done by XML::DOM::Comment, which replaces both | ||
| 118 | hyphens with '-'. An XML::DOM::Comment object is returned. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub xmlcmnt { | ||
| 123 | my $this = shift; | ||
| 124 | my $root = $this->{dom}; | ||
| 125 | my $xml = join '', @_; | ||
| 126 | return $root->createComment($xml); | ||
| 127 | } | ||
| 128 | |||
| 129 | my $config = 'XML::Generator::util::config'; | ||
| 130 | |||
| 131 | =head2 xmldecl | ||
| 132 | |||
| 133 | Returns an XML::DOM::XMLDecl object. Respects 'version', 'encoding' | ||
| 134 | and 'dtd' settings in the object. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub xmldecl { | ||
| 139 | my $this = shift; | ||
| 140 | my $root = $this->{dom}; | ||
| 141 | |||
| 142 | my $version = $this->$config('version') || '1.0'; | ||
| 143 | my $encoding = $this->$config('encoding') || undef; | ||
| 144 | |||
| 145 | my $standalone = $this->xmldtd($this->$config('dtd')) | ||
| 146 | ? "no" : "yes"; | ||
| 147 | |||
| 148 | return $root->createXMLDecl($version, $encoding, $standalone) | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 xmldecl | ||
| 152 | |||
| 153 | Returns an XML::DOM::DocumentType object. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub xmldtd { | ||
| 158 | my($this, $dtd) = @_; | ||
| 159 | my $root = $this->{dom}; | ||
| 160 | $dtd ||= $this->$config('dtd'); | ||
| 161 | return unless $dtd && ref($dtd) eq "ARRAY"; | ||
| 162 | |||
| 163 | return $root->createDocumentType(@{ $dtd }); | ||
| 164 | } | ||
| 165 | |||
| 166 | =head2 xmlcdata | ||
| 167 | |||
| 168 | Returns an XML::DOM::CDATASection object. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub xmlcdata { | ||
| 173 | my $this = shift; | ||
| 174 | my $data = join '', @_; | ||
| 175 | my $root = $this->{dom}; | ||
| 176 | return $root->createCDATASection($data); | ||
| 177 | } | ||
| 178 | |||
| 179 | =head2 xml | ||
| 180 | |||
| 181 | As described above, xml() can only be used when dom_document was not | ||
| 182 | set in the object. The automatically created document will have its XML | ||
| 183 | Declaration set and the arguments to xml() will be appended to it. Then | ||
| 184 | a new DOM document is automatically generated and the old one is | ||
| 185 | returned. This is the only way to get a DOM document from this module. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub xml { | ||
| 190 | my $this = shift; | ||
| 191 | my $root = $this->{dom}; | ||
| 192 | |||
| 193 | if ($root != $XML::Generator::DOM::util::root) { | ||
| 194 | croak "xml() method not allowed when dom_document option specified"; | ||
| 195 | } | ||
| 196 | |||
| 197 | $this->{dom} = XML::Generator::DOM::util::new_dom_root(); | ||
| 198 | |||
| 199 | $root->setXMLDecl($this->xmldecl()); | ||
| 200 | |||
| 201 | $root->appendChild($_) for @_; | ||
| 202 | return $root; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub AUTOLOAD { | ||
| 206 | my $this = shift; | ||
| 207 | |||
| 208 | (my $tag = $AUTOLOAD) =~ s/.*:://;; | ||
| 209 | |||
| 210 | my $root = $this->{'dom'}; | ||
| 211 | |||
| 212 | my($namespace, $attr, @args) = $this->XML::Generator::util::parse_args(@_); | ||
| 213 | |||
| 214 | $namespace = $namespace->[1] ? $namespace->[1] . ':' : ''; | ||
| 215 | |||
| 216 | my $xml = $root->createDocumentFragment(); | ||
| 217 | |||
| 218 | my $node = $xml->appendChild($root->createElement("$namespace$tag")); | ||
| 219 | |||
| 220 | if ($attr) { | ||
| 221 | while (my($k, $v) = each %$attr) { | ||
| 222 | unless ($k =~ /^[^:]+:/) { | ||
| 223 | $k = "$namespace$k"; | ||
| 224 | } | ||
| 225 | $node->setAttribute($k, $v); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | for (@args) { | ||
| 230 | if (UNIVERSAL::isa($_, 'XML::DOM::Node')) { | ||
| 231 | $node->appendChild($_); | ||
| 232 | } else { | ||
| 233 | $node->appendChild($root->createTextNode($_)); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | return $xml; | ||
| 238 | } | ||
| 239 | |||
| 240 | package XML::Generator::DOM::util; | ||
| 241 | |||
| 242 | use XML::DOM; | ||
| 243 | use vars qw($root $parser); | ||
| 244 | |||
| 245 | $parser = XML::DOM::Parser->new; | ||
| 246 | |||
| 247 | sub new_dom_root { | ||
| 248 | $root = $parser->parse('<_/>'); | ||
| 249 | $root->removeChild($root->getFirstChild); | ||
| 250 | |||
| 251 | return $root; | ||
| 252 | } | ||
| 253 | |||
| 254 | 1; |