List of usage examples for javax.xml.transform.sax SAXTransformerFactory setAttribute
public abstract void setAttribute(String name, Object value);
From source file:Main.java
/** * Creates a {@link TransformerHandler}. * //w w w .j av a 2 s . com * @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: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(); }/* w ww . ja v a 2s . co 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; }
From source file:net.sf.joost.plugins.traxfilter.THResolver.java
/** * Set to the SAX TrAX Factory attributes by inspecting the given parameters * for those which are from TrAX namespace * *///w w w . ja v a 2 s . c om protected void setTraxFactoryAttributes(SAXTransformerFactory saxtf, Hashtable params) { // loop over all parameters Enumeration e = params.keys(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); // is this one from TrAX namespace? if (key.startsWith(tmp_TRAX_ATTR_NS)) { // it is, remove the namespace prefix and set it to the factory String name = key.substring(tmp_TRAX_ATTR_NS.length()).toLowerCase(); saxtf.setAttribute(name, params.get(key)); if (DEBUG) log.debug("newTHOutOfTrAX(): set factory attribute " + name + "=" + params.get(key)); } } }
From source file:org.apache.cocoon.components.xslt.TraxProcessor.java
/** * Get the TransformerFactory associated with the given classname. If the * class can't be found or the given class doesn't implement the required * interface, the default factory is returned. *///w w w . j a v a2 s.c om private SAXTransformerFactory getTransformerFactory(String factoryName) { SAXTransformerFactory _factory; if (null == factoryName) { _factory = (SAXTransformerFactory) TransformerFactory.newInstance(); } else { try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = getClass().getClassLoader(); } _factory = (SAXTransformerFactory) loader.loadClass(factoryName).newInstance(); } catch (ClassNotFoundException cnfe) { getLogger().error("Cannot find the requested TrAX factory '" + factoryName + "'. Using default TrAX Transformer Factory instead."); if (m_factory != null) return m_factory; _factory = (SAXTransformerFactory) TransformerFactory.newInstance(); } catch (ClassCastException cce) { getLogger().error("The indicated class '" + factoryName + "' is not a TrAX Transformer Factory. Using default TrAX Transformer Factory instead."); if (m_factory != null) return m_factory; _factory = (SAXTransformerFactory) TransformerFactory.newInstance(); } catch (Exception e) { getLogger().error("Error found loading the requested TrAX Transformer Factory '" + factoryName + "'. Using default TrAX Transformer Factory instead."); if (m_factory != null) return m_factory; _factory = (SAXTransformerFactory) TransformerFactory.newInstance(); } } _factory.setErrorListener(new TraxErrorListener(getLogger(), null)); _factory.setURIResolver(this); // FIXME (SM): implementation-specific parameter passing should be // made more extensible. if (_factory.getClass().getName().equals("org.apache.xalan.processor.TransformerFactoryImpl")) { _factory.setAttribute("http://xml.apache.org/xalan/features/incremental", BooleanUtils.toBooleanObject(m_incrementalProcessing)); } // SAXON 8 will not report errors unless version warning is set to false. if (_factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { _factory.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); } return _factory; }