KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > NameBase


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: NameBase.java,v 1.12 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import org.apache.bcel.generic.ConstantPoolGen;
25 import org.apache.bcel.generic.INVOKESTATIC;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
28 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
29 import org.apache.xalan.xsltc.compiler.util.Type;
30 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
31
32 /**
33  * @author Morten Jorgensen
34  * @author Erwin Bolwidt <ejb@klomp.org>
35  */

36 class NameBase extends FunctionCall {
37
38     private Expression _param = null;
39     private Type _paramType = Type.Node;
40
41     /**
42      * Handles calls with no parameter (current node is implicit parameter).
43      */

44     public NameBase(QName fname) {
45     super(fname);
46     }
47
48     /**
49      * Handles calls with one parameter (either node or node-set).
50      */

51     public NameBase(QName fname, Vector JavaDoc arguments) {
52     super(fname, arguments);
53     _param = argument(0);
54     }
55
56
57     /**
58      * Check that we either have no parameters or one parameter that is
59      * either a node or a node-set.
60      */

61     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
62
63     // Check the argument type (if any)
64
switch(argumentCount()) {
65     case 0:
66         _paramType = Type.Node;
67         break;
68     case 1:
69         _paramType = _param.typeCheck(stable);
70         break;
71     default:
72         throw new TypeCheckError(this);
73     }
74
75     // The argument has to be a node, a node-set or a node reference
76
if ((_paramType != Type.NodeSet) &&
77         (_paramType != Type.Node) &&
78         (_paramType != Type.Reference)) {
79         throw new TypeCheckError(this);
80     }
81
82     return (_type = Type.String);
83     }
84
85     public Type getType() {
86     return _type;
87     }
88
89     /**
90      * Translate the code required for getting the node for which the
91      * QName, local-name or namespace URI should be extracted.
92      */

93     public void translate(ClassGenerator classGen,
94               MethodGenerator methodGen) {
95     final ConstantPoolGen cpg = classGen.getConstantPool();
96     final InstructionList il = methodGen.getInstructionList();
97
98     il.append(methodGen.loadDOM());
99     
100     // Function was called with no parameters
101
if (argumentCount() == 0) {
102         il.append(methodGen.loadContextNode());
103     }
104     // Function was called with node parameter
105
else if (_paramType == Type.Node) {
106         _param.translate(classGen, methodGen);
107     }
108     else if (_paramType == Type.Reference) {
109         _param.translate(classGen, methodGen);
110         il.append(new INVOKESTATIC(cpg.addMethodref
111                        (BASIS_LIBRARY_CLASS,
112                     "referenceToNodeSet",
113                     "("
114                     + OBJECT_SIG
115                     + ")"
116                     + NODE_ITERATOR_SIG)));
117         il.append(methodGen.nextNode());
118     }
119     // Function was called with node-set parameter
120
else {
121         _param.translate(classGen, methodGen);
122         _param.startIterator(classGen, methodGen);
123         il.append(methodGen.nextNode());
124     }
125     }
126 }
127
Popular Tags