protected boolean needsNamespacesAsAttributes()

in src/main/java/org/apache/sling/rewriter/impl/components/AbstractTraxSerializerFactory.java [107:130]


    protected boolean needsNamespacesAsAttributes() throws Exception {
        // Serialize a minimal document to check how namespaces are handled.
        final StringWriter writer = new StringWriter();

        final String uri = "namespaceuri";
        final String prefix = "nsp";
        final String check = "xmlns:" + prefix + "='" + uri + "'";

        final TransformerHandler handler = this.tfactory.newTransformerHandler();
        handler.setResult(new StreamResult(writer));

        // Output a single element
        handler.startDocument();
        handler.startPrefixMapping(prefix, uri);
        handler.startElement(uri, "element", "element", new AttributesImpl());
        handler.endElement(uri, "element", "element");
        handler.endPrefixMapping(prefix);
        handler.endDocument();

        final String text = writer.toString();

        // Check if the namespace is there (replace " by ' to be sure of what we search in)
        return (text.replace('"', '\'').indexOf(check) == -1);
    }