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:不支持:缩进数
我的爪哇:
java version "1.6.0_33"
为什么我得到这个错误?