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:se.inera.intyg.intygstjanst.web.integration.stub.RevokeMedicalCertificateResponderStubTest.java

@Test
public void testName() throws Exception {
    // read request from file
    JAXBContext jaxbContext = JAXBContext.newInstance(RevokeMedicalCertificateRequestType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    RevokeMedicalCertificateRequestType request = unmarshaller.unmarshal(new StreamSource(
            new ClassPathResource("revoke-medical-certificate/revoke-medical-certificate-request.xml")
                    .getInputStream()),/*  w ww . java 2s .  c o  m*/
            RevokeMedicalCertificateRequestType.class).getValue();

    stub.revokeMedicalCertificate(null, request);

    verify(store).makulera(UTLATANDE_ID, REVOKE_MESSAGE);
}

From source file:Main.java

/**
 *
 * @param source/*from  www .ja va2 s . c  om*/
 * @param xsltSource
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static synchronized Document transform(Element source, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException,
        ParserConfigurationException, SAXException, IOException {
    Document obj = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));
        obj = dbf.newDocumentBuilder().newDocument();
        Result result = new DOMResult(obj);
        transformer.transform(new DOMSource(source), result);
    }
    return obj;
}

From source file:arxiv.xml.XMLParser.java

/**
 * Constructs a new XML parser by initializing the JAXB unmarshaller and setting up the XML validation.
 *
 * @throws HarvesterError if there are any problems
 *///from  ww w.ja v a 2s.  c  om
public XMLParser() {
    try {
        unmarshaller = JAXBContext.newInstance("org.openarchives.oai._2:org.arxiv.oai.arxivraw")
                .createUnmarshaller();
    } catch (JAXBException e) {
        throw new HarvesterError("Error creating JAXB unmarshaller", e);
    }

    ClassLoader classLoader = this.getClass().getClassLoader();
    List<Source> schemaSources = Lists.newArrayList();

    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("OAI-PMH.xsd")));
    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("arXivRaw.xsd")));

    try {
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
        unmarshaller.setSchema(schema);
    } catch (SAXException e) {
        throw new HarvesterError("Error creating validation schema", e);
    }
}

From source file:eu.scape_project.tool.toolwrapper.data.tool_spec.utils.Utils.java

/**
 * Unmarshals an input stream of xml data to a Tool.
 *//*from w ww.  j a v a2s.com*/
public static Tool fromInputStream(InputStream input) throws JAXBException, IOException, SAXException {
    Unmarshaller unmarshaller = Utils.createUnmarshaller();
    JAXBElement<Tool> unmarshalled = unmarshaller.unmarshal(new StreamSource(input), Tool.class);
    return unmarshalled.getValue();
}

From source file:Main.java

public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException {
    try {// www.  ja v a  2s  .co m
        Source source = new StreamSource(xsd);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);

        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(root));
    } finally {
        closeStream(xsd);
    }
}

From source file:com.valco.utility.FacturasUtility.java

public static OutputStream getCadenaOriginal(String cadenaOriginalDir, String xml) throws Exception {
    StreamSource sourceXSL = new StreamSource(new File(cadenaOriginalDir));
    StringReader reader = new StringReader(xml);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = tFactory.newTransformer(sourceXSL);
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    OutputStream output = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(reader), new StreamResult(output));

    return output;
}

From source file:org.apache.servicemix.http.HttpAddressingTest.java

public void testOk() throws Exception {
    DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
    InOut me = client.createInOutExchange();
    me.setService(new QName("http://test", "MyProviderService"));
    InputStream fis = getClass().getResourceAsStream("addressing-request.xml");
    me.getInMessage().setContent(new StreamSource(fis));
    client.sendSync(me);/*from w w  w .  j a  va  2  s  .  com*/
    if (me.getStatus() == ExchangeStatus.ERROR) {
        if (me.getError() != null) {
            throw me.getError();
        } else {
            fail("Received ERROR status");
        }
    } else if (me.getFault() != null) {
        String txt = new SourceTransformer().toString(me.getFault().getContent());
        client.done(me);
        fail("Received fault: " + txt);
    } else {
        Node node = new SourceTransformer().toDOMNode(me.getOutMessage());
        client.done(me);
        log.info(new SourceTransformer().toString(node));
        assertEquals("myid", textValueOfXPath(node, "//*[local-name()='RelatesTo']"));
        assertNotNull(textValueOfXPath(node, "//*[local-name()='MessageID']"));
    }
}

From source file:it.inserpio.mapillary.gopro.importer.parser.bikematepro.parser.impl.BikeMateProGPXParser.java

@Override
public List<GPXDateTimePoint> parse(File GPXFile)
        throws GPXParsingException, XmlMappingException, IOException, ParseException {
    CastorMarshaller castorMarshaller = new CastorMarshaller();

    Resource mappingLocation = new ClassPathResource("bike-mate-pro-mapping.xml");

    castorMarshaller.setMappingLocation(mappingLocation);
    castorMarshaller.afterPropertiesSet();

    Source GPXSource = new StreamSource(new FileInputStream(GPXFile));

    BikeMateProGPX bikeMateProGPX = (BikeMateProGPX) castorMarshaller.unmarshal(GPXSource);

    return BikeMateProGPXTransformer.translate(bikeMateProGPX);
}

From source file:com.elsevier.spark_xml_utils.xslt.S3URIResolver.java

/**
 * Return the requested stylesheet. If the stylesheet hasn't been cached,
 * then save the stylesheet to the cache. The assumption (although not
 * required) is that the imported/included stylesheets will be stored in an
 * S3 bucket and accessible using an S3 url.
 * /*from  www .  j  a  va2s. co m*/
 * @param href url for the stylesheet
 * @param base not used (assuming absolute urls)
 */
public Source resolve(String href, String base) throws TransformerException {

    try {

        // Check local cache
        if (stylesheetMap.containsKey(href)) {

            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));

        } else {

            // Read the data from the URL and populate the cache
            URL theUrl = new URL(href);
            stylesheetMap.put(href, IOUtils.toString(theUrl.openStream()));

            // Return a StreamSource
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));

        }

    } catch (IOException e) {

        log.error("Problems resolving a stylesheet. URI:" + href + " " + e.getMessage(), e);
        throw new TransformerException(e.getMessage());

    }

}

From source file:com.elsevier.xml.S3URIResolver.java

/**
 * Return the requested stylesheet. If the stylesheet hasn't been cached,
 * then save the stylesheet to the cache. The assumption (although not
 * required) is that the imported/included stylesheets will be stored in an
 * S3 bucket and accessible using an S3 url.
 * // w w w . ja v  a2  s  . co  m
 * @param href url for the stylesheet
 * @param base not used (assuming absolute urls)
 */
public Source resolve(String href, String base) throws TransformerException {

    try {
        // Check local cache
        if (stylesheetMap.containsKey(href)) {
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));
        } else {

            // Read the data from the URL and populate the cache
            URL theUrl = new URL(href);
            synchronized (this) {
                stylesheetMap.put(href, IOUtils.toString(theUrl.openStream()));
            }
            // Return a StreamSource
            return new StreamSource(IOUtils.toInputStream(stylesheetMap.get(href)));
        }

    } catch (IOException e) {

        log.error("Problems resolving a stylesheet. URI:" + href + " " + e.getMessage(), e);
        throw new TransformerException(e.getMessage());

    }
}