Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed c1d832a

Modified Files:
Builder.pm
+ enabled start_document to set the document (external) encoding.
this does not affect that all incoming data has to be UTF8. It
just allows to have documents other than
<?xml version="1.0" encoding="UTF8"?>
+ xml_decl() implemented

Parser.pm
+ process_node() is now aware of DocumentFragments. they will be
processed as Documents are processed.
+ generate() will not process any node but only documents. this is
nessecary, because generate() uses Document specific functions.

  • Participants
  • Parent commits 26b7cfb

Comments (0)

Files changed (2)

File lib/XML/LibXML/SAX/Builder.pm

 
 sub new {
     my $class = shift;
-    return bless {}, $class;
+    return bless {@_}, $class;
 }
 
 sub start_document {
     my ($self, $doc) = @_;
-    $self->{DOM} = XML::LibXML::Document->createDocument( );
+
+    $self->{DOM} = XML::LibXML::Document->createDocument({});
+
+    if ( defined $self->{Encoding} ) {
+        $self->xml_decl({Version => ($self->{Version} || '1.0') , Encoding => $self->{Encoding}});
+    }
+
     $self->{Parent} = undef;
 }
 
+sub xml_decl {
+    my $self = shift;
+    my $decl = shift;
+
+    if ( defined $decl->{Version} ) {
+        $self->{DOM}->setVersion( $decl->{Version} );
+    }
+    if ( defined $decl->{Encoding} ) {
+        $self->{DOM}->setEncoding( $decl->{Encoding} );
+    }
+}
+
 sub end_document {
     my ($self, $doc) = @_;
     my $dom = delete $self->{DOM};
     else {
         $node = $self->{DOM}->createElement($el->{Name});
     }
-    
+
     # do attributes
     foreach my $key (keys %{$el->{Attributes}}) {
         my $attr = $el->{Attributes}->{$key};
             $node->setAttribute($key => $attr);
         }
     }
-    
+
     # append
     if ($self->{Parent}) {
         $self->{Parent}->appendChild($node);
 This is a SAX handler that generates a DOM tree from SAX events. Usage
 is as above. Input is accepted from any SAX1 or SAX2 event generator.
 
-=cut
+=cut

File lib/XML/LibXML/SAX/Parser.pm

 sub generate {
     my $self = shift;
     my ($node) = @_;
-    
-    $self->start_document({});
-    $self->xml_decl({Version => $node->getVersion, Encoding => $node->getEncoding});
-    $self->process_node($node);
-    
-    $self->end_document({});
+
+    if ( $node->getType() == XML_DOCUMENT_NODE ) {
+        $self->start_document({});
+        $self->xml_decl({Version => $node->getVersion, Encoding => $node->getEncoding});
+        $self->process_node($node);
+        $self->end_document({});
+    }
 }
 
 sub process_node {
         # warn("</" . $node->getName . ">\n");
     }
     elsif ($node_type == XML_ENTITY_REF_NODE) {
-        foreach my $kid ($node->getChildnodes) {
+        foreach my $kid ($node->childNodes) {
             # warn("child of entity ref: " . $kid->getType() . " called: " . $kid->getName . "\n");
             $self->process_node($kid);
         }
     }
-    elsif ($node_type == XML_DOCUMENT_NODE) {
-        foreach my $kid ($node->getChildnodes) {
+#    elsif ($node_type == XML_DOCUMENT_NODE) {
+    elsif ($node_type == XML_DOCUMENT_NODE
+           || $node_type == XML_DOCUMENT_FRAG_NODE) {
+        # some times it is just usefull to generate SAX events from
+        # a document fragment (very good with filters).
+        foreach my $kid ($node->childNodes) {
             $self->process_node($kid);
         }
     }
 
 sub process_element {
     my ($self, $element) = @_;
-    
+
     my $attribs = {};
 
     foreach my $attr ($element->getAttributes) {
         # use Data::Dumper;
         # warn("Attr made: ", Dumper($attribs->{$key}), "\n");
     }
-    
+
     my $node = {
         Name => $element->getName,
         Attributes => $attribs,
         Prefix => $element->getPrefix,
         LocalName => $element->getLocalName,
     };
-    
+
     $self->start_element($node);
-    
-    foreach my $child ($element->getChildnodes) {
+
+    foreach my $child ($element->childNodes) {
         $self->process_node($child);
     }
-    
+
     delete $node->{Attributes};
 
     $self->end_element($node);