Search in sources :

Example 1 with NodeInputStream

use of org.exist.validation.internal.node.NodeInputStream in project exist by eXist-db.

the class EXIUtils method getInputStream.

protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
    switch(item.getType()) {
        case Type.ANY_URI:
            LOG.debug("Streaming xs:anyURI");
            // anyURI provided
            String url = item.getStringValue();
            // Fix URL
            if (url.startsWith("/")) {
                url = "xmldb:exist://" + url;
            }
            return new URL(url).openStream();
        case Type.ELEMENT:
        case Type.DOCUMENT:
            LOG.debug("Streaming element or document node");
            /*
            if (item instanceof NodeProxy) {
                NodeProxy np = (NodeProxy) item;
                String url = "xmldb:exist://" + np.getDocument().getBaseURI();
                LOG.debug("Document detected, adding URL " + url);
                streamSource.setSystemId(url);
            }
            */
            // Node provided
            final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
                final Serializer serializer = context.getBroker().borrowSerializer();
                try {
                    fn.accept(serializer);
                } finally {
                    context.getBroker().returnSerializer(serializer);
                }
            };
            NodeValue node = (NodeValue) item;
            return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        default:
            LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
            throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) Logger(org.apache.logging.log4j.Logger) NodeValue(org.exist.xquery.value.NodeValue) Serializer(org.exist.storage.serializers.Serializer) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) URL(java.net.URL) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Example 2 with NodeInputStream

use of org.exist.validation.internal.node.NodeInputStream in project exist by eXist-db.

the class Shared method getStreamSource.

public static StreamSource getStreamSource(Item item, XQueryContext context) throws XPathException, IOException {
    final StreamSource streamSource = new StreamSource();
    if (item.getType() == Type.JAVA_OBJECT) {
        LOG.debug("Streaming Java object");
        final Object obj = ((JavaObjectValue) item).getObject();
        if (!(obj instanceof File)) {
            throw new XPathException("Passed java object should be a File");
        }
        final File inputFile = (File) obj;
        final InputStream is = new FileInputStream(inputFile);
        streamSource.setInputStream(is);
        streamSource.setSystemId(inputFile.toURI().toURL().toString());
    } else if (item.getType() == Type.ANY_URI) {
        LOG.debug("Streaming xs:anyURI");
        // anyURI provided
        String url = item.getStringValue();
        // Fix URL
        if (url.startsWith("/")) {
            url = "xmldb:exist://" + url;
        }
        final InputStream is = new URL(url).openStream();
        streamSource.setInputStream(is);
        streamSource.setSystemId(url);
    } else if (item.getType() == Type.ELEMENT || item.getType() == Type.DOCUMENT) {
        LOG.debug("Streaming element or document node");
        if (item instanceof NodeProxy) {
            final NodeProxy np = (NodeProxy) item;
            final String url = "xmldb:exist://" + np.getOwnerDocument().getBaseURI();
            LOG.debug("Document detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
        // Node provided
        final DBBroker broker = context.getBroker();
        final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
            final Serializer serializer = broker.borrowSerializer();
            try {
                fn.accept(serializer);
            } finally {
                broker.returnSerializer(serializer);
            }
        };
        final NodeValue node = (NodeValue) item;
        final InputStream is = new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        streamSource.setInputStream(is);
    } else if (item.getType() == Type.BASE64_BINARY || item.getType() == Type.HEX_BINARY) {
        LOG.debug("Streaming base64 binary");
        final BinaryValue binary = (BinaryValue) item;
        final byte[] data = binary.toJavaObject(byte[].class);
        final InputStream is = new UnsynchronizedByteArrayInputStream(data);
        streamSource.setInputStream(is);
        if (item instanceof Base64BinaryDocument) {
            final Base64BinaryDocument b64doc = (Base64BinaryDocument) item;
            final String url = "xmldb:exist://" + b64doc.getUrl();
            LOG.debug("Base64BinaryDocument detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
    } else {
        LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
        throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
    return streamSource;
}
Also used : ValidationReport(org.exist.validation.ValidationReport) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeProxy(org.exist.dom.persistent.NodeProxy) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ArrayList(java.util.ArrayList) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeValue(org.exist.xquery.value.NodeValue) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) NodeImpl(org.exist.dom.memtree.NodeImpl) XQueryContext(org.exist.xquery.XQueryContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) InputSource(org.xml.sax.InputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) ValidationReportItem(org.exist.validation.ValidationReportItem) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Logger(org.apache.logging.log4j.Logger) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) DBBroker(org.exist.storage.DBBroker) Serializer(org.exist.storage.serializers.Serializer) Sequence(org.exist.xquery.value.Sequence) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) FileInputStream(java.io.FileInputStream) URL(java.net.URL) DBBroker(org.exist.storage.DBBroker) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) File(java.io.File) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Serializer (org.exist.storage.serializers.Serializer)2 NodeInputStream (org.exist.validation.internal.node.NodeInputStream)2 XPathException (org.exist.xquery.XPathException)2 XQueryContext (org.exist.xquery.XQueryContext)2 Item (org.exist.xquery.value.Item)2 NodeValue (org.exist.xquery.value.NodeValue)2 Type (org.exist.xquery.value.Type)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 StreamSource (javax.xml.transform.stream.StreamSource)1 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1