Example usage for javax.xml.transform.stream StreamSource getReader

List of usage examples for javax.xml.transform.stream StreamSource getReader

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamSource getReader.

Prototype

public Reader getReader() 

Source Link

Document

Get the character stream that was set with setReader.

Usage

From source file:org.dhatim.delivery.AbstractParser.java

protected InputSource createInputSource(Source source, String contentEncoding) {
    // Also attach the underlying stream to the InputSource...
    if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        InputStream inputStream;//from   w  ww .  j a  va2  s  . c o m
        Reader reader;

        inputStream = getInputStream(streamSource);
        reader = streamSource.getReader();
        if (reader == null) {
            if (inputStream == null) {
                throw new SmooksException(
                        "Invalid StreamSource.  Unable to extract an InputStream (even by systemId) or Reader instance.");
            }
            reader = streamToReader(inputStream, contentEncoding);
        }

        InputSource inputSource = new InputSource();
        inputSource.setByteStream(inputStream);
        inputSource.setCharacterStream(reader);

        return inputSource;
    } else {
        return new InputSource(getReader(source, contentEncoding));
    }
}

From source file:org.dhatim.delivery.Filter.java

protected Reader getReader(Source source, ExecutionContext executionContext) {
    if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        if (streamSource.getReader() != null) {
            return streamSource.getReader();
        } else if (streamSource.getInputStream() != null) {
            try {
                if (executionContext instanceof ExecutionContext) {
                    return new InputStreamReader(streamSource.getInputStream(),
                            executionContext.getContentEncoding());
                } else {
                    return new InputStreamReader(streamSource.getInputStream(), "UTF-8");
                }//from   ww w.j  a v  a 2 s  .co m
            } catch (UnsupportedEncodingException e) {
                throw new SmooksException("Unable to decode input stream.", e);
            }
        } else {
            throw new SmooksException(
                    "Invalid " + StreamSource.class.getName() + ".  No InputStream or Reader instance.");
        }
    }

    return new NullReader();
}

From source file:org.dhatim.delivery.Filter.java

protected void close(Source source) {
    if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        try {/*from  w  w w.  java 2  s.co m*/
            if (streamSource.getReader() != null) {
                streamSource.getReader().close();
            } else if (streamSource.getInputStream() != null) {
                InputStream inputStream = streamSource.getInputStream();
                if (inputStream != System.in) {
                    inputStream.close();
                }
            }
        } catch (Throwable throwable) {
            logger.debug("Failed to close input stream/reader.", throwable);
        }
    }
}

From source file:org.dita.dost.util.XMLUtils.java

/** Close source. */
public static void close(final Source input) throws IOException {
    if (input != null && input instanceof StreamSource) {
        final StreamSource s = (StreamSource) input;
        final InputStream i = s.getInputStream();
        if (i != null) {
            i.close();/*from   w  w  w .  j  a  va 2 s  . co  m*/
        } else {
            final Reader w = s.getReader();
            if (w != null) {
                w.close();
            }
        }
    }
}

From source file:org.geoserver.rest.util.IOUtils.java

/**
 * Convert the input from the provided {@link Reader} into a {@link String}.
 * //  w w w.  j  a  v a  2  s .c  om
 * @param inputStream the {@link Reader} to copy from.
 * @return a {@link String} that contains the content of the provided {@link Reader}.
 * @throws IOException in case something bad happens.
 */
public static String getStringFromStreamSource(StreamSource src) throws IOException {

    inputNotNull(src);
    InputStream inputStream = src.getInputStream();
    if (inputStream != null) {
        return getStringFromStream(inputStream);
    } else {

        final Reader r = src.getReader();
        return getStringFromReader(r);
    }
}

From source file:org.jboss.bpm.console.server.util.DOMUtils.java

public static Element sourceToElement(Source source) throws IOException {
    Element retElement = null;/*from   ww w . ja  v  a 2 s  . co  m*/

    try {
        if (source instanceof StreamSource) {
            StreamSource streamSource = (StreamSource) source;

            InputStream ins = streamSource.getInputStream();
            if (ins != null) {
                retElement = DOMUtils.parse(ins);
            } else {
                Reader reader = streamSource.getReader();
                retElement = DOMUtils.parse(new InputSource(reader));
            }
        } else if (source instanceof DOMSource) {
            DOMSource domSource = (DOMSource) source;
            Node node = domSource.getNode();
            if (node instanceof Element) {
                retElement = (Element) node;
            } else if (node instanceof Document) {
                retElement = ((Document) node).getDocumentElement();
            } else {
                throw new RuntimeException("Unsupported Node type: " + node.getClass().getName());
            }
        } else if (source instanceof SAXSource) {
            // The fact that JAXBSource derives from SAXSource is an implementation detail.
            // Thus in general applications are strongly discouraged from accessing methods defined on SAXSource.
            // The XMLReader object obtained by the getXMLReader method shall be used only for parsing the InputSource object returned by the getInputSource method.

            TransformerFactory tf = TransformerFactory.newInstance();
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.transform(source, new StreamResult(baos));
            retElement = DOMUtils.parse(new ByteArrayInputStream(baos.toByteArray()));
        } else {
            throw new RuntimeException("Source type not implemented: " + source.getClass().getName());
        }

    } catch (TransformerException ex) {
        IOException ioex = new IOException();
        ioex.initCause(ex);
        throw ioex;
    }

    return retElement;
}

From source file:ORG.oclc.os.SRW.xml2jsonTransformer.java

@Override
public void transform(Source xmlSource, Result outputTarget) throws TransformerException {
    StreamSource source = (StreamSource) xmlSource;
    BufferedReader br = new BufferedReader(source.getReader());
    StringBuilder sb = new StringBuilder();
    String line;/*w w  w.j  ava2 s.  c  om*/
    try {
        while ((line = br.readLine()) != null)
            sb.append(line);
    } catch (IOException ex) {
        throw new TransformerException(ex);
    }
    String xmlRecord = sb.toString();
    if (log.isDebugEnabled())
        log.debug("xmlSource: " + xmlRecord);
    XMLSerializer xmlSerializer = new XMLSerializer();
    try {
        JSON json = xmlSerializer.read(xmlRecord);
        StreamResult sr = (StreamResult) outputTarget;
        json.write(sr.getWriter());
        if (log.isDebugEnabled())
            log.debug("convertedJSON: " + sr.toString());
    } catch (Exception e) {
        log.error("Unable to transform to JSON: " + xmlRecord);
        throw new TransformerException(e);
    }
}

From source file:org.openehealth.ipf.platform.camel.core.adapter.ConverterRouteTest.java

@Test
public void testConverter4() throws Exception {
    StreamSource result = (StreamSource) producerTemplate.sendBody("direct:converter-test",
            ExchangePattern.InOut, new StreamSource(new StringReader("input")));
    assertEquals("source: input", IOUtils.toString(result.getReader()));
}

From source file:org.openehealth.ipf.platform.camel.core.support.transform.min.TestConverter.java

private static String toString(Source source) throws IOException {
    StreamSource s = (StreamSource) source;
    return IOUtils.toString(s.getReader());
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@SuppressWarnings("deprecation") // on JDK 9
private Source processSource(Source source) {
    if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
        return source;
    }/*from   w  ww. j a  v a 2  s . c  o  m*/

    XMLReader xmlReader = null;
    InputSource inputSource = null;

    if (source instanceof SAXSource) {
        SAXSource saxSource = (SAXSource) source;
        xmlReader = saxSource.getXMLReader();
        inputSource = saxSource.getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        if (streamSource.getInputStream() != null) {
            inputSource = new InputSource(streamSource.getInputStream());
        } else if (streamSource.getReader() != null) {
            inputSource = new InputSource(streamSource.getReader());
        } else {
            inputSource = new InputSource(streamSource.getSystemId());
        }
    }

    try {
        if (xmlReader == null) {
            xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
        }
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        String name = "http://xml.org/sax/features/external-general-entities";
        xmlReader.setFeature(name, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        return new SAXSource(xmlReader, inputSource);
    } catch (SAXException ex) {
        logger.warn("Processing of external entities could not be disabled", ex);
        return source;
    }
}