来自 doc-available 的 Saxon 9、Java、NullPointer [英] Saxon 9, Java, NullPointer from doc-available
问题描述
这是在 Saxon 中修复问题后看到的错误9 HE,Java - 静态错误,XTSE0210、XTSE0165、XPST0017
从以下 java 代码调用 XSLT 转换时
When invoking an XSLT transform from the following java code
public static Document transformAsDocument(Document inputDoc, String xslDoc, Map<String,
Object> params, String xslContextPath, String fullXsltFilePath) throws XmlException {
try {
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory factory = TransformerFactory.newInstance();
factory.setURIResolver(new ClasspathResourceURIResolver(xslContextPath));
factory.setAttribute(FeatureKeys.GENERATE_BYTE_CODE, false);
String systemId = XMLUtils.class.getResource(fullXsltFilePath).toString();
Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc), systemId));
Transformer xformer = template.newTransformer();
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
xformer.setParameter(entry.getKey(), entry.getValue());
}
}
DOMResult result = new DOMResult();
DOMSource domSource = new DOMSource(inputDoc);
xformer.transform(domSource, result);
return (Document) result.getNode();
} catch (TransformerConfigurationException e) {
throw new XmlException(e);
} catch (TransformerException e) {
SourceLocator locator = e.getLocator();
if (locator != null) {
Map<String, Object> message = new HashMap<String, Object>();
message.put("col", locator.getColumnNumber());
message.put("line", locator.getLineNumber());
message.put("publicId", locator.getPublicId());
message.put("systemId", locator.getSystemId());
throw new XmlException(message.toString(), e);
}
throw new XmlException(e);
} catch (Exception e) {
throw new XmlException(e);
}
}
我们看到以下堆栈跟踪.
we see the following stack trace.
java.lang.NullPointerException
at net.sf.saxon.lib.StandardURIResolver.resolve(StandardURIResolver.java:153)
at net.sf.saxon.functions.DocumentFn.resolveURI(DocumentFn.java:411)
at net.sf.saxon.functions.DocumentFn.makeDoc(DocumentFn.java:283)
at net.sf.saxon.functions.DocAvailable.docAvailable(DocAvailable.java:68)
at net.sf.saxon.functions.DocAvailable.isDocAvailable(DocAvailable.java:31)
at net.sf.saxon.functions.DocAvailable.call(DocAvailable.java:45)
at net.sf.saxon.functions.DocAvailable.call(DocAvailable.java:21)
at net.sf.saxon.expr.FunctionCall.iterate(FunctionCall.java:547)
at net.sf.saxon.expr.Expression.effectiveBooleanValue(Expression.java:886)
at net.sf.saxon.expr.instruct.Choose.choose(Choose.java:901)
at net.sf.saxon.expr.instruct.Choose.iterate(Choose.java:952)
at net.sf.saxon.expr.instruct.GlobalVariable.getSelectValue(GlobalVariable.java:643)
at net.sf.saxon.expr.instruct.GlobalVariable.actuallyEvaluate(GlobalVariable.java:724)
at net.sf.saxon.expr.instruct.GlobalVariable.evaluateVariable(GlobalVariable.java:692)
at net.sf.saxon.expr.GlobalVariableReference.evaluateVariable(GlobalVariableReference.java:126)
at net.sf.saxon.expr.VariableReference.evaluateItem(VariableReference.java:559)
at net.sf.saxon.expr.SimpleStepExpression.iterate(SimpleStepExpression.java:113)
at net.sf.saxon.expr.SlashExpression.iterate(SlashExpression.java:902)
at net.sf.saxon.expr.Atomizer.iterate(Atomizer.java:321)
at net.sf.saxon.expr.GeneralComparison.effectiveBooleanValue(GeneralComparison.java:685)
at net.sf.saxon.expr.instruct.Choose.choose(Choose.java:901)
at net.sf.saxon.expr.instruct.Choose.iterate(Choose.java:952)
at net.sf.saxon.expr.instruct.GlobalVariable.getSelectValue(GlobalVariable.java:643)
at net.sf.saxon.expr.instruct.GlobalVariable.actuallyEvaluate(GlobalVariable.java:724)
at net.sf.saxon.expr.instruct.GlobalVariable.evaluateVariable(GlobalVariable.java:692)
at net.sf.saxon.expr.GlobalVariableReference.evaluateVariable(GlobalVariableReference.java:126)
at net.sf.saxon.expr.VariableReference.iterate(VariableReference.java:536)
at net.sf.saxon.expr.instruct.ForEach.processLeavingTail(ForEach.java:434)
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:687)
at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:347)
at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:505)
at net.sf.saxon.Controller.transformDocument(Controller.java:2423)
at net.sf.saxon.Controller.transform(Controller.java:1979)
at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:596)
at net.sf.saxon.jaxp.TransformerImpl.transform(TransformerImpl.java:73)
java.lang.AssertionError: Internal error: no value for variable $g_ndsTemplateDoc at line 118 of
at net.sf.saxon.expr.VariableReference.iterate(VariableReference.java:547) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.expr.instruct.ForEach.processLeavingTail(ForEach.java:434) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:687) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:347) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:505) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.Controller.transformDocument(Controller.java:2423) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.Controller.transform(Controller.java:1979) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:596) ~[Saxon-HE-9.8.0-15.jar:na]
at net.sf.saxon.jaxp.TransformerImpl.transform(TransformerImpl.java:73) ~[Saxon-HE-9.8.0-15.jar:na]
错误来自以下 XSLT 片段,该片段位于大型 XSLT 链中.
The error is coming from the following XSLT snippet, which is in chain of large XSLTs.
<xsl:variable name="paramsDoc" select="if (doc-available('input:request')) then doc('input:request') else ()"/>代码>
相同的 XSLT 在命令行中工作,如 Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017
The same XSLT works from the command line, as in Saxon 9 HE, Java - Static errors, XTSE0210, XTSE0165, XPST0017
推荐答案
当被要求解析 URI input:request
时,ClasspathResourceURIResolver
返回什么?它似乎返回了一个 source
对象,该对象将 Saxon 带入代码路径
What does the ClasspathResourceURIResolver
return when asked to resolve the URI input:request
? It seems to have returned a source
object that takes Saxon into the code path
if (source instanceof StreamSource &&
((StreamSource)source).getInputStream() == null &&
((StreamSource)source).getReader() == null) {
Saxon 然后尝试使用标准 URI 解析器从此 Source 对象解析 URI:
Saxon then attempts resolution of the URI from this Source object using the standard URI resolver:
String uri = source.getSystemId();
resolver = context.getController().getStandardURIResolver();
try {
source = resolver.resolve(uri, "");
这会崩溃,因为 uri
为空.
and this crashes out because uri
is null.
Saxon 假设当用户提供的 URIResolver
返回 StreamSource
时,StreamSource
将包含一个 InputStream
或 Reader
或 SystemID
,但它似乎不包含这些.Saxon 可能应该很好地报告这一点而不是崩溃,但它无法对这样的 Source
对象做任何有用的事情.
Saxon's assuming that when a StreamSource
is returned by a user-supplied URIResolver
, then the StreamSource
will contain either an InputStream
or a Reader
or a SystemID
, but it seems it contains none of these. Saxon should probably report this nicely rather than crashing, but it's not going to be able to do anything useful with such a Source
object.
这篇关于来自 doc-available 的 Saxon 9、Java、NullPointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!