KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > AbsolutePathPattern


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

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
23 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
24 import com.sun.org.apache.bcel.internal.generic.GOTO_W;
25 import com.sun.org.apache.bcel.internal.generic.IF_ICMPEQ;
26 import com.sun.org.apache.bcel.internal.generic.ILOAD;
27 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
28 import com.sun.org.apache.bcel.internal.generic.ISTORE;
29 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
30 import com.sun.org.apache.bcel.internal.generic.InstructionList;
31 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
32 import com.sun.org.apache.bcel.internal.generic.PUSH;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
38 import com.sun.org.apache.xml.internal.dtm.DTM;
39
40 /**
41  * @author Jacek Ambroziak
42  * @author Santiago Pericas-Geertsen
43  */

44 final class AbsolutePathPattern extends LocationPathPattern {
45     private final RelativePathPattern _left; // may be null
46

47     public AbsolutePathPattern(RelativePathPattern left) {
48     _left = left;
49     if (left != null) {
50         left.setParent(this);
51     }
52     }
53
54     public void setParser(Parser parser) {
55     super.setParser(parser);
56     if (_left != null)
57         _left.setParser(parser);
58     }
59     
60     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
61     return _left == null ? Type.Root : _left.typeCheck(stable);
62     }
63
64     public boolean isWildcard() {
65     return false;
66     }
67     
68     public StepPattern getKernelPattern() {
69     return _left != null ? _left.getKernelPattern() : null;
70     }
71     
72     public void reduceKernelPattern() {
73     _left.reduceKernelPattern();
74     }
75     
76     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
77     final ConstantPoolGen cpg = classGen.getConstantPool();
78     final InstructionList il = methodGen.getInstructionList();
79
80     if (_left != null) {
81         if (_left instanceof StepPattern) {
82         final LocalVariableGen local =
83             // absolute path pattern temporary
84
methodGen.addLocalVariable2("apptmp",
85                         Util.getJCRefType(NODE_SIG),
86                         il.getEnd());
87         il.append(DUP);
88         il.append(new ISTORE(local.getIndex()));
89         _left.translate(classGen, methodGen);
90         il.append(methodGen.loadDOM());
91         local.setEnd(il.append(new ILOAD(local.getIndex())));
92         methodGen.removeLocalVariable(local);
93         }
94         else {
95         _left.translate(classGen, methodGen);
96         }
97     }
98
99     final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
100                             GET_PARENT,
101                             GET_PARENT_SIG);
102     final int getType = cpg.addInterfaceMethodref(DOM_INTF,
103                               "getExpandedTypeID",
104                                                       "(I)I");
105
106     InstructionHandle begin = il.append(methodGen.loadDOM());
107     il.append(SWAP);
108     il.append(new INVOKEINTERFACE(getParent, 2));
109     if (_left instanceof AncestorPattern) {
110         il.append(methodGen.loadDOM());
111         il.append(SWAP);
112     }
113     il.append(new INVOKEINTERFACE(getType, 2));
114     il.append(new PUSH(cpg, DTM.DOCUMENT_NODE));
115     
116     final BranchHandle skip = il.append(new IF_ICMPEQ(null));
117     _falseList.add(il.append(new GOTO_W(null)));
118     skip.setTarget(il.append(NOP));
119
120     if (_left != null) {
121         _left.backPatchTrueList(begin);
122         
123         /*
124          * If _left is an ancestor pattern, backpatch this pattern's false
125          * list to the loop that searches for more ancestors.
126          */

127         if (_left instanceof AncestorPattern) {
128         final AncestorPattern ancestor = (AncestorPattern) _left;
129         _falseList.backPatch(ancestor.getLoopHandle()); // clears list
130
}
131         _falseList.append(_left._falseList);
132     }
133     }
134     
135     public String JavaDoc toString() {
136     return "absolutePathPattern(" + (_left != null ? _left.toString() : ")");
137     }
138 }
139
Popular Tags