Java 8 Transformer - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Transformer class.

This code example shows how to use the following methods:setOutputProperty, transform
        try { 
            TransformerFactory transFactory = TransformerFactory.newInstance();
            transFactory.setAttribute("indent-number", new Integer(2)); //$NON-NLS-1$
 
            Transformer tf = transFactory.newTransformer();
            tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//$NON-NLS-1$
            tf.setOutputProperty(OutputKeys.INDENT, "yes");//$NON-NLS-1$
            tf.setOutputProperty(OutputKeys.METHOD, "xml");//$NON-NLS-1$
            tf.setOutputProperty(OutputKeys.STANDALONE, "yes");//$NON-NLS-1$
            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
 
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            StreamResult xmlOut = new StreamResult(new BufferedOutputStream(out));
            tf.transform(xml.getSource(StreamSource.class), xmlOut);
 
            return out.toString();
        } catch (Exception e) {
            return xml.getString();
        } 
8
CodeRank
This code example shows how to use the following methods:transform
		Reader reader = new StringReader(XML);
		Source source = new StreamSource(reader);
		StaxResult result = new StaxResult(streamWriter);
		assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter());
		assertNull("EventWriter returned", result.getXMLEventWriter());
		transformer.transform(source, result);
		assertXMLEqual("Invalid result", XML, stringWriter.toString());
	} 
 
	@Test
	public void eventWriterSource() throws Exception {
		StringWriter stringWriter = new StringWriter();
		XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
		Reader reader = new StringReader(XML);
		Source source = new StreamSource(reader);
		StaxResult result = new StaxResult(eventWriter);
		assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
		assertNull("StreamWriter returned", result.getXMLStreamWriter());
		transformer.transform(source, result);
		assertXMLEqual("Invalid result", XML, stringWriter.toString());
8
CodeRank
This code example shows how to use the following methods:setOutputProperty, setURIResolver, transform
This code example shows how to use the following methods:transform
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
		StaxSource source = new StaxSource(streamReader);
		assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
		assertNull("EventReader returned", source.getXMLEventReader());
		StringWriter writer = new StringWriter();
		transformer.transform(source, new StreamResult(writer));
		assertXMLEqual("Invalid result", XML, writer.toString());
	} 
 
	@Test
	public void streamReaderSourceToDOMResult() throws Exception {
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
		StaxSource source = new StaxSource(streamReader);
		assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
		assertNull("EventReader returned", source.getXMLEventReader());
 
		Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
		Document result = documentBuilder.newDocument();
		transformer.transform(source, new DOMResult(result));
		assertXMLEqual("Invalid result", expected, result);
8
CodeRank
This code example shows how to use the following methods:setParameter, transform
        Source xsltSource = new StreamSource(Specdox.class.getResourceAsStream("/specdox.xsl"));
        xsltSource.setSystemId("/specdox.xsl");
         
        TransformerFactory transFact = new TransformerFactoryImpl();
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.setParameter("spec-file-dir", specXmlDir);
        trans.setParameter("xref", xref.getName());
        trans.setParameter("output-dir", outputDir);
        trans.setParameter("frameset-index-filename", filename);
        trans.transform(xmlSource, new StreamResult(System.out));
    } 
} 
7
CodeRank
This code example shows how to use the following methods:transform
        try { 
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(writer);
            transformer.transform(domSource, streamResult); 
        } catch (TransformerException e) {
            throw new RuntimeException(
                    "Error when serializing XML document.", e);
        } 
        return writer.toString();
    } 
 
    /** 
     * Parse an XML file to DOM 
     * @return null if an error occurs during parsing. 
     *  
     */ 
    public static Document getDocument(File file) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
7
CodeRank
This code example shows how to use the following methods:setOutputProperty
	 * @param indentAmount the size of the indent (2 characters, 3 characters, etc.) 
	 * @see javax.xml.transform.Transformer#setOutputProperty(String, String) 
	 * @see javax.xml.transform.OutputKeys#INDENT 
	 */ 
	public static void enableIndenting(Transformer transformer, int indentAmount) {
		Assert.notNull(transformer, "Transformer must not be null");
		Assert.isTrue(indentAmount > -1, "The indent amount cannot be less than zero : got " + indentAmount);
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		try { 
			// Xalan-specific, but this is the most common XSLT engine in any case 
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
		} 
		catch (IllegalArgumentException ignored) {
		} 
	} 
 
	/** 
	 * Disable indenting for the supplied {@link javax.xml.transform.Transformer}. 
	 * 
	 * @param transformer the target transformer 
7
CodeRank
This code example shows how to use the following methods:getOutputProperty
 
	@Test
	public void enableIndentingSunnyDayWithCustomKosherIndentAmount() throws Exception {
		final String indentAmountProperty = "10";
		Transformer transformer = new StubTransformer();
		TransformerUtils.enableIndenting(transformer, Integer.valueOf(indentAmountProperty));
		String indent = transformer.getOutputProperty(OutputKeys.INDENT);
		assertNotNull(indent);
		assertEquals("yes", indent);
		String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
		assertNotNull(indentAmount);
		assertEquals(indentAmountProperty, indentAmount);
	} 
 
	@Test
	public void disableIndentingSunnyDay() throws Exception {
		Transformer transformer = new StubTransformer();
		TransformerUtils.disableIndenting(transformer);
		String indent = transformer.getOutputProperty(OutputKeys.INDENT);
		assertNotNull(indent);
7
CodeRank
This code example shows how to use the following methods:setOutputProperty
	} 
	public static final Transformer createIndentingTransformer() {
		Transformer xformer;
		try { 
			xformer = transformerFactory.newTransformer();
			xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			xformer.setOutputProperty(OutputKeys.INDENT, "yes");
			xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2));
		} catch (Exception ex) {
			throw new IllegalStateException(ex);
		} 
		xformer.setOutputProperty(OutputKeys.INDENT, "yes");
		xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		return xformer;
	} 
 
	public void afterPropertiesSet() throws Exception {
		namespacePrefixes.put("p", "http://ws.cdyne.com/WeatherWS/");
	} 
7
CodeRank
This code example shows how to use the following methods:setOutputProperty, transform
		try { 
			TransformerFactory transFactory = TransformerFactory.newInstance();
			transFactory.setAttribute("indent-number", new Integer(2)); //$NON-NLS-1$
			 
			Transformer tf = transFactory.newTransformer();
			tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
			tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//$NON-NLS-1$
			tf.setOutputProperty(OutputKeys.INDENT, "yes");//$NON-NLS-1$
			tf.setOutputProperty(OutputKeys.METHOD, "xml");//$NON-NLS-1$
			tf.setOutputProperty(OutputKeys.STANDALONE, "yes");//$NON-NLS-1$
			tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
			 
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			StreamResult xmlOut = new StreamResult(new BufferedOutputStream(out));
			tf.transform(xml.getSource(StreamSource.class), xmlOut);
			 
			return out.toString();
		} catch (Exception e) {
			return xml.getString();
		} 
7
CodeRank

Related Java 8 Transformer Questions & Answers:

See Code Examples for Java 8 Transformer Methods: