如何在 Java 中选择撒克逊 TransformerFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2968190/
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
How to select saxon TransformerFactory in Java
提问by pAkY88
In my web application I need to use Saxon TransformerFactory in order to use XSLT 2.0 but I can't use setProperty method because I don't have this right on the web server and there is a Security Manager.
在我的 Web 应用程序中,我需要使用 Saxon TransformerFactory 才能使用 XSLT 2.0,但我不能使用 setProperty 方法,因为我在 Web 服务器上没有这个权限,并且有一个安全管理器。
So I have read that it should be possible to do this:
所以我读到应该可以这样做:
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.
如果可用,请使用服务 API(如 JAR 规范中所述)来确定类名。服务 API 将在文件 META-INF/services/javax.xml.transform.TransformerFactory 中的可用于运行时的 jars 中查找类名。
I found this file in WEB-INF/lib/saxon9.jar but when I istantiate a TransformerFactory, the default factory is always selected instead of a Saxon factory.
我在 WEB-INF/lib/saxon9.jar 中找到了这个文件,但是当我实例化 TransformerFactory 时,总是选择默认工厂而不是 Saxon 工厂。
How can I select Saxon Transformer Factory?
如何选择萨克森变压器厂?
Thanks
谢谢
回答by JoseK
Can you try by setting the system property in your code like
您可以尝试在代码中设置系统属性,例如
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
do this before getting an instance of TransformerFactory.
在获取 TransformerFactory 的实例之前执行此操作。
Note: this will force all the webapps on your Tomcat to use saxon - so you have to make sure any other webapps which were using the default are okay.
注意:这将强制您的 Tomcat 上的所有 webapps 使用 saxon - 因此您必须确保使用默认值的任何其他 webapps 都没有问题。
回答by J?rn Horstmann
If your application really requires saxon and won't work with another processor then it would probably be fine to instantiate the saxon TransformerFactory directly using new net.sf.saxon.TransformerFactoryImpl()
如果您的应用程序确实需要 saxon 并且不能与其他处理器一起使用,那么直接使用实例化 saxon TransformerFactory 可能会很好 new net.sf.saxon.TransformerFactoryImpl()
回答by Andrew B
The proper way to do this is by specifying the factory class when getting a new TransformerFactory.
正确的方法是在获取新的 TransformerFactory 时指定工厂类。
I dont think calling a specific factory implementation will work - I believe the default system transformer might still be returned (at least thats what happened when I had xalan and saxon in the classpath).
我不认为调用特定的工厂实现会起作用 - 我相信默认系统转换器可能仍会返回(至少当我在类路径中有 xalan 和 saxon 时发生了这种情况)。
example:
例子:
TransformerFactory tFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null);
or for saxon
或者对于撒克逊人
TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
Javadocs:
Java 文档:
Obtain a new instance of a TransformerFactory from factory class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded. Once an application has obtained a reference to a TransformerFactory it can use the factory to configure and obtain transformer instances.
从工厂类名称获取 TransformerFactory 的新实例。当类路径中有多个提供者时,此函数很有用。它为应用程序提供了更多控制权,因为它可以指定应该加载哪个提供程序。一旦应用程序获得了对 TransformerFactory 的引用,它就可以使用工厂来配置和获取转换器实例。
回答by yegor256
Create file META-INF/services/javax.xml.transform.TransformerFactory
with content: net.sf.saxon.TransformerFactoryImpl
. That's it.
创建META-INF/services/javax.xml.transform.TransformerFactory
包含内容的文件:net.sf.saxon.TransformerFactoryImpl
. 就是这样。