Search in sources :

Example 1 with JavaErrorCode

use of org.exist.xquery.ErrorCodes.JavaErrorCode 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)

Example 2 with JavaErrorCode

use of org.exist.xquery.ErrorCodes.JavaErrorCode 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

EXIException (com.siemens.ct.exi.exceptions.EXIException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 JavaErrorCode (org.exist.xquery.ErrorCodes.JavaErrorCode)2 XPathException (org.exist.xquery.XPathException)2 Item (org.exist.xquery.value.Item)2 SAXException (org.xml.sax.SAXException)2 EXIFactory (com.siemens.ct.exi.EXIFactory)1 GrammarFactory (com.siemens.ct.exi.GrammarFactory)1 SAXDecoder (com.siemens.ct.exi.api.sax.SAXDecoder)1 Grammars (com.siemens.ct.exi.grammars.Grammars)1 DefaultEXIFactory (com.siemens.ct.exi.helpers.DefaultEXIFactory)1 Properties (java.util.Properties)1 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 AppendingSAXAdapter (org.exist.dom.memtree.AppendingSAXAdapter)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 SAXAdapter (org.exist.dom.memtree.SAXAdapter)1 EXISerializer (org.exist.util.serializer.EXISerializer)1 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)1 BinaryValue (org.exist.xquery.value.BinaryValue)1