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);
}
}
しかし、私はここで例外を取得します:
transformerFactory.setAttribute("indent-number", INDENT); // for Java 6
java.lang.IllegalArgumentException:サポートされていません:indent-number
私のJava:
java version "1.6.0_33"
なぜこのエラーが発生するのですか?