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;
}
};
}
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());
}
}
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());
}
}
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);
}
}
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());
}
}
Aggregations