Search in sources :

Example 11 with EXistException

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

the class ElementImpl method setAttributeNode.

private Attr setAttributeNode(final Attr newAttr, final Function<QName, Attr> getFn) {
    final QName attrName = new QName(newAttr.getLocalName(), newAttr.getNamespaceURI(), newAttr.getPrefix(), ElementValue.ATTRIBUTE);
    final Attr existingAttr = getFn.apply(attrName);
    if (existingAttr != null) {
        if (existingAttr.equals(newAttr)) {
            return newAttr;
        }
        // update an existing attribute
        existingAttr.setValue(newAttr.getValue());
        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());
        }
        return existingAttr;
    } else {
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
            final AttrImpl attrib = new AttrImpl(attrName, newAttr.getValue(), broker.getBrokerPool().getSymbols());
            return (Attr) appendChild(attrib);
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    }
}
Also used : QName(org.exist.dom.QName) IndexController(org.exist.indexing.IndexController) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 12 with EXistException

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

the class ElementImpl method appendChild.

private Node appendChild(final Txn transaction, final NodeId newNodeId, final NodeImplRef last, final NodePath lastPath, final Node child, final StreamListener listener) throws DOMException {
    if (last == null || last.getNode() == null) {
        throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "invalid node");
    }
    final DocumentImpl owner = getOwnerDocument();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        switch(child.getNodeType()) {
            case Node.DOCUMENT_FRAGMENT_NODE:
                appendChildren(transaction, newNodeId, null, last, lastPath, child.getChildNodes(), listener);
                // TODO: implement document fragments so
                return null;
            case Node.ELEMENT_NODE:
                // create new element
                final ElementImpl elem = new ElementImpl(new QName(child.getLocalName() == null ? child.getNodeName() : child.getLocalName(), child.getNamespaceURI(), child.getPrefix()), broker.getBrokerPool().getSymbols());
                elem.setNodeId(newNodeId);
                elem.setOwnerDocument(owner);
                final org.exist.dom.NodeListImpl ch = new org.exist.dom.NodeListImpl();
                final NamedNodeMap attribs = child.getAttributes();
                int numActualAttribs = 0;
                for (int i = 0; i < attribs.getLength(); i++) {
                    final Attr attr = (Attr) attribs.item(i);
                    if (!attr.getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
                        ch.add(attr);
                        numActualAttribs++;
                    } else {
                        final String xmlnsDecl = attr.getNodeName();
                        final String prefix = xmlnsDecl.length() == 5 ? XMLConstants.DEFAULT_NS_PREFIX : xmlnsDecl.substring(6);
                        elem.addNamespaceMapping(prefix, attr.getNodeValue());
                    }
                }
                final NodeList cl = child.getChildNodes();
                for (int i = 0; i < cl.getLength(); i++) {
                    final Node n = cl.item(i);
                    ch.add(n);
                }
                elem.setChildCount(ch.getLength());
                if (numActualAttribs != (short) numActualAttribs) {
                    throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "Too many attributes");
                }
                elem.setAttributes((short) numActualAttribs);
                lastPath.addComponent(elem.getQName());
                // insert the node
                broker.insertNodeAfter(transaction, last.getNode(), elem);
                broker.indexNode(transaction, elem, lastPath);
                final IndexController indexes = broker.getIndexController();
                indexes.startIndexDocument(transaction, listener);
                try {
                    indexes.indexNode(transaction, elem, lastPath, listener);
                    elem.setChildCount(0);
                    last.setNode(elem);
                    // process child nodes
                    elem.appendChildren(transaction, newNodeId.newChild(), null, last, lastPath, ch, listener);
                    broker.endElement(elem, lastPath, null);
                    indexes.endElement(transaction, elem, lastPath, listener);
                } finally {
                    indexes.endIndexDocument(transaction, listener);
                }
                lastPath.removeLastComponent();
                return elem;
            case Node.TEXT_NODE:
                final TextImpl text = new TextImpl(newNodeId, ((Text) child).getData());
                text.setOwnerDocument(owner);
                // insert the node
                broker.insertNodeAfter(transaction, last.getNode(), text);
                broker.indexNode(transaction, text, lastPath);
                broker.getIndexController().indexNode(transaction, text, lastPath, listener);
                last.setNode(text);
                return text;
            case Node.CDATA_SECTION_NODE:
                final CDATASectionImpl cdata = new CDATASectionImpl(newNodeId, ((CDATASection) child).getData());
                cdata.setOwnerDocument(owner);
                // insert the node
                broker.insertNodeAfter(transaction, last.getNode(), cdata);
                broker.indexNode(transaction, cdata, lastPath);
                last.setNode(cdata);
                return cdata;
            case Node.ATTRIBUTE_NODE:
                final Attr attr = (Attr) child;
                final String ns = attr.getNamespaceURI();
                final String prefix = (Namespaces.XML_NS.equals(ns) ? XMLConstants.XML_NS_PREFIX : attr.getPrefix());
                String name = attr.getLocalName();
                if (name == null) {
                    name = attr.getName();
                }
                final QName attrName = new QName(name, ns, prefix);
                final AttrImpl attrib = new AttrImpl(attrName, attr.getValue(), broker.getBrokerPool().getSymbols());
                attrib.setNodeId(newNodeId);
                attrib.setOwnerDocument(owner);
                if (ns != null && attrName.compareTo(Namespaces.XML_ID_QNAME) == Constants.EQUAL) {
                    // an xml:id attribute. Normalize the attribute and set its type to ID
                    attrib.setValue(StringValue.trimWhitespace(StringValue.collapseWhitespace(attrib.getValue())));
                    attrib.setType(AttrImpl.ID);
                } else {
                    attrib.setQName(new QName(attrib.getQName(), ElementValue.ATTRIBUTE));
                }
                broker.insertNodeAfter(transaction, last.getNode(), attrib);
                broker.indexNode(transaction, attrib, lastPath);
                broker.getIndexController().indexNode(transaction, attrib, lastPath, listener);
                last.setNode(attrib);
                return attrib;
            case Node.COMMENT_NODE:
                final CommentImpl comment = new CommentImpl(((Comment) child).getData());
                comment.setNodeId(newNodeId);
                comment.setOwnerDocument(owner);
                // insert the node
                broker.insertNodeAfter(transaction, last.getNode(), comment);
                broker.indexNode(transaction, comment, lastPath);
                last.setNode(comment);
                return comment;
            case Node.PROCESSING_INSTRUCTION_NODE:
                final ProcessingInstructionImpl pi = new ProcessingInstructionImpl(newNodeId, ((ProcessingInstruction) child).getTarget(), ((ProcessingInstruction) child).getData());
                pi.setOwnerDocument(owner);
                // insert the node
                broker.insertNodeAfter(transaction, last.getNode(), pi);
                broker.indexNode(transaction, pi, lastPath);
                last.setNode(pi);
                return pi;
            default:
                throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "Unknown node type: " + child.getNodeType() + " " + child.getNodeName());
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while appending node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : org.w3c.dom(org.w3c.dom) QName(org.exist.dom.QName) IndexController(org.exist.indexing.IndexController) EXistException(org.exist.EXistException)

Example 13 with EXistException

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

the class ElementImpl method insertBefore.

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {
    if (refChild == null) {
        return appendChild(newChild);
    } else if (!(refChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
    }
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        insertBefore(transaction, nl, refChild);
        broker.storeXMLResource(transaction, getOwnerDocument());
        transact.commit(transaction);
        return refChild.getPreviousSibling();
    } catch (final TransactionException e) {
        throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, e.getMessage());
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : org.w3c.dom(org.w3c.dom) TransactionException(org.exist.storage.txn.TransactionException) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 14 with EXistException

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

the class ElementImpl method getFirstChild.

@Override
public Node getFirstChild() {
    if (!hasChildNodes()) {
        return null;
    }
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final INodeIterator iterator = broker.getNodeIterator(this)) {
        iterator.next();
        IStoredNode next;
        for (int i = 0; i < getChildCount(); i++) {
            next = iterator.next();
            if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
                return next;
            }
        }
    } catch (final EXistException | IOException e) {
        LOG.warn("Exception while retrieving child node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) EXistException(org.exist.EXistException) IOException(java.io.IOException)

Example 15 with EXistException

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

the class ElementImpl method appendChildren.

@Override
public void appendChildren(final Txn transaction, NodeList nodes, final int child) throws DOMException {
    // attributes are handled differently. Call checkForAttributes to extract them.
    nodes = checkForAttributes(transaction, nodes);
    if (nodes == null || nodes.getLength() == 0) {
        return;
    }
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        final NodePath path = getPath();
        StreamListener listener = null;
        final IndexController indexes = broker.getIndexController();
        // May help getReindexRoot() to make some useful things
        indexes.setDocument(ownerDocument);
        final IStoredNode reindexRoot = indexes.getReindexRoot(this, path, true, true);
        indexes.setMode(ReindexMode.STORE);
        // only reindex if reindexRoot is an ancestor of the current node
        if (reindexRoot == null) {
            listener = indexes.getStreamListener();
        }
        if (children == 0) {
            // no children: append a new child
            appendChildren(transaction, nodeId.newChild(), null, new NodeImplRef(this), path, nodes, listener);
        } else {
            if (child == 1) {
                final Node firstChild = getFirstChild();
                insertBefore(transaction, nodes, firstChild);
            } else {
                if (child > 1 && child <= children) {
                    final NodeList cl = getAttrsAndChildNodes();
                    final IStoredNode<?> last = (IStoredNode<?>) cl.item(child - 2);
                    insertAfter(transaction, nodes, last);
                } else {
                    final IStoredNode<?> last = (IStoredNode<?>) getLastChild(true);
                    appendChildren(transaction, last.getNodeId().nextSibling(), null, new NodeImplRef(getLastNode(last)), path, nodes, listener);
                }
            }
        }
        broker.updateNode(transaction, this, false);
        indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
        broker.flush();
    } catch (final EXistException e) {
        LOG.warn("Exception while appending child node: {}", e.getMessage(), e);
    }
}
Also used : IndexController(org.exist.indexing.IndexController) EXistException(org.exist.EXistException) StreamListener(org.exist.indexing.StreamListener)

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