List of usage examples for javax.xml.transform.sax TransformerHandler getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
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>//from www . j a v a 2s . 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; } }