KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.ConstantPoolGen;
23 import org.apache.bcel.generic.ILOAD;
24 import org.apache.bcel.generic.INVOKEINTERFACE;
25 import org.apache.bcel.generic.ISTORE;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.bcel.generic.LocalVariableGen;
28 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
29 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
30 import org.apache.xalan.xsltc.compiler.util.Type;
31 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
32 import org.apache.xalan.xsltc.compiler.util.Util;
33
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  */

38 final class ParentPattern extends RelativePathPattern {
39     private final Pattern _left;
40     private final RelativePathPattern _right;
41         
42     public ParentPattern(Pattern left, RelativePathPattern right) {
43     (_left = left).setParent(this);
44     (_right = right).setParent(this);
45     }
46
47     public void setParser(Parser parser) {
48     super.setParser(parser);
49     _left.setParser(parser);
50     _right.setParser(parser);
51     }
52     
53     public boolean isWildcard() {
54     return false;
55     }
56     
57     public StepPattern getKernelPattern() {
58     return _right.getKernelPattern();
59     }
60     
61     public void reduceKernelPattern() {
62     _right.reduceKernelPattern();
63     }
64
65     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
66     _left.typeCheck(stable);
67     return _right.typeCheck(stable);
68     }
69
70     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
71     final ConstantPoolGen cpg = classGen.getConstantPool();
72     final InstructionList il = methodGen.getInstructionList();
73     final LocalVariableGen local =
74         methodGen.addLocalVariable2("ppt",
75                     Util.getJCRefType(NODE_SIG),
76                     il.getEnd());
77     
78     final org.apache.bcel.generic.Instruction loadLocal =
79         new ILOAD(local.getIndex());
80     final org.apache.bcel.generic.Instruction storeLocal =
81         new ISTORE(local.getIndex());
82
83     if (_right.isWildcard()) {
84         il.append(methodGen.loadDOM());
85         il.append(SWAP);
86     }
87     else if (_right instanceof StepPattern) {
88         il.append(DUP);
89         il.append(storeLocal);
90         
91         _right.translate(classGen, methodGen);
92         
93         il.append(methodGen.loadDOM());
94         local.setEnd(il.append(loadLocal));
95     }
96     else {
97         _right.translate(classGen, methodGen);
98
99         if (_right instanceof AncestorPattern) {
100         il.append(methodGen.loadDOM());
101         il.append(SWAP);
102         }
103     }
104
105     final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
106                             GET_PARENT,
107                             GET_PARENT_SIG);
108     il.append(new INVOKEINTERFACE(getParent, 2));
109
110     final SyntaxTreeNode p = getParent();
111     if (p == null || p instanceof Instruction ||
112         p instanceof TopLevelElement)
113     {
114         _left.translate(classGen, methodGen);
115     }
116     else {
117         il.append(DUP);
118         il.append(storeLocal);
119         
120         _left.translate(classGen, methodGen);
121
122         il.append(methodGen.loadDOM());
123         local.setEnd(il.append(loadLocal));
124     }
125
126     methodGen.removeLocalVariable(local);
127     
128     /*
129      * If _right is an ancestor pattern, backpatch _left false
130      * list to the loop that searches for more ancestors.
131      */

132     if (_right instanceof AncestorPattern) {
133         final AncestorPattern ancestor = (AncestorPattern) _right;
134         _left.backPatchFalseList(ancestor.getLoopHandle()); // clears list
135
}
136
137     _trueList.append(_right._trueList.append(_left._trueList));
138     _falseList.append(_right._falseList.append(_left._falseList));
139     }
140
141     public String JavaDoc toString() {
142     return "Parent(" + _left + ", " + _right + ')';
143     }
144 }
145
Popular Tags