You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Jason Gilbreath <jg...@avolent.com> on 2002/08/29 01:58:46 UTC

XSLTC compilation error using Java Extensions

hello all,

I am getting a NPE while trying to compile a stylesheet -

here is the error

java.lang.NullPointerException
	at
org.apache.xalan.xsltc.compiler.util.SlotAllocator.allocateSlot(SlotAllocato
r.java:88)
	at
org.apache.xalan.xsltc.compiler.util.MethodGenerator.addLocalVariable2(Metho
dGenerator.java:197)
	at
org.apache.xalan.xsltc.compiler.VariableBase.mapRegister(VariableBase.java:1
51)
	at
org.apache.xalan.xsltc.compiler.Variable.translate(Variable.java:233)
	at
org.apache.xalan.xsltc.compiler.SyntaxTreeNode.translateContents(SyntaxTreeN
ode.java:533)
	at org.apache.xalan.xsltc.compiler.Choose.translate(Choose.java:169)
	at
org.apache.xalan.xsltc.compiler.SyntaxTreeNode.translateContents(SyntaxTreeN
ode.java:533)
	at
org.apache.xalan.xsltc.compiler.Template.translate(Template.java:366)
	at
org.apache.xalan.xsltc.compiler.TopLevelElement.compile(TopLevelElement.java
:96)
	at
org.apache.xalan.xsltc.compiler.Mode.compileNamedTemplate(Mode.java:436)
	at
org.apache.xalan.xsltc.compiler.Mode.compileTemplates(Mode.java:453)
	at
org.apache.xalan.xsltc.compiler.Mode.compileApplyTemplates(Mode.java:718)
	at
org.apache.xalan.xsltc.compiler.Stylesheet.compileModes(Stylesheet.java:441)
	at
org.apache.xalan.xsltc.compiler.Stylesheet.translate(Stylesheet.java:542)
	at org.apache.xalan.xsltc.compiler.XSLTC.compile(XSLTC.java:336)
	at org.apache.xalan.xsltc.compiler.XSLTC.compile(XSLTC.java:244)
	at org.apache.xalan.xsltc.compiler.XSLTC.compile(XSLTC.java:370)
	at org.apache.xalan.xsltc.cmdline.Compile.main(Compile.java:184)
Compiler warning(s): 
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedDateByPreferencesWithLeadingZeros'
.  
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedDateByPreferences'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedDateByPreferences'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedShortDateByPreferencesWithLeadingZ
eros'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedShortDateByPreferences'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedNumber'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedNumber'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedNumber'.
  file:/Uil.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedNumber'.
  file:/Util.xsl: Unable to resolve call to function
'http://xml.apache.org/xslt/java:formattedNumber'.
Compiler error(s): 
  null


All of these calls are to an Extensions class that has the methods above
defined in the XSL similar to this

 <xsl:template name="formattedDate">
        <xsl:param name="date"/>
        <xsl:param name="leadingZero"/>

        <!-- zero padding -->
        <xsl:choose>
          <xsl:when test="$leadingZero = 'yes' or $leadingZero = 'true' or
$leadingZero = 'on'">
            <xsl:value-of
select="java:com.foo.Extensions.formattedDateByPreferencesWithLeadingZeros($
date/yyyy, $date/M, $date/d, $PREFERRED_DATE_FORMAT)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of
select="java:com.foo.Extensions.formattedDateByPreferences($date/yyyy,
$date/M, $date/d, $PREFERRED_DATE_FORMAT)"/>     
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

or

 <xsl:template name="formattedNumber">
     <xsl:param name="amount"/>
     <xsl:param name="numberFormat"/>
     <xsl:param name="decimalFormat"/>
     
     <xsl:value-of
select="java:com.foo.Extensions.formattedNumber(number($amount),
$numberFormat, $decimalFormat)"/>    
   </xsl:template>


the Extensions class then has methods defined like so:

public static String  formattedDateByPreferences(String year, String month,
String day, String format) {
        StringBuffer aBuffer = new StringBuffer(10) ;

        if(format.equals("mm/dd/yyyy")) {
            aBuffer.append(month) ;
            aBuffer.append("/") ;
            aBuffer.append(day) ;
            aBuffer.append("/") ;
            aBuffer.append(year) ;
        } else if(format.equals("dd/mm/yyyy")) {
            aBuffer.append(day) ;
            aBuffer.append("/") ;
            aBuffer.append(month) ;
            aBuffer.append("/") ;
            aBuffer.append(year) ;
        } else if(format.equals("yyyy/mm/dd")) {
            aBuffer.append(year) ;
            aBuffer.append("/") ;
            aBuffer.append(month) ;
            aBuffer.append("/") ;
            aBuffer.append(day) ;
        } else {
            aBuffer.append(day) ;
            aBuffer.append("/") ;
            aBuffer.append(month) ;
            aBuffer.append("/") ;
            aBuffer.append(year) ;
        }  

        return aBuffer.toString() ;
    }

and

      public static String formattedNumber(Double amount, String
numberPattern, String decimalFormat) {
            DecimalFormat fmt = new DecimalFormat(numberPattern) ;
            if (amount.isNaN())  {        //If number is really really
small, like -5.6843E-14
                amount = new Double(0.0);
            }
            return fmt.format(amount) ;
    }


so - why the error?  This stylesheet works with xalan just fine - it just
will not compile using XSLTC
any help is appreciated


Re: XSLTC compilation error using Java Extensions

Posted by Santiago Pericas-Geertsen <Sa...@sun.com>.
>
>  <xsl:template name="formattedNumber">
>      <xsl:param name="amount"/>
>      <xsl:param name="numberFormat"/>
>      <xsl:param name="decimalFormat"/>
>
>      <xsl:value-of
> select="java:com.foo.Extensions.formattedNumber(number($amount),
> $numberFormat, $decimalFormat)"/>
>    </xsl:template>
>

1) The type of a parameter cannot be detected at compile time in XSLT. Thus,
for XSLTC to generate the appropriate call when invoking an external Java
function, you often need to use explicit casts. For example, in the template
above you should use 'string($numberFormat)' and 'string($decimalFormat)'.
This additional info is required *only* for parameters, since the type of a
variable can always be inferred statically.

2) Additionally, you need to ensure that the class defining the static
methods is accessible from your class path.

3) What version of XSLTC are you using? I think we have fixed the NPE that
you're getting in the current version.

--
Santiago Pericas-Geertsen
Sun Microsystems