java 默认的 TransformerFactory 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29450535/
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
What is the default TransformerFactory?
提问by user3014901
I'm using JAXP XSLT APIs (javax.xml.transform) to transform xml file.
我正在使用 JAXP XSLT API (javax.xml.transform) 来转换 xml 文件。
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslSource);
transformer.transform(inputSource, outputResult);
The javadoc for TransformerFactory says: It uses the following ordered lookup procedure to determine the TransformerFactory implementation class to load:
TransformerFactory 的 javadoc 说:它使用以下有序查找过程来确定要加载的 TransformerFactory 实现类:
- Use the javax.xml.transform.TransformerFactory system property.
- Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.
- Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.transform.TransformerFactory in jars available to the runtime.
- Platform default TransformerFactory instance.
- 使用 javax.xml.transform.TransformerFactory 系统属性。
- 使用 JRE 目录中的属性文件“lib/jaxp.properties”。这个配置文件是标准的 java.util.Properties 格式,包含实现类的全限定名,键是上面定义的系统属性。jaxp.properties 文件只被 JAXP 实现读取一次,然后它的值被缓存以备将来使用。如果第一次尝试读取文件时该文件不存在,则不会再尝试检查它是否存在。jaxp.properties 中的任何属性在第一次读取后都无法更改其值。
- 如果可用,请使用服务 API(如 JAR 规范中所述)来确定类名。服务 API 将在文件 META-INF/services/javax.xml.transform.TransformerFactory 中的可用于运行时的 jar 文件中查找类名。
- 平台默认 TransformerFactory 实例。
I wonder how to decide which is the default TransformerFactory instance?
我想知道如何确定哪个是默认的 TransformerFactory 实例?
采纳答案by Michael Kay
"Platform" here is Java-speak for the Java compiler / runtime you are using. So the "platform default" means whatever the JDK decides. In the case of the Oracle JDK, it's a version of the Xalan XSLT 1.0 engine that's built in to the JDK. A different JDK could use a different default.
这里的“平台”是您使用的 Java 编译器/运行时的 Java 语言。因此,“平台默认值”意味着 JDK 决定的任何内容。对于 Oracle JDK,它是 JDK 中内置的 Xalan XSLT 1.0 引擎的一个版本。不同的 JDK 可以使用不同的默认值。
回答by Alain Pannetier
From Oracle JDK 1.7
从 Oracle JDK 1.7
Class javax.xml.transform.TransformerFactory
:
班级javax.xml.transform.TransformerFactory
:
Default Transformer is XSLTC (originally forked from Xalan). XSLTC is the compiling version (the 'C' in XSLTC)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
默认 Transformer 是 XSLTC(最初从 Xalan 派生而来)。XSLTC 是编译版本(XSLTC 中的“C”)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
public static TransformerFactory newInstance()
throws TransformerFactoryConfigurationError {
try {
return (TransformerFactory) FactoryFinder.find(
/* The default property name according to the JAXP spec */
"javax.xml.transform.TransformerFactory",
/* The fallback implementation class name, XSLTC */
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
} catch (FactoryFinder.ConfigurationError e) {
throw new TransformerFactoryConfigurationError(
e.getException(),
e.getMessage());
}
}