List of usage examples for javax.xml.transform.sax SAXTransformerFactory getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:net.sf.joost.plugins.traxfilter.THResolver.java
/** * Creates new TH instance out of TrAX factory * @param method/*from www . j a v a2s. c om*/ * @param source * @return TH */ protected TransformerHandler newTHOutOfTrAX(String method, Source source, Hashtable params, ErrorListener errorListener, URIResolver uriResolver) throws SAXException { if (DEBUG) log.debug("newTHOutOfTrAX()"); SAXTransformerFactory saxtf; if (FACTORY.getValueStr().length() > 0) { // create factory as asked by the client try { saxtf = (SAXTransformerFactory) (Class.forName(FACTORY.getValueStr())).newInstance(); if (DEBUG) log.debug("newTHOutOfTrAX(): use custom TrAX factory " + FACTORY.getValueStr()); } catch (InstantiationException e) { throw new SAXException(e); } catch (ClassNotFoundException e) { throw new SAXException(e); } catch (IllegalAccessException e) { throw new SAXException(e); } } else if (STX_METHOD.equals(method)) { saxtf = new TransformerFactoryImpl(); if (DEBUG) log.debug("newTHOutOfTrAX(): use default Joost factory " + saxtf.getClass().toString()); } else { final String TFPROP = "javax.xml.transform.TransformerFactory"; final String STXIMP = "net.sf.joost.trax.TransformerFactoryImpl"; synchronized (SYNCHRONIZE_GUARD) { String propVal = System.getProperty(TFPROP); boolean propChanged = false; String xsltFac = System.getProperty(TrAXConstants.KEY_XSLT_FACTORY); if (xsltFac != null || STXIMP.equals(propVal)) { // change this property, // otherwise we wouldn't get an XSLT transformer if (xsltFac != null) System.setProperty(TFPROP, xsltFac); else { Properties props = System.getProperties(); props.remove(TFPROP); System.setProperties(props); } propChanged = true; } saxtf = (SAXTransformerFactory) TransformerFactory.newInstance(); if (propChanged) { // reset property if (propVal != null) System.setProperty(TFPROP, propVal); else { Properties props = System.getProperties(); props.remove(TFPROP); System.setProperties(props); } } } if (DEBUG) log.debug("newTHOutOfTrAX(): use default TrAX factory " + saxtf.getClass().toString()); } // set factory attributes setTraxFactoryAttributes(saxtf, params); setupTransformerFactory(saxtf, errorListener, uriResolver); try { if (DEBUG) log.debug("newTHOutOfTrAX(): creating factory's reusable TH"); // TrAX way to create TH TransformerHandler th = saxtf.newTransformerHandler(source); setupTransformer(th.getTransformer(), errorListener, uriResolver); return th; } catch (TransformerConfigurationException ex) { throw new SAXException(ex); } }
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 ww .j a va 2 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; }
From source file:org.apache.cocoon.serialization.AbstractTextSerializer.java
/** * Checks if the used Trax implementation correctly handles namespaces set using * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes. * <p>/* w w w. j av a2s .c o m*/ * The check consists in sending SAX events representing a minimal namespaced document * with namespaces defined only with calls to <code>startPrefixMapping</code> (no * xmlns:xxx attributes) and check if they are present in the resulting text. */ protected boolean needsNamespacesAsAttributes() throws Exception { SAXTransformerFactory factory = getTransformerFactory(); Boolean cacheValue = (Boolean) needsNamespaceCache.get(factory.getClass().getName()); if (cacheValue != null) { return cacheValue.booleanValue(); } else { // Serialize a minimal document to check how namespaces are handled. StringWriter writer = new StringWriter(); String uri = "namespaceuri"; String prefix = "nsp"; String check = "xmlns:" + prefix + "='" + uri + "'"; TransformerHandler handler = this.getTransformerHandler(); handler.getTransformer().setOutputProperties(format); handler.setResult(new StreamResult(writer)); // Output a single element handler.startDocument(); handler.startPrefixMapping(prefix, uri); handler.startElement(uri, "element", "element", XMLUtils.EMPTY_ATTRIBUTES); handler.endElement(uri, "element", "element"); handler.endPrefixMapping(prefix); handler.endDocument(); String text = writer.toString(); // Check if the namespace is there (replace " by ' to be sure of what we search in) boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1); String msg = needsIt ? " needs namespace attributes (will be slower)." : " handles correctly namespaces."; getLogger().debug("Trax handler " + handler.getClass().getName() + msg); needsNamespaceCache.put(factory.getClass().getName(), BooleanUtils.toBooleanObject(needsIt)); return needsIt; } }