Saxon 9, Java, NullPointer from doc-available

蹲街弑〆低调 提交于 2020-04-18 12:35:46

问题


This is an error seen after fixing the issue in Saxon 9 HE, Java - Static errors, XTSE0210, XTSE0165, XPST0017

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]

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 ()"/>

The same XSLT works from the command line, as in Saxon 9 HE, Java - Static errors, XTSE0210, XTSE0165, XPST0017


回答1:


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 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, "");

and this crashes out because uri is null.

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.



来源:https://stackoverflow.com/questions/60675550/saxon-9-java-nullpointer-from-doc-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!