List of usage examples for javax.xml.transform TransformerFactory setAttribute
public abstract void setAttribute(String name, Object value);
From source file:Main.java
private static void setIndent(TransformerFactory factory, int indent) { // TODO: support older parser attribute values as well try {/*from w ww . j av a 2 s. com*/ factory.setAttribute("indent-number", indent); } catch (IllegalArgumentException e) { // ignore for factories that don't support this } }
From source file:Main.java
/** * Generates a XML string from DOM.// ww w. jav a2s. c o m * * @param xmlDocument the DOM * @return XML string */ public static String build(Document xmlDocument) throws TransformerException { if (transformer == null) { TransformerFactory xformFactory = TransformerFactory.newInstance(); try { xformFactory.setAttribute("indent-number", new Integer(4)); } catch (IllegalArgumentException e) { // ignore } transformer = xformFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } StringWriter out = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult(out)); return out.getBuffer().toString(); }
From source file:Main.java
public static void saveDocument(Document doc, File transform, File file) throws IOException { try {//from w w w . ja va2s . c o m TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(2)); Transformer transformer = tFactory.newTransformer(new StreamSource(transform)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(file.getPath()); DOMSource source = new DOMSource(doc); CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder(); StreamResult result = new StreamResult(new OutputStreamWriter(fos, utf8Encoder)); transformer.transform(source, result); fos.close(); } catch (TransformerException e) { throw new IOException(e); } }
From source file:com.sdl.odata.renderer.util.PrettyPrinter.java
/** * Pretty-print a given XML.//from w w w . ja v a 2s . c o m * * @param xml The not-formatted XML. * @return The pretty-printed XML. */ public static String prettyPrintXml(String xml) throws TransformerException, IOException { Source xmlInput = new StreamSource(new StringReader(xml)); try (StringWriter stringWriter = new StringWriter()) { StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); transformer.setOutputProperty(OutputKeys.VERSION, "1.0"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } }
From source file:Main.java
private static void writeTo(Result result, Document document) throws TransformerConfigurationException, TransformerException, FileNotFoundException, UnsupportedEncodingException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(4)); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N // indent the output to make it more legible... try {/* w w w. ja v a 2s .com*/ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N } catch (IllegalArgumentException e) { // the JAXP implementation doesn't support indentation, no big deal //e.printStackTrace(); } transformer.transform(source, result); }
From source file:StreamSrcStAXRst.java
public static void transform(InputStream xmlIn, InputStream xsltIn, OutputStream out) throws Exception { javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(xmlIn); javax.xml.transform.Result xmlResult = new javax.xml.transform.stax.StAXResult( XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(fileName))); javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltIn); javax.xml.transform.Source source = new javax.xml.transform.stream.StreamSource(xmlIn); // create an instance of TransformerFactory javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance(); transFact.setAttribute("debug", new String("true")); transFact.setAttribute("translet-name", new String("suresh")); transFact.setAttribute("generate-translet", new String("true")); transFact.setAttribute("jar-name", new String("transletjar")); javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource); trans.transform(source, xmlResult);//from w w w.ja v a2 s . com }
From source file:Main.java
public static String toString(Document doc) { try {//from w w w .j a va 2 s.c o m StringWriter sw = new StringWriter(); Source source = new DOMSource(doc); Result result = new StreamResult(sw); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(4)); Transformer xformer = tf.newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return sw.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Convenience method to get a new instance of a TransformerFactory. * If the {@link #TransformerFactory} is an instance of * net.sf.saxon.TransformerFactoryImpl, the attribute * {@link #FeatureKeys.VERSION_WARNING} will be set to false in order to * suppress the warning about using an XSLT1 stylesheet with an XSLT2 * processor.//from www. j a va2 s . c o m * * @return a new instance of TransformerFactory */ public static TransformerFactory getTransformerFactory() { TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { factory.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); } return factory; }
From source file:Main.java
/** * DOCUMENT ME!/*from www. j a v a 2 s .c o m*/ * * @return DOCUMENT ME! * * @throws TransformerConfigurationException DOCUMENT ME! * @throws TransformerException DOCUMENT ME! * @throws Exception DOCUMENT ME! */ public static byte[] writeToBytes(Document document) throws TransformerConfigurationException, TransformerException, Exception { byte[] ret = null; TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(4)); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(bos); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N // indent the output to make it more legible... try { transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N } catch (IllegalArgumentException e) { // the JAXP implementation doesn't support indentation, no big deal //e.printStackTrace(); } try { transformer.transform(source, result); ret = bos.toByteArray(); } finally { if (bos != null) { bos.flush(); bos.close(); } } return ret; }
From source file:Main.java
public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber, boolean omitXmlDeclaration) throws TransformerException { TransformerFactory f = TransformerFactory.newInstance(); f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); if (indent) { f.setAttribute("indent-number", indentNumber); }/* w w w.j av a 2 s . c om*/ Transformer t = f.newTransformer(); if (standalone) { t.setOutputProperty(OutputKeys.STANDALONE, "yes"); } if (indent) { t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber); } if (omitXmlDeclaration) { t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } return t; }