View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2005 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: AbsoluteLocationPath.java,v 1.2.4.1 2005/09/12 09:44:03 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.ALOAD;
27  import com.sun.org.apache.bcel.internal.generic.ASTORE;
28  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29  import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
30  import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
31  import com.sun.org.apache.bcel.internal.generic.InstructionList;
32  import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
33  import com.sun.org.apache.bcel.internal.generic.NEW;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
37  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
38  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
39  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
40  
41  /**
42   * @author Jacek Ambroziak
43   * @author Santiago Pericas-Geertsen
44   */
45  final class AbsoluteLocationPath extends Expression {
46      private Expression _path;   // may be null
47  
48      public AbsoluteLocationPath() {
49          _path = null;
50      }
51  
52      public AbsoluteLocationPath(Expression path) {
53          _path = path;
54          if (path != null) {
55              _path.setParent(this);
56          }
57      }
58  
59      public void setParser(Parser parser) {
60          super.setParser(parser);
61          if (_path != null) {
62              _path.setParser(parser);
63          }
64      }
65  
66      public Expression getPath() {
67          return(_path);
68      }
69  
70      public String toString() {
71          return "AbsoluteLocationPath(" +
72              (_path != null ? _path.toString() : "null") + ')';
73      }
74  
75      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
76          if (_path != null) {
77              final Type ptype = _path.typeCheck(stable);
78              if (ptype instanceof NodeType) {            // promote to node-set
79                  _path = new CastExpr(_path, Type.NodeSet);
80              }
81          }
82          return _type = Type.NodeSet;
83      }
84  
85      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
86          final ConstantPoolGen cpg = classGen.getConstantPool();
87          final InstructionList il = methodGen.getInstructionList();
88          if (_path != null) {
89              final int initAI = cpg.addMethodref(ABSOLUTE_ITERATOR,
90                                                  "<init>",
91                                                  "("
92                                                  + NODE_ITERATOR_SIG
93                                                  + ")V");
94  
95              // Compile relative path iterator(s)
96              //
97              // Backwards branches are prohibited if an uninitialized object is
98              // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
99              // We don't know whether this code might contain backwards branches,
100             // so we mustn't create the new object until after we've created
101             // this argument to its constructor.  Instead we calculate the
102             // value of the argument to the constructor first, store it in
103             // a temporary variable, create the object and reload the argument
104             // from the temporary to avoid the problem.
105             _path.translate(classGen, methodGen);
106             LocalVariableGen relPathIterator
107                     = methodGen.addLocalVariable("abs_location_path_tmp",
108                                        Util.getJCRefType(NODE_ITERATOR_SIG),
109                                        null, null);
110             relPathIterator.setStart(
111                     il.append(new ASTORE(relPathIterator.getIndex())));
112 
113             // Create new AbsoluteIterator
114             il.append(new NEW(cpg.addClass(ABSOLUTE_ITERATOR)));
115             il.append(DUP);
116             relPathIterator.setEnd(
117                     il.append(new ALOAD(relPathIterator.getIndex())));
118 
119             // Initialize AbsoluteIterator with iterator from the stack
120             il.append(new INVOKESPECIAL(initAI));
121         }
122         else {
123             final int gitr = cpg.addInterfaceMethodref(DOM_INTF,
124                                                        "getIterator",
125                                                        "()"+NODE_ITERATOR_SIG);
126             il.append(methodGen.loadDOM());
127             il.append(new INVOKEINTERFACE(gitr, 1));
128         }
129     }
130 }