Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4848172

xsltc throws null ptr exception with xsl:include,uri resolver combination

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 1.2.3
    • Fix Version/s: 1.2.3
    • Component/s: xml
    • Labels:

      Description

      Tested with jaxp jars from jwsdp-1_2-fcs-bin-b10-unix-10_apr_2003.sh,
      detected by astro app. A standalone program was then written to demonstrate
      the problem observed in astro app:

      In the standalone program provided below (ProtoURIResolver.java), a stylesheet
      'ra-uri.xsl' is used that contains a xsl:include to an href that is resolved by
      a uri resolver (also in the java file). The href is supposed to be resolved to another stylesheet, 'toptemplate.xsl'. Using Xalan, the standalone program works fine. Using xsltc (set on commandline with -Djavax.xml.transform.TransformerFactory prop), a null ptr is observed.

      java -Djavax.xml.transform.TransformerFactory=org.apache.xalan.xsltc.trax.TransformerFactoryImpl -cp "." ProtoURIResolver
      templates = null
      java.lang.NullPointerException
              at org.apache.xalan.xsltc.trax.TransformerFactoryImpl.newTransformerHandler(TransformerFactoryImpl.java:739)
              at ProtoURIResolver.run(ProtoURIResolver.java:76)
              at ProtoURIResolver.main(ProtoURIResolver.java:51)

      Furthermore, if the stylesheet 'ra.xsl' is used (which does not use xsl:include) then xsltc works fine. SO the problem is definitely related to xsl:include.

      This was also tested with the latest xsltc at Apache.org and the problem is there as well. There is a bug in bugzilla (#10626) that may be related.

      Here is the standalone program:
      ----------------------------------
      import javax.xml.transform.URIResolver;
      import javax.xml.transform.Source;
      import javax.xml.transform.stream.StreamResult;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.transform.sax.TransformerHandler;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerException;
      import javax.xml.transform.sax.TransformerHandler;
      import javax.xml.transform.sax.SAXTransformerFactory;
      import javax.xml.transform.TransformerConfigurationException;
      import javax.xml.parsers.SAXParserFactory;
      import javax.xml.parsers.ParserConfigurationException;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.XMLReader;
      import javax.xml.transform.sax.TemplatesHandler;
      import javax.xml.transform.TransformerConfigurationException;
      import java.net.MalformedURLException;
      import javax.xml.transform.Templates;
      import java.io.IOException;


      class MyURIResolver implements URIResolver {
          public Source resolve(String href, String base) throws TransformerException
          {
              if (href.equals("http://astro.com/stylesheets/topleveltemplate")){
                  String xsl = "toptemplate.xsl";
                  StreamSource ss = new StreamSource(xsl);
                  String sysId = null;
                  try {
                      sysId = new java.io.File(xsl).toURL().toExternalForm();
                  }
                  catch (java.net.MalformedURLException e){
                      System.err.println(
                          "URL for the toplevel Template stylesheet '" +
                           xsl + "' could not be created.");
                  }
                  ss.setSystemId(sysId);
                  return ss;
              }
              else {
                  return null;
              }
          }
      }


      public class ProtoURIResolver {
         public static void main(String[] args){
              ProtoURIResolver app = new ProtoURIResolver();
              app.run(args);
         }

         public void run(String[] args){
              String inputFilename = "smallcat.xml";
              String stylesheet = "ra-uri.xsl"; // uses uri resolver
              //String stylesheet = "ra.xsl"; // does not use uri resolver

              TransformerFactory factory = TransformerFactory.newInstance();

              try {
                  factory.setURIResolver(new MyURIResolver());
                  SAXTransformerFactory sf = (SAXTransformerFactory)factory;
                  TemplatesHandler templhdlr = sf.newTemplatesHandler();
                  SAXParserFactory pfactory= SAXParserFactory.newInstance();
                  pfactory.setNamespaceAware(true);
                  XMLReader xmlreader = pfactory.newSAXParser().getXMLReader();
                  InputSource xslsrc = new InputSource(stylesheet);
                  String sysId = null;
                  sysId = new java.io.File(stylesheet).toURL().toExternalForm();
                  xslsrc.setSystemId(sysId);
                  xmlreader.setContentHandler(templhdlr);
                  xmlreader.parse(xslsrc);
                  Templates templates = templhdlr.getTemplates();
      System.out.println("templates = " + templates); // is null for xsltc
                  TransformerHandler xfhdlr = sf.newTransformerHandler(templates);
                  Transformer transformer = xfhdlr.getTransformer();
                  transformer.transform(new StreamSource(inputFilename),
                                        new StreamResult(System.out));
              } catch (Exception e) {
                  e.printStackTrace();
              }
              System.exit(0);
         }

         public void usage() {
              System.err.println(
                  "Usage: run <xml_file> <xsl_file>");
              System.exit(1);
         }
      }

      ----------------
      ra-uri.xsl:

      <xsl:transform
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:output method="xml"/>

      <!-- ra between 00:00:00 and 01:00:00 -->
      <xsl:param name="ra_min_hr" select="0.106"/>
      <xsl:param name="ra_max_hr" select="0.108"/>

      <!-- will use the uri resolver to resolve to 'toptemplate.xsl' -->
      <xsl:include href="http://astro.com/stylesheets/topleveltemplate"/>

      <xsl:template match="star">
         <xsl:if test="(
                        (number(ra/dv) >= $ra_min_hr) and
                        (number(ra/dv) <= $ra_max_hr))" >
                <xsl:copy-of select="."/>
         </xsl:if>
      </xsl:template>

      </xsl:transform>

      ------------------------
      toptemplate.xsl:

      <?xml version="1.0"?>

      <xsl:transform
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:template match="stardb">
            <stardb>
            <xsl:apply-templates/>
            </stardb>
      </xsl:template>

      </xsl:transform>

      -------------------------
      ra.xsl:

      <xsl:transform
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:output method="xml"/>

      <!-- ra between 00:00:00 and 01:00:00 -->
      <xsl:param name="ra_min_hr" select="0.106"/>
      <xsl:param name="ra_max_hr" select="0.108"/>

      <xsl:template match="stardb">
            <stardb>
            <xsl:apply-templates/>
            </stardb>
      </xsl:template>

      <xsl:template match="star">
         <xsl:if test="(
                        (number(ra/dv) >= $ra_min_hr) and
                        (number(ra/dv) <= $ra_max_hr))" >
                <xsl:copy-of select="."/>
         </xsl:if>
      </xsl:template>

      </xsl:transform>
      -------------------------

      The xml input file, smallcat.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <stardb><star><hr>1</hr><constellation/><fullname/><ra><h>00</h><m>05</m><s>09.9</s><dv>0.08608333333333333</dv></ra><dec><sgn/><d>45</d><m>13</m><s>45</s><dv>45.22916666666667</dv></dec><glng>114.44</glng><glat>-16.88</glat><vmag>6.70</vmag><spec>A1Vn</spec></star><star><hr>2</hr><constellation/><fullname/><ra><h>00</h><m>05</m><s>03.8</s><dv>0.08438888888888889</dv></ra><dec><sgn>-</sgn><d>00</d><m>30</m><s>11</s><dv>-0.5030555555555556</dv></dec><glng>98.33</glng><glat>-61.14</glat><vmag>6.29</vmag><spec>gG9</spec></star><star><hr>3</hr><constellation>Psc</constellation><fullname>33 Psc</fullname><ra><h>00</h><m>05</m><s>20.1</s><dv>0.08891666666666666</dv></ra><dec><sgn>-</sgn><d>05</d><m>42</m><s>27</s><dv>-5.7075000000000005</dv></dec><glng>93.75</glng><glat>-65.93</glat><vmag>4.61</vmag><spec>K0IIIbCN-0.5</spec></star><star><hr>4</hr><constellation>Peg</constellation><fullname>86 Peg</fullname><ra><h>00</h><m>05</m><s>42.0</s><dv>0.095</dv></ra><dec><sgn/><d>13</d><m>23</m><s>46</s><dv>13.39611111111111</dv></dec><glng>106.19</glng><glat>-47.98</glat><vmag>5.51</vmag><spec>G5III</spec></star><star><hr>5</hr><constellation/><fullname/><ra><h>00</h><m>06</m><s>16.0</s><dv>0.10444444444444445</dv></ra><dec><sgn/><d>58</d><m>26</m><s>12</s><dv>58.43666666666666</dv></dec><glng>117.03</glng><glat>-03.92</glat><vmag>5.96</vmag><spec>G5V</spec></star><star><hr>6</hr><constellation/><fullname/><ra><h>00</h><m>06</m><s>19.0</s><dv>0.10527777777777779</dv></ra><dec><sgn>-</sgn><d>49</d><m>04</m><s>30</s><dv>-49.075</dv></dec><glng>321.61</glng><glat>-66.38</glat><vmag>5.70</vmag><spec>G1IV</spec></star><star><hr>7</hr><constellation>Cas</constellation><fullname>10 Cas</fullname><ra><h>00</h><m>06</m><s>26.5</s><dv>0.10736111111111112</dv></ra><dec><sgn/><d>64</d><m>11</m><s>46</s><dv>64.19611111111111</dv></dec><glng>118.06</glng><glat>1.75</glat><vmag>5.59</vmag><spec>B9III</spec></star><star><hr>8</hr><constellation/><fullname/><ra><h>00</h><m>06</m><s>36.8</s><dv>0.11022222222222222</dv></ra><dec><sgn/><d>29</d><m>01</m><s>17</s><dv>29.02138888888889</dv></dec><glng>111.26</glng><glat>-32.83</glat><vmag>6.13</vmag><spec>K0V</spec></star><star><hr>9</hr><constellation/><fullname/><ra><h>00</h><m>06</m><s>50.1</s><dv>0.11391666666666667</dv></ra><dec><sgn>-</sgn><d>23</d><m>06</m><s>27</s><dv>-23.1075</dv></dec><glng>52.21</glng><glat>-79.14</glat><vmag>6.18</vmag><spec>A7V</spec></star><star><hr>10</hr><constellation/><fullname/><ra><h>00</h><m>07</m><s>18.2</s><dv>0.12172222222222222</dv></ra><dec><sgn>-</sgn><d>17</d><m>23</m><s>11</s><dv>-17.386388888888888</dv></dec><glng>74.36</glng><glat>-75.90</glat><vmag>6.19</vmag><spec>A6Vn</spec></star></stardb>


      -----------
      Expected output (from Xalan):

      java -cp "." ProtoURIResolver
      templates = org.apache.xalan.templates.StylesheetRoot@b8f82d
      <?xml version="1.0" encoding="UTF-8"?>
      <stardb><star><hr>7</hr><constellation>Cas</constellation><fullname>10 Cas</fullname><ra><h>00</h><m>06</m><s>26.5</s><dv>0.10736111111111112</dv></ra><dec><sgn/><d>64</d><m>11</m><s>46</s><dv>64.19611111111111</dv></dec><glng>118.06</glng><glat>1.75</glat><vmag>5.59</vmag><spec>B9III</spec></star></stardb>


      ###@###.### 2003-04-15

        Attachments

          Activity

            People

            • Assignee:
              ayadavsunw Arun Yadav (Inactive)
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: