Search in sources :

Example 6 with BinaryValue

use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.

the class InflateFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to inflate?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    boolean rawflag = false;
    if (args.length > 1 && !args[1].isEmpty())
        rawflag = args[1].itemAt(0).convertTo(Type.BOOLEAN).effectiveBooleanValue();
    Inflater infl = new Inflater(rawflag);
    // uncompress the data
    try (final InflaterInputStream iis = new InflaterInputStream(bin.getInputStream(), infl);
        final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        int read = -1;
        final byte[] b = new byte[4096];
        while ((read = iis.read(b)) != -1) {
            baos.write(b, 0, read);
        }
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (final IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) InflaterInputStream(java.util.zip.InflaterInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Inflater(java.util.zip.Inflater) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 7 with BinaryValue

use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.

the class DeflateFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to Deflate?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    boolean rawflag = false;
    if (args.length > 1 && !args[1].isEmpty())
        rawflag = args[1].itemAt(0).convertTo(Type.BOOLEAN).effectiveBooleanValue();
    Deflater defl = new Deflater(java.util.zip.Deflater.DEFAULT_COMPRESSION, rawflag);
    // deflate the data
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(baos, defl)) {
        bin.streamBinaryTo(dos);
        dos.flush();
        dos.finish();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    }
}
Also used : Deflater(java.util.zip.Deflater) XPathException(org.exist.xquery.XPathException) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 8 with BinaryValue

use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.

the class ContentFunctions method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is argument the empty sequence?
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    ContentExtraction ce = new ContentExtraction();
    if (isCalledAs("stream-content")) {
        /* binary content */
        BinaryValue binary = (BinaryValue) args[0].itemAt(0);
        /* callback function */
        FunctionReference ref = (FunctionReference) args[2].itemAt(0);
        Map<String, String> mappings = new HashMap<>();
        if (args[3].hasOne()) {
            NodeValue namespaces = (NodeValue) args[3].itemAt(0);
            parseMappings(namespaces, mappings);
        }
        return streamContent(ce, binary, args[1], ref, mappings, args[4]);
    } else {
        try {
            if (isCalledAs("get-metadata")) {
                context.pushDocumentContext();
                try {
                    final MemTreeBuilder builder = context.getDocumentBuilder();
                    builder.startDocument();
                    builder.startElement(new QName("html", XHTML_NS), null);
                    builder.startElement(new QName("head", XHTML_NS), null);
                    final QName qnMeta = new QName("meta", XHTML_NS);
                    final Metadata metadata = ce.extractMetadata((BinaryValue) args[0].itemAt(0));
                    for (final String name : metadata.names()) {
                        for (final String value : metadata.getValues(name)) {
                            final AttributesImpl attributes = new AttributesImpl();
                            attributes.addAttribute("", "name", "name", "string", name);
                            attributes.addAttribute("", "content", "content", "string", value);
                            builder.startElement(qnMeta, attributes);
                            builder.endElement();
                        }
                    }
                    builder.endElement();
                    builder.endElement();
                    builder.endDocument();
                    return builder.getDocument();
                } finally {
                    context.popDocumentContext();
                }
            } else {
                final DocumentBuilderReceiver builder = new DocumentBuilderReceiver();
                builder.setSuppressWhitespace(false);
                final Metadata metadata = ce.extractContentAndMetadata((BinaryValue) args[0].itemAt(0), (ContentHandler) builder);
                return (NodeValue) builder.getDocument();
            }
        } catch (IOException | SAXException | ContentExtractionException ex) {
            LOG.error(ex.getMessage(), ex);
            throw new XPathException(this, ex.getMessage(), ex);
        }
    }
}
Also used : ContentExtraction(org.exist.contentextraction.ContentExtraction) NodeValue(org.exist.xquery.value.NodeValue) HashMap(java.util.HashMap) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) Metadata(org.apache.tika.metadata.Metadata) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) ContentExtractionException(org.exist.contentextraction.ContentExtractionException) SAXException(org.xml.sax.SAXException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) FunctionReference(org.exist.xquery.value.FunctionReference)

Example 9 with BinaryValue

use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.

the class StreamBinary method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final ResponseWrapper response) throws XPathException {
    if (args[0].isEmpty() || args[1].isEmpty()) {
        return (Sequence.EMPTY_SEQUENCE);
    }
    final BinaryValue binary = (BinaryValue) args[0].itemAt(0);
    final String contentType = args[1].getStringValue();
    String filename = null;
    if ((args.length > 2) && !args[2].isEmpty()) {
        filename = args[2].getStringValue();
    }
    if (!"org.exist.http.servlets.HttpResponseWrapper".equals(response.getClass().getName())) {
        throw (new XPathException(this, ErrorCodes.XPDY0002, signature.toString() + " can only be used within the EXistServlet or XQueryServlet"));
    }
    response.setHeader("Content-Type", contentType);
    if (filename != null) {
        response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
    }
    try {
        final OutputStream os = response.getOutputStream();
        binary.streamBinaryTo(response.getOutputStream());
        os.close();
        // commit the response
        response.flushBuffer();
    } catch (final IOException e) {
        throw (new XPathException(this, "IO exception while streaming data: " + e.getMessage(), e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : OutputStream(java.io.OutputStream) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException)

Example 10 with BinaryValue

use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.

the class DecodeExiFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    try {
        BinaryValue exiBinary = ((BinaryValue) args[0].itemAt(0));
        context.pushDocumentContext();
        try {
            MemTreeBuilder builder = context.getDocumentBuilder();
            // create default factory and EXI grammar for schema
            EXIFactory exiFactory = DefaultEXIFactory.newInstance();
            if (args.length > 1) {
                if (!args[1].isEmpty()) {
                    Item xsdItem = args[1].itemAt(0);
                    try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
                        GrammarFactory grammarFactory = GrammarFactory.newInstance();
                        Grammars grammar = grammarFactory.createGrammars(xsdInputStream);
                        exiFactory.setGrammars(grammar);
                    }
                }
            }
            SAXDecoder decoder = new SAXDecoder(exiFactory);
            SAXAdapter adapter = new AppendingSAXAdapter(builder);
            decoder.setContentHandler(adapter);
            try (InputStream inputStream = exiBinary.getInputStream()) {
                decoder.parse(new InputSource(inputStream));
            }
            return (NodeValue) builder.getDocument().getDocumentElement();
        } finally {
            context.popDocumentContext();
        }
    } catch (EXIException | SAXException | IOException exie) {
        throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) InputSource(org.xml.sax.InputSource) SAXDecoder(com.siemens.ct.exi.api.sax.SAXDecoder) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) GrammarFactory(com.siemens.ct.exi.GrammarFactory) EXIException(com.siemens.ct.exi.exceptions.EXIException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JavaErrorCode(org.exist.xquery.ErrorCodes.JavaErrorCode) Item(org.exist.xquery.value.Item) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) AppendingSAXAdapter(org.exist.dom.memtree.AppendingSAXAdapter) SAXAdapter(org.exist.dom.memtree.SAXAdapter) AppendingSAXAdapter(org.exist.dom.memtree.AppendingSAXAdapter) Grammars(com.siemens.ct.exi.grammars.Grammars) DefaultEXIFactory(com.siemens.ct.exi.helpers.DefaultEXIFactory) EXIFactory(com.siemens.ct.exi.EXIFactory)

Aggregations

BinaryValue (org.exist.xquery.value.BinaryValue)17 IOException (java.io.IOException)10 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)7 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)5 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)5 Image (java.awt.Image)4 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 IntegerValue (org.exist.xquery.value.IntegerValue)3 NodeValue (org.exist.xquery.value.NodeValue)3 BufferedImage (java.awt.image.BufferedImage)2 XQueryContext (org.exist.xquery.XQueryContext)2 BinaryValueFromInputStream (org.exist.xquery.value.BinaryValueFromInputStream)2 Item (org.exist.xquery.value.Item)2 Sequence (org.exist.xquery.value.Sequence)2 Test (org.junit.Test)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)1