KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > util > ResultTreeType


1 /*
2  * Copyright 2001-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: ResultTreeType.java,v 1.21 2004/02/16 22:26:44 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler.util;
21
22 import org.apache.bcel.generic.ALOAD;
23 import org.apache.bcel.generic.ASTORE;
24 import org.apache.bcel.generic.CHECKCAST;
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.GETFIELD;
27 import org.apache.bcel.generic.IFEQ;
28 import org.apache.bcel.generic.INVOKEINTERFACE;
29 import org.apache.bcel.generic.INVOKESPECIAL;
30 import org.apache.bcel.generic.INVOKEVIRTUAL;
31 import org.apache.bcel.generic.Instruction;
32 import org.apache.bcel.generic.InstructionList;
33 import org.apache.bcel.generic.LocalVariableGen;
34 import org.apache.bcel.generic.NEW;
35 import org.apache.bcel.generic.PUSH;
36 import org.apache.xalan.xsltc.compiler.Constants;
37 import org.apache.xalan.xsltc.compiler.FlowList;
38
39 /**
40  * @author Jacek Ambroziak
41  * @author Santiago Pericas-Geertsen
42  * @author Morten Jorgensen
43  */

44 public final class ResultTreeType extends Type {
45     private final String JavaDoc _methodName;
46
47     protected ResultTreeType() {
48     _methodName = null;
49     }
50
51     public ResultTreeType(String JavaDoc methodName) {
52     _methodName = methodName;
53     }
54
55     public String JavaDoc toString() {
56     return "result-tree";
57     }
58
59     public boolean identicalTo(Type other) {
60     return (other instanceof ResultTreeType);
61     }
62
63     public String JavaDoc toSignature() {
64     return DOM_INTF_SIG;
65     }
66
67     public org.apache.bcel.generic.Type toJCType() {
68     return Util.getJCRefType(toSignature());
69     }
70
71     public String JavaDoc getMethodName() {
72     return _methodName;
73     }
74
75     public boolean implementedAsMethod() {
76     return _methodName != null;
77     }
78
79     /**
80      * Translates a result tree to object of internal type <code>type</code>.
81      * The translation to int is undefined since result trees
82      * are always converted to reals in arithmetic expressions.
83      *
84      * @param classGen A BCEL class generator
85      * @param methodGen A BCEL method generator
86      * @param type An instance of the type to translate the result tree to
87      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
88      */

89     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
90                 Type type) {
91     if (type == Type.String) {
92         translateTo(classGen, methodGen, (StringType)type);
93     }
94     else if (type == Type.Boolean) {
95         translateTo(classGen, methodGen, (BooleanType)type);
96     }
97     else if (type == Type.Real) {
98         translateTo(classGen, methodGen, (RealType)type);
99     }
100     else if (type == Type.NodeSet) {
101         translateTo(classGen, methodGen, (NodeSetType)type);
102     }
103     else if (type == Type.Reference) {
104         translateTo(classGen, methodGen, (ReferenceType)type);
105     }
106     else if (type == Type.Object) {
107         translateTo(classGen, methodGen, (ObjectType) type);
108     }
109     else {
110         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
111                     toString(), type.toString());
112         classGen.getParser().reportError(Constants.FATAL, err);
113     }
114     }
115
116     /**
117      * Expects an result tree on the stack and pushes a boolean.
118      * Translates a result tree to a boolean by first converting it to string.
119      *
120      * @param classGen A BCEL class generator
121      * @param methodGen A BCEL method generator
122      * @param type An instance of BooleanType (any)
123      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
124      */

125     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
126                 BooleanType type) {
127     // A result tree is always 'true' when converted to a boolean value,
128
// since the tree always has at least one node (the root).
129
final ConstantPoolGen cpg = classGen.getConstantPool();
130     final InstructionList il = methodGen.getInstructionList();
131     il.append(POP); // don't need the DOM reference
132
il.append(ICONST_1); // push 'true' on the stack
133
}
134
135     /**
136      * Expects an result tree on the stack and pushes a string.
137      *
138      * @param classGen A BCEL class generator
139      * @param methodGen A BCEL method generator
140      * @param type An instance of StringType (any)
141      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
142      */

143     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
144                 StringType type) {
145     final ConstantPoolGen cpg = classGen.getConstantPool();
146     final InstructionList il = methodGen.getInstructionList();
147     
148     if (_methodName == null) {
149         int index = cpg.addInterfaceMethodref(DOM_INTF,
150                           "getStringValue",
151                           "()"+STRING_SIG);
152         il.append(new INVOKEINTERFACE(index, 1));
153     }
154     else {
155         final String JavaDoc className = classGen.getClassName();
156         final int current = methodGen.getLocalIndex("current");
157         
158         // Push required parameters
159
il.append(classGen.loadTranslet());
160         if (classGen.isExternal()) {
161         il.append(new CHECKCAST(cpg.addClass(className)));
162         }
163         il.append(DUP);
164         il.append(new GETFIELD(cpg.addFieldref(className, "_dom",
165                            DOM_INTF_SIG)));
166
167         // Create a new instance of a StringValueHandler
168
int index = cpg.addMethodref(STRING_VALUE_HANDLER, "<init>", "()V");
169         il.append(new NEW(cpg.addClass(STRING_VALUE_HANDLER)));
170         il.append(DUP);
171         il.append(DUP);
172         il.append(new INVOKESPECIAL(index));
173         
174         // Store new Handler into a local variable
175
final LocalVariableGen handler =
176         methodGen.addLocalVariable("rt_to_string_handler",
177                        Util.getJCRefType(STRING_VALUE_HANDLER_SIG),
178                        null, null);
179         il.append(new ASTORE(handler.getIndex()));
180
181         // Call the method that implements this result tree
182
index = cpg.addMethodref(className, _methodName,
183                      "("+DOM_INTF_SIG+TRANSLET_OUTPUT_SIG+")V");
184         il.append(new INVOKEVIRTUAL(index));
185         
186         // Restore new handler and call getValue()
187
il.append(new ALOAD(handler.getIndex()));
188         index = cpg.addMethodref(STRING_VALUE_HANDLER,
189                      "getValue",
190                      "()" + STRING_SIG);
191         il.append(new INVOKEVIRTUAL(index));
192     }
193     }
194
195     /**
196      * Expects an result tree on the stack and pushes a real.
197      * Translates a result tree into a real by first converting it to string.
198      *
199      * @param classGen A BCEL class generator
200      * @param methodGen A BCEL method generator
201      * @param type An instance of RealType (any)
202      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
203      */

204     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
205                 RealType type) {
206     translateTo(classGen, methodGen, Type.String);
207     Type.String.translateTo(classGen, methodGen, Type.Real);
208     }
209
210     /**
211      * Expects a result tree on the stack and pushes a boxed result tree.
212      * Result trees are already boxed so the translation is just a NOP.
213      *
214      * @param classGen A BCEL class generator
215      * @param methodGen A BCEL method generator
216      * @param type An instance of ReferenceType (any)
217      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
218      */

219     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
220                 ReferenceType type) {
221     final ConstantPoolGen cpg = classGen.getConstantPool();
222     final InstructionList il = methodGen.getInstructionList();
223
224     if (_methodName == null) {
225         il.append(NOP);
226     }
227     else {
228         LocalVariableGen domBuilder, newDom;
229         final String JavaDoc className = classGen.getClassName();
230         final int current = methodGen.getLocalIndex("current");
231
232         // Push required parameters
233
il.append(classGen.loadTranslet());
234         if (classGen.isExternal()) {
235         il.append(new CHECKCAST(cpg.addClass(className)));
236         }
237         il.append(methodGen.loadDOM());
238
239         // Create new instance of DOM class (with RTF_INITIAL_SIZE nodes)
240
il.append(methodGen.loadDOM());
241         int index = cpg.addInterfaceMethodref(DOM_INTF,
242                  "getResultTreeFrag",
243                  "(IZ)" + DOM_INTF_SIG);
244         il.append(new PUSH(cpg, RTF_INITIAL_SIZE));
245         il.append(new PUSH(cpg, false));
246         il.append(new INVOKEINTERFACE(index,3));
247         il.append(DUP);
248         
249         // Store new DOM into a local variable
250
newDom = methodGen.addLocalVariable("rt_to_reference_dom",
251                         Util.getJCRefType(DOM_INTF_SIG),
252                         null, null);
253         il.append(new CHECKCAST(cpg.addClass(DOM_INTF_SIG)));
254         il.append(new ASTORE(newDom.getIndex()));
255
256         // Overwrite old handler with DOM handler
257
index = cpg.addInterfaceMethodref(DOM_INTF,
258                  "getOutputDomBuilder",
259                  "()" + TRANSLET_OUTPUT_SIG);
260
261         il.append(new INVOKEINTERFACE(index,1));
262         //index = cpg.addMethodref(DOM_IMPL,
263
// "getOutputDomBuilder",
264
// "()" + TRANSLET_OUTPUT_SIG);
265
//il.append(new INVOKEVIRTUAL(index));
266
il.append(DUP);
267         il.append(DUP);
268
269         // Store DOM handler in a local in order to call endDocument()
270
domBuilder =
271         methodGen.addLocalVariable("rt_to_reference_handler",
272                        Util.getJCRefType(TRANSLET_OUTPUT_SIG),
273                        null, null);
274         il.append(new ASTORE(domBuilder.getIndex()));
275
276         // Call startDocument on the new handler
277
index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
278                           "startDocument", "()V");
279         il.append(new INVOKEINTERFACE(index, 1));
280
281         // Call the method that implements this result tree
282
index = cpg.addMethodref(className,
283                      _methodName,
284                      "("
285                      + DOM_INTF_SIG
286                      + TRANSLET_OUTPUT_SIG
287                      +")V");
288         il.append(new INVOKEVIRTUAL(index));
289
290         // Call endDocument on the DOM handler
291
il.append(new ALOAD(domBuilder.getIndex()));
292         index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
293                           "endDocument", "()V");
294         il.append(new INVOKEINTERFACE(index, 1));
295
296         // Push the new DOM on the stack
297
il.append(new ALOAD(newDom.getIndex()));
298     }
299     }
300
301     /**
302      * Expects a result tree on the stack and pushes a node-set (iterator).
303      * Note that the produced iterator is an iterator for the DOM that
304      * contains the result tree, and not the DOM that is currently in use.
305      * This conversion here will therefore not directly work with elements
306      * such as <xsl:apply-templates> and <xsl:for-each> without the DOM
307      * parameter/variable being updates as well.
308      *
309      * @param classGen A BCEL class generator
310      * @param methodGen A BCEL method generator
311      * @param type An instance of NodeSetType (any)
312      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
313      */

314     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
315                 NodeSetType type) {
316     final ConstantPoolGen cpg = classGen.getConstantPool();
317     final InstructionList il = methodGen.getInstructionList();
318
319     // Put an extra copy of the result tree (DOM) on the stack
320
il.append(DUP);
321
322     // DOM adapters containing a result tree are not initialised with
323
// translet-type to DOM-type mapping. This must be done now for
324
// XPath expressions and patterns to work for the iterator we create.
325
il.append(classGen.loadTranslet()); // get names array
326
il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
327                            NAMES_INDEX,
328                            NAMES_INDEX_SIG)));
329     il.append(classGen.loadTranslet()); // get uris array
330
il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
331                            URIS_INDEX,
332                            URIS_INDEX_SIG)));
333     il.append(classGen.loadTranslet()); // get types array
334
il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
335                            TYPES_INDEX,
336                            TYPES_INDEX_SIG)));
337     il.append(classGen.loadTranslet()); // get namespaces array
338
il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
339                            NAMESPACE_INDEX,
340                            NAMESPACE_INDEX_SIG)));
341     // Pass the type mappings to the DOM adapter
342
final int mapping = cpg.addInterfaceMethodref(DOM_INTF,
343                               "setupMapping",
344                               "(["+STRING_SIG+
345                               "["+STRING_SIG+
346                               "[I" +
347                               "["+STRING_SIG+")V");
348     il.append(new INVOKEINTERFACE(mapping, 5));
349     il.append(DUP);
350
351     // Create an iterator for the root node of the DOM adapter
352
final int iter = cpg.addInterfaceMethodref(DOM_INTF,
353                            "getIterator",
354                            "()"+NODE_ITERATOR_SIG);
355     il.append(new INVOKEINTERFACE(iter, 1));
356     }
357
358     /**
359      * Subsume result tree into ObjectType.
360      *
361      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
362      */

363     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
364                 ObjectType type) {
365     methodGen.getInstructionList().append(NOP);
366     }
367
368     /**
369      * Translates a result tree into a non-synthesized boolean.
370      * It does not push a 0 or a 1 but instead returns branchhandle list
371      * to be appended to the false list.
372      *
373      * @param classGen A BCEL class generator
374      * @param methodGen A BCEL method generator
375      * @param type An instance of BooleanType (any)
376      * @see org.apache.xalan.xsltc.compiler.util.Type#translateToDesynthesized
377      */

378     public FlowList translateToDesynthesized(ClassGenerator classGen,
379                          MethodGenerator methodGen,
380                          BooleanType type) {
381     final InstructionList il = methodGen.getInstructionList();
382     translateTo(classGen, methodGen, Type.Boolean);
383     return new FlowList(il.append(new IFEQ(null)));
384     }
385
386     /**
387      * Translates a result tree to a Java type denoted by <code>clazz</code>.
388      * Expects a result tree on the stack and pushes an object
389      * of the appropriate type after coercion. Result trees are translated
390      * to W3C Node or W3C NodeList and the translation is done
391      * via node-set type.
392      *
393      * @param classGen A BCEL class generator
394      * @param methodGen A BCEL method generator
395      * @param type An reference to the Class to translate to
396      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
397      */

398     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
399                 Class JavaDoc clazz) {
400     final String JavaDoc className = clazz.getName();
401     final ConstantPoolGen cpg = classGen.getConstantPool();
402     final InstructionList il = methodGen.getInstructionList();
403
404     if (className.equals("org.w3c.dom.Node")) {
405         translateTo(classGen, methodGen, Type.NodeSet);
406         int index = cpg.addInterfaceMethodref(DOM_INTF,
407                           MAKE_NODE,
408                           MAKE_NODE_SIG2);
409         il.append(new INVOKEINTERFACE(index, 2));
410     }
411     else if (className.equals("org.w3c.dom.NodeList")) {
412         translateTo(classGen, methodGen, Type.NodeSet);
413         int index = cpg.addInterfaceMethodref(DOM_INTF,
414                           MAKE_NODE_LIST,
415                           MAKE_NODE_LIST_SIG2);
416         il.append(new INVOKEINTERFACE(index, 2));
417     }
418     else if (className.equals("java.lang.Object")) {
419         il.append(NOP);
420     }
421         else if (className.equals("java.lang.String")) {
422             translateTo(classGen, methodGen, Type.String);
423         }
424     else {
425         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
426                     toString(), className);
427         classGen.getParser().reportError(Constants.FATAL, err);
428     }
429     }
430
431     /**
432      * Translates an object of this type to its boxed representation.
433      */

434     public void translateBox(ClassGenerator classGen,
435                  MethodGenerator methodGen) {
436     translateTo(classGen, methodGen, Type.Reference);
437     }
438
439     /**
440      * Translates an object of this type to its unboxed representation.
441      */

442     public void translateUnBox(ClassGenerator classGen,
443                    MethodGenerator methodGen) {
444     methodGen.getInstructionList().append(NOP);
445     }
446
447     /**
448      * Returns the class name of an internal type's external representation.
449      */

450     public String JavaDoc getClassName() {
451     return(DOM_INTF);
452     }
453
454     public Instruction LOAD(int slot) {
455     return new ALOAD(slot);
456     }
457     
458     public Instruction STORE(int slot) {
459     return new ASTORE(slot);
460     }
461 }
462
Popular Tags