| 1 | package XML::Toolset::DOM; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION @ISA); | ||
| 5 | |||
| 6 | use XML::Toolset; | ||
| 7 | |||
| 8 | $VERSION = sprintf"%d.%03d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/; | ||
| 9 | @ISA = ("XML::Toolset"); | ||
| 10 | |||
| 11 | sub get_first_tag { | ||
| 12 | &App::sub_entry if ($App::trace); | ||
| 13 | my ($self, $doc, $xpath) = @_; | ||
| 14 | my $dom = $self->doc2dom($doc); | ||
| 15 | my $node = $xpath ? $self->_dom_get_value_node($dom, $xpath) : $dom; | ||
| 16 | my ($child_node, $node_type_name, $first_tag); | ||
| 17 | my $child_nodes = $node->getChildNodes(); | ||
| 18 | my $length = $child_nodes->getLength(); | ||
| 19 | for (my $i = 0; $i < $length; $i++) { | ||
| 20 | $child_node = $child_nodes->item($i); | ||
| 21 | $node_type_name = $self->node_type_name($child_node->getNodeType()); | ||
| 22 | if ($node_type_name eq "ELEMENT_NODE") { | ||
| 23 | $first_tag = $child_node->getNodeName(); | ||
| 24 | last; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | &App::sub_exit($first_tag) if ($App::trace); | ||
| 28 | return($first_tag); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub get_value { | ||
| 32 | &App::sub_entry if ($App::trace); | ||
| 33 | my ($self, $doc, $xpath) = @_; | ||
| 34 | my $dom = $self->doc2dom($doc); | ||
| 35 | my $node = $self->_dom_get_value_node($dom, $xpath); | ||
| 36 | my $value = $node ? $node->getNodeValue() : undef; | ||
| 37 | &App::sub_exit($value) if ($App::trace); | ||
| 38 | return($value); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub set_value { | ||
| 42 | &App::sub_entry if ($App::trace); | ||
| 43 | my ($self, $doc, $xpath, $value) = @_; | ||
| 44 | my $dom = $self->doc2dom($doc); | ||
| 45 | # print "set_value($doc, $xpath, $value) dom=[$dom]\n"; | ||
| 46 | my ($node); | ||
| 47 | eval { | ||
| 48 | $node = $self->_dom_get_value_node($dom, $xpath, undef, 1); | ||
| 49 | }; | ||
| 50 | if ($@) { | ||
| 51 | print "ERROR: ", $@; | ||
| 52 | } | ||
| 53 | # print "set_value() node=[$node]\n"; | ||
| 54 | if ($node) { | ||
| 55 | #$self->print_node($node); | ||
| 56 | my $old_text_node = $node->getFirstChild(); | ||
| 57 | my $text_node = $dom->createTextNode($value); | ||
| 58 | if ($old_text_node) { | ||
| 59 | $node->replaceChild($text_node, $old_text_node); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $node->appendChild($text_node); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | &App::sub_exit() if ($App::trace); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub transform { | ||
| 69 | &App::sub_entry if ($App::trace); | ||
| 70 | my ($self, $doc, $xform) = @_; | ||
| 71 | die "XML::Toolset::Base::transform must be overridden. (XML::Toolset::Base is an abstract class.)"; | ||
| 72 | &App::sub_exit() if ($App::trace); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub to_string { | ||
| 76 | &App::sub_entry if ($App::trace); | ||
| 77 | my ($self, $doc) = @_; | ||
| 78 | my $xml = $self->doc2xml($doc); | ||
| 79 | &App::sub_exit($xml) if ($App::trace); | ||
| 80 | return($xml); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub parse_document { | ||
| 84 | &App::sub_entry if ($App::trace); | ||
| 85 | my ($self, $doc) = @_; | ||
| 86 | |||
| 87 | my $xml = $self->doc2xml($doc); | ||
| 88 | $self->{parser} = undef; | ||
| 89 | |||
| 90 | die "parse_document() called with no xml data\n" unless defined $xml and length $xml > 0; | ||
| 91 | |||
| 92 | my $parser = $self->parser(); | ||
| 93 | my ($dom); | ||
| 94 | eval { | ||
| 95 | $dom = $parser->parse($xml); | ||
| 96 | }; | ||
| 97 | if ($@) { | ||
| 98 | $self->add_error({message => $@}); | ||
| 99 | } | ||
| 100 | $self->{parser} = $parser; | ||
| 101 | &App::sub_exit($dom) if ($App::trace); | ||
| 102 | return($dom); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub format_dom_to_xml { | ||
| 106 | &App::sub_entry if ($App::trace); | ||
| 107 | my ($self, $dom) = @_; | ||
| 108 | my $xml = $dom->toString(); | ||
| 109 | &App::sub_exit($xml) if ($App::trace); | ||
| 110 | return($xml); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub print_node { | ||
| 114 | &App::sub_entry if ($App::trace); | ||
| 115 | my ($self, $node, $indent, $no_children) = @_; | ||
| 116 | $indent ||= 0; | ||
| 117 | |||
| 118 | my $node_type_name = $self->node_type_name($node->getNodeType()); | ||
| 119 | my $value = $node->getNodeValue(); | ||
| 120 | $value = "" if (!defined $value); | ||
| 121 | #if ($node_type_name eq "TEXT_NODE" && $value =~ /^[ \n\t]*$/s) { | ||
| 122 | # do nothing | ||
| 123 | #} | ||
| 124 | #else { | ||
| 125 | print "", (" " x $indent); | ||
| 126 | $value =~ s/\n/\\n/g; | ||
| 127 | printf("%s: %s=%s\n", $node_type_name, $node->getNodeName(), $value); | ||
| 128 | #} | ||
| 129 | |||
| 130 | my ($child_node); | ||
| 131 | my $attributes = $node->getAttributes(); | ||
| 132 | if ($attributes) { | ||
| 133 | my $length = $attributes->getLength(); | ||
| 134 | for (my $i = 0; $i < $length; $i++) { | ||
| 135 | $child_node = $attributes->item($i); | ||
| 136 | $self->print_node($child_node, $indent + 1, 1); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | #my $content = $node->getTextContent(); | ||
| 141 | #$content =~ s/\n/\\n/g; | ||
| 142 | #if (defined $content && $content ne "") { | ||
| 143 | # print "", (" " x $indent), "[$content]\n"; | ||
| 144 | #} | ||
| 145 | |||
| 146 | if (!$no_children) { | ||
| 147 | my ($child_nodes, $child_node); | ||
| 148 | $child_nodes = $node->getChildNodes(); | ||
| 149 | my $length = $child_nodes->getLength(); | ||
| 150 | for (my $i = 0; $i < $length; $i++) { | ||
| 151 | # print "node $i of $length\n"; | ||
| 152 | $child_node = $child_nodes->item($i); | ||
| 153 | $self->print_node($child_node, $indent + 1); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | &App::sub_exit() if ($App::trace); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub node_type_name { | ||
| 160 | &App::sub_entry if ($App::trace); | ||
| 161 | my ($self, $node_type) = @_; | ||
| 162 | my %node_type_name = ( | ||
| 163 | 1 => "ELEMENT_NODE", | ||
| 164 | 2 => "ATTRIBUTE_NODE", | ||
| 165 | 3 => "TEXT_NODE", | ||
| 166 | 4 => "CDATA_SECTION_NODE", | ||
| 167 | 5 => "ENTITY_REFERENCE_NODE", | ||
| 168 | 6 => "ENTITY_NODE", | ||
| 169 | 7 => "PROCESSING_INSTRUCTION_NODE", | ||
| 170 | 8 => "COMMENT_NODE", | ||
| 171 | 9 => "DOCUMENT_NODE", | ||
| 172 | 10 => "DOCUMENT_TYPE_NODE", | ||
| 173 | 11 => "DOCUMENT_FRAGMENT_NODE", | ||
| 174 | 12 => "NOTATION_NODE", | ||
| 175 | ); | ||
| 176 | my $node_type_name = $node_type_name{$node_type} || "UNKNOWN"; | ||
| 177 | &App::sub_exit($node_type_name) if ($App::trace); | ||
| 178 | return($node_type_name); | ||
| 179 | } | ||
| 180 | |||
| 181 | # http://www.w3.org/TR/xpath - Abbreviated Syntax | ||
| 182 | # * para selects the para element children of the context node | ||
| 183 | # * * selects all element children of the context node | ||
| 184 | # * text() selects all text node children of the context node | ||
| 185 | # * @name selects the name attribute of the context node | ||
| 186 | # * @* selects all the attributes of the context node | ||
| 187 | # * para[1] selects the first para child of the context node | ||
| 188 | # * para[last()] selects the last para child of the context node | ||
| 189 | # * */para selects all para grandchildren of the context node | ||
| 190 | # * /doc/chapter[5]/section[2] selects the second section of the fifth chapter of the doc | ||
| 191 | # * chapter//para selects the para element descendants of the chapter element children of the context node | ||
| 192 | # * //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context | ||
| 193 | # * //olist/item selects all the item elements in the same document as the context node that have an olist parent | ||
| 194 | # * . selects the context node | ||
| 195 | # * .//para selects the para element descendants of the context node | ||
| 196 | # * .. selects the parent of the context node | ||
| 197 | # * ../@lang selects the lang attribute of the parent of the context node | ||
| 198 | # * para[@type="warning"] selects all para children of the context node that have a type attribute with value warning | ||
| 199 | # * para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning | ||
| 200 | # * para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning | ||
| 201 | # * chapter[title="Introduction"] selects the chapter children that have one or more title children with string-value = Introduction | ||
| 202 | # * chapter[title] selects the chapter children of the context node that have one or more title children | ||
| 203 | # * employee[@secretary and @assistant] selects all employee children that have both a secretary attribute and an assistant attribute | ||
| 204 | #Location Paths | ||
| 205 | #[1] LocationPath ::= RelativeLocationPath | ||
| 206 | # | AbsoluteLocationPath | ||
| 207 | #[2] AbsoluteLocationPath ::= '/' RelativeLocationPath? | ||
| 208 | # | AbbreviatedAbsoluteLocationPath | ||
| 209 | #[3] RelativeLocationPath ::= Step | ||
| 210 | # | RelativeLocationPath '/' Step | ||
| 211 | # | AbbreviatedRelativeLocationPath | ||
| 212 | #Location Steps | ||
| 213 | #[4] Step ::= AxisSpecifier NodeTest Predicate* | ||
| 214 | # | AbbreviatedStep | ||
| 215 | #[5] AxisSpecifier ::= AxisName '::' | ||
| 216 | # | AbbreviatedAxisSpecifier | ||
| 217 | #Axes | ||
| 218 | #[6] AxisName ::= 'ancestor' | ||
| 219 | # | 'ancestor-or-self' | ||
| 220 | # | 'attribute' | ||
| 221 | # | 'child' | ||
| 222 | # | 'descendant' | ||
| 223 | # | 'descendant-or-self' | ||
| 224 | # | 'following' | ||
| 225 | # | 'following-sibling' | ||
| 226 | # | 'namespace' | ||
| 227 | # | 'parent' | ||
| 228 | # | 'preceding' | ||
| 229 | # | 'preceding-sibling' | ||
| 230 | # | 'self' | ||
| 231 | #A node test node() is true for any node of any type whatsoever. | ||
| 232 | #[7] NodeTest ::= NameTest | ||
| 233 | # | NodeType '(' ')' | ||
| 234 | # | 'processing-instruction' '(' Literal ')' | ||
| 235 | #Predicates | ||
| 236 | #[8] Predicate ::= '[' PredicateExpr ']' | ||
| 237 | #[9] PredicateExpr ::= Expr | ||
| 238 | #Abbreviations | ||
| 239 | #[10] AbbreviatedAbsoluteLocationPath ::= '//' RelativeLocationPath | ||
| 240 | #[11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' | ||
| 241 | #Step | ||
| 242 | #[12] AbbreviatedStep ::= '.' | ||
| 243 | # | '..' | ||
| 244 | #[13] AbbreviatedAxisSpecifier ::= '@'? | ||
| 245 | #Parentheses may be used for grouping. | ||
| 246 | #[14] Expr ::= OrExpr | ||
| 247 | #[15] PrimaryExpr ::= VariableReference | ||
| 248 | # | '(' Expr ')' | ||
| 249 | # | Literal | ||
| 250 | # | Number | ||
| 251 | # | FunctionCall | ||
| 252 | #[16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)* )? ')' | ||
| 253 | |||
| 254 | # This is the XML::DOM::XPath way | ||
| 255 | #sub _dom_get_value_node_std { | ||
| 256 | # my ($self, $dom, $xpath) = @_; | ||
| 257 | # | ||
| 258 | # my ($node); | ||
| 259 | # my @nodes = $dom->findnodes($xpath); | ||
| 260 | # $node = $nodes[0] if ($#nodes > -1); | ||
| 261 | # | ||
| 262 | # return($node); | ||
| 263 | #} | ||
| 264 | |||
| 265 | # This is our own version (which also works with Xerces) | ||
| 266 | sub _dom_get_value_node { | ||
| 267 | &App::sub_entry if ($App::trace); | ||
| 268 | my ($self, $dom, $xpath, $context_node, $create) = @_; | ||
| 269 | # print "_dom_get_value_node($dom, $xpath, $context_node, $create)\n"; | ||
| 270 | |||
| 271 | $context_node = $dom if ($xpath =~ s!^/!!); | ||
| 272 | $context_node = $dom if (!$context_node); | ||
| 273 | |||
| 274 | #$self->print_node($context_node); | ||
| 275 | |||
| 276 | my ($node, $child_nodes, $child_node, $new_node); | ||
| 277 | my ($i, $length, $found, $node_type_name, $nn); | ||
| 278 | my @xpath = split(/\//, $xpath); | ||
| 279 | $found = 0; | ||
| 280 | foreach my $node_name (@xpath) { | ||
| 281 | $found = 0; | ||
| 282 | # print "_dom_get_value_node() node_name=[$node_name]\n"; | ||
| 283 | if ($node_name =~ s!^@!!) { | ||
| 284 | $node_type_name = "ATTRIBUTE_NODE"; | ||
| 285 | $child_node = $context_node->getAttributeNode($node_name); | ||
| 286 | if ($child_node) { | ||
| 287 | $found = 1; | ||
| 288 | $context_node = $child_node; | ||
| 289 | } | ||
| 290 | # print "_dom_get_value_node() [attrib] node_name=[$node_name] node=[$child_node]\n"; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | $node_type_name = "ELEMENT_NODE"; | ||
| 294 | # print "_dom_get_value_node(a) [element] node_name=[$node_name]\n"; | ||
| 295 | $child_nodes = $context_node->getChildNodes(); | ||
| 296 | # print "_dom_get_value_node(b) [element] node_name=[$node_name]\n"; | ||
| 297 | $length = $child_nodes->getLength(); | ||
| 298 | # print "_dom_get_value_node(c) [element] node_name=[$node_name] children=$length\n"; | ||
| 299 | for ($i = 0; $i < $length; $i++) { | ||
| 300 | # print "_dom_get_value_node(d$i) [element] node_name=[$node_name]\n"; | ||
| 301 | $child_node = $child_nodes->item($i); | ||
| 302 | $nn = $child_node->getNodeName(); | ||
| 303 | # print "_dom_get_value_node(e$i) [element] node_name=[$node_name] vs [$nn]\n"; | ||
| 304 | if ($nn eq $node_name) { | ||
| 305 | # print "_dom_get_value_node(f$i) [element] node_name=[$node_name]\n"; | ||
| 306 | $found = 1; | ||
| 307 | $context_node = $child_node; | ||
| 308 | # print "Found Node [$node_name]\n"; | ||
| 309 | last; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | # print "_dom_get_value_node(g) [element] node_name=[$node_name]\n"; | ||
| 313 | } | ||
| 314 | # print "_dom_get_value_node(h) found=[$found] create=[$create]\n"; | ||
| 315 | if (!$found) { | ||
| 316 | if ($create) { | ||
| 317 | if ($node_type_name eq "ELEMENT_NODE") { | ||
| 318 | # print "_dom_get_value_node(i) element : 1\n"; | ||
| 319 | $new_node = $dom->createElement($node_name); | ||
| 320 | # print "_dom_get_value_node(i) element : 2 : context_node=[$context_node] new_node=[$new_node]\n"; | ||
| 321 | $context_node->appendChild($new_node); | ||
| 322 | # print "_dom_get_value_node(i) element : 3\n"; | ||
| 323 | } | ||
| 324 | elsif ($node_type_name eq "ATTRIBUTE_NODE") { | ||
| 325 | # print "_dom_get_value_node(i) attribute : 1\n"; | ||
| 326 | $context_node->setAttribute($node_name, ""); | ||
| 327 | # print "_dom_get_value_node(i) attribute : 2\n"; | ||
| 328 | $new_node = $context_node->getAttributeNode($node_name); | ||
| 329 | # print "_dom_get_value_node(i) attribute : 3\n"; | ||
| 330 | } | ||
| 331 | $context_node = $new_node; | ||
| 332 | $found = 1; | ||
| 333 | } | ||
| 334 | else { | ||
| 335 | last; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | # print "_dom_get_value_node(ENDLOOP)\n"; | ||
| 339 | } | ||
| 340 | if ($found) { | ||
| 341 | $node = $context_node; | ||
| 342 | } | ||
| 343 | # print "_dom_get_value_node() = [$node]\n"; | ||
| 344 | |||
| 345 | &App::sub_exit($node) if ($App::trace); | ||
| 346 | return($node); | ||
| 347 | } | ||
| 348 | |||
| 349 | 1; | ||
| 350 | |||
| 351 | =head1 NAME | ||
| 352 | |||
| 353 | XML::Toolset::DOM - An intermediate base class which defines methods which are shared in common between the DOM-based XML::Toolset implementations | ||
| 354 | |||
| 355 | =head1 SYNOPSIS | ||
| 356 | |||
| 357 | package XML::Toolset::XMLDOM; | ||
| 358 | use XML::Toolset::DOM; | ||
| 359 | @ISA = ("XML::Toolset::DOM"); | ||
| 360 | ... | ||
| 361 | |||
| 362 | =head1 DESCRIPTION | ||
| 363 | |||
| 364 | XML::Toolset::DOM is an intermediate base class which defines methods which are | ||
| 365 | shared in common between the DOM-based XML::Toolset implementations. | ||
| 366 | |||
| 367 | =head1 AUTHOR | ||
| 368 | |||
| 369 | Stephen Adkins <spadkins@gmail.com> | ||
| 370 | |||
| 371 | =head1 COPYRIGHT | ||
| 372 | |||
| 373 | (c) 2007 Stephen Adkins, for the purpose of making it Free. | ||
| 374 | This is Free Software. It is licensed under the same terms as Perl itself. | ||
| 375 | |||
| 376 | =cut |