Search in sources :

Example 46 with NodeImpl

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

the class GetIndexStatistics method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final IndexStatistics index = (IndexStatistics) context.getBroker().getBrokerPool().getIndexManager().getIndexById(IndexStatistics.ID);
    if (index == null) {
        // module may not be enabled
        return Sequence.EMPTY_SEQUENCE;
    }
    final SAXAdapter adapter = new SAXAdapter(context);
    try {
        adapter.startDocument();
        index.toSAX(adapter);
        adapter.endDocument();
    } catch (final SAXException e) {
        throw new XPathException(this, "Error caught while retrieving statistics: " + e.getMessage(), e);
    }
    final DocumentImpl doc = (DocumentImpl) adapter.getDocument();
    return (NodeImpl) doc.getFirstChild();
}
Also used : IndexStatistics(org.exist.storage.statistics.IndexStatistics) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) SAXAdapter(org.exist.dom.memtree.SAXAdapter) DocumentImpl(org.exist.dom.memtree.DocumentImpl) SAXException(org.xml.sax.SAXException)

Example 47 with NodeImpl

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

the class PIConstructor method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (newDocumentContext) {
        context.pushDocumentContext();
    }
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final int nodeNr = builder.processingInstruction(target, data);
        final NodeImpl node = builder.getDocument().getNode(nodeNr);
        return node;
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeImpl(org.exist.dom.memtree.NodeImpl)

Example 48 with NodeImpl

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

the class DynamicAttributeConstructor 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();
    }
    NodeImpl node;
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.setReplaceAttributeFlag(replaceAttribute);
        context.proceed(this, builder);
        final Sequence nameSeq = qnameExpr.eval(contextSequence, contextItem);
        if (!nameSeq.hasOne()) {
            throw new XPathException(this, "The name expression should evaluate to a single value");
        }
        final Item qnItem = nameSeq.itemAt(0);
        QName qn;
        if (qnItem.getType() == Type.QNAME) {
            qn = ((QNameValue) qnItem).getQName();
        } else
            try {
                qn = QName.parse(context, nameSeq.getStringValue(), null);
            } catch (final QName.IllegalQNameException e) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "'" + nameSeq.getStringValue() + "' is not a valid attribute name");
            }
        // Not in the specs but... makes sense
        if (!XMLNames.isName(qn.getLocalPart())) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qn.getLocalPart() + "' is not a valid attribute name");
        }
        if ("xmlns".equals(qn.getLocalPart()) && qn.getNamespaceURI().isEmpty()) {
            throw new XPathException(this, ErrorCodes.XQDY0044, "'" + qn.getLocalPart() + "' is not a valid attribute name");
        }
        String value;
        final Sequence valueSeq = valueExpr.eval(contextSequence, contextItem);
        if (valueSeq.isEmpty()) {
            value = "";
        } else {
            final StringBuilder buf = new StringBuilder();
            for (final SequenceIterator i = Atomize.atomize(valueSeq).iterate(); i.hasNext(); ) {
                final Item next = i.nextItem();
                buf.append(next.getStringValue());
                if (i.hasNext()) {
                    buf.append(' ');
                }
            }
            value = buf.toString();
        }
        value = DynamicAttributeConstructor.normalize(this, qn, value);
        node = null;
        try {
            final int nodeNr = builder.addAttribute(qn, value);
            node = builder.getDocument().getAttribute(nodeNr);
        } catch (final DOMException e) {
            throw new XPathException(this, ErrorCodes.XQDY0025, "element has more than one attribute '" + qn + "'");
        }
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", node);
    }
    return node;
}
Also used : DOMException(org.w3c.dom.DOMException) NodeImpl(org.exist.dom.memtree.NodeImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName)

Example 49 with NodeImpl

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

the class ArrayListValueSequence method expand.

/**
 * Scan the sequence and check all in-memory documents.
 * They may contains references to nodes stored in the database.
 * Expand those references to get a pure in-memory DOM tree.
 */
private void expand() {
    final Set<DocumentImpl> docs = new HashSet<>();
    for (final Item value : values) {
        final NodeImpl node = (NodeImpl) value;
        final DocumentImpl ownerDoc = node.getNodeType() == Node.DOCUMENT_NODE ? (DocumentImpl) node : node.getOwnerDocument();
        if (ownerDoc.hasReferenceNodes()) {
            docs.add(ownerDoc);
        }
    }
    for (final DocumentImpl doc : docs) {
        doc.expand();
    }
}
Also used : NodeImpl(org.exist.dom.memtree.NodeImpl) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 50 with NodeImpl

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

the class ArrayListValueSequence method getParents.

@Override
public Sequence getParents(final NodeTest test) throws XPathException {
    final ArrayListValueSequence nodes = new ArrayListValueSequence();
    for (final Item value : values) {
        final NodeImpl node = (NodeImpl) value;
        final NodeImpl parent = (NodeImpl) node.selectParentNode();
        if (parent != null && test.matches(parent)) {
            nodes.add(parent);
        }
    }
    return nodes;
}
Also used : NodeImpl(org.exist.dom.memtree.NodeImpl)

Aggregations

NodeImpl (org.exist.dom.memtree.NodeImpl)53 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)14 XPathException (org.exist.xquery.XPathException)9 DocumentImpl (org.exist.dom.memtree.DocumentImpl)8 Sequence (org.exist.xquery.value.Sequence)8 SAXException (org.xml.sax.SAXException)8 SAXAdapter (org.exist.dom.memtree.SAXAdapter)6 NodeProxy (org.exist.dom.persistent.NodeProxy)6 ValueSequence (org.exist.xquery.value.ValueSequence)6 IOException (java.io.IOException)5 Document (org.w3c.dom.Document)5 InputSource (org.xml.sax.InputSource)5 StringReader (java.io.StringReader)4 QName (org.exist.dom.QName)4 SequenceIterator (org.exist.xquery.value.SequenceIterator)4 XMLReader (org.xml.sax.XMLReader)4 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)3 NodeSet (org.exist.dom.persistent.NodeSet)3 NodeId (org.exist.numbering.NodeId)3 ValidationReport (org.exist.validation.ValidationReport)3