KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > util > StringType


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: StringType.java,v 1.12 2004/02/16 22:26:45 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler.util;
21
22 import org.apache.bcel.generic.ALOAD;
23 import org.apache.bcel.generic.ASTORE;
24 import org.apache.bcel.generic.BranchHandle;
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.GOTO;
27 import org.apache.bcel.generic.IFEQ;
28 import org.apache.bcel.generic.IFNONNULL;
29 import org.apache.bcel.generic.INVOKESTATIC;
30 import org.apache.bcel.generic.INVOKEVIRTUAL;
31 import org.apache.bcel.generic.Instruction;
32 import org.apache.bcel.generic.InstructionList;
33 import org.apache.bcel.generic.PUSH;
34 import org.apache.xalan.xsltc.compiler.Constants;
35 import org.apache.xalan.xsltc.compiler.FlowList;
36
37 /**
38  * @author Jacek Ambroziak
39  * @author Santiago Pericas-Geertsen
40  */

41 public class StringType extends Type {
42     protected StringType() {}
43
44     public String JavaDoc toString() {
45     return "string";
46     }
47
48     public boolean identicalTo(Type other) {
49     return this == other;
50     }
51
52     public String JavaDoc toSignature() {
53     return "Ljava/lang/String;";
54     }
55
56     public boolean isSimple() {
57     return true;
58     }
59
60     public org.apache.bcel.generic.Type toJCType() {
61     return org.apache.bcel.generic.Type.STRING;
62     }
63
64     /**
65      * Translates a string into an object of internal type <code>type</code>.
66      * The translation to int is undefined since strings are always converted
67      * to reals in arithmetic expressions.
68      *
69      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
70      */

71     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
72                 Type type) {
73     if (type == Type.Boolean) {
74         translateTo(classGen, methodGen, (BooleanType) type);
75     }
76     else if (type == Type.Real) {
77         translateTo(classGen, methodGen, (RealType) type);
78     }
79     else if (type == Type.Reference) {
80         translateTo(classGen, methodGen, (ReferenceType) type);
81     }
82         else if (type == Type.ObjectString) {
83             // NOP -> same representation
84
}
85     else {
86         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
87                     toString(), type.toString());
88         classGen.getParser().reportError(Constants.FATAL, err);
89     }
90     }
91
92     /**
93      * Translates a string into a synthesized boolean.
94      *
95      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
96      */

97     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
98                 BooleanType type) {
99     final InstructionList il = methodGen.getInstructionList();
100     FlowList falsel = translateToDesynthesized(classGen, methodGen, type);
101     il.append(ICONST_1);
102     final BranchHandle truec = il.append(new GOTO(null));
103     falsel.backPatch(il.append(ICONST_0));
104     truec.setTarget(il.append(NOP));
105     }
106
107     /**
108      * Translates a string into a real by calling stringToReal() from the
109      * basis library.
110      *
111      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
112      */

113     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
114                 RealType type) {
115     final ConstantPoolGen cpg = classGen.getConstantPool();
116     final InstructionList il = methodGen.getInstructionList();
117     il.append(new INVOKESTATIC(cpg.addMethodref(BASIS_LIBRARY_CLASS,
118                             STRING_TO_REAL,
119                             STRING_TO_REAL_SIG)));
120     }
121
122     /**
123      * Translates a string into a non-synthesized boolean. It does not push a
124      * 0 or a 1 but instead returns branchhandle list to be appended to the
125      * false list.
126      *
127      * @see org.apache.xalan.xsltc.compiler.util.Type#translateToDesynthesized
128      */

129     public FlowList translateToDesynthesized(ClassGenerator classGen,
130                          MethodGenerator methodGen,
131                          BooleanType type) {
132     final ConstantPoolGen cpg = classGen.getConstantPool();
133     final InstructionList il = methodGen.getInstructionList();
134
135     il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
136                              "length", "()I")));
137     return new FlowList(il.append(new IFEQ(null)));
138     }
139
140     /**
141      * Expects a string on the stack and pushes a boxed string.
142      * Strings are already boxed so the translation is just a NOP.
143      *
144      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
145      */

146     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
147                 ReferenceType type) {
148     methodGen.getInstructionList().append(NOP);
149     }
150
151     /**
152      * Translates a internal string into an external (Java) string.
153      *
154      * @see org.apache.xalan.xsltc.compiler.util.Type#translateFrom
155      */

156     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
157                 Class JavaDoc clazz)
158     {
159         // Is String <: clazz? I.e. clazz in { String, Object }
160
if (clazz.isAssignableFrom(java.lang.String JavaDoc.class)) {
161         methodGen.getInstructionList().append(NOP);
162     }
163     else {
164         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
165                     toString(), clazz.getName());
166         classGen.getParser().reportError(Constants.FATAL, err);
167     }
168     }
169     
170     /**
171      * Translates an external (primitive) Java type into a string.
172      *
173      * @see org.apache.xalan.xsltc.compiler.util.Type#translateFrom
174      */

175     public void translateFrom(ClassGenerator classGen,
176     MethodGenerator methodGen, Class JavaDoc clazz)
177     {
178     final ConstantPoolGen cpg = classGen.getConstantPool();
179     final InstructionList il = methodGen.getInstructionList();
180
181     if (clazz.getName().equals("java.lang.String")) {
182         // same internal representation, convert null to ""
183
il.append(DUP);
184         final BranchHandle ifNonNull = il.append(new IFNONNULL(null));
185         il.append(POP);
186         il.append(new PUSH(cpg, ""));
187         ifNonNull.setTarget(il.append(NOP));
188     }
189     else {
190         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
191                     toString(), clazz.getName());
192         classGen.getParser().reportError(Constants.FATAL, err);
193     }
194     }
195
196     /**
197      * Translates an object of this type to its boxed representation.
198      */

199     public void translateBox(ClassGenerator classGen,
200                  MethodGenerator methodGen) {
201     translateTo(classGen, methodGen, Type.Reference);
202     }
203
204     /**
205      * Translates an object of this type to its unboxed representation.
206      */

207     public void translateUnBox(ClassGenerator classGen,
208                    MethodGenerator methodGen) {
209     methodGen.getInstructionList().append(NOP);
210     }
211
212     /**
213      * Returns the class name of an internal type's external representation.
214      */

215     public String JavaDoc getClassName() {
216     return(STRING_CLASS);
217     }
218
219
220     public Instruction LOAD(int slot) {
221     return new ALOAD(slot);
222     }
223     
224     public Instruction STORE(int slot) {
225     return new ASTORE(slot);
226     }
227 }
228
Popular Tags