Java.lang.IllegalArgumentException: not supported: indent-number

public String filter(String message) {
        if (message == null) {
            return null;
        }

        // Remove formatting, transformer fails to handle wrong indentation correctly.
        message = message.replaceAll(">\\s*[\\r\\n]+\\s*", ">");
        message = message.replaceAll("\\s*[\\r\\n]+\\s*", " "); // for wrapped attribute lists

        Source xmlInput = new StreamSource(new StringReader(message));
        StringWriter stringWriter = new StringWriter();
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setAttribute("indent-number", INDENT); // for Java 6

            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", INDENT.toString()); // Java 1.5
            transformer.transform(xmlInput, new StreamResult(stringWriter));

            String pretty = stringWriter.toString();
            pretty = pretty.replace("\r\n", "\n");
            return pretty;
        } catch (TransformerException e) {
            if (e.getCause() != null && e.getCause() instanceof SAXParseException) {
                return message;
            }
            throw new RuntimeException(e);
        }
    }

but I get an exception here:

transformerFactory.setAttribute("indent-number", INDENT); // for Java 6

java.lang.IllegalArgumentException: not supported: indent-number

my java:

java version "1.6.0_33"

why am i getting this error?

+5
source share
4 answers

I fixed this exception by commenting on this line:

transformerFactory.setAttribute("indent-number", indent);

and adding this line:

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

Excluded even if the pointer displayed in the browser is incorrect.

+10
source

Instead

TransformerFactory transformerFactory = TransformerFactory.newInstance();

you should write

TransformerFactory transformerFactory = new TransformerFactoryImpl();

because not all implementations TransformerFactoryhave this field "indent-number".

+1
source

Probably because Xalan (as in JDK1.6 / 1.7) supports "indent-number", but others do not and have their own way of determining the size of the indent. Therefore, you must enter a string suitable for the XSLT provider. Design what you use and see its docs

Are standards that do not indicate such things?

0
source

You must use a predefined constant OutputKeys.INDENT, or if you really insist on hard coding the value, it should be indentation, not indentation number.

-2
source

All Articles