View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: NameBase.java,v 1.2.4.1 2005/09/02 10:17:31 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.Vector;
27  
28  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29  import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
30  import com.sun.org.apache.bcel.internal.generic.InstructionList;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
33  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
35  
36  /**
37   * @author Morten Jorgensen
38   * @author Erwin Bolwidt <ejb@klomp.org>
39   */
40  class NameBase extends FunctionCall {
41  
42      private Expression _param = null;
43      private Type       _paramType = Type.Node;
44  
45      /**
46       * Handles calls with no parameter (current node is implicit parameter).
47       */
48      public NameBase(QName fname) {
49          super(fname);
50      }
51  
52      /**
53       * Handles calls with one parameter (either node or node-set).
54       */
55      public NameBase(QName fname, Vector arguments) {
56          super(fname, arguments);
57          _param = argument(0);
58      }
59  
60  
61      /**
62       * Check that we either have no parameters or one parameter that is
63       * either a node or a node-set.
64       */
65      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
66  
67          // Check the argument type (if any)
68          switch(argumentCount()) {
69          case 0:
70              _paramType = Type.Node;
71              break;
72          case 1:
73              _paramType = _param.typeCheck(stable);
74              break;
75          default:
76              throw new TypeCheckError(this);
77          }
78  
79          // The argument has to be a node, a node-set or a node reference
80          if ((_paramType != Type.NodeSet) &&
81              (_paramType != Type.Node) &&
82              (_paramType != Type.Reference)) {
83              throw new TypeCheckError(this);
84          }
85  
86          return (_type = Type.String);
87      }
88  
89      public Type getType() {
90          return _type;
91      }
92  
93      /**
94       * Translate the code required for getting the node for which the
95       * QName, local-name or namespace URI should be extracted.
96       */
97      public void translate(ClassGenerator classGen,
98                            MethodGenerator methodGen) {
99          final ConstantPoolGen cpg = classGen.getConstantPool();
100         final InstructionList il = methodGen.getInstructionList();
101 
102         il.append(methodGen.loadDOM());
103 
104         // Function was called with no parameters
105         if (argumentCount() == 0) {
106             il.append(methodGen.loadContextNode());
107         }
108         // Function was called with node parameter
109         else if (_paramType == Type.Node) {
110             _param.translate(classGen, methodGen);
111         }
112         else if (_paramType == Type.Reference) {
113             _param.translate(classGen, methodGen);
114             il.append(new INVOKESTATIC(cpg.addMethodref
115                                        (BASIS_LIBRARY_CLASS,
116                                         "referenceToNodeSet",
117                                         "("
118                                         + OBJECT_SIG
119                                         + ")"
120                                         + NODE_ITERATOR_SIG)));
121             il.append(methodGen.nextNode());
122         }
123         // Function was called with node-set parameter
124         else {
125             _param.translate(classGen, methodGen);
126             _param.startIterator(classGen, methodGen);
127             il.append(methodGen.nextNode());
128         }
129     }
130 }