Java 应用程序 FreeMind 使用 XSLT 从旧文件进行更新。XSLT 使用静态 java 函数来简化字符串操作。这在 Java 7 update 2 之前都可以正常工作,并在 windows 下的 java7 update 4 中给出以下异常:
STDERR: ERROR: 'The first argument to the non-static Java function 'replaceSpacesToNonbreakableSpaces' is not a valid object reference.'
STDERR: FATAL ERROR: 'Could not compile stylesheet'May 26, 2012 10:50:06 PM freemind.main.Resources logException
SEVERE: An exception occured:
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
at freemind.main.Tools$1TransformerRunnable.run(Tools.java:1023)
at java.lang.Thread.run(Unknown Source)
把它归结为一个最小的例子,它出现了,自定义 SecurityManager 是罪魁祸首。如果设置,代码将失败。如果注释掉,代码就可以工作。有谁知道,在更新 2 和更新 4 之间 java7 的安全系统发生了什么变化,或者还有什么可能导致这种情况?
这是一个更简单的示例:
package freemind.main;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public final class FreeMindSecurityTest {
public static void main(String[] args) throws Exception {
// if commented out, this program works as expected.
System.setSecurityManager(new SecurityManager());
String input = "<map version=\"0.9.0\">"
+ "</map>";
String xslt = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ " <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:FreeMindSecurityTest=\"xalan://freemind.main.FreeMindSecurityTest\" exclude-result-prefixes=\"FreeMindSecurityTest\">"
+ " <xsl:template match=\"/ | node() | @* | comment() | processing-instruction()\">"
+ " <xsl:value-of select=\"FreeMindSecurityTest:replaceSpacesToNonbreakableSpaces('test')\"/>"
+ " </xsl:template>" + " </xsl:stylesheet>";
StringWriter writer = new StringWriter();
final Result result = new StreamResult(writer);
final StreamSource sr = new StreamSource(new StringReader(input));
// create an instance of TransformerFactory
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(new StreamSource(
new StringReader(xslt)));
trans.transform(sr, result);
System.out.println("Transformed: " + writer.getBuffer());
writer.close();
}
public static String replaceSpacesToNonbreakableSpaces(String input) {
return input;
}
}
简而言之,java 认为该方法不是静态的(但它是静态的)并且错过了对象引用。
TIA,来自 FreeMind 的 Chris
编辑:添加了最短的示例(只需设置普通安全管理器的副本即可)。