Search in sources :

Example 51 with Resource

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

the class LocalCollection method createResource.

@Override
public Resource createResource(String id, final String type) throws XMLDBException {
    if (id == null) {
        id = createId();
    }
    final XmldbURI idURI;
    try {
        idURI = XmldbURI.xmldbUriFor(id);
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    final Resource r;
    switch(type) {
        case XMLResource.RESOURCE_TYPE:
            r = new LocalXMLResource(user, brokerPool, this, idURI);
            break;
        case BinaryResource.RESOURCE_TYPE:
            r = new LocalBinaryResource(user, brokerPool, this, idURI);
            break;
        default:
            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type: " + type);
    }
    ((AbstractEXistResource) r).isNewResource = true;
    return r;
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException)

Example 52 with Resource

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

the class LocalCollection method getResource.

Resource getResource(final DBBroker broker, final Txn transaction, final XmldbURI idURI) throws XMLDBException {
    return this.<Resource>read(broker, transaction).apply((collection, broker1, transaction1) -> {
        try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, idURI, LockMode.READ_LOCK)) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            final DocumentImpl document = lockedDocument == null ? null : lockedDocument.getDocument();
            if (document == null) {
                LOG.warn("Resource {} not found", idURI);
                return null;
            }
            final Resource r;
            switch(document.getResourceType()) {
                case DocumentImpl.XML_FILE:
                    r = new LocalXMLResource(user, brokerPool, this, idURI);
                    break;
                case DocumentImpl.BINARY_FILE:
                    r = new LocalBinaryResource(user, brokerPool, this, idURI);
                    break;
                default:
                    throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type");
            }
            ((AbstractEXistResource) r).setMimeType(document.getMimeType());
            return r;
        }
    });
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 53 with Resource

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

the class LocalResourceSet method getMembersAsResource.

@Override
public Resource getMembersAsResource() throws XMLDBException {
    return this.<Resource>withDb((broker, transaction) -> {
        final Serializer serializer = broker.borrowSerializer();
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final StringWriter writer = new StringWriter();
        handler.setOutput(writer, outputProperties);
        try {
            // configure the serializer
            collection.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
            serializer.setProperties(outputProperties);
            serializer.setUser(user);
            serializer.setSAXHandlers(handler, handler);
            // serialize results
            handler.startDocument();
            handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(resources.size()));
            handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
            Item current;
            char[] value;
            for (Object resource : resources) {
                current = (Item) resource;
                if (Type.subTypeOf(current.getType(), Type.NODE)) {
                    ((NodeValue) current).toSAX(broker, handler, outputProperties);
                } else {
                    value = current.toString().toCharArray();
                    handler.characters(value, 0, value.length);
                }
            }
            handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
            handler.endPrefixMapping("exist");
            handler.endDocument();
            final Resource res = new LocalXMLResource(user, brokerPool, collection, XmldbURI.EMPTY_URI);
            res.setContent(writer.toString());
            return res;
        } catch (final SAXException e) {
            throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "serialization error", e);
        } finally {
            SerializerPool.getInstance().returnObject(handler);
            broker.returnSerializer(serializer);
        }
    });
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 54 with Resource

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

the class ConcurrencyTest method initDB.

@BeforeClass
public static void initDB() throws XMLDBException {
    final CollectionManagementService mgmt = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    final Collection test = mgmt.createCollection("test");
    for (int i = 1; i <= DOC_COUNT; i++) {
        final Resource r = test.createResource("test" + i + ".xml", "XMLResource");
        final String XML = "<test id='" + i + "'>" + "   <a>b</a>" + "   <c>d</c>" + "</test>";
        r.setContent(XML);
        test.storeResource(r);
    }
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) BeforeClass(org.junit.BeforeClass)

Example 55 with Resource

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

the class XQueryTriggerTest method documentBinaryCreate.

/**
 * test a trigger fired by creating a new Binary Document
 */
@Test
public void documentBinaryCreate() throws XMLDBException {
    // configure the Collection with the trigger under test
    final IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(COLLECTION_CONFIG);
    // this will fire the trigger
    final Resource res = testCollection.createResource(BINARY_DOCUMENT_NAME, "BinaryResource");
    final byte[] content = Base64.decodeBase64(BINARY_DOCUMENT_CONTENT);
    res.setContent(content);
    testCollection.storeResource(res);
    // remove the trigger for the Collection under test
    idxConf.configureCollection(EMPTY_COLLECTION_CONFIG);
    final XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    // TODO : understand why it is necessary !
    service.setProperty(OutputKeys.INDENT, "no");
    ResourceSet result = service.query(BEFORE + CREATE + DOCUMENT + binaryURI);
    assertEquals(1, result.getSize());
    result = service.query(AFTER + CREATE + DOCUMENT + binaryURI);
    assertEquals(1, result.getSize());
    result = service.query(EVENTS);
    assertEquals(2, result.getSize());
// TODO: document itself
// result = service.query("/events/event[@id = 'trigger1'][@type = 'finish'][collection = '" + DBBroker.ROOT_COLLECTION +  "/" + TEST_COLLECTION + "'][uri = '" + DBBroker.ROOT_COLLECTION +  "/" + TEST_COLLECTION + "/" + BINARY_DOCUMENT_NAME + "'][event = 'CREATE-DOCUMENT']/document");
// assertEquals(1, result.getSize());
// assertEquals("<document>" + BINARY_DOCUMENT_CONTENT + "</document>", result.getResource(0).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) IndexQueryService(org.exist.xmldb.IndexQueryService) EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

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