KickJava   Java API By Example, From Geeks To Geeks.

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


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: CopyOf.java,v 1.15 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.INVOKEINTERFACE;
24 import org.apache.bcel.generic.INVOKESTATIC;
25 import org.apache.bcel.generic.INVOKEVIRTUAL;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
28 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
29 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
30 import org.apache.xalan.xsltc.compiler.util.NodeSetType;
31 import org.apache.xalan.xsltc.compiler.util.NodeType;
32 import org.apache.xalan.xsltc.compiler.util.ReferenceType;
33 import org.apache.xalan.xsltc.compiler.util.ResultTreeType;
34 import org.apache.xalan.xsltc.compiler.util.Type;
35 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
36 import org.apache.xalan.xsltc.compiler.util.Util;
37
38 /**
39  * @author Jacek Ambroziak
40  * @author Santiago Pericas-Geertsen
41  */

42 final class CopyOf extends Instruction {
43     private Expression _select;
44     
45     public void display(int indent) {
46     indent(indent);
47     Util.println("CopyOf");
48     indent(indent + IndentIncrement);
49     Util.println("select " + _select.toString());
50     }
51
52     public void parseContents(Parser parser) {
53     _select = parser.parseExpression(this, "select", null);
54         // make sure required attribute(s) have been set
55
if (_select.isDummy()) {
56         reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
57         return;
58         }
59     }
60     
61     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
62     final Type tselect = _select.typeCheck(stable);
63     if (tselect instanceof NodeType ||
64         tselect instanceof NodeSetType ||
65         tselect instanceof ReferenceType ||
66         tselect instanceof ResultTreeType) {
67         // falls through
68
}
69     else {
70         _select = new CastExpr(_select, Type.String);
71     }
72     return Type.Void;
73     }
74     
75     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
76     final ConstantPoolGen cpg = classGen.getConstantPool();
77     final InstructionList il = methodGen.getInstructionList();
78     final Type tselect = _select.getType();
79
80     final String JavaDoc CPY1_SIG = "("+NODE_ITERATOR_SIG+TRANSLET_OUTPUT_SIG+")V";
81     final int cpy1 = cpg.addInterfaceMethodref(DOM_INTF, "copy", CPY1_SIG);
82
83     final String JavaDoc CPY2_SIG = "("+NODE_SIG+TRANSLET_OUTPUT_SIG+")V";
84     final int cpy2 = cpg.addInterfaceMethodref(DOM_INTF, "copy", CPY2_SIG);
85     
86     final String JavaDoc getDoc_SIG = "()"+NODE_SIG;
87     final int getDoc = cpg.addInterfaceMethodref(DOM_INTF, "getDocument", getDoc_SIG);
88
89
90     if (tselect instanceof NodeSetType) {
91         il.append(methodGen.loadDOM());
92
93         // push NodeIterator
94
_select.translate(classGen, methodGen);
95         _select.startIterator(classGen, methodGen);
96
97         // call copy from the DOM 'library'
98
il.append(methodGen.loadHandler());
99         il.append(new INVOKEINTERFACE(cpy1, 3));
100     }
101     else if (tselect instanceof NodeType) {
102         il.append(methodGen.loadDOM());
103         _select.translate(classGen, methodGen);
104         il.append(methodGen.loadHandler());
105         il.append(new INVOKEINTERFACE(cpy2, 3));
106     }
107     else if (tselect instanceof ResultTreeType) {
108         _select.translate(classGen, methodGen);
109         // We want the whole tree, so we start with the root node
110
il.append(DUP); //need a pointer to the DOM ;
111
il.append(new INVOKEINTERFACE(getDoc,1)); //ICONST_0);
112
il.append(methodGen.loadHandler());
113         il.append(new INVOKEINTERFACE(cpy2, 3));
114     }
115     else if (tselect instanceof ReferenceType) {
116         _select.translate(classGen, methodGen);
117         il.append(methodGen.loadHandler());
118         il.append(methodGen.loadCurrentNode());
119         il.append(methodGen.loadDOM());
120         final int copy = cpg.addMethodref(BASIS_LIBRARY_CLASS, "copy",
121                           "("
122                           + OBJECT_SIG
123                           + TRANSLET_OUTPUT_SIG
124                           + NODE_SIG
125                           + DOM_INTF_SIG
126                           + ")V");
127         il.append(new INVOKESTATIC(copy));
128     }
129     else {
130         il.append(classGen.loadTranslet());
131         _select.translate(classGen, methodGen);
132         il.append(methodGen.loadHandler());
133         il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
134                              CHARACTERSW,
135                              CHARACTERSW_SIG)));
136     }
137
138     }
139 }
140
Popular Tags