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:Main.java

/**
 * Converts an XML String to a Document.
 *
 * @param string//from  ww  w  . ja va 2 s .  c om
 * @return
 * @throws TransformerException
 * @throws ParserConfigurationException
 */
public static Document toDocument(String string) throws TransformerException, ParserConfigurationException {

    // if there is a byte order mark, strip it off.
    // otherwise, we get a org.xml.sax.SAXParseException: Content is not allowed in prolog
    if (string.startsWith("\uFEFF")) {
        string = string.substring(1);
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // StringReader source
    Source source = new StreamSource(new StringReader(string));

    // Document result
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    Result result = new DOMResult(document);

    transformer.transform(source, result);
    return document;
}

From source file:com.mingo.parser.xml.dom.DocumentBuilderFactoryCreator.java

/**
 * Creates schema source./*from w ww.  ja  v a 2s.c  o  m*/
 *
 * @param xsdSchemaPath path to schema location
 * @return {@link Source}
 */
private static Source createSchemaSource(String xsdSchemaPath) {
    return new StreamSource(ParserFactory.class.getResourceAsStream(xsdSchemaPath));
}

From source file:com.aurel.track.report.export.bl.XsltTransformer.java

public Document transform(Document dom, String xsltPath) {
    Document domResult = null;/*from   ww w.j  a  v a  2  s.c o  m*/
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        File stylesheet = new File(xsltPath);
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);
        DOMSource source = new DOMSource(dom);
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        StreamResult sr = new StreamResult(result);
        transformer.transform(source, sr);
        LOGGER.debug("--------------------");
        LOGGER.debug(result.toString());
        LOGGER.debug("--------------------");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            domResult = builder.parse(new InputSource(new ByteArrayInputStream(result.toByteArray())));
        } catch (Exception e) {
            LOGGER.error(ExceptionUtils.getStackTrace(e));
        }
    } catch (TransformerConfigurationException tce) {
        LOGGER.error("Can't transform dom using xslt path :" + xsltPath, tce);
    } catch (TransformerException te) {
        LOGGER.error("Can't transform dom using xslt path :" + xsltPath, te);
    }
    return domResult;
}

From source file:gov.medicaid.verification.BaseSOAPClient.java

/**
 * Transforms the given request.//from w ww  .  ja  v  a2s. co m
 * @param xsltFile the transformation file to use
 * @param request the request to be transformed
 * @return the transformation result
 * @throws TransformerException for any transformation errors
 */
protected String transform(String xsltFile, String request) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory
            .newTransformer(new StreamSource(getClass().getResourceAsStream(xsltFile)));
    StringWriter writer = new StringWriter();
    transformer.transform(new StreamSource(new StringReader(request)), new StreamResult(writer));
    return writer.toString();
}

From source file:com.premiumminds.billy.gin.services.impl.pdf.FOPPDFTransformer.java

private Source mapParamsToSource(ParamsTree<String, String> documentParams) {
    return new StreamSource(new StringReader(this.generateXML(documentParams)));
}

From source file:com.jaeksoft.searchlib.util.XPathParser.java

public XPathParser(File file) throws ParserConfigurationException, SAXException, IOException {
    this(file, DomUtils.readXml(new StreamSource(file.getAbsoluteFile()), true));
}

From source file:org.jellycastle.maven.Maven.java

/**
 * Loads project from normal POM location in working directory.
 * @return//w w  w. j a  v a2  s . c om
 */
public Project fromPom() {
    try {
        return (Project) marshaller.unmarshal(new StreamSource(getPomFile().getInputStream()));
    } catch (IOException e) {
        throw new RuntimeException("Failed to read Maven pom.xml", e);
    }
}

From source file:au.id.hazelwood.xmltvguidebuilder.config.ConfigFactory.java

public Config create(File file) throws InvalidConfigException {
    try {/*from  w  ww  .j  a va2 s.co  m*/
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        LOGGER.debug("Validating config file {}", file);
        schema.newValidator().validate(new StreamSource(file));
        LOGGER.debug("Loading config file {}", file);
        HierarchicalConfiguration configuration = new XMLConfiguration(file);
        int offset = configuration.getInt("listing.offset");
        int days = configuration.getInt("listing.days");
        int synopsis = configuration.getInt("listing.synopsis");
        String regionId = configuration.getString("listing.regionId");
        Map<Integer, ChannelConfig> channelConfigById = loadChannelConfigs(configuration);
        Config config = new Config(offset, days, synopsis, regionId,
                new ArrayList<>(channelConfigById.values()));
        LOGGER.debug("Loaded {} from {} in {}", config, file, formatDurationWords(stopWatch.getTime()));
        return config;
    } catch (Throwable e) {
        throw new InvalidConfigException(e.getMessage(), e);
    }
}

From source file:edu.wisc.hrs.dao.roles.SoapHrsRolesDaoTest.java

@Test
@Override/* ww  w  .jav  a 2 s.c  o m*/
public void testGetRoles() throws Exception {
    final WebServiceMessage webServiceMessage = setupWebServiceMessageSender();

    when(webServiceMessage.getPayloadSource())
            .thenReturn(new StreamSource(this.getClass().getResourceAsStream("/hrs/roles.xml")));

    super.testGetRoles();
}

From source file:org.web4thejob.orm.TypeSerailizationTest.java

@Test
public void marshallingQueryTest() throws XmlMappingException, IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);//w  w  w .  j a v a2 s . c om

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    final Query query1 = entityFactory.buildQuery(Master1.class);
    query1.setName("123");
    query1.setOwner(ContextUtil.getBean(SecurityService.class).getAdministratorIdentity());
    ContextUtil.getDWS().save(query1);
    marshaller.marshal(query1, result);

    final Unmarshaller unmarshaller = ContextUtil.getBean(Unmarshaller.class);
    final Query query2 = (Query) unmarshaller
            .unmarshal(new StreamSource(new ByteArrayInputStream(out.toByteArray())));

    Assert.assertEquals(query1, query2);
}