* 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", "", new AttributesImpl());
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(), new Boolean(needsIt));
return needsIt;
}
}