KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemExsltFunction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ElemExsltFunction.java,v 1.9 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.extensions.ExtensionNamespaceSupport;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xpath.VariableStack;
26 import org.apache.xpath.XPathContext;
27 import org.apache.xpath.objects.XObject;
28
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32
33 /**
34  * Implement func:function.
35  * @xsl.usage advanced
36  */

37 public class ElemExsltFunction extends ElemTemplate
38 {
39   /**
40    * Get an integer representation of the element type.
41    *
42    * @return An integer representation of the element, defined in the
43    * Constants class.
44    * @see org.apache.xalan.templates.Constants
45    */

46   public int getXSLToken()
47   {
48     return Constants.EXSLT_ELEMNAME_FUNCTION;
49   }
50
51    /**
52    * Return the node name, defined in the
53    * Constants class.
54    * @see org.apache.xalan.templates.Constants.
55    * @return The node name
56    *
57    */

58   public String JavaDoc getNodeName()
59   {
60     return Constants.EXSLT_ELEMNAME_FUNCTION_STRING;
61   }
62   
63   public void execute(TransformerImpl transformer, XObject[] args)
64           throws TransformerException JavaDoc
65   {
66     XPathContext xctxt = transformer.getXPathContext();
67     VariableStack vars = xctxt.getVarStack();
68     
69     // Increment the frame bottom of the variable stack by the
70
// frame size
71
int thisFrame = vars.getStackFrame();
72     int nextFrame = vars.link(m_frameSize);
73
74     if (m_inArgsSize < args.length) {
75       throw new TransformerException JavaDoc ("function called with too many args");
76     }
77     
78     // Set parameters,
79
// have to clear the section of the stack frame that has params.
80
if (m_inArgsSize > 0) {
81       vars.clearLocalSlots(0, m_inArgsSize);
82
83       if (args.length > 0) {
84         vars.setStackFrame(thisFrame);
85         NodeList JavaDoc children = this.getChildNodes();
86         
87         for (int i = 0; i < args.length; i ++) {
88           Node JavaDoc child = children.item(i);
89           if (children.item(i) instanceof ElemParam) {
90             ElemParam param = (ElemParam)children.item(i);
91             vars.setLocalVariable(param.getIndex(), args[i], nextFrame);
92           }
93         }
94         
95         vars.setStackFrame(nextFrame);
96       }
97     }
98
99     // Removed ElemTemplate 'push' and 'pop' of RTFContext, in order to avoid losing the RTF context
100
// before a value can be returned. ElemExsltFunction operates in the scope of the template that called
101
// the function.
102
// xctxt.pushRTFContext();
103

104     if (TransformerImpl.S_DEBUG)
105       transformer.getTraceManager().fireTraceEvent(this);
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     if (TransformerImpl.S_DEBUG)
114       transformer.getTraceManager().fireTraceEndEvent(this);
115
116     // Following ElemTemplate 'pop' removed -- see above.
117
// xctxt.popRTFContext();
118

119   }
120   
121   /**
122    * Called after everything else has been
123    * recomposed, and allows the function to set remaining
124    * values that may be based on some other property that
125    * depends on recomposition.
126    */

127   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
128   {
129     super.compose(sroot);
130     
131     // Register the function namespace (if not already registered).
132
String JavaDoc namespace = getName().getNamespace();
133     String JavaDoc handlerClass = "org.apache.xalan.extensions.ExtensionHandlerExsltFunction";
134     Object JavaDoc[] args ={namespace, sroot};
135     ExtensionNamespaceSupport extNsSpt =
136                          new ExtensionNamespaceSupport(namespace, handlerClass, args);
137     sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
138     // Make sure there is a handler for the EXSLT functions namespace
139
// -- for isElementAvailable().
140
if (!(namespace.equals(Constants.S_EXSLT_FUNCTIONS_URL)))
141     {
142       namespace = Constants.S_EXSLT_FUNCTIONS_URL;
143       args = new Object JavaDoc[]{namespace, sroot};
144       extNsSpt = new ExtensionNamespaceSupport(namespace, handlerClass, args);
145       sroot.getExtensionNamespacesManager().registerExtension(extNsSpt);
146     }
147   }
148 }
149
Popular Tags