private static void setIndent(TransformerFactory factory, int indent) { // TODO: support older parser attribute values as well try { factory.setAttribute("indent-number", indent); } catch (IllegalArgumentException e) { // ignore for factories that don't support this } } }
//(1) TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(2)); //(2) Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); //(3) t.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "utf-8"));
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(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } } public static String prettyFormat(String input) { return prettyFormat(input, 2); }
transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformerFactory.setAttribute("indent-number", 3); } catch (IllegalArgumentException iae) {
public static String getTextFromSource(Source source) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult stream = new StreamResult(new StringWriter()); transformer.transform(source, stream); return stream.getWriter().toString(); } catch (Exception e) { throw new IllegalStateException(e); } } }
/** Utility method to print out a dom. */ protected void print(Document dom) throws Exception { TransformerFactory txFactory = TransformerFactory.newInstance(); try { txFactory.setAttribute( "{http://xml.apache.org/xalan}indent-number", Integer.valueOf(2)); } catch (Exception e) { // some } Transformer tx = txFactory.newTransformer(); tx.setOutputProperty(OutputKeys.METHOD, "xml"); tx.setOutputProperty(OutputKeys.INDENT, "yes"); tx.transform( new DOMSource(dom), new StreamResult(new OutputStreamWriter(System.out, "utf-8"))); }
tfactory.setAttribute((String) pair[0], pair[1]);
tfactory.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); else printInvalidXSLTCOption("-L"); tfactory.setAttribute ("http://xml.apache.org/xalan/features/incremental", java.lang.Boolean.TRUE); tfactory.setAttribute ("http://xml.apache.org/xalan/features/optimize", java.lang.Boolean.FALSE); tfactory.setAttribute("generate-translet", "true"); tfactory.setAttribute("translet-name", argv[++i]); tfactory.setAttribute("generate-translet", "true"); tfactory.setAttribute("destination-directory", argv[++i]); else System.err.println( tfactory.setAttribute("generate-translet", "true"); tfactory.setAttribute("jar-name", argv[++i]); tfactory.setAttribute("package-name", argv[++i]); else System.err.println( tfactory.setAttribute("enable-inlining", "true");
/** * @return a transformer that indents entries by 4 characters (never null) */ public static Transformer createIndentingTransformer() { Transformer transformer; try { TRANSFORMER_FACTORY.setAttribute("indent-number", 4); transformer = TRANSFORMER_FACTORY.newTransformer(); } catch (final Exception e) { throw new IllegalStateException(e); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return transformer; }
private String prettyPrint(Document document) { try { StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(document), xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception ex) { throw new PayloadHandlingException(ex); } }
public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException { TransformerFactory f = TransformerFactory.newInstance(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt)); f.setURIResolver(new ZipURIResolver(files)); Transformer t = f.newTransformer(xsrc); t.setURIResolver(new ZipURIResolver(files)); StreamSource src = new StreamSource(new ByteArrayInputStream(source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); t.transform(src, res); return out.toByteArray(); }
public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws Exception { TransformerFactory f = TransformerFactory.newInstance(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt)); f.setURIResolver(new ZipURIResolver(files)); Transformer t = f.newTransformer(xsrc); t.setURIResolver(new ZipURIResolver(files)); StreamSource src = new StreamSource(new ByteArrayInputStream(source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); t.transform(src, res); return out.toByteArray(); }
public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException { TransformerFactory f = TransformerFactory.newInstance(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt)); f.setURIResolver(new ZipURIResolver(files)); Transformer t = f.newTransformer(xsrc); t.setURIResolver(new ZipURIResolver(files)); StreamSource src = new StreamSource(new ByteArrayInputStream(source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); t.transform(src, res); return out.toByteArray(); }
/** Create a Transformer which is initialized with the settings of this TransformerBase. */ public Transformer createTransformer() throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); if (indentation > -1) { try { tFactory.setAttribute("indent-number", Integer.valueOf(indentation)); } catch (IllegalArgumentException e) { // throw away (java 1.4 doesn't support this method, but java 1.5 requires it) } } Transformer transformer = tFactory.newTransformer(); if (indentation > -1) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", Integer.toString(indentation)); } else { transformer.setOutputProperty(OutputKeys.INDENT, "no"); } if (xmlDecl) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.setOutputProperty(OutputKeys.ENCODING, charset.name()); return transformer; }
public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws Exception { TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt)); f.setURIResolver(new ZipURIResolver(files)); Transformer t = f.newTransformer(xsrc); t.setURIResolver(new ZipURIResolver(files)); StreamSource src = new StreamSource(new ByteArrayInputStream(source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); t.transform(src, res); return out.toByteArray(); }
public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException { TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt)); f.setURIResolver(new ZipURIResolver(files)); Transformer t = f.newTransformer(xsrc); t.setURIResolver(new ZipURIResolver(files)); StreamSource src = new StreamSource(new ByteArrayInputStream(source)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); t.transform(src, res); return out.toByteArray(); }
private TransformerFactory createTransformerFactory() throws ClassNotFoundException { TransformerFactory transformerFactory; if (this.transformerFactoryClassName != null) { @SuppressWarnings("unchecked") Class<TransformerFactory> transformerFactoryClass = (Class<TransformerFactory>) ClassUtils.forName(this.transformerFactoryClassName, this.classLoader); transformerFactory = TransformerFactoryUtils.newInstance(transformerFactoryClass); } else { transformerFactory = TransformerFactoryUtils.newInstance(); } try { transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file"); } catch (IllegalArgumentException ex) { if (logger.isInfoEnabled()) { logger.info("The '" + XMLConstants.ACCESS_EXTERNAL_STYLESHEET + "' property is not supported by " + transformerFactory.getClass().getCanonicalName()); } } return transformerFactory; }
@Test public void transformerFactoryProperties() { SetPropertyAnswer setPropertyAnswer = new SetPropertyAnswer(transformerFactory); doAnswer(setPropertyAnswer).when(transformerFactoryWrapper).setAttribute(anyString(), anyObject()); defaultXMLSecureFactories.configureTransformerFactory(transformerFactoryWrapper); assertThat(setPropertyAnswer.exception, is(nullValue())); for (String property : FACTORY_ATTRIBUTES) { verify(transformerFactoryWrapper).setAttribute(property, ""); } }
@Override public Void answer(InvocationOnMock invocation) { String name = (String) invocation.getArguments()[0]; Object value = invocation.getArguments()[1]; try { if (propertySetter instanceof SchemaFactory) { ((SchemaFactory) propertySetter).setProperty(name, value); } else if (propertySetter instanceof Validator) { ((Validator) propertySetter).setProperty(name, value); } else if (propertySetter instanceof TransformerFactory) { ((TransformerFactory) propertySetter).setAttribute(name, value); } else { throw new IllegalArgumentException("Invalid property setter."); } } catch (Exception e) { exception = e; } return null; } }