| 1 | package XML::XPath::Node::Element; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent qw/XML::XPath::Node/; | ||
| 6 | |||
| 7 | our $VERSION = '1.49'; | ||
| 8 | |||
| 9 | package XML::XPath::Node::ElementImpl; | ||
| 10 | |||
| 11 | use parent qw/-norequire XML::XPath::NodeImpl XML::XPath::Node::Element/; | ||
| 12 | use XML::XPath::Node ':node_keys'; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, $tag, $prefix) = @_; | ||
| 16 | |||
| 17 | my $pos = XML::XPath::Node->nextPos; | ||
| 18 | my @vals; | ||
| 19 | @vals[node_global_pos, node_prefix, node_children, node_name, node_attribs] = | ||
| 20 | ($pos, $prefix, [], $tag, []); | ||
| 21 | |||
| 22 | my $self = \@vals; | ||
| 23 | bless $self, $class; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub getNodeType { ELEMENT_NODE } | ||
| 27 | |||
| 28 | sub isElementNode { 1; } | ||
| 29 | |||
| 30 | sub appendChild { | ||
| 31 | my $self = shift; | ||
| 32 | my $newnode = shift; | ||
| 33 | |||
| 34 | if (shift) { # called from internal to XML::XPath | ||
| 35 | # warn "AppendChild $newnode to $self\n"; | ||
| 36 | push @{$self->[node_children]}, $newnode; | ||
| 37 | $newnode->setParentNode($self); | ||
| 38 | $newnode->set_pos($#{$self->[node_children]}); | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | if (@{$self->[node_children]}) { | ||
| 42 | $self->insertAfter($newnode, $self->[node_children][-1]); | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | my $pos_number = $self->get_global_pos() + 1; | ||
| 46 | |||
| 47 | if (my $brother = $self->getNextSibling()) { # optimisation | ||
| 48 | if ($pos_number == $brother->get_global_pos()) { | ||
| 49 | $self->renumber('following::node()', +5); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | eval { | ||
| 54 | if ($pos_number == $self->findnodes('following::node()')->get_node(1)->get_global_pos()) { | ||
| 55 | $self->renumber('following::node()', +5); | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | push @{$self->[node_children]}, $newnode; | ||
| 61 | $newnode->setParentNode($self); | ||
| 62 | $newnode->set_pos($#{$self->[node_children]}); | ||
| 63 | $newnode->set_global_pos($pos_number); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub removeChild { | ||
| 69 | my ($self, $delnode) = @_; | ||
| 70 | |||
| 71 | my $pos = $delnode->get_pos; | ||
| 72 | # warn "removeChild: $pos\n"; | ||
| 73 | # warn "children: ", scalar @{$self->[node_children]}, "\n"; | ||
| 74 | |||
| 75 | # my $node = $self->[node_children][$pos]; | ||
| 76 | # warn "child at $pos is: $node\n"; | ||
| 77 | |||
| 78 | splice @{$self->[node_children]}, $pos, 1; | ||
| 79 | |||
| 80 | # warn "children now: ", scalar @{$self->[node_children]}, "\n"; | ||
| 81 | |||
| 82 | for (my $i = $pos; $i < @{$self->[node_children]}; $i++) { | ||
| 83 | # warn "Changing pos of child: $i\n"; | ||
| 84 | $self->[node_children][$i]->set_pos($i); | ||
| 85 | } | ||
| 86 | |||
| 87 | $delnode->del_parent_link; | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | sub appendIdElement { | ||
| 92 | my ($self, $val, $element) = @_; | ||
| 93 | # warn "Adding '$val' to ID hash\n"; | ||
| 94 | $self->[node_ids]{$val} = $element; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub DESTROY { | ||
| 98 | my $self = shift; | ||
| 99 | # warn "DESTROY ELEMENT: ", $self->[node_name], "\n"; | ||
| 100 | # warn "DESTROY ROOT\n" unless $self->[node_name]; | ||
| 101 | |||
| 102 | foreach my $kid ($self->getChildNodes) { | ||
| 103 | $kid && $kid->del_parent_link; | ||
| 104 | } | ||
| 105 | |||
| 106 | foreach my $attr ($self->getAttributeNodes) { | ||
| 107 | $attr && $attr->del_parent_link; | ||
| 108 | } | ||
| 109 | |||
| 110 | foreach my $ns ($self->getNamespaceNodes) { | ||
| 111 | $ns && $ns->del_parent_link; | ||
| 112 | } | ||
| 113 | |||
| 114 | # $self->[node_children] = undef; | ||
| 115 | # $self->[node_attribs] = undef; | ||
| 116 | # $self->[node_namespaces] = undef; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub getName { | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | $self->[node_name]; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub getTagName { | ||
| 126 | shift->getName(@_); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub getLocalName { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | my $local = $self->[node_name]; | ||
| 133 | $local =~ s/.*://; | ||
| 134 | return $local; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub getChildNodes { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | return wantarray ? @{$self->[node_children]} : $self->[node_children]; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub getChildNode { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | my ($pos) = @_; | ||
| 147 | if ($pos < 1 || $pos > @{$self->[node_children]}) { | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | return $self->[node_children][$pos - 1]; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub getFirstChild { | ||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | return unless @{$self->[node_children]}; | ||
| 157 | return $self->[node_children][0]; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub getLastChild { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | return unless @{$self->[node_children]}; | ||
| 164 | return $self->[node_children][-1]; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub getAttributeNode { | ||
| 168 | my ($self, $name) = @_; | ||
| 169 | |||
| 170 | my $attribs = $self->[node_attribs]; | ||
| 171 | foreach my $attr (@$attribs) { | ||
| 172 | return $attr if $attr->getName eq $name; | ||
| 173 | } | ||
| 174 | |||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub getAttribute { | ||
| 179 | my $self = shift; | ||
| 180 | |||
| 181 | my $attr = $self->getAttributeNode(@_); | ||
| 182 | if ($attr) { | ||
| 183 | return $attr->getValue; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | sub getAttributes { | ||
| 188 | my $self = shift; | ||
| 189 | |||
| 190 | if ($self->[node_attribs]) { | ||
| 191 | return wantarray ? @{$self->[node_attribs]} : $self->[node_attribs]; | ||
| 192 | } | ||
| 193 | |||
| 194 | return wantarray ? () : []; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub appendAttribute { | ||
| 198 | my $self = shift; | ||
| 199 | my $attribute = shift; | ||
| 200 | |||
| 201 | if (shift) { # internal call | ||
| 202 | push @{$self->[node_attribs]}, $attribute; | ||
| 203 | $attribute->setParentNode($self); | ||
| 204 | $attribute->set_pos($#{$self->[node_attribs]}); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | my $node_num; | ||
| 208 | if (@{$self->[node_attribs]}) { | ||
| 209 | $node_num = $self->[node_attribs][-1]->get_global_pos() + 1; | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | $node_num = $self->get_global_pos() + 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | eval { | ||
| 216 | if (@{$self->[node_children]}) { | ||
| 217 | if ($node_num == $self->[node_children][-1]->get_global_pos()) { | ||
| 218 | $self->renumber('descendant::node() | following::node()', +5); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | elsif ($node_num == | ||
| 222 | $self->findnodes('following::node()')->get_node(1)->get_global_pos()) { | ||
| 223 | $self->renumber('following::node()', +5); | ||
| 224 | } | ||
| 225 | }; | ||
| 226 | |||
| 227 | push @{$self->[node_attribs]}, $attribute; | ||
| 228 | $attribute->setParentNode($self); | ||
| 229 | $attribute->set_pos($#{$self->[node_attribs]}); | ||
| 230 | $attribute->set_global_pos($node_num); | ||
| 231 | |||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | sub removeAttribute { | ||
| 236 | my ($self, $attrib) = @_; | ||
| 237 | |||
| 238 | if (!ref($attrib)) { | ||
| 239 | $attrib = $self->getAttributeNode($attrib); | ||
| 240 | } | ||
| 241 | |||
| 242 | my $pos = $attrib->get_pos; | ||
| 243 | |||
| 244 | splice @{$self->[node_attribs]}, $pos, 1; | ||
| 245 | |||
| 246 | for (my $i = $pos; $i < @{$self->[node_attribs]}; $i++) { | ||
| 247 | $self->[node_attribs][$i]->set_pos($i); | ||
| 248 | } | ||
| 249 | |||
| 250 | $attrib->del_parent_link; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub setAttribute { | ||
| 254 | my ($self, $name, $value) = @_; | ||
| 255 | |||
| 256 | if (my $attrib = $self->getAttributeNode($name)) { | ||
| 257 | $attrib->setNodeValue($value); | ||
| 258 | return $attrib; | ||
| 259 | } | ||
| 260 | |||
| 261 | my ($nsprefix) = ($name =~ /^($XML::XPath::Parser::NCName):($XML::XPath::Parser::NCName)$/o); | ||
| 262 | |||
| 263 | if ($nsprefix && !$self->getNamespace($nsprefix)) { | ||
| 264 | die "No namespace matches prefix: $nsprefix"; | ||
| 265 | } | ||
| 266 | |||
| 267 | my $newnode = XML::XPath::Node::Attribute->new($name, $value, $nsprefix); | ||
| 268 | $self->appendAttribute($newnode); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub setAttributeNode { | ||
| 272 | my ($self, $node) = @_; | ||
| 273 | |||
| 274 | if (my $attrib = $self->getAttributeNode($node->getName)) { | ||
| 275 | $attrib->setNodeValue($node->getValue); | ||
| 276 | return $attrib; | ||
| 277 | } | ||
| 278 | |||
| 279 | my ($nsprefix) = ($node->getName() =~ /^($XML::XPath::Parser::NCName):($XML::XPath::Parser::NCName)$/o); | ||
| 280 | |||
| 281 | if ($nsprefix && !$self->getNamespace($nsprefix)) { | ||
| 282 | die "No namespace matches prefix: $nsprefix"; | ||
| 283 | } | ||
| 284 | |||
| 285 | $self->appendAttribute($node); | ||
| 286 | } | ||
| 287 | |||
| 288 | sub getNamespace { | ||
| 289 | my ($self, $prefix) = @_; | ||
| 290 | |||
| 291 | $prefix ||= $self->getPrefix || '#default'; | ||
| 292 | my $namespaces = $self->[node_namespaces] || []; | ||
| 293 | foreach my $ns (@$namespaces) { | ||
| 294 | return $ns if $ns->getPrefix eq $prefix; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $parent = $self->getParentNode; | ||
| 298 | |||
| 299 | return $parent->getNamespace($prefix) if $parent; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub getNamespaces { | ||
| 303 | my $self = shift; | ||
| 304 | |||
| 305 | if ($self->[node_namespaces]) { | ||
| 306 | return wantarray ? @{$self->[node_namespaces]} : $self->[node_namespaces]; | ||
| 307 | } | ||
| 308 | |||
| 309 | return wantarray ? () : []; | ||
| 310 | } | ||
| 311 | |||
| 312 | sub getNamespaceNodes { goto &getNamespaces } | ||
| 313 | |||
| 314 | sub appendNamespace { | ||
| 315 | my ($self, $ns) = @_; | ||
| 316 | |||
| 317 | push @{$self->[node_namespaces]}, $ns; | ||
| 318 | $ns->setParentNode($self); | ||
| 319 | $ns->set_pos($#{$self->[node_namespaces]}); | ||
| 320 | } | ||
| 321 | |||
| 322 | sub getPrefix { | ||
| 323 | my $self = shift; | ||
| 324 | $self->[node_prefix]; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub getExpandedName { | ||
| 328 | my $self = shift; | ||
| 329 | warn "Expanded name not implemented for ", ref($self), "\n"; | ||
| 330 | return; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub _to_sax { | ||
| 334 | my ($self, $doch, $dtdh, $enth) = @_; | ||
| 335 | |||
| 336 | my $tag = $self->getName; | ||
| 337 | my @attr; | ||
| 338 | |||
| 339 | for my $attr ($self->getAttributes) { | ||
| 340 | push @attr, $attr->getName, $attr->getValue; | ||
| 341 | } | ||
| 342 | |||
| 343 | my $ns = $self->getNamespace($self->[node_prefix]); | ||
| 344 | if ($ns) { | ||
| 345 | $doch->start_element( | ||
| 346 | { | ||
| 347 | Name => $tag, | ||
| 348 | Attributes => { @attr }, | ||
| 349 | NamespaceURI => $ns->getExpanded, | ||
| 350 | Prefix => $ns->getPrefix, | ||
| 351 | LocalName => $self->getLocalName, | ||
| 352 | } | ||
| 353 | ); | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | $doch->start_element( | ||
| 357 | { | ||
| 358 | Name => $tag, | ||
| 359 | Attributes => { @attr }, | ||
| 360 | } | ||
| 361 | ); | ||
| 362 | } | ||
| 363 | |||
| 364 | for my $kid ($self->getChildNodes) { | ||
| 365 | $kid->_to_sax($doch, $dtdh, $enth); | ||
| 366 | } | ||
| 367 | |||
| 368 | if ($ns) { | ||
| 369 | $doch->end_element( | ||
| 370 | { | ||
| 371 | Name => $tag, | ||
| 372 | NamespaceURI => $ns->getExpanded, | ||
| 373 | Prefix => $ns->getPrefix, | ||
| 374 | LocalName => $self->getLocalName | ||
| 375 | } | ||
| 376 | ); | ||
| 377 | } | ||
| 378 | else { | ||
| 379 | $doch->end_element( { Name => $tag } ); | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | sub string_value { | ||
| 384 | my $self = shift; | ||
| 385 | |||
| 386 | my $string = ''; | ||
| 387 | foreach my $kid (@{$self->[node_children]}) { | ||
| 388 | if ($kid->getNodeType == ELEMENT_NODE | ||
| 389 | || $kid->getNodeType == TEXT_NODE) { | ||
| 390 | $string .= $kid->string_value; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | return $string; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub toString { | ||
| 398 | my ($self, $norecurse) = @_; | ||
| 399 | |||
| 400 | my $string = ''; | ||
| 401 | if (! $self->[node_name] ) { | ||
| 402 | # root node | ||
| 403 | return join('', map { $_->toString($norecurse) } @{$self->[node_children]}); | ||
| 404 | } | ||
| 405 | |||
| 406 | $string .= "<" . $self->[node_name]; | ||
| 407 | $string .= join('', map { $_->toString } @{$self->[node_namespaces]}); | ||
| 408 | $string .= join('', map { $_->toString } @{$self->[node_attribs]}); | ||
| 409 | |||
| 410 | if (@{$self->[node_children]}) { | ||
| 411 | $string .= ">"; | ||
| 412 | |||
| 413 | if (!$norecurse) { | ||
| 414 | $string .= join('', map { $_->toString($norecurse) } @{$self->[node_children]}); | ||
| 415 | } | ||
| 416 | |||
| 417 | $string .= "</" . $self->[node_name] . ">"; | ||
| 418 | } | ||
| 419 | else { | ||
| 420 | $string .= " />"; | ||
| 421 | } | ||
| 422 | |||
| 423 | return $string; | ||
| 424 | } | ||
| 425 | |||
| 426 | 1; | ||
| 427 | __END__ |