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

/**
 * Transforms an XML file using an XSL DOM Document and an array of parameters.
 * The parameter array consists of a sequence of pairs of (String parametername)
 * followed by (Object parametervalue) in an Object[].
 * @param docFile the file containing the XML to transform.
 * @param xslFile the file containing the XSL transformation program.
 * @param params the array of transformation parameters.
 * @return the transformed DOM Document.
 *//* w w w  .j av a  2 s.  c  om*/
public static Document getTransformedDocument(File docFile, Document xslFile, Object[] params)
        throws Exception {
    return getTransformedDocument(new StreamSource(docFile), new DOMSource(xslFile), params);
}

From source file:io.mapzone.controller.catalog.http.CatalogRequest.java

<T> T parseBody() throws Exception {
    Unmarshaller unmarshaller = CswResponse.jaxbContext.get().createUnmarshaller();
    InputStream in = httpRequest.getInputStream();
    System.out.println("REQUEST: ");
    TeeInputStream tee = new TeeInputStream(in, System.out);
    parsedBody = ((JAXBElement) unmarshaller.unmarshal(new StreamSource(tee))).getValue();
    return (T) parsedBody;
}

From source file:ru.retbansk.utils.UsefulMethods.java

/**
 * For testing usage. Take unformatted xml string. Return pretty readable xml string
 * //w  ww . ja va 2 s. co m
 * @param input take unformatted xml string
 * @param indent a number of white spaces before each line
 * @return pretty readable xml string
 */

public static String prettyFormat(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        // This statement works with JDK 6
        transformerFactory.setAttribute("indent-number", indent);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        // You'll come here if you are using JDK 1.5
        // you are getting an the following exeption
        // java.lang.IllegalArgumentException: Not supported: indent-number
        // Use this code (Set the output property in transformer.
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}

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

/**
 * Method that creates a {@link Components} instance from the provided
 * component spec file, validating it against component spec XML Schema
 * //from w  w w.  j  a  v a  2s  . com
 * @param componentSpecFilePath
 *            path to the component spec file
 */
public static Components createComponents(String componentSpecFilePath) {
    Components component = null;
    File schemaFile = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Components.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // copy XML Schema from resources to a temporary location
        schemaFile = File.createTempFile("schema", null);
        FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES),
                schemaFile);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

        // validate provided toolspec against XML Schema
        unmarshaller.setSchema(schema);

        // unmarshal it
        final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath));
        try {
            component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue();
        } finally {
            stream.close();
        }
    } catch (JAXBException e) {
        log.error("The component spec provided doesn't validate against its schema!", e);
    } catch (SAXException e) {
        log.error("The XML Schema is not valid!", e);
    } catch (IOException e) {
        log.error("An error occured while copying the XML Schema from the resources to a temporary location!",
                e);
    } catch (Exception e) {
        log.error("An error occured!", e);
    } finally {
        if (schemaFile != null) {
            schemaFile.deleteOnExit();
        }
    }
    return component;
}

From source file:com.predic8.membrane.core.interceptor.rest.SOAPRESTHelper.java

protected StreamSource getRequestXMLSource(Exchange exc) throws Exception {
    Request req = new Request(exc.getRequest());

    String res = req.toXml();/*from  w  ww.  j  a va 2s  . c  o m*/
    log.debug("http-xml: " + res);

    return new StreamSource(new StringReader(res));
}

From source file:es.caib.sgtsic.xml.XmlManager.java

private T unmarshal(InputStream is) throws JAXBException {

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(new StreamSource(is), clazz);

}

From source file:com.edmunds.etm.common.xml.XmlValidator.java

/**
 * Validate configuration against XSD schema.
 *
 * @param configuration XML configuration as a byte array
 * @param schemaFile    XSD schema file name.
 * @throws XmlValidationException when validation fails
 *///  www. j ava  2  s .co m
public void validate(byte[] configuration, String schemaFile) throws XmlValidationException {
    Source source = new StreamSource(new ByteArrayInputStream(configuration));
    validate(source, schemaFile);
}

From source file:org.testingsoftware.JMeterSoapPlugin.sampler.JMeterSoapSampler.java

private String sendSoapMsg(String uri, String msg) {
    // assembles the soap message and sends it to webservice

    StreamSource source = new StreamSource(new StringReader("<n/>"));
    StringResult result = new StringResult();

    SoapMessageInterceptor messageInterceptor = new SoapMessageInterceptor(msg);
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    if (getUser().length() > 0) {
        // add the security interceptor
        Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
        securityInterceptor.setSecurementActions("UsernameToken");
        securityInterceptor.setSecurementUsername(getUser());
        securityInterceptor.setSecurementPassword(getPassword());
        securityInterceptor.setSecurementPasswordType(WSConstants.PW_DIGEST);
        try {/*  w w  w.  ja  va2 s  . com*/
            securityInterceptor.afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        webServiceTemplate.setInterceptors(new ClientInterceptor[] { messageInterceptor, securityInterceptor });
    } else {
        webServiceTemplate.setInterceptors(new ClientInterceptor[] { messageInterceptor });
    }
    webServiceTemplate.afterPropertiesSet();

    webServiceTemplate.sendSourceAndReceiveToResult(uri, source, result);
    return result.toString();
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.XMLSchemaValidator.java

@Override
protected List<Validator> createValidators() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(Constants.XSD_NS);
    sf.setResourceResolver(resourceResolver.toLSResourceResolver());
    List<Validator> validators = new ArrayList<Validator>();
    log.debug("Creating validator for schema: " + location);
    StreamSource ss = new StreamSource(resourceResolver.resolve(location));
    ss.setSystemId(location);// w w  w  .j  ava 2s  .c om
    Validator validator = sf.newSchema(ss).newValidator();
    validator.setResourceResolver(resourceResolver.toLSResourceResolver());
    validator.setErrorHandler(new SchemaValidatorErrorHandler());
    validators.add(validator);
    return validators;
}

From source file:ch.sourcepond.maven.plugin.jenkins.process.xslt.StreamFactoryImpl.java

@Override
public Source newSource(final File pFile) {
    return new StreamSource(pFile);
}