Search in sources :

Example 16 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class ElementImpl method replaceChild.

/**
 * Replaces the oldNode with the newChild
 *
 * @param transaction the transaction
 * @param newChild to replace oldChild
 * @param oldChild to be replace by newChild
 * @return The new node (this differs from the {@link org.w3c.dom.Node#replaceChild(Node, Node)} specification)
 * @throws DOMException in case of a DOM error
 * @see org.w3c.dom.Node#replaceChild(org.w3c.dom.Node, org.w3c.dom.Node)
 */
@Override
public Node replaceChild(final Txn transaction, final Node newChild, final Node oldChild) throws DOMException {
    if (!(oldChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
    }
    final IStoredNode<?> oldNode = (IStoredNode<?>) oldChild;
    if (!oldNode.getNodeId().getParentId().equals(nodeId)) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "Node is not a child of this element");
    }
    final NodePath thisPath = getPath();
    IStoredNode<?> previous = (IStoredNode<?>) oldNode.getPreviousSibling();
    if (previous == null) {
        previous = this;
    } else {
        previous = getLastNode(previous);
    }
    final NodePath oldPath = oldNode.getPath();
    StreamListener listener = null;
    Node newNode = null;
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        final IndexController indexes = broker.getIndexController();
        // May help getReindexRoot() to make some useful things
        indexes.setDocument(ownerDocument);
        final IStoredNode reindexRoot = broker.getIndexController().getReindexRoot(oldNode, oldPath, false);
        indexes.setMode(ReindexMode.REMOVE_SOME_NODES);
        if (reindexRoot == null) {
            listener = indexes.getStreamListener();
        } else {
            indexes.reindex(transaction, reindexRoot, ReindexMode.REMOVE_SOME_NODES);
        }
        broker.removeAllNodes(transaction, oldNode, oldPath, listener);
        broker.endRemove(transaction);
        broker.flush();
        indexes.setMode(ReindexMode.STORE);
        listener = indexes.getStreamListener();
        newNode = appendChild(transaction, oldNode.getNodeId(), new NodeImplRef(previous), thisPath, newChild, listener);
        // Reindex if required
        broker.storeXMLResource(transaction, getOwnerDocument());
        broker.updateNode(transaction, this, false);
        indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
        broker.flush();
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    // returning the newNode is more sensible than returning the oldNode
    return newNode;
}
Also used : IndexController(org.exist.indexing.IndexController) EXistException(org.exist.EXistException) StreamListener(org.exist.indexing.StreamListener)

Example 17 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class ElementImpl method setAttribute.

private void setAttribute(final QName attrName, final String value, final Function<QName, Attr> getFn) {
    final Attr existingAttr = getFn.apply(attrName);
    if (existingAttr != null) {
        // update an existing attribute
        existingAttr.setValue(value);
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
            final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
            if (!(existingAttr instanceof IStoredNode)) {
                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
            }
            final IStoredNode<?> existing = (IStoredNode<?>) existingAttr;
            if (!existing.getNodeId().isChildOf(nodeId)) {
                throw new DOMException(DOMException.NOT_FOUND_ERR, "node " + existing.getNodeId().getParentId() + " is not a child of element " + nodeId);
            }
            // update old custom indexes
            final IndexController indexes = broker.getIndexController();
            indexes.reindex(transaction, existing, ReindexMode.STORE);
            broker.updateNode(transaction, existing, true);
            transaction.commit();
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    } else {
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
            final AttrImpl attrib = new AttrImpl(attrName, value, broker.getBrokerPool().getSymbols());
            appendChild(attrib);
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    }
}
Also used : IndexController(org.exist.indexing.IndexController) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 18 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Launcher method registerObserver.

private void registerObserver() {
    try {
        final BrokerPool pool = BrokerPool.getInstance();
        final Optional<ExistRepository> repo = pool.getExpathRepo();
        if (repo.isPresent()) {
            repo.get().addObserver(this);
            repo.get().addObserver(utilityPanel);
        } else {
            System.err.println("EXPath repository is not available.");
        }
    } catch (final EXistException e) {
        System.err.println("Failed to register as observer for package manager events");
        e.printStackTrace();
    }
}
Also used : EXistException(org.exist.EXistException) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Example 19 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Launcher method checkInstalledApps.

private void checkInstalledApps() {
    try {
        final BrokerPool pool = BrokerPool.getInstance();
        try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
            final XQuery xquery = pool.getXQueryService();
            final Sequence pkgs = xquery.execute(broker, "repo:list()", null);
            for (final SequenceIterator i = pkgs.iterate(); i.hasNext(); ) {
                final ExistRepository.Notification notification = new ExistRepository.Notification(ExistRepository.Action.INSTALL, i.nextItem().getStringValue());
                final Optional<ExistRepository> expathRepo = pool.getExpathRepo();
                if (expathRepo.isPresent()) {
                    update(expathRepo.get(), notification);
                    utilityPanel.update(expathRepo.get(), notification);
                }
                expathRepo.orElseThrow(() -> new EXistException("EXPath repository is not available."));
            }
        }
    } catch (final EXistException | XPathException | PermissionDeniedException e) {
        System.err.println("Failed to check installed packages: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) DBBroker(org.exist.storage.DBBroker) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Example 20 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class AbstractExistHttpServlet method doDatabaseStartup.

private void doDatabaseStartup(Configuration configuration) throws ServletException {
    if (configuration == null) {
        throw new ServletException("Database has not been " + "configured");
    }
    getLog().info("Configuring eXist instance");
    try {
        if (!BrokerPool.isConfigured()) {
            BrokerPool.configure(1, 5, configuration);
        }
    } catch (final EXistException | DatabaseConfigurationException e) {
        throw new ServletException(e.getMessage(), e);
    }
    try {
        getLog().info("Registering XMLDB driver");
        final Class<?> clazz = Class.forName("org.exist.xmldb.DatabaseImpl");
        final Database database = (Database) clazz.newInstance();
        DatabaseManager.registerDatabase(database);
    } catch (final ClassNotFoundException | XMLDBException | IllegalAccessException | InstantiationException e) {
        getLog().info("ERROR", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) Database(org.xmldb.api.base.Database) XMLDBException(org.xmldb.api.base.XMLDBException) EXistException(org.exist.EXistException)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17