Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed 3133aca

A bunch of functionality moved from LibXML.pm into the C-layer again.
this time i did not change the interface though :)

Modified Files:
LibXML.pm
- parser initialisation
- parse_html functions (no use for an extra perl layer anymore)

LibXML.xs
+ parser initialisation
the current implementation is more straight, but still has to make
use of global LibXML_*_cb variables for the private callbacks.
this is ONLY because parse_xml_chunk is not implemented with a
parser context, so we can't really pass private information in that
case.
+ parse_html function
+ $XML::LibXML::ORIGINAL_STRING to toggle the encoding of functions return
values
o one or two more xmlFree calls
! if xincludes should be processed one should use $parser->processXInclude()
instead of $dom->process_xinclude(). the latter will not know about
the parser settings.

dom.c
- implicit encoding
o corrected intendation

t/05dombasic.t t/06nodetypes.t
t/07nodelist.t t/14sax.t t/17callbacks.t
o updated and fixed the test to work with the utf8
only return values

  • Participants
  • Parent commits a1442b1

Comments (0)

Files changed (8)

 
 sub match_callback {
     my $self = shift;
-    return $self->{XML_LIBXML_MATCH_CB} = shift;
+    $self->{XML_LIBXML_MATCH_CB} = shift if scalar @_;
+    return $self->{XML_LIBXML_MATCH_CB};
 }
 
 sub read_callback {
     my $self = shift;
-    return $self->{XML_LIBXML_READ_CB} = shift;
+    $self->{XML_LIBXML_READ_CB} = shift if scalar @_;
+    return $self->{XML_LIBXML_READ_CB};
 }
 
 sub close_callback {
     my $self = shift;
-    return $self->{XML_LIBXML_CLOSE_CB} = shift;
+    $self->{XML_LIBXML_CLOSE_CB} = shift if scalar @_;
+    return $self->{XML_LIBXML_CLOSE_CB};
 }
 
 sub open_callback {
     my $self = shift;
-    return $self->{XML_LIBXML_OPEN_CB} = shift;
+    $self->{XML_LIBXML_OPEN_CB} = shift if scalar @_;
+    return $self->{XML_LIBXML_OPEN_CB};
 }
 
 sub callbacks {
     return $self->{XML_LIBXML_EXPAND_XINCLUDE};
 }
 
-sub init_parser {
-    my $self = shift;
-    $self->_match_callback( $self->{XML_LIBXML_MATCH_CB} )
-      if $self->{XML_LIBXML_MATCH_CB};
-    $self->_read_callback( $self->{XML_LIBXML_READ_CB} )
-      if $self->{XML_LIBXML_READ_CB};
-    $self->_open_callback( $self->{XML_LIBXML_OPEN_CB} )
-      if $self->{XML_LIBXML_OPEN_CB};
-    $self->_close_callback( $self->{XML_LIBXML_CLOSE_CB} )
-      if $self->{XML_LIBXML_CLOSE_CB};
-
-    $self->_validation( $self->{XML_LIBXML_VALIDATION} )
-      if exists $self->{XML_LIBXML_VALIDATION};
-    $self->_expand_entities( $self->{XML_LIBXML_EXPAND_ENTITIES} )
-      if exists $self->{XML_LIBXML_EXPAND_ENTITIES};
-    $self->_keep_blanks( $self->{XML_LIBXML_KEEP_BLANKS} )
-      if exists $self->{XML_LIBXML_KEEP_BLANKS};
-    $self->_pedantic_parser( $self->{XML_LIBXML_PEDANTIC} )
-      if exists $self->{XML_LIBXML_PEDANTIC};
-    $self->_load_ext_dtd( $self->{XML_LIBXML_EXT_DTD} )
-      if exists $self->{XML_LIBXML_EXT_DTD};
-    $self->_complete_attributes( $self->{XML_LIBXML_COMPLETE_ATTR} )
-      if exists $self->{XML_LIBXML_COMPLETE_ATTR};
-}
-
 sub parse_string {
     my $self = shift;
     croak("parse already in progress") if $self->{_State_};
     $self->{_State_} = 1;
     my $result;
     eval {
-        $self->init_parser();
         $result = $self->_parse_string( @_ );
         $result->_fix_extra;
-        if ( $self->{XML_LIBXML_EXPAND_XINCLUDE} ) {
-            $result->process_xinclude();
-        }
+    };
+    my $err = $@;
+    $self->{_State_} = 0;
+    if ($err) {
+        croak $err;
+    }
+    return $result;
+}
+
+sub parse_fh {
+    my $self = shift;
+    croak("parse already in progress") if $self->{_State_};
+    $self->{_State_} = 1;
+    my $result;
+    eval {
+        $result = $self->_parse_fh( @_ );
+        $result->_fix_extra;
+    };
+    my $err = $@;
+    $self->{_State_} = 0;
+    if ($err) {
+        croak $err;
+    }
+    return $result;
+}
+
+sub parse_file {
+    my $self = shift;
+    croak("parse already in progress") if $self->{_State_};
+    $self->{_State_} = 1;
+    my $result;
+    eval {
+        $result = $self->_parse_file(@_);
+        $result->_fix_extra;
     };
     my $err = $@;
     $self->{_State_} = 0;
     $self->{_State_} = 1;
     my $result;
     eval {
-        $self->init_parser();
         $result = $self->_parse_xml_chunk( @_ );
         if ( $result ) {
             $result->_fix_extra;
         }
-
-        # we should include this to document fragments as well ...
-        # if ( $self->{XML_LIBXML_EXPAND_XINCLUDE} ) {
-        #     $result->process_xinclude();
-        # }
-    };
-    my $err = $@;
-    $self->{_State_} = 0;
-    if ($err) {
-        croak $err;
-    }
-    return $result;
-}
-
-sub parse_fh {
-    my $self = shift;
-    croak("parse already in progress") if $self->{_State_};
-    $self->{_State_} = 1;
-    my $result;
-    eval {
-        $self->init_parser();
-        $result = $self->_parse_fh( @_ );
-        $result->_fix_extra;
-        if ( $self->{XML_LIBXML_EXPAND_XINCLUDE} ) {
-            warn "use xinclude!" ;
-            $result->process_xinclude();
-        }
     };
     my $err = $@;
     $self->{_State_} = 0;
     return $result;
 }
 
-sub parse_file {
-    my $self = shift;
-    croak("parse already in progress") if $self->{_State_};
-    $self->{_State_} = 1;
-    my $result;
-    eval {
-        $self->init_parser();
-        $result = $self->_parse_file(@_);
-        $result->_fix_extra;
-        if ( $self->{XML_LIBXML_EXPAND_XINCLUDE} ) {
-            # warn "use xinclude!" ;
-            $result->process_xinclude();
-        }
-    };
-    my $err = $@;
-    $self->{_State_} = 0;
-    if ($err) {
-        croak $err;
-    }
-    return $result;
-}
-
-sub parse_html_string {
-    my $self = shift;
-
-    $self->init_parser();
-    my $retval = $self->_parse_html_string( @_ );
-
-    return $retval;
-}
-
-sub parse_html_fh {
-    my $self = shift;
-
-    $self->init_parser();
-    my $retval = $self->_parse_html_fh( @_ );
-
-    return $retval;
-}
-
-sub parse_html_file {
-    my $self = shift;
-
-    $self->init_parser();
-    my $retval = $self->_parse_html_file( @_ );
-
-    return $retval;
-}
-
-sub processXIncludes {
-    my $self = shift;
-    my $dom  = shift;
-
-    $self->init_parser();
-    $dom->process_xinclude();
-}
-
 sub __read {
     read($_[0], $_[1], $_[2]);
 }
         }\
     }
 
+#define TEST_PERL_FLAG(flag) \
+    SvTRUE(get_sv(flag, FALSE)) ? 1 : 0
+
 typedef struct _ProxyObject ProxyObject;
 
 struct _ProxyObject {
     SV * global_cb;
     SV * callback = NULL;
 
-    if ((global_cb = perl_get_sv("XML::LibXML::match_cb", FALSE))
-            && SvTRUE(global_cb)) {
+    if (LibXML_match_cb && SvTRUE(LibXML_match_cb)) {
+        callback = LibXML_match_cb;
+    }
+    else if ((global_cb = perl_get_sv("XML::LibXML::match_cb", FALSE))
+             && SvTRUE(global_cb)) {
         callback = global_cb;
     }
-    else if (LibXML_match_cb && SvTRUE(LibXML_match_cb)) {
-        callback = LibXML_match_cb;
-    }
 
     if (callback) {
         int count;
     SV * global_cb;
     SV * callback = NULL;
 
-    if ((global_cb = perl_get_sv("XML::LibXML::open_cb", FALSE))
+    if (LibXML_open_cb && SvTRUE(LibXML_open_cb)) {
+        callback = LibXML_open_cb;
+    }
+    else if ((global_cb = perl_get_sv("XML::LibXML::open_cb", FALSE))
             && SvTRUE(global_cb)) {
         callback = global_cb;
     }
-    else if (LibXML_open_cb && SvTRUE(LibXML_open_cb)) {
-        callback = LibXML_open_cb;
-    }
 
     if (callback) {
         int count;
     SV * callback = NULL;
     SV * ctxt = (SV *)context;
 
-    if ((global_cb = perl_get_sv("XML::LibXML::read_cb", FALSE))
+    if (LibXML_read_cb && SvTRUE(LibXML_read_cb)) {
+        callback = LibXML_read_cb;
+    }
+    else if ((global_cb = perl_get_sv("XML::LibXML::read_cb", FALSE))
             && SvTRUE(global_cb)) {
         callback = global_cb;
     }
-    else if (LibXML_read_cb && SvTRUE(LibXML_read_cb)) {
-        callback = LibXML_read_cb;
-    }
     
     if (callback) {
         int count;
     SV * callback = NULL;
     SV * ctxt = (SV *)context;
 
-    if ((global_cb = perl_get_sv("XML::LibXML::close_cb", FALSE))
+    if (LibXML_close_cb && SvTRUE(LibXML_close_cb)) {
+        callback = LibXML_close_cb;
+    }
+    else if ((global_cb = perl_get_sv("XML::LibXML::close_cb", FALSE))
             && SvTRUE(global_cb)) {
         callback = global_cb;
     }
-    else if (LibXML_close_cb && SvTRUE(LibXML_close_cb)) {
-        callback = LibXML_close_cb;
-    }
 
     if (callback) {
         int count;
     xmlLoadExtDtdDefaultValue = 5;
     xmlPedanticParserDefaultValue = 0;
     xmlDoValidityCheckingDefaultValue = 0;
+    xmlSetGenericErrorFunc(NULL, NULL);
 }
 
 void
-LibXML_init_callbacks() {
+LibXML_init_parser( SV * self ) {
+    /* we fetch all switches and callbacks from the hash */
+
     xmlSetGenericErrorFunc(PerlIO_stderr(), 
                            (xmlGenericErrorFunc)LibXML_error_handler);
+
+    if ( self != NULL ) {
+        /* first fetch the values from the hash */
+        HV* real_obj = (HV *)SvRV(self);
+        SV** item    = NULL;
+        SV * RETVAL  = NULL; /* dummy for the stupid macro */
+
+        item = hv_fetch( real_obj, "XML_LIBXML_VALIDATION", 21, 0 );
+        xmlDoValidityCheckingDefaultValue = item != NULL && SvTRUE(*item) ? 1 : 0;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_EXPAND_ENTITIES", 26, 0 );
+        xmlSubstituteEntitiesDefaultValue = item != NULL && SvTRUE(*item) ? 1 : 0;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_KEEP_BLANKS", 22, 0 );
+        xmlKeepBlanksDefaultValue = item != NULL && SvTRUE(*item) ? 1 : 0;
+        item = hv_fetch( real_obj, "XML_LIBXML_PEDANTIC", 19, 0 );
+        xmlPedanticParserDefaultValue = item != NULL && SvTRUE(*item) ? 1 : 0;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_EXT_DTD", 18, 0 );
+        if ( item != NULL && SvTRUE(*item) )
+            xmlLoadExtDtdDefaultValue |= 1;
+        else
+            xmlLoadExtDtdDefaultValue ^= 1;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_COMPLETE_ATTR", 24, 0 );
+        if (item != NULL && SvTRUE(*item))
+            xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
+        else
+            xmlLoadExtDtdDefaultValue ^= XML_COMPLETE_ATTRS;
+        /* now fetch the callbacks */
+
+        item = hv_fetch( real_obj, "XML_LIBXML_READ_CB", 18, 0 );
+        if ( item != NULL && SvTRUE(*item))
+            LibXML_read_cb= *item;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_MATCH_CB", 19, 0 );
+        if ( item != NULL  && SvTRUE(*item)) 
+            LibXML_match_cb= *item;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_OPEN_CB", 18, 0 );
+        if ( item != NULL  && SvTRUE(*item)) 
+            LibXML_open_cb = *item;
+
+        item = hv_fetch( real_obj, "XML_LIBXML_CLOSE_CB", 19, 0 );
+        if ( item != NULL  && SvTRUE(*item)) 
+            LibXML_close_cb = *item;
+
+    }
+
     return;
 /*    LibXML_old_ext_ent_loader =  xmlGetExternalEntityLoader(); */
 /*    warn("      init parser callbacks!\n"); */
 
     xmlSetExternalEntityLoader( (xmlExternalEntityLoader)LibXML_load_external_entity );
 
-
 }
 
 void
 LibXML_cleanup_callbacks() {
-    xmlSetGenericErrorFunc(NULL, NULL);
+    
     return; 
 /*   warn("      cleanup parser callbacks!\n"); */
 
     CODE:
         xmlCleanupParser();
 
-SV *
-_match_callback(self, ...)
-        SV * self
-    CODE:
-        if (items > 1) {
-            SET_CB(LibXML_match_cb, ST(1));
-            /* LibXML_update_callbacks(); */
-        }
-        else {
-            RETVAL = LibXML_match_cb ? sv_2mortal(LibXML_match_cb) : &PL_sv_undef;
-        }
-    OUTPUT:
-        RETVAL
-
-SV *
-_open_callback(self, ...)
-        SV * self
-    CODE:
-        if (items > 1) {
-            SET_CB(LibXML_open_cb, ST(1));
-            /* LibXML_update_callbacks(); */
-
-        }
-        else {
-            RETVAL = LibXML_open_cb ? sv_2mortal(LibXML_open_cb) : &PL_sv_undef;
-        }
-    OUTPUT:
-        RETVAL
-
-SV *
-_read_callback(self, ...)
-        SV * self
-    CODE:
-        if (items > 1) {
-            SET_CB(LibXML_read_cb, ST(1));
-            /* LibXML_update_callbacks(); */
-        }
-        else {
-            RETVAL = LibXML_read_cb ? sv_2mortal(LibXML_read_cb) : &PL_sv_undef;
-        }
-    OUTPUT:
-        RETVAL
-
-SV *
-_close_callback(self, ...)
-        SV * self
-    CODE:
-        if (items > 1) {
-            SET_CB(LibXML_close_cb, ST(1));
-            /* LibXML_update_callbacks(); */
-        }
-        else {
-            RETVAL = LibXML_close_cb ? sv_2mortal(LibXML_close_cb) : &PL_sv_undef;
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_validation(self, ...)
-        SV * self
-    CODE:
-        /* init retval with the current default value */
-        RETVAL = xmlDoValidityCheckingDefaultValue; 
-        if (items > 1) {
-            xmlDoValidityCheckingDefaultValue = SvTRUE(ST(1)) ? 1 : 0;
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_expand_entities(self, ...)
-        SV * self
-    CODE:
-        RETVAL = xmlSubstituteEntitiesDefaultValue;
-        if (items > 1) {
-            xmlSubstituteEntitiesDefaultValue = SvTRUE(ST(1)) ? 1 : 0;
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_keep_blanks(self, ...)
-        SV * self
-    CODE:
-        RETVAL = xmlKeepBlanksDefaultValue;
-        if (items > 1) {
-            xmlKeepBlanksDefaultValue = SvTRUE(ST(1)) ? 1 : 0;
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_pedantic_parser(self, ...)
-        SV * self
-    CODE:
-        RETVAL = xmlPedanticParserDefaultValue;
-        if (items > 1)  {
-            xmlPedanticParserDefaultValue = SvTRUE(ST(1)) ? 1 : 0;
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_load_ext_dtd(self, ...)
-        SV * self
-    CODE:
-        RETVAL = ( xmlLoadExtDtdDefaultValue == (xmlLoadExtDtdDefaultValue | 1 ) );
-        if (items > 1) {
-            if (SvTRUE(ST(1)))
-                xmlLoadExtDtdDefaultValue |= 1;
-            else 
-                xmlLoadExtDtdDefaultValue ^= 1;
-            RETVAL = ( xmlLoadExtDtdDefaultValue == (xmlLoadExtDtdDefaultValue | 1 ) );
-        }
-    OUTPUT:
-        RETVAL
-
-int
-_complete_attributes(self, ...)
-        SV * self
-    CODE:
-        RETVAL = ( xmlLoadExtDtdDefaultValue == (xmlLoadExtDtdDefaultValue | XML_COMPLETE_ATTRS));
-        if (items > 1) {
-            if (SvTRUE(ST(1)))
-                xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
-            else
-                xmlLoadExtDtdDefaultValue ^= XML_COMPLETE_ATTRS;
-        }
-    OUTPUT:
-        RETVAL
-
-
 char *
 get_last_error(CLASS)
         char * CLASS 
         int ret;
         xmlDocPtr real_dom;
         ProxyObject * proxy;
+        HV* real_obj = (HV *)SvRV(self);
+        SV** item    = NULL;
     CODE:
         ptr = SvPV(string, len);
         if (len == 0) {
         sv_setpvn(LibXML_error, "", 0);
         
         # warn( "context initialized \n");        
-        LibXML_init_callbacks();
+        LibXML_init_parser(self);
         ret = xmlParseDocument(ctxt);
-        LibXML_cleanup_callbacks();
-        LibXML_cleanup_parser();
 
         # warn( "document parsed \n");
 
 
         real_dom = ctxt->myDoc;
         xmlFreeParserCtxt(ctxt);
+
         sv_2mortal(LibXML_error);
         if (!well_formed || (xmlDoValidityCheckingDefaultValue && !valid)) {
             xmlFreeDoc(real_dom);
         else {
             STRLEN n_a;
             SV * newURI = newSVpvf("unknown-%12.12d", real_dom);
+            # ok check the xincludes
+            item = hv_fetch( real_obj, "XML_LIBXML_EXPAND_XINCLUDE", 26, 0 );
+            if ( item != NULL && SvTRUE(*item) ) {
+                # warn( "xinclude\n" );
+                xmlXIncludeProcess(real_dom);
+            }
+
             real_dom->URL = xmlStrdup(SvPV(newURI, n_a));
             SvREFCNT_dec(newURI);
             proxy = make_proxy_node( (xmlNodePtr)real_dom ); 
             sv_setref_pv( RETVAL, (char *)CLASS, (void*)proxy );
             proxy->extra = RETVAL;
             SvREFCNT_inc(RETVAL);
-        }
- 
+        }        
+        LibXML_cleanup_callbacks();
+        LibXML_cleanup_parser(); 
     OUTPUT:
         RETVAL
 
         LibXML_error = NEWSV(0, 512);
         sv_setpvn(LibXML_error, "", 0);
 
-        LibXML_init_callbacks();
+        LibXML_init_parser(self);
         real_dom = LibXML_parse_stream(self, fh, directory);
-        LibXML_cleanup_callbacks();
-        LibXML_cleanup_parser();
 
         sv_2mortal(LibXML_error);
         
         else {
             STRLEN n_a;
             SV * newURI = newSVpvf("unknown-%12.12d", real_dom);
+            HV* real_self = (HV*)SvRV(self);
+            SV** item;
+            # ok check the xincludes
+            item = hv_fetch( real_self, "XML_LIBXML_EXPAND_XINCLUDE", 26, 0 );
+            if ( item != NULL && SvTRUE(*item) ) 
+                xmlXIncludeProcess(real_dom);
+
             real_dom->URL = xmlStrdup(SvPV(newURI, n_a));
             SvREFCNT_dec(newURI);
             proxy = make_proxy_node( (xmlNodePtr)real_dom ); 
             proxy->extra = RETVAL;
             SvREFCNT_inc(RETVAL);
         }
+        LibXML_cleanup_callbacks();
+        LibXML_cleanup_parser();
     OUTPUT:
         RETVAL
         
         xmlDocPtr real_dom = NULL;
         ProxyObject * proxy = NULL;
     CODE:
+        LibXML_init_parser(self);
         ctxt = xmlCreateFileParserCtxt(filename);
 
         if (ctxt == NULL) {
         LibXML_error = NEWSV(0, 512);
         sv_setpvn(LibXML_error, "", 0);
 
-        LibXML_init_callbacks();        
         xmlParseDocument(ctxt);
-        LibXML_cleanup_callbacks();
-        LibXML_cleanup_parser();
 
         well_formed = ctxt->wellFormed;
         valid = ctxt->valid;
             croak(SvPV(LibXML_error, len));
         }
         else {
+            HV* real_self = (HV*)SvRV(self);
+            SV** item = NULL;
+
+            # ok check the xincludes
+            item = hv_fetch( real_self, "XML_LIBXML_EXPAND_XINCLUDE", 26, 0 );
+            if ( item != NULL && SvTRUE(*item) )  {
+                # warn( "xincludes\n" );
+                xmlXIncludeProcess(real_dom);
+            }
+
             proxy = make_proxy_node( (xmlNodePtr)real_dom ); 
 
             RETVAL = sv_newmortal();
             proxy->extra = RETVAL;
             SvREFCNT_inc(RETVAL);
         }
+        LibXML_cleanup_callbacks();
+        LibXML_cleanup_parser();
     OUTPUT:
         RETVAL
 
 SV*
-_parse_html_string(self, string)
+parse_html_string(self, string)
         SV * self
         SV * string
     PREINIT:
         LibXML_error = NEWSV(0, 512);
         sv_setpvn(LibXML_error, "", 0);
         
-        LibXML_init_callbacks();
+        LibXML_init_parser(self);
         real_dom = htmlParseDoc(ptr, NULL);
         LibXML_cleanup_callbacks();
         LibXML_cleanup_parser();        
         RETVAL
 
 SV*
-_parse_html_fh(self, fh)
+parse_html_fh(self, fh)
         SV * self
         SV * fh
     PREINIT:
         LibXML_error = NEWSV(0, 512);
         sv_setpvn(LibXML_error, "", 0);
         
-        LibXML_init_callbacks();
+        LibXML_init_parser(self);
         real_dom = LibXML_parse_html_stream(self, fh);
         LibXML_cleanup_callbacks();
         LibXML_cleanup_parser();
         RETVAL
         
 SV*
-_parse_html_file(self, filename)
+parse_html_file(self, filename)
         SV * self
         const char * filename
     PREINIT:
         LibXML_error = NEWSV(0, 512);
         sv_setpvn(LibXML_error, "", 0);
         
-        LibXML_init_callbacks();
+        LibXML_init_parser(self);
         real_dom = htmlParseFile(filename, NULL);
         LibXML_cleanup_callbacks();
         LibXML_cleanup_parser();
 
         if ( chunk != NULL ) {
             LibXML_error = sv_2mortal(newSVpv("", 0));
-            LibXML_init_callbacks();
+            LibXML_init_parser(self);
             rv = domReadWellBalancedString( NULL, chunk );
             LibXML_cleanup_callbacks();
             LibXML_cleanup_parser();    
     OUTPUT:
         RETVAL
 
+void
+processXIncludes( self, dom )
+        SV * self
+        SV * dom
+    PREINIT:
+        xmlDocPtr real_dom = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
+    CODE:
+        # first init the stuff for the parser
+        LibXML_init_parser(self);
+        xmlXIncludeProcess(real_dom);        
+        LibXML_cleanup_callbacks();
+        LibXML_cleanup_parser();
+
 SV*
 encodeToUTF8( encoding, string )
         const char * encoding
 
 void
 process_xinclude(self)
-        ProxyObject* self
+        SV* self
+    PREINIT:
+        ProxyObject* real_self = (ProxyObject*)SvIV((SV*)SvRV(self));
     CODE:
-        LibXML_init_callbacks();
-        xmlXIncludeProcess((xmlDocPtr)self->object);
+        LibXML_init_parser( NULL );
+        xmlXIncludeProcess((xmlDocPtr)real_self->object);
         LibXML_cleanup_callbacks();
         LibXML_cleanup_parser();
 
         # SvREFCNT_inc(docfrag_sv);    
 
         # warn("xmlNewNode\n");
-        elname = domEncodeString( real_dom->encoding, name);
-        newNode = xmlNewNode( NULL , elname );
+        if ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ) {
+            elname = domEncodeString( real_dom->encoding, name);
+        }
+        else {
+            elname = xmlStrdup(name);
+        }
+        newNode = xmlNewNode(NULL , elname);
         xmlFree(elname);
         
         newNode->doc = real_dom;
 
         if ( nsURI != NULL && strlen(nsURI)!=0 ){
             lname = xmlSplitQName2(qname, &prefix);
-            encstring = domEncodeString( real_dom->encoding, prefix );
+            if ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING" ) ) {
+                encstring = domEncodeString( real_dom->encoding, prefix );
+            }
+            else {
+                encstring = xmlStrdup( prefix );
+            }
             ns = domNewNs (0 , encstring, nsURI );
-            xmlFree(encstring);
+            xmlFree( encstring );
         }
         else {
             lname = qname;
         }
-        encstring = domEncodeString( real_dom->encoding, lname );
+
+        if ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ) {
+            encstring = domEncodeString( real_dom->encoding, lname );
+        }
+        else {
+            encstring = xmlStrdup( lname );
+        }
+
         newNode = xmlNewNode( ns , encstring );
         xmlFree(encstring);
 
         # warn( "NEW FRAGMENT TEXT");
         # SvREFCNT_inc(docfrag_sv);    
 
-        encstring = domEncodeString( real_dom->encoding, content );
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            encstring = domEncodeString( real_dom->encoding, content );
+        }
+        else {
+            encstring = xmlStrdup( content );
+        }
+
         newNode = xmlNewDocText( real_dom, encstring );
         xmlFree(encstring);
 
         xmlNodePtr docfrag = NULL;
         ProxyObject * dfProxy= NULL;
         SV * docfrag_sv = NULL;
+        char * encstr = NULL;
     CODE:
         real_dom = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
-        content = domEncodeString( real_dom->encoding, content );
-        
-        newNode = xmlNewDocComment( real_dom, content );
-        
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            encstr = domEncodeString( real_dom->encoding, content );
+        }
+        else {
+            encstr = xmlStrdup( content );
+        }        
+
+        newNode = xmlNewDocComment( real_dom, encstr );
+        xmlFree( encstr );
         docfrag = xmlNewDocFragment( real_dom );
         dfProxy = make_proxy_node( docfrag );
         docfrag_sv =sv_newmortal();
         xmlNodePtr docfrag = NULL;
         ProxyObject * dfProxy= NULL;
         SV * docfrag_sv = NULL;
+        char * encstr = NULL;
     CODE:
         real_dom = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
-        content = domEncodeString( real_dom->encoding, content );
-
-        newNode = domCreateCDATASection( real_dom, content );
-        
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+            encstr = domEncodeString( real_dom->encoding, content );
+        else 
+            encstr = xmlStrdup( content );
+
+        newNode = domCreateCDATASection( real_dom, encstr );
+        xmlFree(encstr);        
+
         docfrag = xmlNewDocFragment( real_dom );
         dfProxy = make_proxy_node( docfrag );
-        docfrag_sv =sv_newmortal();
+        docfrag_sv = sv_newmortal();
         sv_setref_pv( docfrag_sv, "XML::LibXML::DocumentFragment", (void*)dfProxy );
         dfProxy->extra = docfrag_sv;
         # warn( "NEW FRAGMENT CDATA");
         const char* CLASS = "XML::LibXML::Attr";
         xmlNodePtr newNode;
         xmlDocPtr real_dom;
+        xmlChar *encname = NULL;
+        xmlChar *encval  = NULL;
     CODE:
         real_dom = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
-        name  = domEncodeString( real_dom->encoding, name );
-        value = domEncodeString( real_dom->encoding, value );
-        
-        newNode = (xmlNodePtr)xmlNewProp(NULL, name , value );
-        xmlFree(name);
-        xmlFree(value);
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            encname  = domEncodeString( real_dom->encoding, name );
+            encval   = domEncodeString( real_dom->encoding, value );
+        }
+        else {
+            encname = xmlStrdup( name );
+            encval  = xmlStrdup( value );
+        }    
+        newNode = (xmlNodePtr)xmlNewProp(NULL, encname , encval );
+        xmlFree(encname);
+        xmlFree(encval);
+
         newNode->doc = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
         if ( newNode->children!=NULL ) {
             newNode->children->doc = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
         xmlNodePtr newNode;
         xmlChar *prefix;
         xmlChar *lname =NULL;
+        xmlChar *enclname =NULL;
+        xmlChar *encval =NULL;
         xmlNsPtr ns=NULL;
         xmlDocPtr real_dom;
     CODE:
         else{
             lname = qname;
         }
-        lname = domEncodeString( real_dom->encoding, lname );
-        value = domEncodeString( real_dom->encoding, value );
+    
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            enclname = domEncodeString( real_dom->encoding, lname );
+            encval = domEncodeString( real_dom->encoding, value );
+        }
+        else {
+            enclname = xmlStrdup( lname );
+            encval   = xmlStrdup( value );
+        }
         if ( ns != NULL ) {
             newNode = (xmlNodePtr) xmlNewNsProp(NULL, ns, lname , value );
         }
         }
         
         xmlFree(lname);
-        xmlFree(value);
+        xmlFree(enclname);
+        xmlFree(encval);
 
         newNode->doc = real_dom;
 
         xmlChar * encdata;
     CODE:
         real_dom = (xmlDocPtr)((ProxyObject*)SvIV((SV*)SvRV(dom)))->object;
-        enctarg = domEncodeString( real_dom->encoding, name );
-        encdata = domEncodeString( real_dom->encoding, content );
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            enctarg = domEncodeString( real_dom->encoding, name );
+            encdata = domEncodeString( real_dom->encoding, content );
+        }
+        else {
+            enctarg = xmlStrdup( name );
+            encdata = xmlStrdup( content );
+        }
+
         pinode = xmlNewPI( enctarg, encdata );
         
         xmlFree(enctarg);
         # warn( "NEW FRAGMENT ELEMNT (%s)", name);
         # SvREFCNT_inc(docfrag_sv);    
 
-        enctarg = domEncodeString( real_dom->encoding, name );
-        encdata = domEncodeString( real_dom->encoding, content );
+        if( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") ){
+            enctarg = domEncodeString( real_dom->encoding, name );
+            encdata = domEncodeString( real_dom->encoding, content );
+        }
+        else {
+            enctarg = xmlStrdup( name );
+            encdata = xmlStrdup( content );
+        }
+
         newNode = xmlNewPI( enctarg, encdata );
         xmlFree(enctarg);
         xmlFree(encdata);
     CODE:
         if( node != NULL ) {
             name =  domName( node );
+            if( ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+                && ( node->doc != NULL ) ){
+                char* strDecoded;
+                strDecoded = domDecodeString( node->doc->encoding, name );
+                xmlFree( name );
+                name = strDecoded;
+            }
             RETVAL = newSVpvn( (char *)name, xmlStrlen( name ) );
             xmlFree( name );
         }
         else {
+            # warn( "got no node\n" );
             RETVAL = &PL_sv_undef;
         }
     OUTPUT:
         xmlNodePtr node
         char * value
     CODE:
+        if( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+            && (node->doc != NULL) ){
+            xmlChar* strEncoded = domEncodeString( node->doc->encoding, value );
+            xmlFree( value );
+            value = strEncoded;
+        }
         domSetName( node, value );
 
 SV*
 
         if( node != NULL ) {
             if ( node->type != XML_ATTRIBUTE_NODE ){
-                if ( node->doc != NULL ){
+                if ( ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+                     && ( node->doc != NULL ) ){
                     content = domDecodeString( node->doc->encoding,
                                                node->content );
                 }
                 }
             }
             else if ( node->children != NULL ) {
-                if ( node->doc != NULL ){
+                if ( ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+                     && ( node->doc != NULL ) ){
                     content = domDecodeString( node->doc->encoding,
                                                node->children->content );
                 }
         }
         xmlBufferFree( buffer );
         
-        if ( self->doc != NULL ) {
+        if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+             && (self->doc != NULL) ) {
             xmlChar *retDecoded = domDecodeString( self->doc->encoding, ret );
             xmlFree( ret );
             ret= retDecoded;
         char * lname;
     CODE:
         if( node != NULL ) {
-            if ( node->doc != NULL ) {
+            if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+                 && (node->doc != NULL) ) {
                 lname = domDecodeString( node->doc->encoding, node->name );
             }
             else {
         if( node != NULL 
             && node->ns != NULL
             && node->ns->prefix != NULL ) {
-            if ( node->doc != NULL ) {
+            if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+                 && (node->doc != NULL) ) {
                 prefix = domDecodeString( node->doc->encoding, 
                                           node->ns->prefix );
             }
         char * name
         char * value
     CODE:
-        if( elem->doc != NULL ) {
+        if( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+            && (elem->doc != NULL) ) {
             name  = domEncodeString( elem->doc->encoding, name );
             value = domEncodeString( elem->doc->encoding, value );
         }
         xmlChar *lname = NULL;
         xmlNsPtr ns = NULL;
     CODE:
-        if( elem->doc != NULL ) {
+        if( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+            && (elem->doc != NULL) ) {
             qname = domEncodeString( elem->doc->encoding, qname );
             value = domEncodeString( elem->doc->encoding, value );
         }
             xmlSetProp( elem, qname, value );
         }
 
-        if ( elem->doc != NULL ) {
+        if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+             && (elem->doc != NULL) ) {
             xmlFree( qname );
             xmlFree( value );
         }
     CODE:
         content = xmlGetProp( elem->object, name );
         if ( content != NULL ) {
-            if ( ((xmlNodePtr)elem->object)->doc != NULL ){
+            if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING"))
+                 && (((xmlNodePtr)elem->object)->doc != NULL) ){
                 xmlChar* deccontent = domDecodeString( ((xmlNodePtr)elem->object)->doc->encoding, content );
                xmlFree( content);
                content = deccontent;
             content = xmlStrdup( att->children->content ); 
         }
         if ( content != NULL ) {
-            if ( ((xmlNodePtr)elem->object)->doc != NULL ){
+            if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+                 && (((xmlNodePtr)elem->object)->doc != NULL) ){
                 xmlChar *deccontent = domDecodeString( ((xmlNodePtr)elem->object)->doc->encoding, content );
                 xmlFree( content );
                 content = deccontent;
     PREINIT:
         xmlNodePtr rv;
     CODE:
-        if( self->doc != NULL ) {
+        if( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING"))
+            && (self->doc != NULL) ) {
             chunk = domEncodeString( self->doc->encoding, chunk );
         }
         LibXML_error = sv_2mortal(newSVpv("", 0));
         xmlNodePtr tn;
     CODE:
         if ( self->doc != NULL && xmlString != NULL ) {
-            if ( self->doc != NULL ) {
+            if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING"))
+                 && (self->doc != NULL) ) {
                 xmlString = domEncodeString( self->doc->encoding, xmlString );
                 tn = xmlNewDocText( self->doc, xmlString ); 
                 xmlFree(xmlString);
         xmlChar * encname = NULL;
         xmlChar * enccontent= NULL;
     CODE:
-        if( self->doc != NULL ) {
+        if( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING"))
+            && (self->doc != NULL) ) {
             childname = domEncodeString( self->doc->encoding, childname );
             xmlString = domEncodeString( self->doc->encoding, xmlString );
         }
         xmlNewTextChild( self, NULL, childname, xmlString );
-        if (self->doc != NULL) {
+        if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+             && (self->doc != NULL)) {
             xmlFree(childname);
             xmlFree(xmlString);
         }
 _setData( node, value )
         xmlNodePtr node
         char * value
+    PREINIT:
+        xmlChar* encstr;
     CODE:
-        domSetNodeValue(node,domEncodeString(node->doc->encoding,value));
+        if ( (TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING")) 
+             && (node->doc != NULL) ) {
+            encstr = domEncodeString(node->doc->encoding,value);
+        }
+        else {
+            encstr = xmlStrdup( value );
+        }
+        domSetNodeValue( node, encstr );
+        xmlFree( encstr );
 
 MODULE = XML::LibXML         PACKAGE = XML::LibXML::Text
 
         char * value
     ALIAS:
         XML::LibXML::Attr::setValue = 1 
+    PREINIT:
+        xmlChar* encstr = NULL;
     CODE:
-        if ( node->doc != NULL ) {
-            value = domEncodeString( node->doc->encoding, value );
+        if ( ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+             && ( node->doc != NULL ) 
+             && ( value != NULL ) ) {
+            char * encvalue = domEncodeString( node->doc->encoding, value );
             # encode the entities
-            value = xmlEncodeEntitiesReentrant( node->doc, value );
+            value = xmlEncodeEntitiesReentrant( node->doc, encvalue );
+            if ( encvalue != NULL ) {
+                xmlFree( encvalue );
+            }
         }
         domSetNodeValue( node, value );
+        if ( ( TEST_PERL_FLAG("XML::LibXML::ORIGINAL_STRING") )
+             && ( node->doc != NULL ) 
+             && ( value != NULL ) ) {
+            xmlFree( value );
+        }
 
 ProxyObject *
 new( CLASS, content )
         xmlNodePtr attr = NULL;
     CODE:
         attr = (xmlNodePtr)xmlNewProp( NULL, name, value );
+        RETVAL = NULL;
         if ( attr ) {
+            attr->doc = NULL; /* # just to be save */
             RETVAL = make_proxy_node( attr );
         }
     OUTPUT:
  **/
 xmlNodePtr 
 domReadWellBalancedString( xmlDocPtr doc, xmlChar* block ) {
-  int retCode       = -1;
-  xmlNodePtr helper = NULL;
-  xmlNodePtr nodes  = NULL;
-  
-  if ( block ) {
-    /* read and encode the chunk */
-    retCode = xmlParseBalancedChunkMemory( doc, 
-                                           NULL,
-                                           NULL,
-                                           0,
-                                           block,
-                                           &nodes );
+    int retCode       = -1;
+    xmlNodePtr helper = NULL;
+    xmlNodePtr nodes  = NULL;
+    
+    if ( block ) {
+        /* read and encode the chunk */
+        retCode = xmlParseBalancedChunkMemory( doc, 
+                                               NULL,
+                                               NULL,
+                                               0,
+                                               block,
+                                               &nodes );
 
-    /* error handling */
-    if ( retCode != 0 ) {
-      /* if the code was not well balanced, we will not return 
-       * a bad node list, but we have to free the nodes */
-      while( nodes != NULL ) {
-        helper = nodes->next;
-        xmlFreeNode( nodes );
-        nodes = helper;
-      }
+        /* error handling */
+        if ( retCode != 0 ) {
+            /* if the code was not well balanced, we will not return 
+             * a bad node list, but we have to free the nodes */
+            while( nodes != NULL ) {
+                helper = nodes->next;
+                xmlFreeNode( nodes );
+                nodes = helper;
+            }
+        }
     }
-  }
 
-  return nodes;
+    return nodes;
 }
 
 /** 
  **/ 
 xmlChar*
 domEncodeString( const char *encoding, const char *string ){
-  xmlCharEncoding enc;
-  xmlChar *ret = NULL;
-
-  if ( string != NULL ) {
-    if( encoding != NULL ) {
-      enc = xmlParseCharEncoding( encoding );
-      if ( enc > 0 ) {
-        if( enc > 1 ) {
-          xmlBufferPtr in, out;
-          xmlCharEncodingHandlerPtr coder ;
-          in  = xmlBufferCreate();
-          out = xmlBufferCreate();
-          
-          coder = xmlGetCharEncodingHandler( enc );
-          
-          xmlBufferCCat( in, string );
-          
-          if ( xmlCharEncInFunc( coder, out, in ) >= 0 ) {
-            ret = xmlStrdup( out->content );
-          }
-          else {
-            /* printf("encoding error\n"); */
-          }
-          
-          xmlBufferFree( in );
-          xmlBufferFree( out );
+    xmlCharEncoding enc;
+    xmlChar *ret = NULL;
+    
+    if ( string != NULL ) {
+        if( encoding != NULL ) {
+            enc = xmlParseCharEncoding( encoding );
+            if ( enc > 0 ) {
+                if( enc > 1 ) {
+                    xmlBufferPtr in, out;
+                    xmlCharEncodingHandlerPtr coder ;
+                    in  = xmlBufferCreate();
+                    out = xmlBufferCreate();
+                    
+                    coder = xmlGetCharEncodingHandler( enc );
+                    
+                    xmlBufferCCat( in, string );
+                    
+                    if ( xmlCharEncInFunc( coder, out, in ) >= 0 ) {
+                        ret = xmlStrdup( out->content );
+                    }
+                    else {
+                        /* printf("encoding error\n"); */
+                    }
+                    
+                    xmlBufferFree( in );
+                    xmlBufferFree( out );
+                }
+                else {
+                    /* if utf-8 is requested we do nothing */
+                    ret = xmlStrdup( string );
+                }
+            }
+            else {
+                /* printf( "encoding error: no enciding\n" ); */
+            }
         }
         else {
-          /* if utf-8 is requested we do nothing */
-          ret = xmlStrdup( string );
+            /* if utf-8 is requested we do nothing */
+            ret = xmlStrdup( string );
         }
-      }
-      else {
-        /* printf( "encoding error: no enciding\n" ); */
-      }
     }
-    else {
-      /* if utf-8 is requested we do nothing */
-      ret = xmlStrdup( string );
-    }
-  }
-  return ret;
+    return ret;
 }
 
 /**
  **/
 char*
 domDecodeString( const char *encoding, const xmlChar *string){
-  char *ret=NULL;
-  xmlBufferPtr in, out;
- 
-  if ( string != NULL ) {
-    if( encoding != NULL ) {
-      xmlCharEncoding enc = xmlParseCharEncoding( encoding );
-      /*      printf("encoding: %d\n", enc ); */
-      if ( enc > 0 ) {
-        if( enc > 1 ) {
-          xmlBufferPtr in, out;
-          xmlCharEncodingHandlerPtr coder;
-          in  = xmlBufferCreate();
-          out = xmlBufferCreate();
-
-          coder = xmlGetCharEncodingHandler( enc );
-          xmlBufferCat( in, string );        
-          
-          if ( xmlCharEncOutFunc( coder, out, in ) >= 0 ) {
-            ret=xmlStrdup(out->content);
-          }
-          else {
-            /* printf("decoding error \n"); */
-          }
-
-          xmlBufferFree( in );
-          xmlBufferFree( out );
+    char *ret=NULL;
+    xmlBufferPtr in, out;
+    
+    if ( string != NULL ) {
+        if( encoding != NULL ) {
+            xmlCharEncoding enc = xmlParseCharEncoding( encoding );
+            /*      printf("encoding: %d\n", enc ); */
+            if ( enc > 0 ) {
+                if( enc > 1 ) {
+                    xmlBufferPtr in, out;
+                    xmlCharEncodingHandlerPtr coder;
+                    in  = xmlBufferCreate();
+                    out = xmlBufferCreate();
+                    
+                    coder = xmlGetCharEncodingHandler( enc );
+                    xmlBufferCat( in, string );        
+                    
+                    if ( xmlCharEncOutFunc( coder, out, in ) >= 0 ) {
+                        ret=xmlStrdup(out->content);
+                    }
+                    else {
+                        /* printf("decoding error \n"); */
+                    }
+                    
+                    xmlBufferFree( in );
+                    xmlBufferFree( out );
+                }
+                else {
+                    ret = xmlStrdup(string);
+                }
+            }
+            else {
+                /* warn( "decoding error:no encoding\n" ); */
+                ret = xmlStrdup( string );
+            }
         }
         else {
-          ret = xmlStrdup(string);
+            /* if utf-8 is requested we do nothing */
+            ret = xmlStrdup( string );
         }
-      }
-      else {
-        /* warn( "decoding error:no encoding\n" ); */
-        ret = xmlStrdup( string );
-      }
     }
-    else {
-      /* if utf-8 is requested we do nothing */
-      ret = xmlStrdup( string );
-    }
-  }
-  return ret;
+    return ret;
 }
 
 /** 
  **/
 xmlNodePtr 
 insert_node_to_nodelist( xmlNodePtr lead, xmlNodePtr node, xmlNodePtr follow ){
-  xmlNodePtr cld1, cld2, par;
-  if( node == NULL ) {
-    return;
-  }
+    xmlNodePtr cld1, cld2, par;
+    if( node == NULL ) {
+        return;
+    }
 
-  cld1 = node;
-  cld2 = node;
-  par = NULL;
+    cld1 = node;
+    cld2 = node;
+    par = NULL;
 
-  if( lead != NULL ) {
-    par = lead->parent;
-  }
-  else if( follow != NULL ) {
-    par = follow->parent;
-  }
+    if( lead != NULL ) {
+        par = lead->parent;
+    }
+    else if( follow != NULL ) {
+        par = follow->parent;
+    }
 
-  if( node->type == XML_DOCUMENT_FRAG_NODE ) {
-    xmlNodePtr hn = node->children;
+    if( node->type == XML_DOCUMENT_FRAG_NODE ) {
+        xmlNodePtr hn = node->children;
+        
+        cld1 = node->children;
+        cld2 = node->last;
+        node->last = node->children = NULL;
 
-    cld1 = node->children;
-    cld2 = node->last;
-    node->last = node->children = NULL;
-
-    while ( hn ) {
-      hn->parent = par;
-      hn = hn->next;
+        while ( hn ) {
+            hn->parent = par;
+            hn = hn->next;
+        }
     }
-  }
   
 
-  if( cld1 != NULL && cld2 != NULL && par != NULL ) {
-    cld1->parent = par;
-    cld2->parent = par;
+    if( cld1 != NULL && cld2 != NULL && par != NULL ) {
+        cld1->parent = par;
+        cld2->parent = par;
    
-    if ( lead == NULL ) {
-      par->children = cld1;
+        if ( lead == NULL ) {
+            par->children = cld1;
+        }
+        else {
+            lead->next = cld1;
+            cld1->prev  = lead;
+        }
+  
+        if ( follow == NULL ){
+            par->last = cld2;
+        } 
+        else {
+            follow->prev = cld2;
+            cld2->next  = follow;
+        }
     }
-    else {
-        lead->next = cld1;
-        cld1->prev  = lead;
-    }
-  
-    if ( follow == NULL ){
-      par->last = cld2;
-    } 
-    else {
-      follow->prev = cld2;
-      cld2->next  = follow;
-    }
-  }
 
-  return cld1;
+    return cld1;
 }
 
 xmlNodePtr
  **/
 xmlChar*
 domName(xmlNodePtr node) {
-  xmlChar *qname = NULL; 
-  if ( node ) {
-    if (node->ns != NULL) {
-      if (node->ns->prefix != NULL) {
-        xmlChar *tname = xmlStrdup( node->ns->prefix );
-        tname = xmlStrcat( tname , ":" );
-        tname = xmlStrcat( tname , node->name );
-        if ( node->doc != NULL ) {
-          qname = domDecodeString( node->doc->encoding , tname );
-          xmlFree( tname );
+    xmlChar *qname = NULL; 
+    if ( node ) {
+        if (node->ns != NULL && node->ns->prefix != NULL) {
+            xmlChar *tname = xmlStrdup( node->ns->prefix );
+            tname = xmlStrcat( tname , ":" );
+            tname = xmlStrcat( tname , node->name );
+            qname = tname;
+        } 
+        else {
+            qname = xmlStrdup( node->name );
         }
-        else {
-          qname = tname;
-        }
-      } 
-      else {
-        if ( node->doc != NULL ) {
-          qname = domDecodeString( node->doc->encoding , node->name );
-        }
-        else {
-          qname = xmlStrdup( node->name );
-        }
-      }
-    } 
-    else {
-      if ( node->doc != NULL ) {
-        qname = domDecodeString( node->doc->encoding , node->name );
-      }
-      else {
-        qname = xmlStrdup( node->name );
-      }
     }
-  }
-  return qname;
+
+    return qname;
 }
 
 void
 domSetName( xmlNodePtr node, char* name ) {
-  xmlChar* str = NULL;  
-  /* TODO: add ns support */
-  if ( node == NULL || name == NULL ) 
-    return ;
-  if ( node->name != NULL ) {
-    /* required since node->name is const! */
-    xmlFree( (void*) node->name );
-  }
+    xmlChar* str = NULL;  
+    /* TODO: add ns support */
+    if ( node == NULL || name == NULL ) 
+        return ;
+    if ( node->name != NULL ) {
+        /* required since node->name is const! */
+        xmlFree( (void*) node->name );
+    }
 
-  if ( node->doc != NULL ) {
-    str = domEncodeString( node->doc->encoding , name );
-  }
-  else {
-    str = xmlStrdup( name );
-  }
-  node->name = str;
+    node->name = xmlStrdup( name );
 }
 
 /**
 
 xmlNodePtr
 domReplaceChild( xmlNodePtr self, xmlNodePtr new, xmlNodePtr old ) {
-  if ( self== NULL ){
-    return NULL;
-  }
-  if ( new == NULL ) {
-    /* level2 sais nothing about this case :( */
-    return domRemoveChild( self, old );
-  }
+    if ( self== NULL ){
+        return NULL;
+    }
+    if ( new == NULL ) {
+        /* level2 sais nothing about this case :( */
+        return domRemoveChild( self, old );
+    }
 
-  /* handle the different node types */
-  switch( new->type ) {
-  case XML_ATTRIBUTE_NODE:
-    return NULL;
-    break;
-  default:
-    break;
-  }
+    /* handle the different node types */
+    switch( new->type ) {
+    case XML_ATTRIBUTE_NODE:
+        return NULL;
+        break;
+    default:
+        break;
+    }
+    
+    if( ( old != NULL 
+          && ( old->type == XML_ATTRIBUTE_NODE 
+               || old->type == XML_DOCUMENT_FRAG_NODE 
+               || old->parent != self ) )
+        || self->type== XML_ATTRIBUTE_NODE
+        || domIsNotParentOf( new, self ) == NULL 
+        || ( new->type == XML_DOCUMENT_FRAG_NODE && new->children == NULL ) ) { 
+        
+        /* HIERARCHY_REQUEST_ERR */
+        return NULL;
+    }
+    
+    if ( new == old ) {
+        /* dom level 2 throws no exception if new and old are equal */ 
+        return new;
+    }
 
-  if( ( old != NULL 
-        && ( old->type == XML_ATTRIBUTE_NODE 
-             || old->type == XML_DOCUMENT_FRAG_NODE 
-             || old->parent != self ) )
-      || self->type== XML_ATTRIBUTE_NODE
-      || domIsNotParentOf( new, self ) == NULL 
-      || ( new->type == XML_DOCUMENT_FRAG_NODE && new->children == NULL ) ) { 
+    if ( old == NULL ) {
+        domAppendChild( self, new );
+        return old;
+    }
 
-    /* HIERARCHY_REQUEST_ERR */
-    return NULL;
-  }
+    if ( new->doc == self->doc ) {
+      new = domUnbindNode( new ) ;
+    }
+    else {
+        /* WRONG_DOCUMENT_ERR - non conform implementation */
+        new = domImportNode( self->doc, new, 1 );
+    }
+    
+    if( old == self->children && old == self->last ) {
+        domRemoveChild( self, old );
+        domAppendChild( self, new );
+    }
+    else {
+        insert_node_to_nodelist( old->prev, new, old->next );
+        old->parent = NULL;
+        old->next   = NULL;
+        old->prev   = NULL;    
+    }
 
-  if ( new == old ) {
-    /* dom level 2 throws no exception if new and old are equal */ 
-    return new;
-  }
-
-  if ( old == NULL ) {
-    domAppendChild( self, new );
     return old;
-  }
-
-  if ( new->doc == self->doc ) {
-    new = domUnbindNode( new ) ;
-  }
-  else {
-    /* WRONG_DOCUMENT_ERR - non conform implementation */
-    new = domImportNode( self->doc, new, 1 );
-  }
-  
-  if( old == self->children && old == self->last ) {
-    domRemoveChild( self, old );
-    domAppendChild( self, new );
-  }
-  else {
-    insert_node_to_nodelist( old->prev, new, old->next );
-    old->parent = NULL;
-    old->next   = NULL;
-    old->prev   = NULL;    
-  }
-
-  return old;
 }
 
 xmlNodePtr
 domRemoveChild( xmlNodePtr self, xmlNodePtr old ) {
-  if ( (self != NULL)  
-       && (old!=NULL) 
-       && (self == old->parent )
-       && (old->type != XML_ATTRIBUTE_NODE) 
-       ) {
-    old = domUnbindNode( old );
-  }
-  return old ;
+    if ( (self != NULL)  
+         && (old!=NULL) 
+         && (self == old->parent )
+         && (old->type != XML_ATTRIBUTE_NODE) 
+         ) {
+        old = domUnbindNode( old );
+    }
+    return old ;
 }
 
 xmlNodePtr
 
 xmlNodePtr
 domReplaceNode( xmlNodePtr oldnode, xmlNodePtr newnode ){
-  xmlNodePtr prev, next, par;
-  if ( oldnode != NULL ) {
-    if( newnode == NULL ) {
-      domUnbindNode( oldnode );
+    xmlNodePtr prev, next, par;
+    if ( oldnode != NULL ) {
+        if( newnode == NULL ) {
+            domUnbindNode( oldnode );
+        }
+        else {
+            par  = oldnode->parent;
+            prev = oldnode->prev;
+            next = oldnode->next;
+            domUnbindNode( oldnode );
+            if( prev == NULL && next == NULL ) {
+                domAppendChild( par ,newnode ); 
+            }
+            else {
+                insert_node_to_nodelist( prev, newnode, next );
+            }
+        }
     }
-    else {
-      par  = oldnode->parent;
-      prev = oldnode->prev;
-      next = oldnode->next;
-      domUnbindNode( oldnode );
-      if( prev == NULL && next == NULL ) {
-        domAppendChild( par ,newnode ); 
-      }
-      else {
-        insert_node_to_nodelist( prev, newnode, next );
-      }
-    }
-  }
-  return oldnode;
+    return oldnode;
 }
 
 void
 domSetNodeValue( xmlNodePtr n , xmlChar* val ){
-  if ( n == NULL ) 
-    return;
-  if ( val == NULL ){
-    val = "";
-  }
+    if ( n == NULL ) 
+        return;
+    if ( val == NULL ){
+        val = "";
+    }
   
-  if( n->type == XML_ATTRIBUTE_NODE ){
-    if ( n->children != NULL ) {
-      n->last = NULL;
-      xmlFreeNodeList( n->children );
+    if( n->type == XML_ATTRIBUTE_NODE ){
+        if ( n->children != NULL ) {
+            n->last = NULL;
+            xmlFreeNodeList( n->children );
+        }
+        n->children = xmlNewText( val );
+        n->children->parent = n;
+        n->children->doc = n->doc;
+        n->last = n->children; 
     }
-    n->children = xmlNewText( val );
-    n->children->parent = n;
-    n->children->doc = n->doc;
-    n->last = n->children; 
-  }
-  else if( n->content != NULL ) {
-    /* free old content */
-    xmlFree( n->content );
-    n->content = xmlStrdup(val);
-
-  }
+    else if( n->content != NULL ) {
+        /* free old content */
+        xmlFree( n->content );
+        n->content = xmlStrdup(val);   
+    }
 }
 
 
 void
 domSetParentNode( xmlNodePtr self, xmlNodePtr p ) {
-  /* never set the parent to a node in the own subtree */ 
-  self = domIsNotParentOf( self, p );
-  if( self != NULL ){
-    if( self->parent != p ){
-      domUnbindNode( self );
-      self->parent = p;
-      if( p->doc != self->doc ) {
-	self->doc = p->doc;
-      }
+    /* never set the parent to a node in the own subtree */ 
+    self = domIsNotParentOf( self, p );
+    if( self != NULL ){
+        if( self->parent != p ){
+            domUnbindNode( self );
+            self->parent = p;
+            if( p->doc != self->doc ) {
+                self->doc = p->doc;
+            }
+        }
     }
-  }
 }
 
 xmlNodePtr
 domUnbindNode( xmlNodePtr self ) {
-  if ( (self != NULL) && (self->parent != NULL) ) { 
-    if( self->parent!=NULL ) {
-      if( self->parent->properties == (xmlAttrPtr) self ) 
-        self->parent->properties = (xmlAttrPtr)self->next;
-      if ( self == self->parent->last ) 
-        self->parent->last = self->prev;
-      if ( self == self->parent->children ) 
-        self->parent->children = self->next;
+    if ( (self != NULL) && (self->parent != NULL) ) { 
+        if( self->parent!=NULL ) {
+            if( self->parent->properties == (xmlAttrPtr) self ) 
+                self->parent->properties = (xmlAttrPtr)self->next;
+            if ( self == self->parent->last ) 
+                self->parent->last = self->prev;
+            if ( self == self->parent->children ) 
+                self->parent->children = self->next;
+        }
+        if ( self->next != NULL )
+            self->next->prev = self->prev;
+        if ( self->prev != NULL )
+            self->prev->next = self->next;
+        
+        self->parent = NULL;
+        self->next   = NULL;
+        self->prev   = NULL;
     }
-    if ( self->next != NULL )
-      self->next->prev = self->prev;
-    if ( self->prev != NULL )
-      self->prev->next = self->next;
-
-    self->parent = NULL;
-    self->next   = NULL;
-    self->prev   = NULL;
-  }
-  return self;
+    return self;
 }
 
 /**
 
 const char*
 domNodeTypeName( xmlNodePtr elem ){
-  const char *name = "XML::LibXML::Node";
+    const char *name = "XML::LibXML::Node";
 
-  if ( elem != NULL ) {
-    char * ptrHlp;
-    switch ( elem->type ) {
-    case XML_ELEMENT_NODE:
-      name = "XML::LibXML::Element";   
-      break;
-    case XML_TEXT_NODE:
-      name = "XML::LibXML::Text";
-      break;
-    case XML_COMMENT_NODE:
-      name = "XML::LibXML::Comment";
-      break;
-    case XML_CDATA_SECTION_NODE:
-      name = "XML::LibXML::CDATASection";
-      break;
-    case XML_ATTRIBUTE_NODE:
-      name = "XML::LibXML::Attr"; 
-      break;
-    case XML_DOCUMENT_NODE:
-      name = "XML::LibXML::Document";
-      break;
-    case XML_DOCUMENT_FRAG_NODE:
-      name = "XML::LibXML::DocumentFragment";
-      break;
-    case XML_NAMESPACE_DECL:
-      name = "XML::LibXML::Namespace";
-      break;
-    case XML_DTD_NODE:
-      name = "XML::LibXML::Dtd";
-      break;
-    case XML_PI_NODE:
-      name = "XML::LibXML::PI";
-      break;
-    default:
-      name = "XML::LibXML::Node";
-      break;
-    };
-    return name;
-  }
+    if ( elem != NULL ) {
+        char * ptrHlp;
+        switch ( elem->type ) {
+        case XML_ELEMENT_NODE:
+            name = "XML::LibXML::Element";   
+            break;
+        case XML_TEXT_NODE:
+            name = "XML::LibXML::Text";
+            break;
+        case XML_COMMENT_NODE:
+            name = "XML::LibXML::Comment";
+            break;
+        case XML_CDATA_SECTION_NODE:
+            name = "XML::LibXML::CDATASection";
+            break;
+        case XML_ATTRIBUTE_NODE:
+            name = "XML::LibXML::Attr"; 
+            break;
+        case XML_DOCUMENT_NODE:
+            name = "XML::LibXML::Document";
+            break;
+        case XML_DOCUMENT_FRAG_NODE:
+            name = "XML::LibXML::DocumentFragment";
+            break;
+        case XML_NAMESPACE_DECL:
+            name = "XML::LibXML::Namespace";
+            break;
+        case XML_DTD_NODE:
+            name = "XML::LibXML::Dtd";
+            break;
+        case XML_PI_NODE:
+            name = "XML::LibXML::PI";
+            break;
+        default:
+            name = "XML::LibXML::Node";
+            break;
+        };
+        return name;
+    }
 
-  return "";
+    return "";
 }
 
 xmlNodePtr
 domCreateCDATASection( xmlDocPtr self , xmlChar * strNodeContent ){
-  xmlNodePtr elem = NULL;
+    xmlNodePtr elem = NULL;
 
-  if ( ( self != NULL ) && ( strNodeContent != NULL ) ) {
-    strNodeContent = domEncodeString( self->encoding, strNodeContent );
-    elem = xmlNewCDataBlock( self, strNodeContent, xmlStrlen(strNodeContent) );
-    elem->next = NULL;
-    elem->prev = NULL;
-    elem->children = NULL ;
-    elem->last = NULL;
-    elem->doc = self->doc;   
-  }
+    if ( ( self != NULL ) && ( strNodeContent != NULL ) ) {
+        elem = xmlNewCDataBlock( self, strNodeContent, xmlStrlen(strNodeContent) );
+        elem->next = NULL;
+        elem->prev = NULL;
+        elem->children = NULL ;
+        elem->last = NULL;
+        elem->doc = self->doc;   
+    }
 
-  return elem;
+    return elem;
 }
 
 
 xmlNodePtr 
 domDocumentElement( xmlDocPtr doc ) {
-  xmlNodePtr cld=NULL;
-  if ( doc != NULL && doc->doc != NULL && doc->doc->children != NULL ) {
-    cld= doc->doc->children;
-    while ( cld != NULL && cld->type != XML_ELEMENT_NODE ) 
-      cld= cld->next;
+    xmlNodePtr cld=NULL;
+    if ( doc != NULL && doc->doc != NULL && doc->doc->children != NULL ) {
+        cld= doc->doc->children;
+        while ( cld != NULL && cld->type != XML_ELEMENT_NODE ) 
+            cld= cld->next;
   
-  }
-  return cld;
+    }
+    return cld;
 }
 
 /**
  **/
 xmlNodePtr
 domSetDocumentElement( xmlDocPtr doc, xmlNodePtr newRoot ) { 
-  return domReplaceChild( (xmlNodePtr)doc, 
-			  newRoot, 
-			  domDocumentElement( doc )) ;
+    return domReplaceChild( (xmlNodePtr)doc, 
+                            newRoot, 
+                            domDocumentElement( doc )) ;
 }
 
 xmlNodePtr
 domImportNode( xmlDocPtr doc, xmlNodePtr node, int move ) {
-  xmlNodePtr return_node = node;
+    xmlNodePtr return_node = node;
 
-  if ( node && node->doc != doc ) {
-    if ( move ) {
-      return_node = domUnbindNode( node );
+    if ( node && node->doc != doc ) {
+        if ( move ) {
+            return_node = domUnbindNode( node );
+        }
+        else {
+            return_node = xmlCopyNode( node, 1 );
+        }
+        /* tell all children about the new boss */ 
+        return_node = domSetOwnerDocument( return_node, doc ); 
     }
-    else {
-      return_node = xmlCopyNode( node, 1 );
-    }
-    /* tell all children about the new boss */ 
-    return_node = domSetOwnerDocument( return_node, doc ); 
-  }
  
-  return return_node;
+    return return_node;
 }
 
 xmlNodeSetPtr
 domGetElementsByTagName( xmlNodePtr n, xmlChar* name ){
-  xmlNodeSetPtr rv = NULL;
-  xmlNodePtr cld = NULL;
+    xmlNodeSetPtr rv = NULL;
+    xmlNodePtr cld = NULL;
 
-  if ( n != NULL && name != NULL ) {
-    cld = n->children;
-    while ( cld != NULL ) {
-      if ( xmlStrcmp( name, cld->name ) == 0 ){
-        if ( rv == NULL ) {
-          rv = xmlXPathNodeSetCreate( cld ) ;
+    if ( n != NULL && name != NULL ) {
+        cld = n->children;
+        while ( cld != NULL ) {
+            if ( xmlStrcmp( name, cld->name ) == 0 ){
+                if ( rv == NULL ) {
+                    rv = xmlXPathNodeSetCreate( cld ) ;
+                }
+                else {
+                    xmlXPathNodeSetAdd( rv, cld );
+                }
+            }
+            cld = cld->next;
         }
-        else {
-          xmlXPathNodeSetAdd( rv, cld );
-        }
-      }
-      cld = cld->next;
     }
-  }
   
-  return rv;
+    return rv;
 }
 
 
 xmlNodeSetPtr
 domGetElementsByTagNameNS( xmlNodePtr n, xmlChar* nsURI, xmlChar* name ){
-  xmlNodeSetPtr rv = NULL;
+    xmlNodeSetPtr rv = NULL;
 
-  if ( nsURI == NULL ) {
-    return domGetElementsByTagName( n, name );
-  }
+    if ( nsURI == NULL ) {
+        return domGetElementsByTagName( n, name );
+    }
   
-  if ( n != NULL && name != NULL  ) {
-    xmlNodePtr cld = n->children;
-    while ( cld != NULL ) {
-      if ( xmlStrcmp( name, cld->name ) == 0 
-           && cld->ns != NULL
-           && xmlStrcmp( nsURI, cld->ns->href ) == 0  ){
-        if ( rv == NULL ) {
-          rv = xmlXPathNodeSetCreate( cld ) ;
+    if ( n != NULL && name != NULL  ) {
+        xmlNodePtr cld = n->children;
+        while ( cld != NULL ) {
+            if ( xmlStrcmp( name, cld->name ) == 0 
+                 && cld->ns != NULL
+                 && xmlStrcmp( nsURI, cld->ns->href ) == 0  ){
+                if ( rv == NULL ) {
+                    rv = xmlXPathNodeSetCreate( cld ) ;
+                }
+                else {
+                    xmlXPathNodeSetAdd( rv, cld );
+                }
+            }
+            cld = cld->next;
         }
-        else {
-          xmlXPathNodeSetAdd( rv, cld );
-        }
-      }
-      cld = cld->next;
     }
-  }
   
-  return rv;
+    return rv;
 }
 
 xmlNodePtr
 
 xmlNsPtr
 domNewNs ( xmlNodePtr elem , xmlChar *prefix, xmlChar *href ) {
-  xmlNsPtr ns = NULL;
+    xmlNsPtr ns = NULL;
   
-  if (elem != NULL) {
-    ns = xmlSearchNs( elem->doc, elem, prefix );
-  }
-  /* prefix is not in use */
-  if (ns == NULL) {
-    ns = xmlNewNs( elem , href , prefix );
-  } else {
-    /* prefix is in use; if it has same URI, let it go, otherwise it's
-       an error */
-    if (!xmlStrEqual(href, ns->href)) {
-      ns = NULL;
+    if (elem != NULL) {
+        ns = xmlSearchNs( elem->doc, elem, prefix );
     }
-  }
-  return ns;
+    /* prefix is not in use */
+    if (ns == NULL) {
+        ns = xmlNewNs( elem , href , prefix );
+    } else {
+        /* prefix is in use; if it has same URI, let it go, otherwise it's
+           an error */
+        if (!xmlStrEqual(href, ns->href)) {
+            ns = NULL;
+        }
+    }
+    return ns;
 }
 
 /* This routine may or may not make it into libxml2; Matt wanted it in
  */
 xmlAttrPtr
 domHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
-  xmlAttrPtr prop;
-  xmlDocPtr doc;
-  xmlNsPtr ns;
-  
-  if (node == NULL)
- 	return(NULL);
-  
-  prop = node->properties;
-  if (namespace == NULL)
-    return(xmlHasProp(node, name));
-  while (prop != NULL) {
-    /*
-     * One need to have
-     *   - same attribute names
-     *   - and the attribute carrying that namespace
- 	 *         or
-     
-     SJT: This following condition is wrong IMHO; I reported it as a bug on libxml2
-     
-	 *         no namespace on the attribute and the element carrying it
-	 */
-    if ((xmlStrEqual(prop->name, name)) &&
-        (/* ((prop->ns == NULL) && (node->ns != NULL) &&
-            (xmlStrEqual(node->ns->href, namespace))) || */
-         ((prop->ns != NULL) &&
-          (xmlStrEqual(prop->ns->href, namespace))))) {
-      return(prop);
+    xmlAttrPtr prop;
+    xmlDocPtr doc;
+    xmlNsPtr ns;
+    
+    if (node == NULL)
+        return(NULL);
+    
+    prop = node->properties;
+    if (namespace == NULL)
+        return(xmlHasProp(node, name));
+    while (prop != NULL) {
+        /*
+         * One need to have
+         *   - same attribute names
+         *   - and the attribute carrying that namespace
+         *         or
+         
+         SJT: This following condition is wrong IMHO; I reported it as a bug on libxml2
+         
+         *         no namespace on the attribute and the element carrying it
+         */
+        if ((xmlStrEqual(prop->name, name)) &&
+            (/* ((prop->ns == NULL) && (node->ns != NULL) &&
+                (xmlStrEqual(node->ns->href, namespace))) || */
+             ((prop->ns != NULL) &&
+              (xmlStrEqual(prop->ns->href, namespace))))) {
+            return(prop);
+        }
+        prop = prop->next;
     }
-    prop = prop->next;
-  }
   
 #if 0
-  /* xmlCheckDTD is static in libxml/tree.c; it is set there to 1
-     and never changed, so commenting this out doesn't change the
-     behaviour */
-  if (!xmlCheckDTD) return(NULL);
+    /* xmlCheckDTD is static in libxml/tree.c; it is set there to 1
+       and never changed, so commenting this out doesn't change the
+       behaviour */
+    if (!xmlCheckDTD) return(NULL);
 #endif
   
-  /*
-   * Check if there is a default declaration in the internal
-   * or external subsets
-   */
-  doc =  node->doc;
-  if (doc != NULL) {
-    if (doc->intSubset != NULL) {
-      xmlAttributePtr attrDecl;
+    /*
+     * Check if there is a default declaration in the internal
+     * or external subsets
+     */
+    doc =  node->doc;
+    if (doc != NULL) {