KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > extensions > ExtensionHandlerExsltFunction


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: ExtensionHandlerExsltFunction.java,v 1.8 2004/02/11 20:43:48 zongaro Exp $
18  */

19 package org.apache.xalan.extensions;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import javax.xml.transform.TransformerException JavaDoc;
25
26 import org.apache.xalan.res.XSLMessages;
27 import org.apache.xalan.res.XSLTErrorResources;
28 import org.apache.xalan.templates.Constants;
29 import org.apache.xalan.templates.ElemExsltFuncResult;
30 import org.apache.xalan.templates.ElemExsltFunction;
31 import org.apache.xalan.templates.ElemTemplate;
32 import org.apache.xalan.templates.ElemTemplateElement;
33 import org.apache.xalan.templates.Stylesheet;
34 import org.apache.xalan.templates.StylesheetRoot;
35 import org.apache.xalan.transformer.TransformerImpl;
36 import org.apache.xml.utils.QName;
37 import org.apache.xpath.ExpressionNode;
38 import org.apache.xpath.XPathContext;
39 import org.apache.xpath.functions.FuncExtFunction;
40 import org.apache.xpath.objects.XObject;
41 import org.apache.xpath.objects.XString;
42
43 /**
44  * Execute EXSLT functions, determine the availability of EXSLT functions, and the
45  * availability of an EXSLT result element.
46  */

47 public class ExtensionHandlerExsltFunction extends ExtensionHandler
48 {
49   private String JavaDoc m_namespace;
50   private StylesheetRoot m_stylesheet;
51   private static final QName RESULTQNAME =
52                   new QName(Constants.S_EXSLT_FUNCTIONS_URL,
53                             Constants.EXSLT_ELEMNAME_FUNCRESULT_STRING);
54   /**
55    * Constructor called from ElemExsltFunction runtimeInit().
56    */

57   public ExtensionHandlerExsltFunction(String JavaDoc ns, StylesheetRoot stylesheet)
58   {
59     super(ns, "xml"); // required by ExtensionHandler interface.
60
m_namespace = ns;
61     m_stylesheet = stylesheet;
62   }
63   
64   /**
65    * Required by ExtensionHandler (an abstract method). No-op.
66    */

67   public void processElement(
68     String JavaDoc localPart, ElemTemplateElement element, TransformerImpl transformer,
69     Stylesheet stylesheetTree, Object JavaDoc methodKey) throws TransformerException JavaDoc, IOException JavaDoc
70   {}
71   
72   /**
73    * Get the ElemExsltFunction element associated with the
74    * function.
75    *
76    * @param funcName Local name of the function.
77    * @return the ElemExsltFunction element associated with
78    * the function, null if none exists.
79    */

80   public ElemExsltFunction getFunction(String JavaDoc funcName)
81   {
82     QName qname = new QName(m_namespace, funcName);
83     ElemTemplate templ = m_stylesheet.getTemplateComposed(qname);
84     if (templ != null && templ instanceof ElemExsltFunction)
85       return (ElemExsltFunction) templ;
86     else
87       return null;
88   }
89   
90   
91   /**
92    * Does the EXSLT function exist?
93    *
94    * @param funcName Local name of the function.
95    * @return true if the function exists.
96    */

97   public boolean isFunctionAvailable(String JavaDoc funcName)
98   {
99     return getFunction(funcName)!= null;
100   }
101     
102    /** If an element-available() call applies to an EXSLT result element within
103    * an EXSLT function element, return true.
104    *
105    * Note: The EXSLT function element is a template-level element, and
106    * element-available() returns false for it.
107    *
108    * @param Local name of the function.
109    * @return true if the function is available.
110    */

111   public boolean isElementAvailable(String JavaDoc elemName)
112   {
113     if (!(new QName(m_namespace, elemName).equals(RESULTQNAME)))
114     {
115       return false;
116     }
117     else
118     {
119       ElemTemplateElement elem = m_stylesheet.getFirstChildElem();
120       while (elem != null && elem != m_stylesheet)
121       {
122         if (elem instanceof ElemExsltFuncResult && ancestorIsFunction(elem))
123           return true;
124         ElemTemplateElement nextElem = elem.getFirstChildElem();
125         if (nextElem == null)
126           nextElem = elem.getNextSiblingElem();
127         if (nextElem == null)
128           nextElem = elem.getParentElem();
129         elem = nextElem;
130       }
131     }
132     return false;
133   }
134
135   /**
136    * Determine whether the func:result element is within a func:function element.
137    * If not, it is illegal.
138    */

139   private boolean ancestorIsFunction(ElemTemplateElement child)
140   {
141     while (child.getParentElem() != null
142            && !(child.getParentElem() instanceof StylesheetRoot))
143     {
144       if (child.getParentElem() instanceof ElemExsltFunction)
145         return true;
146       child = child.getParentElem();
147     }
148     return false;
149   }
150
151   /**
152    * Execute the EXSLT function and return the result value.
153    *
154    * @param funcName Name of the EXSLT function.
155    * @param args The arguments of the function call.
156    * @param methodKey Not used.
157    * @param exprContext Used to get the XPathContext.
158    * @return the return value of the function evaluation.
159    * @throws TransformerException
160    */

161   public Object JavaDoc callFunction(
162       String JavaDoc funcName, Vector JavaDoc args, Object JavaDoc methodKey,
163       ExpressionContext exprContext) throws TransformerException JavaDoc
164   {
165     throw new TransformerException JavaDoc("This method should not be called.");
166   }
167
168   /**
169    * Execute the EXSLT function and return the result value.
170    *
171    * @param extFunction The XPath extension function
172    * @param args The arguments of the function call.
173    * @param exprContext The context in which this expression is being executed.
174    * @return the return value of the function evaluation.
175    * @throws TransformerException
176    */

177   public Object JavaDoc callFunction(FuncExtFunction extFunction,
178                              Vector JavaDoc args,
179                              ExpressionContext exprContext)
180       throws TransformerException JavaDoc
181   {
182     // Find the template which invokes this EXSLT function.
183
ExpressionNode parent = extFunction.exprGetParent();
184     while (parent != null && !(parent instanceof ElemTemplate))
185     {
186       parent = parent.exprGetParent();
187     }
188     
189     ElemTemplate callerTemplate = (parent != null) ? (ElemTemplate)parent: null;
190     
191     XObject[] methodArgs;
192     methodArgs = new XObject[args.size()];
193     try
194     {
195       for (int i = 0; i < methodArgs.length; i++)
196       {
197         methodArgs[i] = XObject.create(args.elementAt(i));
198       }
199       
200       ElemExsltFunction elemFunc = getFunction(extFunction.getFunctionName());
201       
202       if (null != elemFunc) {
203         XPathContext context = exprContext.getXPathContext();
204         TransformerImpl transformer = (TransformerImpl)context.getOwnerObject();
205         transformer.pushCurrentFuncResult(null);
206
207         elemFunc.execute(transformer, methodArgs);
208
209         XObject val = (XObject)transformer.popCurrentFuncResult();
210         return (val == null) ? new XString("") // value if no result element.
211
: val;
212       }
213       else {
214         throw new TransformerException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_FUNCTION_NOT_FOUND, new Object JavaDoc[] {extFunction.getFunctionName()}));
215       }
216     }
217     catch (TransformerException JavaDoc e)
218     {
219       throw e;
220     }
221     catch (Exception JavaDoc e)
222     {
223       throw new TransformerException JavaDoc(e);
224     }
225   }
226   
227 }
228
Popular Tags