use of org.exist.xquery.XPathException 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.exist.xquery.XPathException in project exist by eXist-db.
the class XMLDBGetMimeType method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final String path = new AnyURIValue(args[0].itemAt(0).getStringValue()).toString();
if (path.matches("^[a-z]+://.*")) {
// external
final MimeTable mimeTable = MimeTable.getInstance();
final MimeType mimeType = mimeTable.getContentTypeFor(path);
if (mimeType != null) {
return new StringValue(mimeType.getName());
}
} else {
// database
try {
XmldbURI pathUri = XmldbURI.xmldbUriFor(path);
// relative collection Path: add the current base URI
pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
// try to open the document and acquire a lock
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
if (lockedDoc != null) {
return new StringValue(lockedDoc.getDocument().getMimeType());
}
}
} catch (final Exception e) {
logger.error(e.getMessage());
throw new XPathException(this, e);
}
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xquery.XPathException 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.exist.xquery.XPathException in project exist by eXist-db.
the class XMLDBSetMimeType method getMimeTypeStoredResource.
/**
* Determine mimetype of currently stored resource. Copied from
* get-mime-type.
*/
private MimeType getMimeTypeStoredResource(XmldbURI pathUri) throws XPathException {
MimeType returnValue = null;
try {
// relative collection Path: add the current base URI
pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
} catch (final XPathException ex) {
logger.debug("Unable to convert path {}", pathUri);
return returnValue;
}
try (final LockedDocument lockedDocument = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
// try to open the document and acquire a lock
final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
if (doc == null) {
throw new XPathException("Resource '" + pathUri + "' does not exist.");
} else {
final String mimetype = doc.getMimeType();
returnValue = MimeTable.getInstance().getContentType(mimetype);
}
} catch (final PermissionDeniedException ex) {
logger.debug(ex.getMessage());
}
return returnValue;
}
use of org.exist.xquery.XPathException 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);
}
Aggregations