List of usage examples for javax.xml.transform.sax SAXTransformerFactory newTransformerHandler
public abstract TransformerHandler newTransformerHandler() throws TransformerConfigurationException;
From source file:Main.java
/** Set up transformer to output full XML doc. * * @param tf see {@link #saxTransformerFactory()} * @return Transformer that is fully set up. But here we get TransformerHandler first from TransformerFactory. *///from w ww . j a va 2s. c o m public static TransformerHandler newTransformerHandler(final SAXTransformerFactory tf) throws TransformerConfigurationException { final TransformerHandler resultHandler = tf.newTransformerHandler(); final Transformer transformer = resultHandler.getTransformer(); setUtfEncoding(transformer); setIndentFlag(transformer); setTransformerIndent(transformer); return resultHandler; }
From source file:Main.java
/** Set up transformer to output a standalone "fragment" - suppressing xml declaration. * * @param tf see {@link #saxTransformerFactory()} * @return Transformer that is fully set up. But here we get TransformerHandler first from TransformerFactory. *//*from ww w .ja va 2 s. c o m*/ public static TransformerHandler newFragmentTransformerHandler(final SAXTransformerFactory tf) throws TransformerConfigurationException { final TransformerHandler resultHandler = tf.newTransformerHandler(); final Transformer transformer = resultHandler.getTransformer(); setUtfEncoding(transformer); setIndentFlag(transformer); setTransformerIndent(transformer); outputStandaloneFragment(transformer); return resultHandler; }
From source file:com.aurel.track.util.HTMLDiff.java
public static String makeDiff(String newValue, String oldValue, Locale locale) throws URISyntaxException { boolean htmlOut = true; if (newValue == null) { newValue = ""; } else {/*from w w w . ja va 2 s.co m*/ newValue = newValue.replaceAll(" ", " "); } if (oldValue == null || (oldValue != null && oldValue.length() == 0)) { return newValue; } else { oldValue = oldValue.replaceAll(" ", " "); } try { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler result = tf.newTransformerHandler(); StringWriter stringWriter = new StringWriter(); result.setResult(new StreamResult(stringWriter)); XslFilter filter = new XslFilter(); ContentHandler postProcess = htmlOut ? filter.xsl(result, "com/aurel/track/util/htmlheader.xsl") : result; String prefix = "diff"; HtmlCleaner cleaner = new HtmlCleaner(); InputSource oldSource = new InputSource(new StringReader(oldValue)); InputSource newSource = new InputSource(new StringReader(newValue)); DomTreeBuilder oldHandler = new DomTreeBuilder(); cleaner.cleanAndParse(oldSource, oldHandler); TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale); DomTreeBuilder newHandler = new DomTreeBuilder(); cleaner.cleanAndParse(newSource, newHandler); TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale); postProcess.startDocument(); postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl()); postProcess.startElement("", "diff", "diff", new AttributesImpl()); HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix); HTMLDiffer differ = new HTMLDiffer(output); differ.diff(leftComparator, rightComparator); postProcess.endElement("", "diff", "diff"); postProcess.endElement("", "diffreport", "diffreport"); postProcess.endDocument(); return stringWriter.toString(); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); if (e.getCause() != null) { LOGGER.error(ExceptionUtils.getStackTrace(e.getCause())); } if (e instanceof SAXException) { LOGGER.error(ExceptionUtils.getStackTrace(((SAXException) e).getException())); } } return null; }
From source file:Main.java
/** * Creates a {@link TransformerHandler}. * //from w w w . j av a2 s .c o m * @param commentHeader the comment header * @param rootTag the root tag * @param streamResult stream result */ public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag, StreamResult streamResult, Charset charset) throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { tf.setAttribute("indent-number", new Integer(2)); } catch (Exception e) { // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510 } TransformerHandler transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, charset.name()); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); transformerHandler.setResult(streamResult); transformerHandler.startDocument(); String newline = System.getProperty("line.separator"); if (newline == null) { newline = "\n"; } commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " "); transformerHandler.characters("\n".toCharArray(), 0, 1); transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length); transformerHandler.characters("\n".toCharArray(), 0, 1); if (rootTag.length() > 0) { transformerHandler.startElement("", "", rootTag, null); } return transformerHandler; }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;//from ww w.j av a2 s. c om SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:com.benchmark.TikaCLI.java
/** * Returns a transformer handler that serializes incoming SAX events * to XHTML or HTML (depending the given method) using the given output * encoding./*from www . ja v a 2s . c o m*/ * * @see <a href="https://issues.apache.org/jira/browse/TIKA-277">TIKA-277</a> * @param output output stream * @param method "xml" or "html" * @param encoding output encoding, * or <code>null</code> for the platform default * @return {@link System#out} transformer handler * @throws TransformerConfigurationException * if the transformer can not be created */ private static TransformerHandler getTransformerHandler(OutputStream output, String method, String encoding, boolean prettyPrint) throws TransformerConfigurationException { SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, prettyPrint ? "yes" : "no"); if (encoding != null) { handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, encoding); } handler.setResult(new StreamResult(output)); return handler; }
From source file:it.polito.tellmefirst.web.rest.interfaces.AbsResponseInterface.java
public TransformerHandler initXMLDoc(ByteArrayOutputStream out) throws TMFOutputException { LOG.debug("[initXMLDoc] - BEGIN"); TransformerHandler hd;/*from w w w . ja v a2s .c o m*/ try { StreamResult streamResult = new StreamResult(out); SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); hd = tf.newTransformerHandler(); Transformer serializer = hd.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); hd.setResult(streamResult); hd.startDocument(); } catch (Exception e) { throw new TMFOutputException("Error initializing XML.", e); } LOG.debug("[initXMLDoc] - END"); return hd; }
From source file:com.jaeksoft.searchlib.util.XmlWriter.java
public XmlWriter(PrintWriter out, String encoding) throws TransformerConfigurationException, SAXException { StreamResult streamResult = new StreamResult(out); SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, encoding); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); transformerHandler.setResult(streamResult); startedElementStack = new Stack<String>(); transformerHandler.startDocument();//www . java 2 s . c om elementAttributes = new AttributesImpl(); Pattern p = Pattern.compile("\\p{Cntrl}"); controlMatcher = p.matcher(""); }
From source file:com.ikanow.infinit.e.harvest.extraction.document.file.FileHarvester.java
private static TransformerHandler getTransformerHandler(String method, StringWriter sw) { try {/*from w w w .ja va2 s . co m*/ SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(new StreamResult(sw)); return handler; } catch (Exception e) { return null; } }
From source file:lu.tudor.santec.dicom.gui.header.Dcm2Xml.java
private TransformerHandler getTransformerHandler() throws TransformerConfigurationException, IOException { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); if (xslt == null) { return tf.newTransformerHandler(); }//from ww w .ja v a 2s .c o m if (xsltInc) { tf.setAttribute("http://xml.apache.org/xalan/features/incremental", Boolean.TRUE); } TransformerHandler th = tf .newTransformerHandler(new StreamSource(xslt.openStream(), xslt.toExternalForm())); Transformer t = th.getTransformer(); if (xsltParams != null) { for (int i = 0; i + 1 < xsltParams.length; i++, i++) { t.setParameter(xsltParams[i], xsltParams[i + 1]); } } return th; }