Search in sources :

Example 1 with EXISource

use of com.siemens.ct.exi.api.sax.EXISource in project arctic-sea by 52North.

the class XmlToExiConverter method decode.

protected void decode(String fileName) {
    try (InputStream exiIS = FileUtils.openInputStream(getFile(fileName, EXI_EXTENSION));
        OutputStream os = FileUtils.openOutputStream(getFile(fileName, XML_EXTENSION_2))) {
        Reader reader = new InputStreamReader(exiIS, "ISO-8859-1");
        InputSource is = new InputSource(reader);
        is.setEncoding("ISO-8859-1");
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        EXISource exiSource = new EXISource();
        XMLReader exiReader = exiSource.getXMLReader();
        SAXSource saxSource = new SAXSource(is);
        // SAXSource saxSource = new SAXSource(new InputSource(exiIS));
        exiSource.setXMLReader(exiReader);
        transformer.transform(saxSource, new StreamResult(os));
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
}
Also used : InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) InputStreamReader(java.io.InputStreamReader) EXISource(com.siemens.ct.exi.api.sax.EXISource) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) XMLReader(org.xml.sax.XMLReader) XMLReader(org.xml.sax.XMLReader)

Example 2 with EXISource

use of com.siemens.ct.exi.api.sax.EXISource in project arctic-sea by 52North.

the class EXIBinding method decode.

/**
 * Parse the incoming EXI encoded {@link InputStream} transform to
 * {@link XmlObject}
 *
 * @param request
 *            {@link HttpServletRequest} with EXI encoded
 *            {@link InputStream}
 * @return {@link XmlObject} created from the EXI encoded
 *         {@link InputStream}
 * @throws OwsExceptionReport
 *             If an error occurs during parsing
 */
protected XmlObject decode(HttpServletRequest request) throws OwsExceptionReport {
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        EXIFactory ef = this.exiUtils.newEXIFactory();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        if (ef.isFragment()) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        // decode EXI encoded InputStream
        EXISource exiSource = new EXISource(ef);
        XMLReader exiReader = exiSource.getXMLReader();
        InputSource inputSource = new InputSource(request.getInputStream());
        inputSource.setEncoding(request.getCharacterEncoding());
        SAXSource saxSource = new SAXSource(inputSource);
        saxSource.setXMLReader(exiReader);
        transformer.transform(saxSource, new StreamResult(os));
        // create XmlObject from OutputStream
        return XmlHelper.parseXmlString(os.toString(StandardCharsets.UTF_8.name()));
    } catch (IOException | EXIException ex) {
        throw new NoApplicableCodeException().causedBy(ex).withMessage("Error while reading request! Message: %s", ex.getMessage());
    } catch (TransformerException ex) {
        throw new NoApplicableCodeException().causedBy(ex).withMessage("Error while transforming request! Message: %s", ex.getMessage());
    } catch (DecodingException ex) {
        throw new NoApplicableCodeException().causedBy(ex).withMessage("Error while parsing request! Message: %s", ex.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) EXISource(com.siemens.ct.exi.api.sax.EXISource) StreamResult(javax.xml.transform.stream.StreamResult) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) DecodingException(org.n52.svalbard.decode.exception.DecodingException) OwsDecodingException(org.n52.iceland.coding.decode.OwsDecodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) EXIException(com.siemens.ct.exi.exceptions.EXIException) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException) EXIFactory(com.siemens.ct.exi.EXIFactory)

Aggregations

EXISource (com.siemens.ct.exi.api.sax.EXISource)2 Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 SAXSource (javax.xml.transform.sax.SAXSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 InputSource (org.xml.sax.InputSource)2 XMLReader (org.xml.sax.XMLReader)2 EXIFactory (com.siemens.ct.exi.EXIFactory)1 EXIException (com.siemens.ct.exi.exceptions.EXIException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 TransformerException (javax.xml.transform.TransformerException)1 OwsDecodingException (org.n52.iceland.coding.decode.OwsDecodingException)1 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)1 DecodingException (org.n52.svalbard.decode.exception.DecodingException)1