I am using the setContentAsDOM method provided by XMLResource instances
to specify the content that I wish to insert into an Xindice database
but am running into problems if the DOM object that is provided as an
argument has had nodes created using the createElementNS or
setAttributeNS methods.
The attached source code provides two JUnit tests. One works because it
does not have namespace declarations for the nodes in the document. The
other fails because it does. The tests assume that the database URI is:
xmldb:xindice://localhost:8180/db
The problem occurs at the point where the database manager attempts to
insert the XMLResource into the relevant collection, complaining about
difficulties parsing the document.
My environment is as follows:
OS: Debian testing
Container: tomcat 4
Xindice: 1.1b4
Any insights into what is going wrong would be very much appreciated.
Regards
Geoff Shuetrim
package org.xbrlapi.data.xindice.framework.tests;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import org.apache.xindice.client.xmldb.services.CollectionManager;
import org.apache.xindice.xml.dom.DOMParser;
import org.w3c.dom.Document;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.XMLResource;
public class SetContentAsDOMTest extends TestCase {
// The collection to hold the test fragments
private Collection collection = null;
// The root URI for the Xindice database
private String databaseURI = null;
protected void setUp() throws Exception {
super.setUp();
//Establish the connection to the database
Database db = (Database) Class.forName("org.apache.xindice.client.xmldb.DatabaseImpl").newInstance();
DatabaseManager.registerDatabase(db);
databaseURI = "xmldb:xindice://localhost:8180/db";
//Create a collection to hold the resources
Collection container = DatabaseManager.getCollection(databaseURI + "/");
CollectionManager service = (CollectionManager) container.getService("CollectionManager", "1.0");
String collectionConfig =
"<collection compressed=\"true\" name=\"xbrlapiCollection\">"
+ " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\"/>"
+ "</collection>";
service.createCollection("xbrlapiCollection", DOMParser.toDocument(collectionConfig));
collection = DatabaseManager.getCollection(databaseURI + "/xbrlapiCollection");
container.close();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Remove the temporary test collection
collection.close();
Collection container = DatabaseManager.getCollection(databaseURI + "/");
CollectionManager service = (CollectionManager) container.getService("CollectionManager", "1.0");
service.dropCollection("/xbrlapiCollection");
container.close();
}
public static void main(String[] args) {
junit.textui.TestRunner.run(SetContentAsDOMTest.class);
}
public final void testSetContentAsDOMWithoutNamespaces() {
try {
// Build the DOM document to be stored in the database
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.newDocument();
d.appendChild(d.createElement("root"));
// Create the resource
XMLResource document = (XMLResource) collection.createResource("1", "XMLResource");
String id = document.getId();
document.setContentAsDOM(d);
// Store the document
try {
collection.storeResource(document);
} catch (Exception e) {
e.printStackTrace();
fail("Document storage failed.");
}
// Retrieve the document
Document dom = (Document) ((XMLResource) collection.getResource(id)).getContentAsDOM();
assertEquals("root",dom.getDocumentElement().getLocalName());
} catch (Exception e) {
fail("No luck.");
}
}
public final void testSetContentAsDOMWithNamespaces() {
try {
// Build the DOM document to be stored in the database
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.newDocument();
d.appendChild(d.createElementNS("http://www.xbrlapi.org/","c:root"));
// Create the resource
XMLResource document = (XMLResource) collection.createResource("2", "XMLResource");
String id = document.getId();
document.setContentAsDOM(d);
// Store the document
try {
collection.storeResource(document);
} catch (Exception e) {
e.printStackTrace();
fail("Document storage failed.");
}
// Retrieve the document
Document dom = (Document) ((XMLResource) collection.getResource(id)).getContentAsDOM();
assertEquals("root",dom.getDocumentElement().getLocalName());
} catch (Exception e) {
fail("No luck.");
}
}
}