Search in sources :

Example 91 with Resource

use of org.xmldb.api.base.Resource 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 92 with Resource

use of org.xmldb.api.base.Resource 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)

Example 93 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class StressTest method fetchDb.

private void fetchDb() throws XMLDBException {
    final XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
    final ResourceSet result = xquery.query("for $n in collection('" + XmldbURI.ROOT_COLLECTION + "/test')//* return local-name($n)");
    for (int i = 0; i < result.getSize(); i++) {
        final Resource r = result.getResource(i);
        final String tag = r.getContent().toString();
        final ResourceSet result2 = xquery.query("//" + tag);
        assertEquals(result2.getSize(), 1);
    }
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 94 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class XUpdateTest method removeDocument.

private void removeDocument() throws XMLDBException {
    final Resource document = col.getResource(XUPDATE_FILE);
    col.removeResource(document);
}
Also used : XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource)

Example 95 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class NewResourceDialog method createResource.

private void createResource(final ResourceType resourceType, final String filename, final String moduleNamespace, final String moduleNamespacePrefix) {
    final StringBuilder resourceContentBuilder = new StringBuilder();
    try (final InputStream is = getClass().getResourceAsStream(resourceType.getTemplatePath());
        final Reader reader = new InputStreamReader(is)) {
        final char[] buf = new char[1024];
        int read = -1;
        while ((read = reader.read(buf)) > -1) {
            resourceContentBuilder.append(buf, 0, read);
        }
    } catch (final IOException ioe) {
        ClientFrame.showErrorMessage(ioe.getMessage(), ioe);
    }
    final String resourceContent;
    if (resourceType == ResourceType.XQUERY_LIBRARY) {
        resourceContent = resourceContentBuilder.toString().replaceAll("\\$NS", moduleNamespace).replaceAll("\\$PREFIX", moduleNamespacePrefix);
    } else {
        resourceContent = resourceContentBuilder.toString();
    }
    try {
        final String resName = URIUtils.urlEncodeUtf8((isNullOrEmpty(filename) ? DEFAULT_FILENAME : filename) + "." + resourceType.getFileExtension());
        final String resType = resourceType == ResourceType.XML_DOCUMENT ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
        final Collection collection = client.current;
        final Resource resource = collection.createResource(resName, resType);
        resource.setContent(resourceContent);
        ((EXistResource) resource).setMimeType(resourceType.getMimeType());
        collection.storeResource(resource);
        collection.close();
        client.reloadCollection();
    } catch (final XMLDBException xmldbe) {
        ClientFrame.showErrorMessage(xmldbe.getMessage(), xmldbe);
    }
}
Also used : EXistResource(org.exist.xmldb.EXistResource) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9