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