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

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

Introduction

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

Prototype

public StreamSource(File f) 

Source Link

Document

Construct a StreamSource from a File.

Usage

From source file:restTemplate.blabla.App.java

public void process() throws Exception {
    String name = "source.xml";
    File f = new File("src/main/resources/spring/" + name);
    FileInputStream fis = new FileInputStream(f);
    LicensePostResult lp = (LicensePostResult) getUnmarshaller().unmarshal(new StreamSource(fis));
    System.out.println(lp);/*  w w  w .  j  a  va 2s  . c  om*/
}

From source file:Main.java

/**
 * Unmarshal XML data from XML file path using XSD string and return the resulting JAXB content tree
 * /* w  ww  . ja v  a  2 s. c  om*/
 * @param dummyCtxObject
 *            Dummy contect object for creating related JAXB context
 * @param strXMLFilePath
 *            XML file path
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromXMLFile(Object dummyCtxObject, String strXMLFilePath, String strXSD)
        throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context object (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD (null)!");
    }

    Object unmarshalledObject = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        // unmarshaller.setValidating(true);
        /*
        javax.xml.validation.Schema schema =
        javax.xml.validation.SchemaFactory.newInstance(
        javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
        new java.io.File(m_strXSDFilePath));
         */
        StringReader reader = null;
        FileInputStream fis = null;
        try {
            reader = new StringReader(strXSD);
            javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                    .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                    .newSchema(new StreamSource(reader));
            unmarshaller.setSchema(schema);

            fis = new FileInputStream(strXMLFilePath);
            unmarshalledObject = unmarshaller.unmarshal(fis);
        } finally {
            if (fis != null) {
                fis.close();
                fis = null;
            }
            if (reader != null) {
                reader.close();
                reader = null;
            }
        }
        // } catch (JAXBException e) {
        // //m_logger.error(e);
        // throw new OrderException(e);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return unmarshalledObject;
}

From source file:Main.java

/**
 * @param xmlsource//from   w  w w .ja va2 s .co m
 * @param xsltstream
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, InputStream xsltstream) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltstream);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

static public Document createDocumentFromXMLContent(String docContent) throws SAXException,
        ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException {
    // create builder
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

    // create document
    Document doc = docBuilder.parse(new ByteArrayInputStream(docContent.getBytes()));

    // create transformer to remove spaces
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory
            .newTransformer(new StreamSource("src/test/resources/strip-spaces.xls"));

    // load doc/*from  ww  w.  j a v  a2 s  .c om*/
    DOMSource source = new DOMSource(doc);
    OutputStream os = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(os);

    // remove spaces from doc
    transformer.transform(source, result);

    // re-create doc
    return docBuilder.parse(new ByteArrayInputStream(os.toString().getBytes()));
}

From source file:Main.java

/**
 * Deserialize the XML content of a string into a Java object of the specified type.
 *
 * @param docClass  The class of the object to unmarshall the XML as
 * @param xmlString The XML to deserialize, as a string
 * @param <T>       The type of object to unmarshall the XML as
 * @return The deserialized object/*  ww  w.  ja va 2  s.  co m*/
 * @throws JAXBException if an error occurs during deserialization
 */
public static <T> T deSerialize(Class<T> docClass, String xmlString) throws JAXBException {
    return deSerialize(docClass, new StreamSource(new StringReader(xmlString)));
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

public static <T> T readObjectFromInputStream(final InputStream inputStream, final Class<T> expectedType) {
    try {//from   w  w w  . j  a  va  2  s .co m
        JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
        return element.getValue();
    } catch (final JAXBException e) {
        throw new SwidException("Cannot process resource.", e);
    }
}

From source file:Main.java

public static synchronized void deserialize(InputStream source, Result result)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;//  www  . ja  v  a2 s . c  o m
    transformer = factory.newTransformer();
    transformer.transform(new StreamSource(source), result);
}

From source file:Main.java

/**
 * Gets the transformer.//w  ww.  j  ava  2  s.c  o  m
 *
 * @param xsltFile
 *            the xslt file
 * @return the transformer
 * @throws TransformerFactoryConfigurationError
 *             the transformer factory configuration error
 * @throws TransformerConfigurationException
 *             the transformer configuration exception
 */
public static Transformer getTransformer(File xsltFile)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException {
    return getTransformer(new StreamSource(xsltFile));
}

From source file:Main.java

/**
 * Applies an XSLT.//from  w w  w  . java2  s.  c  om
 *
 * @param xml          The inputstream to read the xml from.
 * @param xsltFilename The filename of the xsl file to use.
 * @param params       A map of parameters to pass to the transformation.
 *                     if null is passed none are used.
 *
 * @return The string representing of the result of the transformation.
 */
public static String applyXSLT(InputStream in, String xsltFilename, Map params) {
    StreamSource src = new StreamSource(in);
    return applyXSLT(src, xsltFilename, params);
}

From source file:IOUtils.java

/**
 * Parse a file and send the sax events to the content handler.
 * @param file/*from  w w  w. j  av a 2s  . c om*/
 * @param handler
 * @throws IOException
 * @throws TransformerException
 */
public static final void parse(File file, ContentHandler handler) throws IOException, TransformerException {
    final Transformer transformer = FACTORY.newTransformer();
    transformer.transform(new StreamSource(new FileReader(file)), new SAXResult(handler));
}