Search in sources :

Example 66 with XMLResource

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

the class ExtDocTest method parse.

@Test
public void parse() throws XMLDBException {
    final URI docUri = externalDoc.toUri();
    final ResourceSet result = existEmbeddedServer.executeQuery("xquery version \"3.1\";\n" + "\n" + "declare namespace output = \"http://www.w3.org/2010/xslt-xquery-serialization\";" + "declare option output:omit-xml-declaration \"yes\";\n" + "\n" + "fn:doc('" + docUri + "')");
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final Resource resource = result.getResource(0);
    assertNotNull(resource);
    assertEquals(XMLResource.RESOURCE_TYPE, resource.getResourceType());
    final Source expectedSource = Input.fromString(docContent).build();
    final Source actualSource = Input.fromNode(((XMLResource) resource).getContentAsDOM()).build();
    final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) URI(java.net.URI) Source(javax.xml.transform.Source) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 67 with XMLResource

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

the class AbstractExtractFunction method processCompressedEntry.

/**
 * Processes a compressed entry from an archive
 *
 * @param name The name of the entry
 * @param isDirectory true if the entry is a directory, false otherwise
 * @param is an InputStream for reading the uncompressed data of the entry
 * @param filterParam is an additional param for entry filtering function
 * @param storeParam is an additional param for entry storing function
 *
 * @return the result of processing the compressed entry.
 *
 * @throws XPathException if a query error occurs
 * @throws XMLDBException if a database error occurs
 * @throws IOException if an I/O error occurs
 */
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
    String dataType = isDirectory ? "folder" : "resource";
    // call the entry-filter function
    Sequence[] filterParams = new Sequence[3];
    filterParams[0] = new StringValue(name);
    filterParams[1] = new StringValue(dataType);
    filterParams[2] = filterParam;
    Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
    if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        Sequence entryDataFunctionResult;
        Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
        if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
            Sequence[] dataParams = new Sequence[3];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
            String path = entryDataFunctionResult.itemAt(0).getStringValue();
            Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
            if (isDirectory) {
                XMLDBAbstractCollectionManipulator.createCollection(root, path);
            } else {
                Resource resource;
                Path file = Paths.get(path).normalize();
                name = FileUtils.fileName(file);
                path = file.getParent().toAbsolutePath().toString();
                Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
                MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                // copy the input data
                final byte[] entryData;
                try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                    baos.write(is);
                    entryData = baos.toByteArray();
                }
                try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                    NodeValue content = ModuleUtils.streamToXML(context, bis);
                    resource = target.createResource(name, "XMLResource");
                    ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
                    handler.startDocument();
                    content.toSAX(context.getBroker(), handler, null);
                    handler.endDocument();
                } catch (SAXException e) {
                    resource = target.createResource(name, "BinaryResource");
                    resource.setContent(entryData);
                }
                if (resource != null) {
                    if (mime != null) {
                        ((EXistResource) resource).setMimeType(mime.getName());
                    }
                    target.storeResource(resource);
                }
            }
        } else {
            // copy the input data
            final byte[] entryData;
            try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                baos.write(is);
                entryData = baos.toByteArray();
            }
            // try and parse as xml, fall back to binary
            try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                uncompressedData = ModuleUtils.streamToXML(context, bis);
            } catch (SAXException saxe) {
                if (entryData.length > 0) {
                    try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                        uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
                    }
                }
            }
            // call the entry-data function
            Sequence[] dataParams = new Sequence[4];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = uncompressedData;
            dataParams[3] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
        }
        return entryDataFunctionResult;
    }
}
Also used : Path(java.nio.file.Path) LocalCollection(org.exist.xmldb.LocalCollection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) MimeType(org.exist.util.MimeType) ContentHandler(org.xml.sax.ContentHandler) XMLResource(org.xmldb.api.modules.XMLResource) SAXException(org.xml.sax.SAXException) EXistResource(org.exist.xmldb.EXistResource) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 68 with XMLResource

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

the class ExpandTest method expandPersistentDomAttrNs.

@Test
public void expandPersistentDomAttrNs() throws XMLDBException {
    final String query = "declare namespace x = \"http://x\";\n" + "util:expand(doc('/db/expand-test/doc4.xml')/doc4/@x:foo)";
    final ResourceSet result = existEmbeddedServer.executeQuery(query);
    final Resource res = result.getResource(0);
    assertEquals(XMLResource.RESOURCE_TYPE, res.getResourceType());
    final XMLResource xmlRes = (XMLResource) res;
    final Node node = xmlRes.getContentAsDOM();
    assertEquals(Node.ATTRIBUTE_NODE, node.getNodeType());
    assertEquals("http://x", node.getNamespaceURI());
    assertEquals("foo", node.getNodeName());
    assertEquals("bar", node.getNodeValue());
}
Also used : Node(org.w3c.dom.Node) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 69 with XMLResource

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

the class UpdateReplaceTest method replaceOnlyChildWhereParentHasNoAttributes.

@Test
public void replaceOnlyChildWhereParentHasNoAttributes() throws XMLDBException {
    final String testDocName = "replaceOnlyChildWhereParentHasNoAttributes.xml";
    final String testDoc = "<Test><Content><A/></Content></Test>";
    final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + "    let $legacy := $content/A\n" + "    return\n" + "      update replace $legacy with <AA/>,\n" + "    doc('/db/test/" + testDocName + "')/Test";
    final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
    final ResourceSet result = xqueryService.query(updateQuery);
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final Resource res1 = result.getResource(0);
    assertNotNull(res1);
    assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
    final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
    final Source actual = Input.fromDocument(doc).build();
    final Source expected = Input.fromString("<Test><Content><AA/></Content></Test>").build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) XQueryService(org.xmldb.api.modules.XQueryService) EXistResource(org.exist.xmldb.EXistResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) Document(org.w3c.dom.Document) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 70 with XMLResource

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

the class UpdateReplaceTest method replaceFirstChildWhereParentHasNoAttributes.

@Test
public void replaceFirstChildWhereParentHasNoAttributes() throws XMLDBException {
    final String testDocName = "replaceFirstChildWhereParentHasNoAttributes.xml";
    final String testDoc = "<Test><Content><A/><A/></Content></Test>";
    final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + "    let $legacy := $content/A[1]\n" + "    return\n" + "      update replace $legacy with <AA/>,\n" + "    doc('/db/test/" + testDocName + "')/Test";
    final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
    final ResourceSet result = xqueryService.query(updateQuery);
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final Resource res1 = result.getResource(0);
    assertNotNull(res1);
    assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
    final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
    final Source actual = Input.fromDocument(doc).build();
    final Source expected = Input.fromString("<Test><Content><AA/><A/></Content></Test>").build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) XQueryService(org.xmldb.api.modules.XQueryService) EXistResource(org.exist.xmldb.EXistResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) Document(org.w3c.dom.Document) Source(javax.xml.transform.Source) Test(org.junit.Test)

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