Search in sources :

Example 26 with EXistResource

use of org.exist.xmldb.EXistResource 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)

Example 27 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class TransformTest method addXMLDocument.

private void addXMLDocument(Collection c, String doc, String id) throws XMLDBException {
    Resource r = c.createResource(id, XMLResource.RESOURCE_TYPE);
    r.setContent(doc);
    ((EXistResource) r).setMimeType("application/xml");
    c.storeResource(r);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) EXistResource(org.exist.xmldb.EXistResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource)

Example 28 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class CleanupTest method setup.

@Before
public void setup() throws XMLDBException {
    final CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    collection = service.createCollection("test");
    final Resource doc = collection.createResource("test-module.xql", "BinaryResource");
    doc.setContent(TEST_MODULE);
    ((EXistResource) doc).setMimeType("application/xquery");
    collection.storeResource(doc);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistResource(org.exist.xmldb.EXistResource)

Example 29 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method wrongAddNamespace_1807014.

/**
 * @see http://sourceforge.net/support/tracker.php?aid=1807014
 */
@Test
public void wrongAddNamespace_1807014() throws XMLDBException {
    Collection testCollection = getTestCollection();
    Resource doc = testCollection.createResource("a.xqy", "BinaryResource");
    doc.setContent("module namespace a = \"http://www.a.com\"; " + "declare function a:selectionList() as element(ul) { " + "<ul class=\"a\"/> " + "};");
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    String query = "declare option exist:serialize 'indent=no';" + "import module namespace a = \"http://www.a.com\" at \"xmldb:exist://db/test/a.xqy\"; " + "<html xmlns=\"http://www.w3.org/1999/xhtml\"> " + "{ a:selectionList() } " + "</html>";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(1, result.getSize());
    assertEquals(query, "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<ul xmlns=\"\" class=\"a\"/></html>", result.getResource(0).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 30 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class DocTest method storeResource.

private void storeResource(final Collection col, final String fileName, final String type, final String mimeType, final String content) throws XMLDBException {
    Resource res = col.createResource(fileName, type);
    res.setContent(content);
    if (mimeType != null) {
        ((EXistResource) res).setMimeType(mimeType);
    }
    col.storeResource(res);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) EXistResource(org.exist.xmldb.EXistResource) LocalXMLResource(org.exist.xmldb.LocalXMLResource) Resource(org.xmldb.api.base.Resource)

Aggregations

EXistResource (org.exist.xmldb.EXistResource)42 Collection (org.xmldb.api.base.Collection)20 BinaryResource (org.xmldb.api.modules.BinaryResource)20 Resource (org.xmldb.api.base.Resource)17 XMLResource (org.xmldb.api.modules.XMLResource)17 ResourceSet (org.xmldb.api.base.ResourceSet)12 Path (java.nio.file.Path)11 XMLDBException (org.xmldb.api.base.XMLDBException)10 ExtendedResource (org.exist.xmldb.ExtendedResource)9 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)8 URISyntaxException (java.net.URISyntaxException)6 XPathQueryService (org.xmldb.api.modules.XPathQueryService)6 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)5 UserManagementService (org.exist.xmldb.UserManagementService)5 BeforeClass (org.junit.BeforeClass)5 Test (org.junit.Test)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 MimeType (org.exist.util.MimeType)4 XPathException (org.exist.xquery.XPathException)4 LocalBinaryResource (org.exist.xmldb.LocalBinaryResource)3