Search in sources :

Example 6 with NodeImpl

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

the class TestCase method loadVarFromString.

public NodeImpl loadVarFromString(XQueryContext context, String source) throws IOException {
    SAXAdapter adapter = new SAXAdapter(context);
    SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
    factory.setNamespaceAware(true);
    XMLReader xr;
    try {
        SAXParser parser = factory.newSAXParser();
        xr = parser.getXMLReader();
        xr.setContentHandler(adapter);
        xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
    } catch (Exception e) {
        throw new IOException(e);
    }
    try {
        InputSource src = new InputSource(new StringReader(source));
        xr.parse(src);
        return (NodeImpl) adapter.getDocument();
    } catch (SAXException e) {
        throw new IOException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeImpl(org.exist.dom.memtree.NodeImpl) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 7 with NodeImpl

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

the class ParsingFunctions method parse.

private Sequence parse(final String xmlContent, final Sequence[] args) throws XPathException {
    final SAXAdapter adapter = new FragmentSAXAdapter(context, isCalledAs("parse-xml-fragment"));
    final ValidationReport report = validate(xmlContent, adapter);
    if (report.isValid()) {
        return adapter.getDocument();
    } else {
        try {
            context.pushDocumentContext();
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeImpl(org.exist.dom.memtree.NodeImpl) ValidationReport(org.exist.validation.ValidationReport) SAXAdapter(org.exist.dom.memtree.SAXAdapter)

Example 8 with NodeImpl

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

the class FunResolveQName method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) 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);
        }
    }
    final Sequence qnameSeq = args[0];
    if (qnameSeq.isEmpty()) {
        return EmptySequence.EMPTY_SEQUENCE;
    } else {
        context.pushInScopeNamespaces();
        final String qnameString = args[0].getStringValue();
        if (QName.isQName(qnameString) == VALID.val) {
            try {
                String prefix = QName.extractPrefix(qnameString);
                if (prefix == null) {
                    prefix = "";
                }
                String uri = null;
                final NodeValue node = (NodeValue) args[1].itemAt(0);
                if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    NodeProxy proxy = (NodeProxy) node;
                    final NodeSet ancestors = proxy.getAncestors(contextId, true);
                    for (NodeProxy ancestor : ancestors) {
                        proxy = ancestor;
                        final ElementImpl e = (ElementImpl) proxy.getNode();
                        uri = findNamespaceURI(e, prefix);
                        if (uri != null) {
                            break;
                        }
                    }
                } else {
                    NodeImpl next = (NodeImpl) node;
                    do {
                        uri = findNamespaceURI((org.exist.dom.memtree.ElementImpl) next, prefix);
                        if (uri != null) {
                            break;
                        } else {
                            next = (NodeImpl) next.getParentNode();
                        }
                    } while (next != null && next.getNodeType() == Node.ELEMENT_NODE);
                }
                if (uri == null && prefix != null && !prefix.isEmpty()) {
                    throw new XPathException(this, ErrorCodes.FONS0004, "No namespace found for prefix. No binding for prefix '" + prefix + "' was found.", args[0]);
                }
                final String localPart = QName.extractLocalName(qnameString);
                final QName qn = new QName(localPart, uri, prefix);
                final QNameValue result = new QNameValue(context, qn);
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().end(this, "", result);
                }
                context.popInScopeNamespaces();
                return result;
            } catch (final QName.IllegalQNameException e) {
                throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
            }
        } else {
            throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) EmptySequence(org.exist.xquery.value.EmptySequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) ElementImpl(org.exist.dom.persistent.ElementImpl)

Example 9 with NodeImpl

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

the class FunRoot method eval.

/* (non-Javadoc)
         * @see org.exist.xquery.Expression#eval(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 (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());
        }
    }
    Sequence seq;
    Sequence result;
    Item item;
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    if (contextSequence == null || contextSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    }
    // If we have one argumment, we take it into account
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else // Otherwise, we take the context sequence and we iterate over it
    {
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    if (seq.isPersistentSet()) {
        result = new ExtArrayNodeSet(seq.getItemCount());
    } else {
        result = new ValueSequence(seq.getItemCount());
    }
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        item = i.nextItem();
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
        }
        final Sequence s = item.toSequence();
        if (s.isPersistentSet()) {
            final NodeProxy p = s.toNodeSet().get(0);
            result.add(new NodeProxy(p.getOwnerDocument()));
        } else {
            if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
                result.add(item);
            } else if (item.getType() == Type.DOCUMENT) {
                result.add((DocumentImpl) item);
            } else {
                result.add(((NodeImpl) item).getOwnerDocument());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 10 with NodeImpl

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

the class ValueSequence 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 (int i = 0; i <= size; i++) {
        final NodeImpl node = (NodeImpl) values[i];
        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)

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