Search in sources :

Example 16 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class MemTreeBuilder method namespaceNode.

public int namespaceNode(final QName qname, final boolean checkNS) {
    final int lastNode = doc.getLastNode();
    boolean addNode = true;
    if (doc.nodeName != null) {
        final QName elemQN = doc.nodeName[lastNode];
        if (elemQN != null) {
            final String elemPrefix = (elemQN.getPrefix() == null) ? XMLConstants.DEFAULT_NS_PREFIX : elemQN.getPrefix();
            final String elemNs = (elemQN.getNamespaceURI() == null) ? XMLConstants.NULL_NS_URI : elemQN.getNamespaceURI();
            final String qnPrefix = (qname.getPrefix() == null) ? XMLConstants.DEFAULT_NS_PREFIX : qname.getPrefix();
            if (checkNS && XMLConstants.DEFAULT_NS_PREFIX.equals(elemPrefix) && XMLConstants.NULL_NS_URI.equals(elemNs) && XMLConstants.DEFAULT_NS_PREFIX.equals(qnPrefix) && XMLConstants.XMLNS_ATTRIBUTE.equals(qname.getLocalPart())) {
                throw new DOMException(DOMException.NAMESPACE_ERR, "Cannot output a namespace node for the default namespace when the element is in no namespace.");
            }
            if (elemPrefix.equals(qname.getLocalPart()) && (elemQN.getNamespaceURI() != null)) {
                addNode = false;
            }
        }
    }
    return (addNode ? doc.addNamespace(lastNode, qname) : -1);
}
Also used : DOMException(org.w3c.dom.DOMException) QName(org.exist.dom.QName)

Example 17 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class RESTServer method declareExternalAndXQJVariables.

private void declareExternalAndXQJVariables(final XQueryContext context, final ElementImpl variables) throws XPathException {
    final ValueSequence varSeq = new ValueSequence();
    variables.selectChildren(new NameTest(Type.ELEMENT, new QName(Variable.xmlKey(), Namespaces.EXIST_NS)), varSeq);
    for (final SequenceIterator i = varSeq.iterate(); i.hasNext(); ) {
        final ElementImpl variable = (ElementImpl) i.nextItem();
        // get the QName of the variable
        final ElementImpl qname = (ElementImpl) variable.getFirstChild(new NameTest(Type.ELEMENT, new QName("qname", Namespaces.EXIST_NS)));
        String localname = null, prefix = null, uri = null;
        NodeImpl child = (NodeImpl) qname.getFirstChild();
        while (child != null) {
            if ("localname".equals(child.getLocalName())) {
                localname = child.getStringValue();
            } else if ("namespace".equals(child.getLocalName())) {
                uri = child.getStringValue();
            } else if ("prefix".equals(child.getLocalName())) {
                prefix = child.getStringValue();
            }
            child = (NodeImpl) child.getNextSibling();
        }
        if (uri != null && prefix != null) {
            context.declareNamespace(prefix, uri);
        }
        if (localname == null) {
            continue;
        }
        final QName q;
        if (prefix != null && localname != null) {
            q = new QName(localname, uri, prefix);
        } else {
            q = new QName(localname, uri, XMLConstants.DEFAULT_NS_PREFIX);
        }
        // get serialized sequence
        final NodeImpl value = variable.getFirstChild(new NameTest(Type.ELEMENT, Marshaller.ROOT_ELEMENT_QNAME));
        final Sequence sequence;
        try {
            sequence = value == null ? Sequence.EMPTY_SEQUENCE : Marshaller.demarshall(value);
        } catch (final XMLStreamException xe) {
            throw new XPathException(xe.toString());
        }
        // now declare variable
        if (prefix != null) {
            context.declareVariable(q.getPrefix() + ":" + q.getLocalPart(), sequence);
        } else {
            context.declareVariable(q.getLocalPart(), sequence);
        }
    }
}
Also used : ElementImpl(org.exist.dom.memtree.ElementImpl) NodeImpl(org.exist.dom.memtree.NodeImpl) XMLStreamException(javax.xml.stream.XMLStreamException) QName(org.exist.dom.QName)

Example 18 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class ElementImpl method setAttributeNS.

@Override
public void setAttributeNS(final String namespaceURI, final String qualifiedName, final String value) throws DOMException {
    final QName qname;
    try {
        qname = QName.parse(namespaceURI, qualifiedName);
    } catch (final IllegalQNameException e) {
        final short errCode;
        if (e.getValidity() == ILLEGAL_FORMAT.val || (e.getValidity() & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
            errCode = DOMException.NAMESPACE_ERR;
        } else {
            errCode = DOMException.INVALID_CHARACTER_ERR;
        }
        throw new DOMException(errCode, "qualified name is invalid");
    }
    // check the QName is valid for use
    final byte validity = qname.isValid(false);
    if ((validity & QName.Validity.INVALID_LOCAL_PART.val) == QName.Validity.INVALID_LOCAL_PART.val) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "qualified name is invalid");
    } else if ((validity & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
        throw new DOMException(DOMException.NAMESPACE_ERR, "qualified name is invalid");
    }
    setAttribute(qname, value, qn -> getAttributeNodeNS(qn.getNamespaceURI(), qn.getLocalPart()));
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 19 with QName

use of org.exist.dom.QName 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 20 with QName

use of org.exist.dom.QName 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)

Aggregations

QName (org.exist.dom.QName)260 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10 PermissionDeniedException (org.exist.security.PermissionDeniedException)10