Search in sources :

Example 11 with DocumentImpl

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

the class EXistTreeBuilder method close.

public DocumentImpl close() {
    builder.endDocument();
    final DocumentImpl doc = builder.getDocument();
    builder.getContext().popDocumentContext();
    return doc;
}
Also used : DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 12 with DocumentImpl

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

the class DynamicPIConstructor method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (newDocumentContext) {
        context.pushDocumentContext();
    }
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        context.proceed(this, builder);
        final Sequence nameSeq = name.eval(contextSequence, contextItem);
        // TODO : get rid of getLength()
        if (!nameSeq.hasOne()) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a single value");
        }
        final Item nameItem = nameSeq.itemAt(0);
        if (!(nameItem.getType() == Type.STRING || nameItem.getType() == Type.NCNAME || nameItem.getType() == Type.UNTYPED_ATOMIC)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a " + Type.getTypeName(Type.STRING) + " or a " + Type.getTypeName(Type.NCNAME) + " or a " + Type.getTypeName(Type.UNTYPED_ATOMIC) + ". Got: " + Type.getTypeName(nameItem.getType()));
        }
        if (!XMLNames.isNCName(nameSeq.getStringValue())) {
            throw new XPathException(this, ErrorCodes.XQDY0041, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
        }
        if (nameSeq.getStringValue().equalsIgnoreCase("XML")) {
            throw new XPathException(this, ErrorCodes.XQDY0064, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
        }
        String contentString;
        final Sequence contentSeq = content.eval(contextSequence, contextItem);
        if (contentSeq.isEmpty()) {
            contentString = "";
        } else {
            final StringBuilder buf = new StringBuilder();
            for (final SequenceIterator i = Atomize.atomize(contentSeq).iterate(); i.hasNext(); ) {
                context.proceed(this, builder);
                final Item next = i.nextItem();
                if (buf.length() > 0) {
                    buf.append(' ');
                }
                buf.append(next.getStringValue());
            }
            while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) buf.deleteCharAt(0);
            contentString = buf.toString();
        }
        if (contentString.contains("?>")) {
            throw new XPathException(this, ErrorCodes.XQDY0026, contentString + "' is not a valid processing intruction content", contentSeq);
        }
        final int nodeNo = builder.processingInstruction(nameSeq.getStringValue(), contentString);
        final Sequence result = ((DocumentImpl) builder.getDocument()).getNode(nodeNo);
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().end(this, "", result);
        }
        return result;
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
}
Also used : Item(org.exist.xquery.value.Item) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 13 with DocumentImpl

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

the class FunDocumentURI method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final boolean contextItemIsAbsent = (contextItem == null);
    final boolean argumentIsOmitted = (getArgumentCount() == 0);
    // Error condition
    if (argumentIsOmitted && contextItemIsAbsent) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Context item is absent.");
    }
    // Get sequence from contextItem or from context Sequence
    final Sequence seq = (argumentIsOmitted) ? contextItem.toSequence() : getArgument(0).eval(contextSequence, contextItem);
    // Rule 1: If $arg is the empty sequence, the function returns the empty sequence.
    if (seq.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // Error condition
    if (argumentIsOmitted && !Type.subTypeOf(seq.getItemType(), Type.NODE)) {
        throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node.");
    }
    // Error condition: Returns the empty sequence if the node is not a document
    Sequence result = Sequence.EMPTY_SEQUENCE;
    final NodeValue value = (NodeValue) seq.itemAt(0);
    if (value.getImplementationType() == NodeValue.PERSISTENT_NODE) {
        final NodeProxy node = (NodeProxy) value;
        if (node.isDocument()) {
            final XmldbURI path = node.getOwnerDocument().getURI();
            result = new AnyURIValue(path);
        }
    } else {
        if (value instanceof DocumentImpl && ((DocumentImpl) value).getDocumentURI() != null) {
            result = new AnyURIValue(((DocumentImpl) value).getDocumentURI());
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.memtree.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI)

Example 14 with DocumentImpl

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

the class OrderedValueSequence method toNodeSet.

@Override
public NodeSet toNodeSet() throws XPathException {
    // return early
    if (isEmpty()) {
        return NodeSet.EMPTY_SET;
    }
    // for this method to work, all items have to be nodes
    if (itemType != Type.ANY_TYPE && Type.subTypeOf(itemType, Type.NODE)) {
        // Was ExtArrayNodeset() which orders the nodes in document order
        // The order seems to change between different invocations !!!
        final NodeSet set = new AVLTreeNodeSet();
        // NodeSet set = new ArraySet(100);
        for (int i = 0; i < count; i++) {
            NodeValue v = (NodeValue) items[i].item;
            if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
                // found an in-memory document
                final org.exist.dom.memtree.DocumentImpl doc = v.getType() == Type.DOCUMENT ? (org.exist.dom.memtree.DocumentImpl) v : ((NodeImpl) v).getOwnerDocument();
                if (doc == null) {
                    continue;
                }
                // make this document persistent: doc.makePersistent()
                // returns a map of all root node ids mapped to the corresponding
                // persistent node. We scan the current sequence and replace all
                // in-memory nodes with their new persistent node objects.
                final DocumentImpl expandedDoc = doc.expandRefs(null);
                final org.exist.dom.persistent.DocumentImpl newDoc = expandedDoc.makePersistent();
                if (newDoc != null) {
                    final NodeId rootId = newDoc.getBrokerPool().getNodeFactory().createInstance();
                    for (int j = i; j < count; j++) {
                        v = (NodeValue) items[j].item;
                        if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
                            NodeImpl node = (NodeImpl) v;
                            final Document nodeOwnerDoc = node.getNodeType() == Node.DOCUMENT_NODE ? (org.exist.dom.memtree.DocumentImpl) v : ((NodeImpl) v).getOwnerDocument();
                            if (nodeOwnerDoc == doc) {
                                node = expandedDoc.getNode(node.getNodeNumber());
                                NodeId nodeId = node.getNodeId();
                                if (nodeId == null) {
                                    throw new XPathException("Internal error: nodeId == null");
                                }
                                if (node.getNodeType() == Node.DOCUMENT_NODE) {
                                    nodeId = rootId;
                                } else {
                                    nodeId = rootId.append(nodeId);
                                }
                                final NodeProxy p = new NodeProxy(newDoc, nodeId, node.getNodeType());
                                if (p != null) {
                                    // replace the node by the NodeProxy
                                    items[j].item = p;
                                }
                            }
                        }
                    }
                }
                set.add((NodeProxy) items[i].item);
            } else {
                set.add((NodeProxy) v);
            }
        }
        return set;
    } else {
        throw new XPathException("Type error: the sequence cannot be converted into" + " a node set. Item type is " + Type.getTypeName(itemType));
    }
}
Also used : AVLTreeNodeSet(org.exist.dom.persistent.AVLTreeNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) AVLTreeNodeSet(org.exist.dom.persistent.AVLTreeNodeSet) Document(org.w3c.dom.Document) DocumentImpl(org.exist.dom.memtree.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) NodeId(org.exist.numbering.NodeId) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 15 with DocumentImpl

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

the class FunId method getId.

private void getId(Sequence result, Sequence seq, String id) throws XPathException {
    final Set<DocumentImpl> visitedDocs = new TreeSet<>();
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        final NodeImpl v = (NodeImpl) i.nextItem();
        final DocumentImpl doc = v.getNodeType() == Node.DOCUMENT_NODE ? (DocumentImpl) v : v.getOwnerDocument();
        if (doc != null && !visitedDocs.contains(doc)) {
            final NodeImpl elem = doc.selectById(id);
            if (elem != null) {
                result.add(elem);
            }
            visitedDocs.add(doc);
        }
    }
}
Also used : NodeImpl(org.exist.dom.memtree.NodeImpl) TreeSet(java.util.TreeSet) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.memtree.DocumentImpl)23 NodeImpl (org.exist.dom.memtree.NodeImpl)9 XPathException (org.exist.xquery.XPathException)8 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)6 Document (org.w3c.dom.Document)6 Sequence (org.exist.xquery.value.Sequence)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 NodeId (org.exist.numbering.NodeId)4 IOException (java.io.IOException)3 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)3 SAXException (org.xml.sax.SAXException)3 HashMap (java.util.HashMap)2 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)2 SimpleNamespaceContext (org.custommonkey.xmlunit.SimpleNamespaceContext)2 XpathEngine (org.custommonkey.xmlunit.XpathEngine)2 QName (org.exist.dom.QName)2 SAXAdapter (org.exist.dom.memtree.SAXAdapter)2 org.exist.dom.persistent (org.exist.dom.persistent)2 NodeSet (org.exist.dom.persistent.NodeSet)2 Subject (org.exist.security.Subject)2