Search in sources :

Example 16 with XPathException

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);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 17 with XPathException

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;
}
Also used : MimeTable(org.exist.util.MimeTable) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) LockedDocument(org.exist.dom.persistent.LockedDocument) StringValue(org.exist.xquery.value.StringValue) MimeType(org.exist.util.MimeType) XmldbURI(org.exist.xmldb.XmldbURI) XPathException(org.exist.xquery.XPathException)

Example 18 with XPathException

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;
}
Also used : XPathException(org.exist.xquery.XPathException) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) AnyURIValue(org.exist.xquery.value.AnyURIValue) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 19 with XPathException

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;
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) MimeType(org.exist.util.MimeType)

Example 20 with XPathException

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);
}
Also used : ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) NodeValue(org.exist.xquery.value.NodeValue) ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) XMLDBException(org.xmldb.api.base.XMLDBException) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20