| 1 |
# $Id$ |
| 2 |
# |
| 3 |
# This is free software, you may use it and distribute it under the same terms as |
| 4 |
# Perl itself. |
| 5 |
# |
| 6 |
# Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas |
| 7 |
# |
| 8 |
# |
| 9 |
|
| 10 |
package XML::LibXML::SAX::Builder; |
| 11 |
|
| 12 |
use strict; |
| 13 |
use warnings; |
| 14 |
|
| 15 |
use XML::LibXML; |
| 16 |
use XML::NamespaceSupport; |
| 17 |
|
| 18 |
use vars qw ($VERSION); |
| 19 |
|
| 20 |
sub CLONE_SKIP { |
| 21 |
return $XML::LibXML::__threads_shared ? 0 : 1; |
| 22 |
} |
| 23 |
|
| 24 |
$VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE |
| 25 |
|
| 26 |
sub new { |
| 27 |
my $class = shift; |
| 28 |
return bless {@_}, $class; |
| 29 |
} |
| 30 |
|
| 31 |
sub result { $_[0]->{LAST_DOM}; } |
| 32 |
|
| 33 |
sub done { |
| 34 |
my ($self) = @_; |
| 35 |
my $dom = $self->{DOM}; |
| 36 |
$dom = $self->{Parent} unless defined $dom; # this is for parsing document chunks |
| 37 |
|
| 38 |
delete $self->{NamespaceStack}; |
| 39 |
delete $self->{Parent}; |
| 40 |
delete $self->{DOM}; |
| 41 |
|
| 42 |
$self->{LAST_DOM} = $dom; |
| 43 |
|
| 44 |
return $dom; |
| 45 |
} |
| 46 |
|
| 47 |
sub set_document_locator { |
| 48 |
} |
| 49 |
|
| 50 |
sub start_dtd { |
| 51 |
my ($self, $dtd) = @_; |
| 52 |
if (defined $dtd->{Name} and |
| 53 |
(defined $dtd->{SystemId} or defined $dtd->{PublicId})) { |
| 54 |
$self->{DOM}->createExternalSubset($dtd->{Name},$dtd->{PublicId},$dtd->{SystemId}); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
sub end_dtd { |
| 59 |
} |
| 60 |
|
| 61 |
sub start_document { |
| 62 |
my ($self, $doc) = @_; |
| 63 |
$self->{DOM} = XML::LibXML::Document->createDocument(); |
| 64 |
|
| 65 |
if ( defined $self->{Encoding} ) { |
| 66 |
$self->xml_decl({Version => ($self->{Version} || '1.0') , Encoding => $self->{Encoding}}); |
| 67 |
} |
| 68 |
|
| 69 |
$self->{NamespaceStack} = XML::NamespaceSupport->new; |
| 70 |
$self->{NamespaceStack}->push_context; |
| 71 |
$self->{Parent} = undef; |
| 72 |
return (); |
| 73 |
} |
| 74 |
|
| 75 |
sub xml_decl { |
| 76 |
my $self = shift; |
| 77 |
my $decl = shift; |
| 78 |
|
| 79 |
if ( defined $decl->{Version} ) { |
| 80 |
$self->{DOM}->setVersion( $decl->{Version} ); |
| 81 |
} |
| 82 |
if ( defined $decl->{Encoding} ) { |
| 83 |
$self->{DOM}->setEncoding( $decl->{Encoding} ); |
| 84 |
} |
| 85 |
return (); |
| 86 |
} |
| 87 |
|
| 88 |
sub end_document { |
| 89 |
my ($self, $doc) = @_; |
| 90 |
my $d = $self->done(); |
| 91 |
return $d; |
| 92 |
} |
| 93 |
|
| 94 |
sub start_prefix_mapping { |
| 95 |
my $self = shift; |
| 96 |
my $ns = shift; |
| 97 |
|
| 98 |
unless ( defined $self->{DOM} or defined $self->{Parent} ) { |
| 99 |
$self->{Parent} = XML::LibXML::DocumentFragment->new(); |
| 100 |
$self->{NamespaceStack} = XML::NamespaceSupport->new; |
| 101 |
$self->{NamespaceStack}->push_context; |
| 102 |
} |
| 103 |
|
| 104 |
$self->{USENAMESPACESTACK} = 1; |
| 105 |
|
| 106 |
$self->{NamespaceStack}->declare_prefix( $ns->{Prefix}, $ns->{NamespaceURI} ); |
| 107 |
return (); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
sub end_prefix_mapping { |
| 112 |
my $self = shift; |
| 113 |
my $ns = shift; |
| 114 |
$self->{NamespaceStack}->undeclare_prefix( $ns->{Prefix} ); |
| 115 |
return (); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
sub start_element { |
| 120 |
my ($self, $el) = @_; |
| 121 |
my $node; |
| 122 |
|
| 123 |
unless ( defined $self->{DOM} or defined $self->{Parent} ) { |
| 124 |
$self->{Parent} = XML::LibXML::DocumentFragment->new(); |
| 125 |
$self->{NamespaceStack} = XML::NamespaceSupport->new; |
| 126 |
$self->{NamespaceStack}->push_context; |
| 127 |
} |
| 128 |
|
| 129 |
if ( defined $self->{Parent} ) { |
| 130 |
$el->{NamespaceURI} ||= ""; |
| 131 |
$node = $self->{Parent}->addNewChild( $el->{NamespaceURI}, |
| 132 |
$el->{Name} ); |
| 133 |
} |
| 134 |
else { |
| 135 |
if ($el->{NamespaceURI}) { |
| 136 |
if ( defined $self->{DOM} ) { |
| 137 |
$node = $self->{DOM}->createRawElementNS($el->{NamespaceURI}, |
| 138 |
$el->{Name}); |
| 139 |
} |
| 140 |
else { |
| 141 |
$node = XML::LibXML::Element->new( $el->{Name} ); |
| 142 |
$node->setNamespace( $el->{NamespaceURI}, |
| 143 |
$el->{Prefix} , 1 ); |
| 144 |
} |
| 145 |
} |
| 146 |
else { |
| 147 |
if ( defined $self->{DOM} ) { |
| 148 |
$node = $self->{DOM}->createRawElement($el->{Name}); |
| 149 |
} |
| 150 |
else { |
| 151 |
$node = XML::LibXML::Element->new( $el->{Name} ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$self->{DOM}->setDocumentElement($node); |
| 156 |
} |
| 157 |
|
| 158 |
# build namespaces |
| 159 |
my $skip_ns= 0; |
| 160 |
foreach my $p ( $self->{NamespaceStack}->get_declared_prefixes() ) { |
| 161 |
$skip_ns= 1; |
| 162 |
my $uri = $self->{NamespaceStack}->get_uri($p); |
| 163 |
my $nodeflag = 0; |
| 164 |
if ( defined $uri |
| 165 |
and defined $el->{NamespaceURI} |
| 166 |
and $uri eq $el->{NamespaceURI} ) { |
| 167 |
# $nodeflag = 1; |
| 168 |
next; |
| 169 |
} |
| 170 |
$node->setNamespace($uri, $p, 0 ); |
| 171 |
} |
| 172 |
|
| 173 |
$self->{Parent} = $node; |
| 174 |
|
| 175 |
$self->{NamespaceStack}->push_context; |
| 176 |
|
| 177 |
# do attributes |
| 178 |
foreach my $key (keys %{$el->{Attributes}}) { |
| 179 |
my $attr = $el->{Attributes}->{$key}; |
| 180 |
if (ref($attr)) { |
| 181 |
# catch broken name/value pairs |
| 182 |
next unless $attr->{Name} ; |
| 183 |
next if $self->{USENAMESPACESTACK} |
| 184 |
and ( $attr->{Name} eq "xmlns" |
| 185 |
or ( defined $attr->{Prefix} |
| 186 |
and $attr->{Prefix} eq "xmlns" ) ); |
| 187 |
|
| 188 |
|
| 189 |
if ( defined $attr->{Prefix} |
| 190 |
and $attr->{Prefix} eq "xmlns" and $skip_ns == 0 ) { |
| 191 |
# ok, the generator does not set namespaces correctly! |
| 192 |
my $uri = $attr->{Value}; |
| 193 |
$node->setNamespace($uri, |
| 194 |
$attr->{LocalName}, |
| 195 |
$uri eq $el->{NamespaceURI} ? 1 : 0 ); |
| 196 |
} |
| 197 |
else { |
| 198 |
$node->setAttributeNS($attr->{NamespaceURI} || "", |
| 199 |
$attr->{Name}, $attr->{Value}); |
| 200 |
} |
| 201 |
} |
| 202 |
else { |
| 203 |
$node->setAttribute($key => $attr); |
| 204 |
} |
| 205 |
} |
| 206 |
return (); |
| 207 |
} |
| 208 |
|
| 209 |
sub end_element { |
| 210 |
my ($self, $el) = @_; |
| 211 |
return unless $self->{Parent}; |
| 212 |
|
| 213 |
$self->{NamespaceStack}->pop_context; |
| 214 |
$self->{Parent} = $self->{Parent}->parentNode(); |
| 215 |
return (); |
| 216 |
} |
| 217 |
|
| 218 |
sub start_cdata { |
| 219 |
my $self = shift; |
| 220 |
$self->{IN_CDATA} = 1; |
| 221 |
return (); |
| 222 |
} |
| 223 |
|
| 224 |
sub end_cdata { |
| 225 |
my $self = shift; |
| 226 |
$self->{IN_CDATA} = 0; |
| 227 |
return (); |
| 228 |
} |
| 229 |
|
| 230 |
sub characters { |
| 231 |
my ($self, $chars) = @_; |
| 232 |
if ( not defined $self->{DOM} and not defined $self->{Parent} ) { |
| 233 |
$self->{Parent} = XML::LibXML::DocumentFragment->new(); |
| 234 |
$self->{NamespaceStack} = XML::NamespaceSupport->new; |
| 235 |
$self->{NamespaceStack}->push_context; |
| 236 |
} |
| 237 |
return unless $self->{Parent}; |
| 238 |
my $node; |
| 239 |
|
| 240 |
unless ( defined $chars and defined $chars->{Data} ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
if ( defined $self->{DOM} ) { |
| 245 |
if ( defined $self->{IN_CDATA} and $self->{IN_CDATA} == 1 ) { |
| 246 |
$node = $self->{DOM}->createCDATASection($chars->{Data}); |
| 247 |
} |
| 248 |
else { |
| 249 |
$node = $self->{Parent}->appendText($chars->{Data}); |
| 250 |
return; |
| 251 |
} |
| 252 |
} |
| 253 |
elsif ( defined $self->{IN_CDATA} and $self->{IN_CDATA} == 1 ) { |
| 254 |
$node = XML::LibXML::CDATASection->new($chars->{Data}); |
| 255 |
} |
| 256 |
else { |
| 257 |
$node = XML::LibXML::Text->new($chars->{Data}); |
| 258 |
} |
| 259 |
|
| 260 |
$self->{Parent}->addChild($node); |
| 261 |
return (); |
| 262 |
} |
| 263 |
|
| 264 |
sub comment { |
| 265 |
my ($self, $chars) = @_; |
| 266 |
my $comment; |
| 267 |
if ( not defined $self->{DOM} and not defined $self->{Parent} ) { |
| 268 |
$self->{Parent} = XML::LibXML::DocumentFragment->new(); |
| 269 |
$self->{NamespaceStack} = XML::NamespaceSupport->new; |
| 270 |
$self->{NamespaceStack}->push_context; |
| 271 |
} |
| 272 |
|
| 273 |
unless ( defined $chars and defined $chars->{Data} ) { |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
if ( defined $self->{DOM} ) { |
| 278 |
$comment = $self->{DOM}->createComment( $chars->{Data} ); |
| 279 |
} |
| 280 |
else { |
| 281 |
$comment = XML::LibXML::Comment->new( $chars->{Data} ); |
| 282 |
} |
| 283 |
|
| 284 |
if ( defined $self->{Parent} ) { |
| 285 |
$self->{Parent}->addChild($comment); |
| 286 |
} |
| 287 |
else { |
| 288 |
$self->{DOM}->addChild($comment); |
| 289 |
} |
| 290 |
return (); |
| 291 |
} |
| 292 |
|
| 293 |
sub processing_instruction { |
| 294 |
my ( $self, $pi ) = @_; |
| 295 |
my $PI; |
| 296 |
return unless defined $self->{DOM}; |
| 297 |
$PI = $self->{DOM}->createPI( $pi->{Target}, $pi->{Data} ); |
| 298 |
|
| 299 |
if ( defined $self->{Parent} ) { |
| 300 |
$self->{Parent}->addChild( $PI ); |
| 301 |
} |
| 302 |
else { |
| 303 |
$self->{DOM}->addChild( $PI ); |
| 304 |
} |
| 305 |
return (); |
| 306 |
} |
| 307 |
|
| 308 |
sub warning { |
| 309 |
my $self = shift; |
| 310 |
my $error = shift; |
| 311 |
# fill $@ but do not die seriously |
| 312 |
eval { $error->throw; }; |
| 313 |
} |
| 314 |
|
| 315 |
sub error { |
| 316 |
my $self = shift; |
| 317 |
my $error = shift; |
| 318 |
delete $self->{NamespaceStack}; |
| 319 |
delete $self->{Parent}; |
| 320 |
delete $self->{DOM}; |
| 321 |
$error->throw; |
| 322 |
} |
| 323 |
|
| 324 |
sub fatal_error { |
| 325 |
my $self = shift; |
| 326 |
my $error = shift; |
| 327 |
delete $self->{NamespaceStack}; |
| 328 |
delete $self->{Parent}; |
| 329 |
delete $self->{DOM}; |
| 330 |
$error->throw; |
| 331 |
} |
| 332 |
|
| 333 |
1; |
| 334 |
|
| 335 |
__END__ |