Search in sources :

Example 6 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class IndexingTest method irregularilyStructured.

private void irregularilyStructured(boolean getContentAsDOM) throws XMLDBException, ParserConfigurationException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    Database database = null;
    final String testName = "IrregularilyStructured";
    startTime = System.currentTimeMillis();
    Collection coll = DatabaseManager.getCollection(baseURI, username, password);
    XMLResource resource = (XMLResource) coll.createResource(name, XMLResource.RESOURCE_TYPE);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    effectiveSiblingCount = populate(doc);
    resource.setContentAsDOM(doc);
    coll.storeResource(resource);
    coll.close();
    coll = DatabaseManager.getCollection(baseURI, username, password);
    resource = (XMLResource) coll.getResource(name);
    Node n;
    if (getContentAsDOM) {
        n = resource.getContentAsDOM();
    } else {
        String s = (String) resource.getContent();
        byte[] bytes = s.getBytes(UTF_8);
        UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(bytes);
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        n = db.parse(bais);
    }
    Element documentElement = null;
    if (n instanceof Element) {
        documentElement = (Element) n;
    } else if (n instanceof Document) {
        documentElement = ((Document) n).getDocumentElement();
    }
    assertions(documentElement);
    coll.removeResource(resource);
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Database(org.xmldb.api.base.Database) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Document(org.w3c.dom.Document) XMLResource(org.xmldb.api.modules.XMLResource)

Example 7 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class BinaryResourceUpdateTest method updateBinary_windows.

// with same docname test fails for windows
@Test
public void updateBinary_windows() throws XMLDBException, URISyntaxException {
    for (int i = 0; i < REPEAT; i++) {
        BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test.xml", "BinaryResource");
        binaryResource.setContent(Paths.get(binFile.toURI()));
        testCollection.storeResource(binaryResource);
        Resource resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
        XMLResource xmlResource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
        xmlResource.setContent(Paths.get(xmlFile.toURI()));
        testCollection.storeResource(xmlResource);
        resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 8 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class DOMTest method _test4.

private void _test4(boolean getContentAsDOM) throws TransformerException, ParserConfigurationException, XMLDBException, IOException, SAXException {
    Collection coll = existEmbeddedServer.getRoot();
    XMLResource resource = (XMLResource) coll.createResource(name, XMLResource.RESOURCE_TYPE);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element rootElem = doc.createElement("element");
    Element propertyElem = doc.createElement("property");
    propertyElem.setAttribute("key", "value");
    propertyElem.appendChild(doc.createTextNode("text"));
    rootElem.appendChild(propertyElem);
    doc.appendChild(rootElem);
    resource.setContentAsDOM(doc);
    coll.storeResource(resource);
    coll.close();
    coll = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
    resource = (XMLResource) coll.getResource(name);
    Node n;
    if (getContentAsDOM) {
        n = resource.getContentAsDOM();
    } else {
        String s = (String) resource.getContent();
        byte[] bytes;
        bytes = s.getBytes(UTF_8);
        try (final UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(bytes)) {
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            n = db.parse(bais);
        }
    }
    Transformer t = TransformerFactory.newInstance().newTransformer();
    DOMSource source = new DOMSource(n);
    SAXResult result = new SAXResult(new DOMTest.SAXHandler());
    t.transform(source, result);
    coll.removeResource(resource);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XMLResource(org.xmldb.api.modules.XMLResource) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 9 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class DOMTest method test2.

/**
 * - create and fill a simple document via DOM and JAXP
 * - store it with setContentAsDOM()
 * - simple access via getContentAsDOM()
 */
@Test
public void test2() throws XMLDBException, InstantiationException, IllegalAccessException, ClassNotFoundException, ParserConfigurationException, IOException {
    for (int i = 0; i < 2; i++) {
        XMLResource resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
        if (resource == null) {
            resource = (XMLResource) existEmbeddedServer.getRoot().createResource(name, XMLResource.RESOURCE_TYPE);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            Element rootElem = doc.createElement("element");
            doc.appendChild(rootElem);
            resource.setContentAsDOM(doc);
            existEmbeddedServer.getRoot().storeResource(resource);
            resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
        }
        String s = (String) resource.getContent();
        Node content = resource.getContentAsDOM();
    }
    existEmbeddedServer.restart();
    XMLResource resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
    existEmbeddedServer.getRoot().removeResource(resource);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 10 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class LocalXMLResourceDOMTest method setup.

@BeforeClass
public static void setup() throws XMLDBException {
    final CollectionManagementService cms = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    final Collection coll = cms.createCollection(TestConstants.TEST_COLLECTION_URI.lastSegment().toString());
    final XMLResource r = (XMLResource) coll.createResource(TEST_RESOURCE_NAME, XMLResource.RESOURCE_TYPE);
    r.setContent("<properties><property key=\"type\">Table</property><test/></properties><!-- comment -->");
    coll.storeResource(r);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Collection(org.xmldb.api.base.Collection) XMLResource(org.xmldb.api.modules.XMLResource) BeforeClass(org.junit.BeforeClass)

Aggregations

XMLResource (org.xmldb.api.modules.XMLResource)142 Collection (org.xmldb.api.base.Collection)56 ResourceSet (org.xmldb.api.base.ResourceSet)56 XPathQueryService (org.xmldb.api.modules.XPathQueryService)49 Test (org.junit.Test)33 Resource (org.xmldb.api.base.Resource)24 XQueryService (org.xmldb.api.modules.XQueryService)23 Node (org.w3c.dom.Node)22 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)20 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)18 Document (org.w3c.dom.Document)16 XMLDBException (org.xmldb.api.base.XMLDBException)15 EXistResource (org.exist.xmldb.EXistResource)9 EXistXQueryService (org.exist.xmldb.EXistXQueryService)9 BinaryResource (org.xmldb.api.modules.BinaryResource)9 Path (java.nio.file.Path)8 Before (org.junit.Before)8 Element (org.w3c.dom.Element)8 Source (javax.xml.transform.Source)7 SAXSerializer (org.exist.util.serializer.SAXSerializer)7