use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XMLDBCopy method evalWithCollection.
@Override
public Sequence evalWithCollection(final Collection collection, final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
final XmldbURI destination = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
final XmldbURI doc = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final Resource resource = collection.getResource(doc.toString());
if (resource == null) {
logger.error("Resource {} not found", doc);
throw new XPathException(this, "Resource " + doc + " not found");
}
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 5) {
final boolean preserveArg = args[4].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
final XmldbURI newName;
if (getArgumentCount() >= 4) {
if (!args[3].isEmpty()) {
newName = XmldbURI.create(args[3].itemAt(0).getStringValue());
} else {
newName = doc.lastSegment();
}
} else {
newName = null;
}
service.copyResource(doc, destination, newName, preserve.name());
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
return new StringValue(destination.append(newName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("XMLDB exception caught: ", e);
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
}
} else {
final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 3) {
final boolean preserveArg = args[2].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
service.copy(XmldbURI.xmldbUriFor(collection.getName()), destination, null, preserve.name());
if (isCalledAs(FS_COPY_COLLECTION_NAME)) {
final XmldbURI targetName = XmldbURI.xmldbUriFor(collection.getName()).lastSegment();
return new StringValue(destination.append(targetName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("Cannot copy collection: ", e);
throw new XPathException(this, "Cannot copy collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error("URI exception: ", e);
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XMLDBMove method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
if (getSignature().getArgumentCount() == 3) {
final XmldbURI doc = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
try {
final Resource resource = collection.getResource(doc.toString());
if (resource == null) {
logger.error("Resource {} not found", doc);
throw new XPathException(this, "Resource " + doc + " not found");
}
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.moveResource(doc, destination, null);
} catch (final XMLDBException e) {
logger.error(e.getMessage());
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
}
} else {
try {
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.move(XmldbURI.xmldbUriFor(collection.getName()), destination, null);
} catch (final XMLDBException e) {
logger.error(e.getMessage());
throw new XPathException(this, "Cannot move collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error(e.getMessage());
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XMLDBXUpdate method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection c, Sequence[] args, Sequence contextSequence) throws XPathException {
final NodeValue data = (NodeValue) args[1].itemAt(0);
final StringWriter writer = new StringWriter();
final Properties properties = new Properties();
properties.setProperty(OutputKeys.INDENT, "yes");
final DOMSerializer serializer = new ExtendedDOMSerializer(context.getBroker(), writer, properties);
try {
serializer.serialize(data.getNode());
} catch (final TransformerException e) {
logger.debug("Exception while serializing XUpdate document", e);
throw new XPathException(this, "Exception while serializing XUpdate document: " + e.getMessage(), e);
}
final String xupdate = writer.toString();
long modifications = 0;
try {
final XUpdateQueryService service = (XUpdateQueryService) c.getService("XUpdateQueryService", "1.0");
logger.debug("Processing XUpdate request: {}", xupdate);
modifications = service.update(xupdate);
} catch (final XMLDBException e) {
throw new XPathException(this, "Exception while processing xupdate: " + e.getMessage(), e);
}
context.getRootExpression().resetState(false);
return new IntegerValue(modifications);
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XmldbApiSecurityTest method createXmlResource.
@Override
protected void createXmlResource(String resourceUri, String content, String uid, String pwd) throws ApiException {
Collection col = null;
try {
col = DatabaseManager.getCollection(getBaseUri() + getCollectionUri(resourceUri), uid, pwd);
Resource resource = col.createResource(getResourceName(resourceUri), XMLResource.RESOURCE_TYPE);
resource.setContent(content);
col.storeResource(resource);
} catch (final XMLDBException xmldbe) {
throw new ApiException(xmldbe);
} finally {
if (col != null) {
try {
col.close();
} catch (final XMLDBException xmldbe) {
throw new ApiException(xmldbe);
}
}
}
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class DtdEntityTest method bugloadWithEntities.
@Test
@Ignore("Entity resolve bug")
public void bugloadWithEntities() throws XMLDBException {
final String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE procedure PUBLIC \"-//AAAA//DTD Procedure 0.4//EN\" \"aaaa.dtd\" >" + "<a>first empty: ∅ then trade: ™ </a>";
Collection col = null;
try {
col = existEmbeddedServer.createCollection(existEmbeddedServer.getRoot(), "entity");
ExistXmldbEmbeddedServer.storeResource(col, "docname.xml", input.getBytes(UTF_8));
// should throw XMLDBException
ExistXmldbEmbeddedServer.getXMLResource(col, "docname.xml");
} catch (final XMLDBException e) {
assertTrue(e.getMessage().contains("The entity \"empty\" was referenced, but not declared"));
return;
} finally {
if (col != null) {
col.close();
}
}
fail("Should have thrown XMLDBException");
}
Aggregations