1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.compiler;
23
24import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
25import com.sun.org.apache.bcel.internal.generic.GOTO;
26import com.sun.org.apache.bcel.internal.generic.IFNE;
27import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
28import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
29import com.sun.org.apache.bcel.internal.generic.InstructionList;
30import com.sun.org.apache.bcel.internal.generic.PUSH;
31import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
32import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
33import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
34import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
35
36/**
37 * @author Jacek Ambroziak
38 * @author Santiago Pericas-Geertsen
39 */
40abstract class IdKeyPattern extends LocationPathPattern {
41
42    protected RelativePathPattern _left = null;;
43    private String _index = null;
44    private String _value = null;;
45
46    public IdKeyPattern(String index, String value) {
47        _index = index;
48        _value = value;
49    }
50
51    public String getIndexName() {
52        return(_index);
53    }
54
55    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
56        return Type.NodeSet;
57    }
58
59    public boolean isWildcard() {
60        return false;
61    }
62
63    public void setLeft(RelativePathPattern left) {
64        _left = left;
65    }
66
67    public StepPattern getKernelPattern() {
68        return(null);
69    }
70
71    public void reduceKernelPattern() { }
72
73    public String toString() {
74        return "id/keyPattern(" + _index + ", " + _value + ')';
75    }
76
77    /**
78     * This method is called when the constructor is compiled in
79     * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
80     */
81    public void translate(ClassGenerator classGen,
82                          MethodGenerator methodGen) {
83
84        final ConstantPoolGen cpg = classGen.getConstantPool();
85        final InstructionList il = methodGen.getInstructionList();
86
87        // Returns the KeyIndex object of a given name
88        final int getKeyIndex = cpg.addMethodref(TRANSLET_CLASS,
89                                                 "getKeyIndex",
90                                                 "(Ljava/lang/String;)"+
91                                                 KEY_INDEX_SIG);
92
93        // Initialises a KeyIndex to return nodes with specific values
94        final int lookupId = cpg.addMethodref(KEY_INDEX_CLASS,
95                                              "containsID",
96                                              "(ILjava/lang/Object;)I");
97        final int lookupKey = cpg.addMethodref(KEY_INDEX_CLASS,
98                                               "containsKey",
99                                               "(ILjava/lang/Object;)I");
100        final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
101                                                           "getNodeIdent",
102                                                           "(I)"+NODE_SIG);
103
104        // Call getKeyIndex in AbstractTranslet with the name of the key
105        // to get the index for this key (which is also a node iterator).
106        il.append(classGen.loadTranslet());
107        il.append(new PUSH(cpg,_index));
108        il.append(new INVOKEVIRTUAL(getKeyIndex));
109
110        // Now use the value in the second argument to determine what nodes
111        // the iterator should return.
112        il.append(SWAP);
113        il.append(new PUSH(cpg,_value));
114        if (this instanceof IdPattern)
115        {
116            il.append(new INVOKEVIRTUAL(lookupId));
117        }
118        else
119        {
120            il.append(new INVOKEVIRTUAL(lookupKey));
121        }
122
123        _trueList.add(il.append(new IFNE(null)));
124        _falseList.add(il.append(new GOTO(null)));
125    }
126
127}
128