Home | History | Annotate | Download | only in templates
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: ElemExsltFunction.java 468643 2006-10-28 06:56:03Z minchau $
     20  */
     21 package org.apache.xalan.templates;
     22 
     23 import javax.xml.transform.TransformerException;
     24 
     25 import org.apache.xalan.extensions.ExtensionNamespaceSupport;
     26 import org.apache.xalan.transformer.TransformerImpl;
     27 import org.apache.xpath.VariableStack;
     28 import org.apache.xpath.XPathContext;
     29 import org.apache.xpath.objects.XObject;
     30 
     31 import org.w3c.dom.Node;
     32 import org.w3c.dom.NodeList;
     33 
     34 
     35 /**
     36  * Implement func:function.
     37  * @xsl.usage advanced
     38  */
     39 public class ElemExsltFunction extends ElemTemplate
     40 {
     41     static final long serialVersionUID = 272154954793534771L;
     42   /**
     43    * Get an integer representation of the element type.
     44    *
     45    * @return An integer representation of the element, defined in the
     46    *     Constants class.
     47    * @see org.apache.xalan.templates.Constants
     48    */
     49   public int getXSLToken()
     50   {
     51     return Constants.EXSLT_ELEMNAME_FUNCTION;
     52   }
     53 
     54    /**
     55    * Return the node name, defined in the
     56    *     Constants class.
     57    * @see org.apache.xalan.templates.Constants
     58    * @return The node name
     59    *
     60    */
     61   public String getNodeName()
     62   {
     63     return Constants.EXSLT_ELEMNAME_FUNCTION_STRING;
     64   }
     65 
     66   public void execute(TransformerImpl transformer, XObject[] args)
     67           throws TransformerException
     68   {
     69     XPathContext xctxt = transformer.getXPathContext();
     70     VariableStack vars = xctxt.getVarStack();
     71 
     72     // Increment the frame bottom of the variable stack by the
     73     // frame size
     74     int thisFrame = vars.getStackFrame();
     75     int nextFrame = vars.link(m_frameSize);
     76 
     77     if (m_inArgsSize < args.length) {
     78       throw new TransformerException ("function called with too many args");
     79     }
     80 
     81     // Set parameters,
     82     // have to clear the section of the stack frame that has params.
     83     if (m_inArgsSize > 0) {
     84       vars.clearLocalSlots(0, m_inArgsSize);
     85 
     86       if (args.length > 0) {
     87         vars.setStackFrame(thisFrame);
     88         NodeList children = this.getChildNodes();
     89 
     90         for (int i = 0; i < args.length; i ++) {
     91           Node child = children.item(i);
     92           if (children.item(i) instanceof ElemParam) {
     93             ElemParam param = (ElemParam)children.item(i);
     94             vars.setLocalVariable(param.getIndex(), args[i], nextFrame);
     95           }
     96         }
     97 
     98         vars.setStackFrame(nextFrame);
     99       }
    100     }
    101 
    102     //  Removed ElemTemplate 'push' and 'pop' of RTFContext, in order to avoid losing the RTF context
    103     //  before a value can be returned. ElemExsltFunction operates in the scope of the template that called
    104     //  the function.
    105     //  xctxt.pushRTFContext();
    106 
    107     vars.setStackFrame(nextFrame);
    108     transformer.executeChildTemplates(this, true);
    109 
    110     // Reset the stack frame after the function call
    111     vars.unlink(thisFrame);
    112 
    113     // Following ElemTemplate 'pop' removed -- see above.
    114     // xctxt.popRTFContext();
    115 
    116   }
    117 
    118   /**
    119    * Called after everything else has been
    120    * recomposed, and allows the function to set remaining
    121    * values that may be based on some other property that
    122    * depends on recomposition.
    123    */
    124   public void compose(StylesheetRoot sroot) throws TransformerException
    125   {
    126     super.compose(sroot);
    127 
    128     // Register the function namespace (if not already registered).
    129     String namespace = getName().getNamespace();
    130     String handlerClass = sroot.getExtensionHandlerClass();
    131     Object[] args ={namespace, sroot};
    132     ExtensionNamespaceSupport extNsSpt =
    133                          new ExtensionNamespaceSupport(namespace, handlerClass, args);
    134     sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
    135     // Make sure there is a handler for the EXSLT functions namespace
    136     // -- for isElementAvailable().
    137     if (!(namespace.equals(Constants.S_EXSLT_FUNCTIONS_URL)))
    138     {
    139       namespace = Constants.S_EXSLT_FUNCTIONS_URL;
    140       args = new Object[]{namespace, sroot};
    141       extNsSpt = new ExtensionNamespaceSupport(namespace, handlerClass, args);
    142       sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
    143     }
    144   }
    145 }
    146