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

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

Introduction

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

Prototype

public void setSystemId(File f) 

Source Link

Document

Set the system ID from a File reference.

Usage

From source file:com.meltmedia.rodimus.RodimusCli.java

public static StreamSource createStreamSource(URL url) throws IOException {
    StreamSource source = new StreamSource();
    source.setSystemId(url.toExternalForm());
    source.setInputStream(url.openStream());
    return source;
}

From source file:Examples.java

/**
 * Show simple transformation from input stream to output stream.
 *///from  w w  w  .j a  va2 s .  co  m
public static void exampleFromStream(String sourceID, String xslID)
        throws TransformerException, TransformerConfigurationException, FileNotFoundException {
    // Create a transform factory instance.
    TransformerFactory tfactory = TransformerFactory.newInstance();

    InputStream xslIS = new BufferedInputStream(new FileInputStream(xslID));
    StreamSource xslSource = new StreamSource(xslIS);
    // Note that if we don't do this, relative URLs can not be resolved correctly!
    xslSource.setSystemId(xslID);

    // Create a transformer for the stylesheet.
    Transformer transformer = tfactory.newTransformer(xslSource);

    InputStream xmlIS = new BufferedInputStream(new FileInputStream(sourceID));
    StreamSource xmlSource = new StreamSource(xmlIS);
    // Note that if we don't do this, relative URLs can not be resolved correctly!
    xmlSource.setSystemId(sourceID);

    // Transform the source XML to System.out.
    transformer.transform(xmlSource, new StreamResult(new OutputStreamWriter(System.out)));
}

From source file:Examples.java

/**
 * Show simple transformation from reader to output stream.  In general 
 * this use case is discouraged, since the XML encoding can not be 
 * processed./*from w  w w .j  a v a  2 s . c o m*/
 */
public static void exampleFromReader(String sourceID, String xslID) throws TransformerException,
        TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException {
    // Create a transform factory instance.
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // Note that in this case the XML encoding can not be processed!
    Reader xslReader = new BufferedReader(new InputStreamReader(new FileInputStream(xslID), "UTF-8"));
    StreamSource xslSource = new StreamSource(xslReader);
    // Note that if we don't do this, relative URLs can not be resolved correctly!
    xslSource.setSystemId(xslID);

    // Create a transformer for the stylesheet.
    Transformer transformer = tfactory.newTransformer(xslSource);

    // Note that in this case the XML encoding can not be processed!
    Reader xmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceID), "UTF-8"));
    StreamSource xmlSource = new StreamSource(xmlReader);
    // Note that if we don't do this, relative URLs can not be resolved correctly!
    xmlSource.setSystemId(sourceID);

    // Transform the source XML to System.out.
    transformer.transform(xmlSource, new StreamResult(new OutputStreamWriter(System.out)));
}

From source file:com.wantez.eregparser.Parser.java

public Parser(final String xsl) throws TransformerConfigurationException {
    final TransformerFactory tFactory = TransformerFactory.newInstance();

    final ClassLoader classLoader = this.getClass().getClassLoader();
    final InputStream stream = classLoader.getResourceAsStream(xsl);
    final StreamSource streamSource = new StreamSource(stream);
    streamSource.setSystemId(classLoader.getResource(xsl).getPath());
    this.transformer = tFactory.newTransformer(streamSource);

}

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);
    Validator validator = sf.newSchema(ss).newValidator();
    validator.setResourceResolver(resourceResolver.toLSResourceResolver());
    validator.setErrorHandler(new SchemaValidatorErrorHandler());
    validators.add(validator);/* w  w  w  . j a v a2 s  .  com*/
    return validators;
}

From source file:com.predic8.membrane.core.interceptor.xslt.XSLTTransformer.java

private void createOneTransformer(ResolverMap rr, String baseLocation)
        throws TransformerConfigurationException, InterruptedException, ResourceRetrievalException {
    Transformer t;/*from   ww w.  java  2s  .  c  o  m*/
    if (isNullOrEmpty(styleSheet))
        t = fac.newTransformer();
    else {
        StreamSource source = new StreamSource(rr.resolve(ResolverMap.combine(baseLocation, styleSheet)));
        source.setSystemId(styleSheet);
        t = fac.newTransformer(source);
    }
    transformers.put(t);
}

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

protected List<Validator> createValidators() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(Constants.XSD_NS);
    List<Validator> validators = new ArrayList<Validator>();
    for (Schema schema : getSchemas()) {
        log.debug("Creating validator for schema: " + schema);
        StreamSource ss = new StreamSource(new StringReader(schema.getAsString()));
        ss.setSystemId(location);
        sf.setResourceResolver(resourceResolver.toLSResourceResolver());
        Validator validator = sf.newSchema(ss).newValidator();
        validator.setResourceResolver(resourceResolver.toLSResourceResolver());
        validator.setErrorHandler(new SchemaValidatorErrorHandler());
        validators.add(validator);/*ww  w.ja va 2  s. co  m*/
    }
    return validators;
}

From source file:TransformThread.java

/** Constructs the TransformThread object
 * @param thrdNum a unique identifier for this object
 *//*from   w  w w  .  j  av  a 2  s  .com*/
public TransformThread(int thrdNum) {
    m_thrdNum = thrdNum;

    m_inStream = new InputStream[NUM_TRANSFORMATIONS];
    m_inSource = new Source[NUM_TRANSFORMATIONS];
    m_outResult = new Result[NUM_TRANSFORMATIONS];

    try {
        initSource();
        initResult();

        // ensure xslSourceURI is a valid URI
        final String xslSourceFileName = XSL_IN_BASE + m_thrdNum + XSL_EXT;
        final String xslSourceURI = (new File(xslSourceFileName)).toURL().toString();
        StreamSource xslSource = new StreamSource(xslSourceFileName);
        xslSource.setSystemId(xslSourceURI);

        // Initialize the tranformer
        m_transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        m_thread = new Thread(this);
    } catch (Throwable e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:eionet.gdem.conversion.converters.ConvertStrategy.java

/**
 * Method transforms XML source to PDF using XSL-FO stream.
 * @param in InputStream containing source XML.
 * @param xsl InputStream containing XSL-FO content.
 * @param out OutputStream for conversion result.
 * @throws GDEMException In case of unexpected XML or XSL errors.
 *///from  w  ww . j  ava 2s  .co  m
protected void runFOPTransformation(InputStream in, InputStream xsl, OutputStream out) throws GDEMException {
    try {
        Driver driver = new Driver();
        driver.setRenderer(Driver.RENDER_PDF);
        driver.setOutputStream(out);
        Result res = new SAXResult(driver.getContentHandler());
        Source src = new StreamSource(in);
        TransformerFactory transformerFactory = transform.getTransformerFactoryInstance();
        TransformerErrorListener errors = new TransformerErrorListener();

        transformerFactory.setErrorListener(errors);
        StreamSource transformerSource = new StreamSource(xsl);
        if (getXslPath() != null) {
            transformerSource.setSystemId(getXslPath());
        }

        Transformer transformer = transformerFactory.newTransformer(transformerSource);
        setTransformerParameters(transformer);
        transformer.setErrorListener(errors);

        long l = System.currentTimeMillis();
        transformer.transform(src, res);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug((new StringBuilder()).append("generate: transformation needed ")
                    .append(System.currentTimeMillis() - l).append(" ms").toString());
        }

    } catch (TransformerConfigurationException tce) {
        throw new GDEMException("Error transforming XML to PDF - incorrect stylesheet file: " + tce.toString(),
                tce);
    } catch (TransformerException tfe) {
        throw new GDEMException(
                "Error transforming XML to PDF - it's not probably well-formed xml file: " + tfe.toString(),
                tfe);
    } catch (Throwable e) {
        LOGGER.error("Error " + e.toString(), e);
        throw new GDEMException("Error transforming XML to PDF " + e.toString());
    }
}

From source file:eionet.gdem.conversion.converters.ConvertStrategy.java

/**
 * Method transforms XML source using XSL stream.
 * @param in InputStream containing source XML.
 * @param xslStream InputStream containing XSL content.
 * @param out OutputStream for conversion result.
 * @throws GDEMException In case of unexpected XML or XSL errors.
 *///from  w w w .ja  va2s. c  om
protected void runXslTransformation(InputStream in, InputStream xslStream, OutputStream out)
        throws GDEMException {
    try {
        TransformerFactory tFactory = transform.getTransformerFactoryInstance();
        TransformerErrorListener errors = new TransformerErrorListener();
        tFactory.setErrorListener(errors);

        StreamSource transformerSource = new StreamSource(xslStream);
        if (getXslPath() != null) {
            transformerSource.setSystemId(getXslPath());
        }

        Transformer transformer = tFactory.newTransformer(transformerSource);
        transformer.setErrorListener(errors);

        transformer.setParameter(DD_DOMAIN_PARAM, Properties.ddURL);
        setTransformerParameters(transformer);
        long l = System.currentTimeMillis();
        transformer.transform(new StreamSource(in), new StreamResult(out));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug((new StringBuilder()).append("generate: transformation needed ")
                    .append(System.currentTimeMillis() - l).append(" ms").toString());
        }
    } catch (TransformerConfigurationException tce) {
        throw new GDEMException("Error transforming XML - incorrect stylesheet file: " + tce.toString(), tce);
    } catch (TransformerException tfe) {
        throw new GDEMException(
                "Error transforming XML - it's not probably well-formed xml file: " + tfe.toString(), tfe);
    } catch (Throwable th) {
        LOGGER.error("Error " + th.toString(), th);
        th.printStackTrace(System.out);
        throw new GDEMException("Error transforming XML: " + th.toString());
    }
}