KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > SAXONFunction


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.tree.NodeImpl;
4 import com.icl.saxon.Context;
5 import com.icl.saxon.Controller;
6 import com.icl.saxon.Bindery;
7 import com.icl.saxon.ParameterSet;
8 import com.icl.saxon.om.NamespaceException;
9 import com.icl.saxon.om.NodeInfo;
10 import com.icl.saxon.om.NamePool;
11 import com.icl.saxon.output.Outputter;
12 import com.icl.saxon.output.ErrorEmitter;
13 import com.icl.saxon.expr.Value;
14 import com.icl.saxon.expr.StringValue;
15 import com.icl.saxon.trace.TraceListener; // e.g.
16
import javax.xml.transform.TransformerException JavaDoc;
17 import javax.xml.transform.TransformerConfigurationException JavaDoc;
18
19 /**
20 * Handler for saxon:function and exslt:function elements in stylesheet. <BR>
21 * Attributes: <br>
22 * name gives the name of the function
23 */

24
25 public class SAXONFunction extends StyleElement {
26
27     int functionFingerprint = -1;
28     Procedure procedure = new Procedure();
29
30     /**
31     * Process the [xsl:]extension-element-prefixes attribute.
32     * This overrides the standard method because saxon:function and exslt:function
33     * implicitly declare saxon/exslt (respectively) as an extension namespace.
34     * @param nc the name code of the attribute required (ignored)
35     */

36
37     protected void processExtensionElementAttribute(int nc)
38     throws TransformerConfigurationException JavaDoc {
39         extensionNamespaces = new short[1];
40         NamePool pool = getNamePool();
41         short uriCode = pool.getURICode(getNameCode());
42         extensionNamespaces[0] = uriCode;
43     }
44     
45     public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
46
47         StandardNames sn = getStandardNames();
48         AttributeCollection atts = getAttributeList();
49
50         String JavaDoc nameAtt = null;
51                 
52         for (int a=0; a<atts.getLength(); a++) {
53             int nc = atts.getNameCode(a);
54             int f = nc & 0xfffff;
55             if (f==sn.NAME) {
56                 nameAtt = atts.getValue(a);
57                 if (nameAtt.indexOf(':')<0) {
58                     compileError("Function name must have a namespace prefix");
59                 }
60                 try {
61                     int functionCode = makeNameCode(nameAtt, false);
62                     functionFingerprint = functionCode & 0xfffff;
63                 } catch (NamespaceException err) {
64                     compileError(err.getMessage());
65                 }
66             } else {
67                 checkUnknownAttribute(nc);
68             }
69         }
70         
71         if (nameAtt==null) {
72             reportAbsence("name");
73         }
74     }
75
76     /**
77     * Determine whether this type of element is allowed to contain a template-body
78     * @return true: yes, it may contain a template-body
79     */

80
81     public boolean mayContainTemplateBody() {
82         return true;
83     }
84
85     public void validate() throws TransformerConfigurationException JavaDoc {
86         checkTopLevel();
87     }
88
89     public void preprocess() throws TransformerConfigurationException JavaDoc {
90         getPrincipalStyleSheet().allocateLocalSlots(procedure.getNumberOfVariables());
91     }
92
93     public void process(Context context) {}
94
95     /**
96     * Get associated Procedure (for details of stack frame)
97     */

98
99     public Procedure getProcedure() {
100         return procedure;
101     }
102
103     public int getFunctionFingerprint() {
104         if (functionFingerprint==-1) {
105             // this is a forwards reference to the function
106
try {
107                 prepareAttributes();
108             } catch (TransformerConfigurationException JavaDoc err) {
109                 return -1; // we'll report the error later
110
}
111         }
112         return functionFingerprint;
113     }
114
115     /**
116     * Get the name fingerprint of the n'th parameter (starting from 0).
117     * Return -1 if there is none such.
118     */

119
120     public int getNthParameter(int n) {
121         NodeImpl node = (NodeImpl)getFirstChild();
122         int pos = 0;
123         while (node!=null) {
124             if (node instanceof XSLParam) {
125                 if (pos==n) {
126                     return ((XSLParam)node).getVariableFingerprint();
127                 } else {
128                     pos++;
129                 }
130             }
131             node = (NodeImpl)node.getNextSibling();
132         }
133         return -1;
134     }
135             
136     /**
137     * Call this function
138     */

139
140     public Value call(ParameterSet params, Context context) throws TransformerException JavaDoc {
141         Bindery bindery = context.getBindery();
142         bindery.openStackFrame(params);
143         Controller controller = context.getController();
144         Outputter old = controller.getOutputter();
145         controller.changeOutputDestination(null, new ErrorEmitter());
146
147         if (controller.isTracing()) { // e.g.
148
TraceListener listener = controller.getTraceListener();
149             listener.enter(this, context);
150             processChildren(context);
151             listener.leave(this, context);
152         } else {
153             processChildren(context);
154         }
155
156         controller.resetOutputDestination(old);
157         bindery.closeStackFrame();
158         Value result = context.getReturnValue();
159         if (result==null) {
160             result = new StringValue("");
161         }
162         context.setReturnValue(null);
163         return result;
164     }
165
166 }
167
168 //
169
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
170
// you may not use this file except in compliance with the License. You may obtain a copy of the
171
// License at http://www.mozilla.org/MPL/
172
//
173
// Software distributed under the License is distributed on an "AS IS" basis,
174
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
175
// See the License for the specific language governing rights and limitations under the License.
176
//
177
// The Original Code is: all this file.
178
//
179
// The Initial Developer of the Original Code is
180
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
181
//
182
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
183
//
184
// Contributor(s):
185
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
186
//
187
Popular Tags