Search in sources :

Example 31 with XMLDBException

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);
        }
    }
}
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 32 with XMLDBException

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;
}
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 33 with XMLDBException

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

Example 34 with XMLDBException

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);
            }
        }
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Example 35 with XMLDBException

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: &empty; then trade: &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");
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Aggregations

XMLDBException (org.xmldb.api.base.XMLDBException)174 Collection (org.xmldb.api.base.Collection)66 Resource (org.xmldb.api.base.Resource)34 URISyntaxException (java.net.URISyntaxException)30 XMLResource (org.xmldb.api.modules.XMLResource)30 ResourceSet (org.xmldb.api.base.ResourceSet)23 BuildException (org.apache.tools.ant.BuildException)21 IOException (java.io.IOException)19 XPathException (org.exist.xquery.XPathException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)16 SAXException (org.xml.sax.SAXException)16 EXistException (org.exist.EXistException)15 UserManagementService (org.exist.xmldb.UserManagementService)15 XPathQueryService (org.xmldb.api.modules.XPathQueryService)13 BinaryResource (org.xmldb.api.modules.BinaryResource)12 ArrayList (java.util.ArrayList)11 Account (org.exist.security.Account)11 EXistResource (org.exist.xmldb.EXistResource)10 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)10 Properties (java.util.Properties)9