[gdome]gdome_doc_importNode



On 24 Apr 2001, rob wrote:

> On 24 Apr 2001 11:57:00 +0200, Paolo Casarini wrote:
> > 
> >   News:
> >   - Fixed a bug in gdome_doc_importNode.
> 
> hi just upgraded to gdome2 0.6.3 and libxml2 2.3.7 and now
> gdome_doc_importNode raises a not supported exception (#9) and returns
> NULL
> 
gdome2 0.6.3 have a shameful bug in gdome_doc_importNode API, I've fixed
it with the release 0.6.4.
But gdome_doc_importNode doesn't work yet fine with entities reference
nodes because "xmlStaticCopyNode on entities references is probably not
handled properly".
So I've made a patch (see the attached file tree.c.patch) to apply to the
libxml2-2.3.7 tree.c file. This patch IS NOT an official libxml2 patch: I
only give you this to make gdome_doc_importNode work with entities
reference nodes.

I attached also a program (flytest.c) and its data file (flytest.xml) to
test gdome_doc_importNode.

Bye
   Paolo
--
Paolo Casarini - casarini cs unibo it
<?xml version="1.0"?>
<!DOCTYPE FLYTEST [
  <!ENTITY entityrefnode "entitynode">
]>

<FLYTEST>
Test
&entityrefnode;
<![CDATA[<<<>>>]]>
<!-- Comment Node -->
<EL/>
<EL2 ATTR="YES"/>
<?sqlprocessor SELECT * FROM blah?>
<EL3>
  Test
  &entityrefnode;
  <![CDATA[<<<>>>]]>
  <!-- Comment Node -->
  <EL/>
  <EL2 ATTR="YES"/>
  <?sqlprocessor SELECT * FROM blah?>
</EL3>
</FLYTEST>
#include <gdome.h>

int main (int argc, char **argv) {
	GdomeDOMImplementation *domimpl;
  GdomeDocument *doc1, *doc2;
  GdomeElement *root1, *root2;
  GdomeNode *node1, *node2, *result;
  GdomeDOMString *name;
  GdomeNodeList *nl;
  GdomeException exc;
  unsigned long len,i;

  /* First I get a DOMImplementation reference */
  domimpl = gdome_DOMImplementation_mkref ();

  /* Create the "source" Document */
  doc1 = gdome_DOMImplementation_parseFile(domimpl, "flytest.xml", &exc);
	if (doc1 == NULL) {
		fprintf (stderr, "DOMImplementation.parseFile: failed\n\tException  #%d\n", exc);
		return 1;
	}

  /* I create a new document with TEST as root element */
  name = gdome_str_mkref ("TEST");
	doc2 = gdome_DOMImplementation_createDocument(domimpl, NULL, name, NULL, &exc);
	if (doc2 == NULL) {
		fprintf (stderr, "DOMImplementation.createDocument: failed\n\tException #%d\n", exc);
		return 1;
	}
	gdome_str_unref (name);

  /* I get reference to the root element of the source document */
  root1 = gdome_doc_documentElement (doc1, &exc);
	if (root1 == NULL) {
		fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
		return 1;
  }

  /* I get reference to the root element of the target document */
  root2 = gdome_doc_documentElement (doc2, &exc);
	if (root2 == NULL) {
		fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
		return 1;
  }


  /* I copy with importNode doc1 in doc2 */
  nl = gdome_el_childNodes (root1, &exc);
  len = gdome_nl_length (nl, &exc);
  for (i=0; i<len; i++) {
    node1 = gdome_nl_item (nl, i, &exc);
    if (node1 != NULL) {
      if (gdome_n_nodeType (node1, &exc) == GDOME_ELEMENT_NODE &&
          (gdome_el_hasChildNodes ((GdomeElement *)node1, &exc) ||
           gdome_el_hasAttributes ((GdomeElement *)node1, &exc)))
        node2 = gdome_doc_importNode (doc2, node1, TRUE, &exc);
      else
        node2 = gdome_doc_importNode (doc2, node1, FALSE, &exc);
      if (node2 != NULL) {
        result = gdome_el_appendChild (root2, node2, &exc);
        gdome_n_unref (result, &exc);
      }
      gdome_n_unref (node2, &exc);
    }
    gdome_n_unref (node1, &exc);
  }

  /* I save the  created document to a file named "examplea.xml */
  if (!gdome_DOMImplementation_saveFile (domimpl, "out.xml", doc2, &exc)) {
		fprintf (stderr, "DOMImplementation.saveFile: failed\n\tException #%d\n", exc);
		return 1;
	}

  /* I free the document structure and the DOMImplementation */
  gdome_DOMImplementation_freeDoc (domimpl, doc1, &exc);
  gdome_DOMImplementation_freeDoc (domimpl, doc2, &exc);
  gdome_DOMImplementation_unref (domimpl, &exc);

  return 0;
}

--- tree.c.2.3.7	Wed Apr 25 00:47:13 2001
+++ tree.c	Wed Apr 25 00:56:07 2001
@@ -2655,10 +2655,10 @@
         xmlAddChild(parent, ret);
     
     if (!recursive) return(ret);
-    if (node->nsDef != NULL)
+    if (node->nsDef != NULL && node->type != XML_ENTITY_DECL)
         ret->nsDef = xmlCopyNamespaceList(node->nsDef);
 
-    if (node->ns != NULL) {
+    if (node->ns != NULL && node->type != XML_ENTITY_DECL) {
         xmlNsPtr ns;
 
 	ns = xmlSearchNs(doc, ret, node->ns->prefix);
@@ -2682,7 +2682,7 @@
 	    ret->ns = ns;
 	}
     }
-    if (node->properties != NULL)
+    if (node->properties != NULL && node->type != XML_ENTITY_DECL)
         ret->properties = xmlCopyPropList(ret, node->properties);
     if (node->children != NULL)
         ret->children = xmlStaticCopyNodeList(node->children, doc, ret);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]