java.lang.IllegalArgumentException:不支持:缩进号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15134861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:36:36  来源:igfitidea点击:

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

javaxmlxslt

提问by senzacionale

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 exception here:

但我在这里得到了例外:

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

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

java.lang.IllegalArgumentException:不支持:缩进号

my java:

我的爪哇:

java version "1.6.0_33"

why i get this error?

为什么我收到这个错误?

回答by Dimitris

I fixed that exception by commenting this line:

我通过评论这一行来修复该异常:

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

and adding this line:

并添加这一行:

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

The exception is gone even though the indetation that appears in the browser is incorrect.

即使浏览器中出现的 indetation 不正确,异常也消失了。

回答by Drakonoved

Instead of

代替

TransformerFactory transformerFactory = TransformerFactory.newInstance();

you should write

你应该写

TransformerFactory transformerFactory = new TransformerFactoryImpl();

because not all implementations of TransformerFactoryhave that field "indent-number".

因为并非所有的实现TransformerFactory都有那个字段"indent-number"

回答by DataNucleus

Likely because Xalan (as packaged in JDK1.6/1.7) support "indent-number", yet others don't and have their own way of specifying the size of indent. So you have to put in the string appropriate for the XSLT provider. Work out which you're using and see its docs

可能是因为 Xalan(打包在 JDK1.6/1.7 中)支持“缩进数”,而其他人不支持并且有自己的方式来指定缩进的大小。因此,您必须输入适合 XSLT 提供程序的字符串。确定您正在使用哪个并查看其文档

Aren't standards that don't specify such things great?

不指定这些事情的标准不是很好吗?

回答by Perception

You should use the predefined constant OutputKeys.INDENT, or if you really insist on hardcoding the value, it should be 'indent', not 'indent-number'.

你应该使用预定义的常量OutputKeys.INDENT,或者如果你真的坚持对值进行硬编码,它应该是'indent',而不是'indent-number'。