Search in sources :

Example 1 with Base64BinaryValueType

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

the class ResourceFunctionExecutorImpl method convertToType.

// TODO this needs to be abstracted into EXQuery library / or not, see the TODOs below
private <X> TypedValue<X> convertToType(final XQueryContext xqueryContext, final String argumentName, final TypedValue typedValue, final org.exquery.xquery.Type destinationType, final Class<X> underlyingDestinationClass) throws RestXqServiceException {
    // TODO consider changing Types that can be used as <T> to TypedValue to a set of interfaces for XDM types that
    // require absolute minimal implementation, and we provide some default or abstract implementations if possible
    final Item convertedValue;
    try {
        final int existDestinationType = TypeAdapter.toExistType(destinationType);
        final Item value;
        // Consider a factory or java.util.ServiceLoader pattern
        if (typedValue instanceof org.exquery.xdm.type.StringTypedValue) {
            value = new StringValue(((org.exquery.xdm.type.StringTypedValue) typedValue).getValue());
        } else if (typedValue instanceof org.exquery.xdm.type.Base64BinaryTypedValue) {
            value = BinaryValueFromInputStream.getInstance(xqueryContext, new Base64BinaryValueType(), ((org.exquery.xdm.type.Base64BinaryTypedValue) typedValue).getValue());
        } else {
            value = (Item) typedValue.getValue();
        }
        if (existDestinationType == value.getType()) {
            convertedValue = value;
        } else if (value instanceof AtomicValue) {
            convertedValue = value.convertTo(existDestinationType);
        } else {
            LOG.warn("Could not convert parameter '{}' from '{}' to '{}'.", argumentName, typedValue.getType().name(), destinationType.name());
            convertedValue = value;
        }
    } catch (final XPathException xpe) {
        // TODO define an ErrorCode
        throw new RestXqServiceException("TODO need to implement error code for problem with parameter conversion!: " + xpe.getMessage(), xpe);
    }
    return new TypedValue<X>() {

        @Override
        public org.exquery.xquery.Type getType() {
            // return destinationType;
            return TypeAdapter.toExQueryType(convertedValue.getType());
        }

        @Override
        public X getValue() {
            return (X) convertedValue;
        }
    };
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) org.exist.xquery(org.exist.xquery) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) AtomicValue(org.exist.xquery.value.AtomicValue) Item(org.exist.xquery.value.Item) StringValue(org.exist.xquery.value.StringValue) TypedValue(org.exquery.xquery.TypedValue)

Example 2 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType 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 Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType 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 4 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType 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)

Example 5 with Base64BinaryValueType

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

the class EncodeExiFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        EXISerializer exiSerializer;
        if (args.length > 1) {
            if (!args[1].isEmpty()) {
                Item xsdItem = args[1].itemAt(0);
                try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
                    exiSerializer = new EXISerializer(baos, xsdInputStream);
                }
            } else {
                exiSerializer = new EXISerializer(baos);
            }
        } else {
            exiSerializer = new EXISerializer(baos);
        }
        Item inputNode = args[0].itemAt(0);
        exiSerializer.startDocument();
        inputNode.toSAX(context.getBroker(), exiSerializer, new Properties());
        exiSerializer.endDocument();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (IOException ioex) {
        // TODO - test!
        throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
    } catch (EXIException | SAXException exie) {
        throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
    }
}
Also used : Item(org.exist.xquery.value.Item) XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) EXIException(com.siemens.ct.exi.exceptions.EXIException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Properties(java.util.Properties) EXISerializer(org.exist.util.serializer.EXISerializer) SAXException(org.xml.sax.SAXException) JavaErrorCode(org.exist.xquery.ErrorCodes.JavaErrorCode)

Aggregations

Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)14 XPathException (org.exist.xquery.XPathException)10 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)7 IOException (java.io.IOException)6 BinaryValue (org.exist.xquery.value.BinaryValue)6 StringValue (org.exist.xquery.value.StringValue)5 InputStream (java.io.InputStream)4 BinaryValueFromInputStream (org.exist.xquery.value.BinaryValueFromInputStream)4 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 Item (org.exist.xquery.value.Item)3 Image (java.awt.Image)2 BufferedImage (java.awt.image.BufferedImage)2 Path (java.nio.file.Path)2 Properties (java.util.Properties)2 AtomicValue (org.exist.xquery.value.AtomicValue)2 NodeValue (org.exist.xquery.value.NodeValue)2 ValueSequence (org.exist.xquery.value.ValueSequence)2 RestXqServiceException (org.exquery.restxq.RestXqServiceException)2