1 16 19 20 package org.apache.xalan.xsltc.compiler.util; 21 22 import org.apache.bcel.generic.BranchHandle; 23 import org.apache.bcel.generic.CHECKCAST; 24 import org.apache.bcel.generic.ConstantPoolGen; 25 import org.apache.bcel.generic.GETFIELD; 26 import org.apache.bcel.generic.GOTO; 27 import org.apache.bcel.generic.IFEQ; 28 import org.apache.bcel.generic.ILOAD; 29 import org.apache.bcel.generic.INVOKEINTERFACE; 30 import org.apache.bcel.generic.INVOKESPECIAL; 31 import org.apache.bcel.generic.ISTORE; 32 import org.apache.bcel.generic.Instruction; 33 import org.apache.bcel.generic.InstructionList; 34 import org.apache.bcel.generic.NEW; 35 import org.apache.bcel.generic.PUSH; 36 import org.apache.xalan.xsltc.compiler.Constants; 37 import org.apache.xalan.xsltc.compiler.FlowList; 38 import org.apache.xalan.xsltc.compiler.NodeTest; 39 40 44 public final class NodeType extends Type { 45 private final int _type; 46 47 protected NodeType() { 48 this(NodeTest.ANODE); 49 } 50 51 protected NodeType(int type) { 52 _type = type; 53 } 54 55 public int getType() { 56 return _type; 57 } 58 59 public String toString() { 60 return "node-type"; 61 } 62 63 public boolean identicalTo(Type other) { 64 return other instanceof NodeType; 65 } 66 67 public int hashCode() { 68 return _type; 69 } 70 71 public String toSignature() { 72 return "I"; 73 } 74 75 public org.apache.bcel.generic.Type toJCType() { 76 return org.apache.bcel.generic.Type.INT; 77 } 78 79 86 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 87 Type type) { 88 if (type == Type.String) { 89 translateTo(classGen, methodGen, (StringType) type); 90 } 91 else if (type == Type.Boolean) { 92 translateTo(classGen, methodGen, (BooleanType) type); 93 } 94 else if (type == Type.Real) { 95 translateTo(classGen, methodGen, (RealType) type); 96 } 97 else if (type == Type.NodeSet) { 98 translateTo(classGen, methodGen, (NodeSetType) type); 99 } 100 else if (type == Type.Reference) { 101 translateTo(classGen, methodGen, (ReferenceType) type); 102 } 103 else if (type == Type.Object) { 104 translateTo(classGen, methodGen, (ObjectType) type); 105 } 106 else { 107 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 108 toString(), type.toString()); 109 classGen.getParser().reportError(Constants.FATAL, err); 110 } 111 } 112 113 118 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 119 StringType type) { 120 final ConstantPoolGen cpg = classGen.getConstantPool(); 121 final InstructionList il = methodGen.getInstructionList(); 122 123 switch (_type) { 124 case NodeTest.ROOT: 125 case NodeTest.ELEMENT: 126 il.append(methodGen.loadDOM()); 127 il.append(SWAP); int index = cpg.addInterfaceMethodref(DOM_INTF, 129 GET_ELEMENT_VALUE, 130 GET_ELEMENT_VALUE_SIG); 131 il.append(new INVOKEINTERFACE(index, 2)); 132 break; 133 134 case NodeTest.ANODE: 135 case NodeTest.COMMENT: 136 case NodeTest.ATTRIBUTE: 137 case NodeTest.PI: 138 il.append(methodGen.loadDOM()); 139 il.append(SWAP); index = cpg.addInterfaceMethodref(DOM_INTF, 141 GET_NODE_VALUE, 142 GET_NODE_VALUE_SIG); 143 il.append(new INVOKEINTERFACE(index, 2)); 144 break; 145 146 default: 147 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 148 toString(), type.toString()); 149 classGen.getParser().reportError(Constants.FATAL, err); 150 break; 151 } 152 } 153 154 162 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 163 BooleanType type) { 164 final InstructionList il = methodGen.getInstructionList(); 165 FlowList falsel = translateToDesynthesized(classGen, methodGen, type); 166 il.append(ICONST_1); 167 final BranchHandle truec = il.append(new GOTO(null)); 168 falsel.backPatch(il.append(ICONST_0)); 169 truec.setTarget(il.append(NOP)); 170 } 171 172 178 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 179 RealType type) { 180 translateTo(classGen, methodGen, Type.String); 181 Type.String.translateTo(classGen, methodGen, Type.Real); 182 } 183 184 190 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 191 NodeSetType type) { 192 ConstantPoolGen cpg = classGen.getConstantPool(); 193 InstructionList il = methodGen.getInstructionList(); 194 195 il.append(new NEW(cpg.addClass(SINGLETON_ITERATOR))); 197 il.append(DUP_X1); 198 il.append(SWAP); 199 final int init = cpg.addMethodref(SINGLETON_ITERATOR, "<init>", 200 "(" + NODE_SIG +")V"); 201 il.append(new INVOKESPECIAL(init)); 202 } 203 204 209 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 210 ObjectType type) { 211 methodGen.getInstructionList().append(NOP); 212 } 213 214 221 public FlowList translateToDesynthesized(ClassGenerator classGen, 222 MethodGenerator methodGen, 223 BooleanType type) { 224 final InstructionList il = methodGen.getInstructionList(); 225 return new FlowList(il.append(new IFEQ(null))); 226 } 227 228 234 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 235 ReferenceType type) { 236 final ConstantPoolGen cpg = classGen.getConstantPool(); 237 final InstructionList il = methodGen.getInstructionList(); 238 il.append(new NEW(cpg.addClass(RUNTIME_NODE_CLASS))); 239 il.append(DUP_X1); 240 il.append(SWAP); 241 il.append(new PUSH(cpg, _type)); 242 il.append(new INVOKESPECIAL(cpg.addMethodref(RUNTIME_NODE_CLASS, 243 "<init>", "(II)V"))); 244 } 245 246 251 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 252 Class clazz) { 253 final ConstantPoolGen cpg = classGen.getConstantPool(); 254 final InstructionList il = methodGen.getInstructionList(); 255 256 String className = clazz.getName(); 257 if (className.equals("java.lang.String")) { 258 translateTo(classGen, methodGen, Type.String); 259 return; 260 } 261 262 il.append(methodGen.loadDOM()); 263 il.append(SWAP); 265 if (className.equals("org.w3c.dom.Node") || 266 className.equals("java.lang.Object")) { 267 int index = cpg.addInterfaceMethodref(DOM_INTF, 268 MAKE_NODE, 269 MAKE_NODE_SIG); 270 il.append(new INVOKEINTERFACE(index, 2)); 271 } 272 else if (className.equals("org.w3c.dom.NodeList")) { 273 int index = cpg.addInterfaceMethodref(DOM_INTF, 274 MAKE_NODE_LIST, 275 MAKE_NODE_LIST_SIG); 276 il.append(new INVOKEINTERFACE(index, 2)); 277 } 278 else { 279 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 280 toString(), className); 281 classGen.getParser().reportError(Constants.FATAL, err); 282 } 283 } 284 285 288 public void translateBox(ClassGenerator classGen, 289 MethodGenerator methodGen) { 290 translateTo(classGen, methodGen, Type.Reference); 291 } 292 293 296 public void translateUnBox(ClassGenerator classGen, 297 MethodGenerator methodGen) { 298 final ConstantPoolGen cpg = classGen.getConstantPool(); 299 final InstructionList il = methodGen.getInstructionList(); 300 il.append(new CHECKCAST(cpg.addClass(RUNTIME_NODE_CLASS))); 301 il.append(new GETFIELD(cpg.addFieldref(RUNTIME_NODE_CLASS, 302 NODE_FIELD, 303 NODE_FIELD_SIG))); 304 } 305 306 309 public String getClassName() { 310 return(RUNTIME_NODE_CLASS); 311 } 312 313 public Instruction LOAD(int slot) { 314 return new ILOAD(slot); 315 } 316 317 public Instruction STORE(int slot) { 318 return new ISTORE(slot); 319 } 320 } 321 322 | Popular Tags |