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;
}
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;
}
});
}
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);
}
});
}
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);
}
}
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());
}
Aggregations