Java Code Examples for javax.xml.transform.stream.StreamSource.setSystemId()

The following are Jave code examples for showing how to use setSystemId() of the javax.xml.transform.stream.StreamSource class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openjdk-jdk10   File: URIResolverTest.java   Source Code and License Vote up 6 votes
/**
 * This is to test the URIResolver.resolve() method when there is an error
 * in the file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public static void docResolver01() throws Exception {
    try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) {
        URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID);
        StreamSource streamSource = new StreamSource(fis);
        streamSource.setSystemId(SYSTEM_ID);

        Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);
        transformer.setURIResolver(resolver);

        File f = new File(XML_DIR + "myFake.xml");
        Document document = DocumentBuilderFactory.newInstance().
                newDocumentBuilder().parse(f);

        // Use a Transformer for output
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.err);
        // No exception is expected because resolver resolve wrong URI.
        transformer.transform(source, result);
    }
}
 
Example 2
Project: joai-project   File: XSLTransformer.java   Source Code and License Vote up 5 votes
/**
 *  Transforms an XML file using a pre-compiled {@link javax.xml.transform.Transformer}. Use {@link
 *  #getTransformer(String xslFilePath)} to produce a reusable {@link javax.xml.transform.Transformer} for a
 *  given XSL stylesheet.
 *
 * @param  inputFile    The XML file to transform.
 * @param  transformer  A pre-compiled {@link javax.xml.transform.Transformer} used to produce transformed
 *      output.
 * @return              A String containing the transformed content.
 */
public final static String transformFile(File inputFile, Transformer transformer) {
	try {
		StringWriter writer = new StringWriter();
		StreamSource ss = new StreamSource(new FileInputStream(inputFile));
		ss.setSystemId(inputFile);
		transformer.transform(ss, new StreamResult(writer));
		return writer.toString();
	} catch (Throwable e) {
		prtlnErr(e);
		return "";
	}
}
 
Example 3
Project: openjdk-jdk10   File: SchemaCache.java   Source Code and License Vote up 5 votes
public ValidatorHandler newValidator() {
    if (schema==null) {
        synchronized (this) {
            if (schema == null) {

                ResourceResolver resourceResolver = null;
                try (InputStream is = clazz.getResourceAsStream(resourceName)) {

                    StreamSource source = new StreamSource(is);
                    source.setSystemId(resourceName);
                    // do not disable secure processing - these are well-known schemas

                    SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                    SchemaFactory schemaFactory = allowExternalAccess(sf, "file", false);

                    if (createResolver) {
                        resourceResolver = new ResourceResolver(clazz);
                        schemaFactory.setResourceResolver(resourceResolver);
                    }
                    schema = schemaFactory.newSchema(source);

                } catch (IOException | SAXException e) {
                    InternalError ie = new InternalError(e.getMessage());
                    ie.initCause(e);
                    throw ie;
                } finally {
                    if (resourceResolver != null) resourceResolver.closeStreams();
                }
            }
        }
    }
    return schema.newValidatorHandler();
}
 
Example 4
Project: openjdk-jdk10   File: URIResolverTest.java   Source Code and License Vote up 5 votes
/**
 * This is to test the URIResolver.resolve() method when a transformer is
 * created using StreamSource. style-sheet file has xsl:include in it.
 *
 * @throws Exception If any errors occur.
 */
@Test
public static void resolver01() throws Exception {
    try (FileInputStream fis = new FileInputStream(XSL_INCLUDE_FILE)) {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
        tfactory.setURIResolver(resolver);

        StreamSource streamSource = new StreamSource(fis);
        streamSource.setSystemId(SYSTEM_ID);
        assertNotNull(tfactory.newTransformer(streamSource));
    }
}
 
Example 5
Project: openjdk-jdk10   File: URIResolverTest.java   Source Code and License Vote up 5 votes
/**
 * This is to test the URIResolver.resolve() method when a transformer is
 * created using StreamSource. style-sheet file has xsl:import in it.
 *
 * @throws Exception If any errors occur.
 */
@Test
public static void resolver04() throws Exception {
    try (FileInputStream fis = new FileInputStream(XSL_IMPORT_FILE)) {
        URIResolverTest resolver = new URIResolverTest(XSL_TEMP_FILE, SYSTEM_ID);
        TransformerFactory tfactory = TransformerFactory.newInstance();
        tfactory.setURIResolver(resolver);
        StreamSource streamSource = new StreamSource(fis);
        streamSource.setSystemId(SYSTEM_ID);
        assertNotNull(tfactory.newTransformer(streamSource));
    }
}
 
Example 6
Project: openjdk-jdk10   File: TemplatesFilterFactoryImpl.java   Source Code and License Vote up 5 votes
public Source resolve(String href, String base) throws TransformerException {
    if ("http://astro.com/stylesheets/topleveltemplate".equals(href)) {
        StreamSource ss = new StreamSource(TOPTEMPLINCXSL);
        ss.setSystemId(filenameToURL(TOPTEMPLINCXSL));
        return ss;
    } else {
        return null;
    }
}
 
Example 7
Project: joai-project   File: XSLTransformer.java   Source Code and License Vote up 3 votes
/**
 *  Gets a specific {@link javax.xml.transform.Transformer}, used to transform
 *  XML using a given XSL stylesheet.
 *
 * @param  xslFilePath                            A path to an XSL stylesheet
 *      file.
 * @param  transformerFactoryClass                TransformerFactory class
 *      (e.g. "net.sf.saxon.TransformerFactoryImpl")
 * @return                                        A Transformer used to
 *      transform XML using a given stylesheet.
 * @exception  TransformerConfigurationException  If error.
 * @exception  FileNotFoundException              If file can not be found.
 */
public static Transformer getTransformer(String xslFilePath, String transformerFactoryClass)
	 throws TransformerConfigurationException, FileNotFoundException {

	TransformerFactory tFactory = getTransformerFactory(transformerFactoryClass);

	File xslFile = new File(xslFilePath);
	StreamSource stream = new StreamSource(new FileInputStream(xslFile));

	// Set systemId so xsl style sheets can find relative style sheets referenced in include and import directives
	stream.setSystemId(xslFile);
	return tFactory.newTransformer(stream);
}