Search in sources :

Example 76 with MemTreeBuilder

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

the class DynamicCommentConstructor 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();
    }
    Sequence result;
    try {
        final Sequence contentSeq = content.eval(contextSequence, contextItem);
        if (contentSeq.isEmpty()) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            context.proceed(this, builder);
            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.toString());
            }
            if (buf.indexOf("--") != Constants.STRING_NOT_FOUND || buf.toString().endsWith("-")) {
                throw new XPathException(this, ErrorCodes.XQDY0072, "'" + buf.toString() + "' is not a valid comment");
            }
            final int nodeNr = builder.comment(buf.toString());
            result = builder.getDocument().getNode(nodeNr);
        }
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
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)

Example 77 with MemTreeBuilder

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

the class DynamicTextConstructor 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();
    }
    Sequence result;
    try {
        final Sequence contentSeq = content.eval(contextSequence, contextItem);
        if (contentSeq.isEmpty()) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            context.proceed(this, builder);
            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.toString());
            }
            // such a text node will be deleted or merged with another text node.
            if (!newDocumentContext && buf.length() == 0) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                final int nodeNr = builder.characters(buf);
                result = builder.getDocument().getNode(nodeNr);
            }
        }
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
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)

Example 78 with MemTreeBuilder

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

the class ModuleUtils method inputSourceToXML.

/**
 * Takes a InputSource of XML and Creates an XML Node from it using SAX in the
 * context of the query
 *
 * @param context
 *            The Context of the calling XQuery
 * @param src
 *            The InputSource of XML
 *
 * @throws SAXException in case of a SAX error
 * @throws IOException in case of error reading input source
 * @return The NodeValue of XML
 */
private static NodeValue inputSourceToXML(XQueryContext context, SAXSource src) throws SAXException, IOException {
    if (src.getXMLReader() == null) {
        throw new SAXException("No XML Reader specified.");
    }
    final XMLReader reader = src.getXMLReader();
    context.pushDocumentContext();
    try {
        // try and construct xml document from input stream, we use eXist's
        // in-memory DOM implementation
        // TODO : we should be able to cope with context.getBaseURI()
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        reader.setContentHandler(receiver);
        reader.parse(src.getInputSource());
        final Document doc = receiver.getDocument();
        return ((NodeValue) doc);
    } finally {
        context.popDocumentContext();
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document)

Example 79 with MemTreeBuilder

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

the class ModuleUtils method inputSourceToXML.

/**
 * Takes a InputSource of XML and Creates an XML Node from it using SAX in the
 * context of the query
 *
 * @param context
 *            The Context of the calling XQuery
 * @param inputSource
 *            The InputSource of XML
 *
 * @return The NodeValue of XML
 * @throws SAXException in case of a SAX error
 * @throws IOException in case of error reading input source
 */
public static NodeValue inputSourceToXML(XQueryContext context, InputSource inputSource) throws SAXException, IOException {
    context.pushDocumentContext();
    XMLReader reader = null;
    try {
        // try and construct xml document from input stream, we use eXist's
        // in-memory DOM implementation
        reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader();
        LOG.debug("Parsing XML response ...");
        // TODO : we should be able to cope with context.getBaseURI()
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        reader.setContentHandler(receiver);
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
        reader.parse(inputSource);
        final Document doc = receiver.getDocument();
        // return (NodeValue)doc.getDocumentElement();
        return ((NodeValue) doc);
    } finally {
        context.popDocumentContext();
        if (reader != null) {
            context.getBroker().getBrokerPool().getParserPool().returnXMLReader(reader);
        }
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document)

Example 80 with MemTreeBuilder

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

the class RestXqServiceImpl method parseAsXml.

private DocumentImpl parseAsXml(final InputStream is) {
    DocumentImpl result = null;
    XMLReader reader = null;
    try {
        // try and construct xml document from input stream, we use eXist's in-memory DOM implementation
        // we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread
        final InputSource src = new InputSource(new CloseShieldInputStream(is));
        reader = getBrokerPool().getParserPool().borrowXMLReader();
        final MemTreeBuilder builder = new MemTreeBuilder();
        builder.startDocument();
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        reader.setContentHandler(receiver);
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
        reader.parse(src);
        builder.endDocument();
        final Document doc = receiver.getDocument();
        result = (DocumentImpl) doc;
    } catch (final SAXException | IOException saxe) {
    // do nothing, we will default to trying to return a string below
    } finally {
        if (reader != null) {
            getBrokerPool().getParserPool().returnXMLReader(reader);
        }
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) IOException(java.io.IOException) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document) DocumentImpl(org.exist.dom.memtree.DocumentImpl) XMLReader(org.xml.sax.XMLReader) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) SAXException(org.xml.sax.SAXException)

Aggregations

MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)81 QName (org.exist.dom.QName)28 XPathException (org.exist.xquery.XPathException)23 Sequence (org.exist.xquery.value.Sequence)19 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)17 SAXException (org.xml.sax.SAXException)16 NodeValue (org.exist.xquery.value.NodeValue)15 IOException (java.io.IOException)14 NodeImpl (org.exist.dom.memtree.NodeImpl)14 Item (org.exist.xquery.value.Item)8 SequenceIterator (org.exist.xquery.value.SequenceIterator)8 Test (org.junit.Test)8 XQueryContext (org.exist.xquery.XQueryContext)7 ValueSequence (org.exist.xquery.value.ValueSequence)7 AttributesImpl (org.xml.sax.helpers.AttributesImpl)7 DocumentImpl (org.exist.dom.memtree.DocumentImpl)6 BrokerPool (org.exist.storage.BrokerPool)6 InputSource (org.xml.sax.InputSource)6 MalformedURLException (java.net.MalformedURLException)5 Path (java.nio.file.Path)5