Search in sources :

Example 1 with BinaryValue

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

the class BinaryToStringTest method roundtrip.

@Test
public void roundtrip() throws XPathException {
    final String value = "hello world";
    final String encoding = "UTF-8";
    TestableBinaryToString testable = new TestableBinaryToString(new MockXQueryContext(), null);
    final BinaryValue binary = testable.stringToBinary(value, encoding);
    StringValue result = testable.binaryToString(binary, encoding);
    assertEquals(value, result.getStringValue());
}
Also used : BinaryValue(org.exist.xquery.value.BinaryValue) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 2 with BinaryValue

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

the class ScaleFunction method eval.

/**
 * evaluate the call to the xquery scale() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the scale() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the scale() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the maximum dimensions to scale to
    int maxHeight = MAXHEIGHT;
    int maxWidth = MAXWIDTH;
    if (!args[1].isEmpty()) {
        maxHeight = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany())
            maxWidth = ((IntegerValue) args[1].itemAt(1)).getInt();
    }
    // get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
    // TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try (// get the image data
    InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
        image = ImageIO.read(inputStream);
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }
        // scale the image
        bImage = ImageModule.createThumb(image, maxHeight, maxWidth);
        // get the new scaled image
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
            ImageIO.write(bImage, formatName, os);
            // return the new scaled image data
            return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
        }
    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XPathException(org.exist.xquery.XPathException)

Example 3 with BinaryValue

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

the class FilterInputStreamCacheMonitorTest method binaryResult.

@Test
public void binaryResult() throws XMLDBException {
    final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
    // assert no binaries in use yet
    int activeCount = monitor.getActive().size();
    if (activeCount != 0) {
        fail("FilterInputStreamCacheMonitor should have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
    }
    ResourceSet resourceSet = null;
    try {
        resourceSet = existXmldbEmbeddedServer.executeQuery("util:binary-doc('/db/" + TEST_COLLECTION_NAME + "/icon.png')");
        assertEquals(1, resourceSet.getSize());
        try (final EXistResource resource = (EXistResource) resourceSet.getResource(0)) {
            assertTrue(resource instanceof LocalBinaryResource);
            assertTrue(((ExtendedResource) resource).getExtendedContent() instanceof BinaryValue);
            // one active binary (as it is in the result set)
            assertEquals(1, monitor.getActive().size());
        }
        // assert no active binaries as we just closed the resource in the try-with-resources
        activeCount = monitor.getActive().size();
        if (activeCount != 0) {
            fail("FilterInputStreamCacheMonitor should again have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
        }
    } finally {
        resourceSet.clear();
    }
}
Also used : EXistResource(org.exist.xmldb.EXistResource) LocalBinaryResource(org.exist.xmldb.LocalBinaryResource) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryValue(org.exist.xquery.value.BinaryValue) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 4 with BinaryValue

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

the class CropFunction method eval.

/**
 * evaluate the call to the xquery crop() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the crop() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the crop() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the maximum dimensions to crop to
    int x1 = 0;
    int y1 = 0;
    int x2 = MAXHEIGHT;
    int y2 = MAXWIDTH;
    int width = 0;
    int height = 0;
    if (!args[1].isEmpty()) {
        x1 = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany()) {
            y1 = ((IntegerValue) args[1].itemAt(1)).getInt();
            x2 = ((IntegerValue) args[1].itemAt(2)).getInt();
            y2 = ((IntegerValue) args[1].itemAt(3)).getInt();
            width = x2 - x1;
            height = y2 - y1;
        }
    }
    if (width < 1) {
        logger.error("cropping error: x2 value must be greater than x1");
        return Sequence.EMPTY_SEQUENCE;
    }
    if (height < 1) {
        logger.error("cropping error: y2 must be greater than y1");
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
    // TODO currently ONLY tested for JPEG!!!
    BufferedImage bImage = null;
    try (InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
        // get the image data
        Image image = ImageIO.read(inputStream);
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }
        // crop the image
        Image cropImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x1, y1, width, height)));
        if (cropImage instanceof BufferedImage) {
            // just in case cropImage is allready an BufferedImage
            bImage = (BufferedImage) cropImage;
        } else {
            bImage = new BufferedImage(cropImage.getWidth(null), cropImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // Paint the image onto the buffered image
            Graphics2D g = bImage.createGraphics();
            g.drawImage(cropImage, 0, 0, null);
            g.dispose();
        }
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
            ImageIO.write(bImage, formatName, os);
            // return the new croped image data
            return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
        }
    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XPathException(org.exist.xquery.XPathException) Graphics2D(java.awt.Graphics2D) FilteredImageSource(java.awt.image.FilteredImageSource) CropImageFilter(java.awt.image.CropImageFilter)

Example 5 with BinaryValue

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

the class GZipFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to GZip?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    // gzip the data
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
        final GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
        bin.streamBinaryTo(gzos);
        gzos.flush();
        gzos.finish();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (final IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) XPathException(org.exist.xquery.XPathException) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

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