KickJava   Java API By Example, From Geeks To Geeks.

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


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: IdKeyPattern.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.ConstantPoolGen;
23 import com.sun.org.apache.bcel.internal.generic.GOTO;
24 import com.sun.org.apache.bcel.internal.generic.IFNE;
25 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
26 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
27 import com.sun.org.apache.bcel.internal.generic.InstructionList;
28 import com.sun.org.apache.bcel.internal.generic.PUSH;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  */

38 abstract class IdKeyPattern extends LocationPathPattern {
39
40     protected RelativePathPattern _left = null;;
41     private String JavaDoc _index = null;
42     private String JavaDoc _value = null;;
43
44     public IdKeyPattern(String JavaDoc index, String JavaDoc value) {
45     _index = index;
46     _value = value;
47     }
48
49     public String JavaDoc getIndexName() {
50     return(_index);
51     }
52
53     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
54     return Type.NodeSet;
55     }
56     
57     public boolean isWildcard() {
58     return false;
59     }
60     
61     public void setLeft(RelativePathPattern left) {
62     _left = left;
63     }
64
65     public StepPattern getKernelPattern() {
66     return(null);
67     }
68     
69     public void reduceKernelPattern() { }
70
71     public String JavaDoc toString() {
72     return "id/keyPattern(" + _index + ", " + _value + ')';
73     }
74
75     /**
76      * This method is called when the constructor is compiled in
77      * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
78      */

79     public void translate(ClassGenerator classGen,
80               MethodGenerator methodGen) {
81
82     final ConstantPoolGen cpg = classGen.getConstantPool();
83     final InstructionList il = methodGen.getInstructionList();
84
85     // Returns the KeyIndex object of a given name
86
final int getKeyIndex = cpg.addMethodref(TRANSLET_CLASS,
87                          "getKeyIndex",
88                          "(Ljava/lang/String;)"+
89                          KEY_INDEX_SIG);
90     
91     // Initialises a KeyIndex to return nodes with specific values
92
final int lookupId = cpg.addMethodref(KEY_INDEX_CLASS,
93                           "containsID",
94                           "(ILjava/lang/Object;)I");
95     final int lookupKey = cpg.addMethodref(KEY_INDEX_CLASS,
96                            "containsKey",
97                            "(ILjava/lang/Object;)I");
98     final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
99                                "getNodeIdent",
100                                "(I)"+NODE_SIG);
101
102     // Call getKeyIndex in AbstractTranslet with the name of the key
103
// to get the index for this key (which is also a node iterator).
104
il.append(classGen.loadTranslet());
105     il.append(new PUSH(cpg,_index));
106     il.append(new INVOKEVIRTUAL(getKeyIndex));
107     
108     // Now use the value in the second argument to determine what nodes
109
// the iterator should return.
110
il.append(SWAP);
111     il.append(new PUSH(cpg,_value));
112     if (this instanceof IdPattern)
113     {
114         il.append(SWAP);
115         il.append(methodGen.loadDOM());
116         il.append(SWAP);
117         il.append(new INVOKEINTERFACE(getNodeIdent, 2));
118         il.append(SWAP);
119         il.append(new INVOKEVIRTUAL(lookupId));
120     }
121     else
122     {
123         il.append(SWAP);
124         il.append(methodGen.loadDOM());
125         il.append(SWAP);
126         il.append(new INVOKEINTERFACE(getNodeIdent, 2));
127         il.append(SWAP);
128         il.append(new INVOKEVIRTUAL(lookupKey));
129     }
130
131     _trueList.add(il.append(new IFNE(null)));
132     _falseList.add(il.append(new GOTO(null)));
133     }
134
135 }
136
137
Popular Tags