286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: NodeType.java,v 1.2.4.1 2005/09/05 11:24:25 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler.util;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.BranchHandle;
286N/Aimport com.sun.org.apache.bcel.internal.generic.CHECKCAST;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
286N/Aimport com.sun.org.apache.bcel.internal.generic.GETFIELD;
286N/Aimport com.sun.org.apache.bcel.internal.generic.GOTO;
286N/Aimport com.sun.org.apache.bcel.internal.generic.IFEQ;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ILOAD;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ISTORE;
286N/Aimport com.sun.org.apache.bcel.internal.generic.Instruction;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.bcel.internal.generic.NEW;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUSH;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.NodeTest;
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A */
286N/Apublic final class NodeType extends Type {
286N/A private final int _type;
286N/A
286N/A protected NodeType() {
286N/A this(NodeTest.ANODE);
286N/A }
286N/A
286N/A protected NodeType(int type) {
286N/A _type = type;
286N/A }
286N/A
286N/A public int getType() {
286N/A return _type;
286N/A }
286N/A
286N/A public String toString() {
286N/A return "node-type";
286N/A }
286N/A
286N/A public boolean identicalTo(Type other) {
286N/A return other instanceof NodeType;
286N/A }
286N/A
286N/A public int hashCode() {
286N/A return _type;
286N/A }
286N/A
286N/A public String toSignature() {
286N/A return "I";
286N/A }
286N/A
286N/A public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
286N/A return com.sun.org.apache.bcel.internal.generic.Type.INT;
286N/A }
286N/A
286N/A /**
286N/A * Translates a node into an object of internal type <code>type</code>.
286N/A * The translation to int is undefined since nodes are always converted
286N/A * to reals in arithmetic expressions.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A Type type) {
286N/A if (type == Type.String) {
286N/A translateTo(classGen, methodGen, (StringType) type);
286N/A }
286N/A else if (type == Type.Boolean) {
286N/A translateTo(classGen, methodGen, (BooleanType) type);
286N/A }
286N/A else if (type == Type.Real) {
286N/A translateTo(classGen, methodGen, (RealType) type);
286N/A }
286N/A else if (type == Type.NodeSet) {
286N/A translateTo(classGen, methodGen, (NodeSetType) type);
286N/A }
286N/A else if (type == Type.Reference) {
286N/A translateTo(classGen, methodGen, (ReferenceType) type);
286N/A }
286N/A else if (type == Type.Object) {
286N/A translateTo(classGen, methodGen, (ObjectType) type);
286N/A }
286N/A else {
286N/A ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
286N/A toString(), type.toString());
286N/A classGen.getParser().reportError(Constants.FATAL, err);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Expects a node on the stack and pushes its string value.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A StringType type) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A
286N/A switch (_type) {
286N/A case NodeTest.ROOT:
286N/A case NodeTest.ELEMENT:
286N/A il.append(methodGen.loadDOM());
286N/A il.append(SWAP); // dom ref must be below node index
286N/A int index = cpg.addInterfaceMethodref(DOM_INTF,
286N/A GET_ELEMENT_VALUE,
286N/A GET_ELEMENT_VALUE_SIG);
286N/A il.append(new INVOKEINTERFACE(index, 2));
286N/A break;
286N/A
286N/A case NodeTest.ANODE:
286N/A case NodeTest.COMMENT:
286N/A case NodeTest.ATTRIBUTE:
286N/A case NodeTest.PI:
286N/A il.append(methodGen.loadDOM());
286N/A il.append(SWAP); // dom ref must be below node index
286N/A index = cpg.addInterfaceMethodref(DOM_INTF,
286N/A GET_NODE_VALUE,
286N/A GET_NODE_VALUE_SIG);
286N/A il.append(new INVOKEINTERFACE(index, 2));
286N/A break;
286N/A
286N/A default:
286N/A ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
286N/A toString(), type.toString());
286N/A classGen.getParser().reportError(Constants.FATAL, err);
286N/A break;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Translates a node into a synthesized boolean.
286N/A * If the expression is "@attr",
286N/A * then "true" is pushed iff "attr" is an attribute of the current node.
286N/A * If the expression is ".", the result is always "true".
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A BooleanType type) {
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A FlowList falsel = translateToDesynthesized(classGen, methodGen, type);
286N/A il.append(ICONST_1);
286N/A final BranchHandle truec = il.append(new GOTO(null));
286N/A falsel.backPatch(il.append(ICONST_0));
286N/A truec.setTarget(il.append(NOP));
286N/A }
286N/A
286N/A /**
286N/A * Expects a node on the stack and pushes a real.
286N/A * First the node is converted to string, and from string to real.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A RealType type) {
286N/A translateTo(classGen, methodGen, Type.String);
286N/A Type.String.translateTo(classGen, methodGen, Type.Real);
286N/A }
286N/A
286N/A /**
286N/A * Expects a node on the stack and pushes a singleton node-set. Singleton
286N/A * iterators are already started after construction.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A NodeSetType type) {
286N/A ConstantPoolGen cpg = classGen.getConstantPool();
286N/A InstructionList il = methodGen.getInstructionList();
286N/A
286N/A // Create a new instance of SingletonIterator
286N/A il.append(new NEW(cpg.addClass(SINGLETON_ITERATOR)));
286N/A il.append(DUP_X1);
286N/A il.append(SWAP);
286N/A final int init = cpg.addMethodref(SINGLETON_ITERATOR, "<init>",
286N/A "(" + NODE_SIG +")V");
286N/A il.append(new INVOKESPECIAL(init));
286N/A }
286N/A
286N/A /**
286N/A * Subsume Node into ObjectType.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A ObjectType type) {
286N/A methodGen.getInstructionList().append(NOP);
286N/A }
286N/A
286N/A /**
286N/A * Translates a node into a non-synthesized boolean. It does not push a
286N/A * 0 or a 1 but instead returns branchhandle list to be appended to the
286N/A * false list.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateToDesynthesized
286N/A */
286N/A public FlowList translateToDesynthesized(ClassGenerator classGen,
286N/A MethodGenerator methodGen,
286N/A BooleanType type) {
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A return new FlowList(il.append(new IFEQ(null)));
286N/A }
286N/A
286N/A /**
286N/A * Expects a node on the stack and pushes a boxed node. Boxed nodes
286N/A * are represented by an instance of <code>com.sun.org.apache.xalan.internal.xsltc.dom.Node</code>.
286N/A *
286N/A * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A ReferenceType type) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A il.append(new NEW(cpg.addClass(RUNTIME_NODE_CLASS)));
286N/A il.append(DUP_X1);
286N/A il.append(SWAP);
286N/A il.append(new PUSH(cpg, _type));
286N/A il.append(new INVOKESPECIAL(cpg.addMethodref(RUNTIME_NODE_CLASS,
286N/A "<init>", "(II)V")));
286N/A }
286N/A
286N/A /**
286N/A * Translates a node into the Java type denoted by <code>clazz</code>.
286N/A * Expects a node on the stack and pushes an object of the appropriate
286N/A * type after coercion.
286N/A */
286N/A public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
286N/A Class clazz) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A
286N/A String className = clazz.getName();
286N/A if (className.equals("java.lang.String")) {
286N/A translateTo(classGen, methodGen, Type.String);
286N/A return;
286N/A }
286N/A
286N/A il.append(methodGen.loadDOM());
286N/A il.append(SWAP); // dom ref must be below node index
286N/A
286N/A if (className.equals("org.w3c.dom.Node") ||
286N/A className.equals("java.lang.Object")) {
286N/A int index = cpg.addInterfaceMethodref(DOM_INTF,
286N/A MAKE_NODE,
286N/A MAKE_NODE_SIG);
286N/A il.append(new INVOKEINTERFACE(index, 2));
286N/A }
286N/A else if (className.equals("org.w3c.dom.NodeList")) {
286N/A int index = cpg.addInterfaceMethodref(DOM_INTF,
286N/A MAKE_NODE_LIST,
286N/A MAKE_NODE_LIST_SIG);
286N/A il.append(new INVOKEINTERFACE(index, 2));
286N/A }
286N/A else {
286N/A ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
286N/A toString(), className);
286N/A classGen.getParser().reportError(Constants.FATAL, err);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Translates an object of this type to its boxed representation.
286N/A */
286N/A public void translateBox(ClassGenerator classGen,
286N/A MethodGenerator methodGen) {
286N/A translateTo(classGen, methodGen, Type.Reference);
286N/A }
286N/A
286N/A /**
286N/A * Translates an object of this type to its unboxed representation.
286N/A */
286N/A public void translateUnBox(ClassGenerator classGen,
286N/A MethodGenerator methodGen) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A il.append(new CHECKCAST(cpg.addClass(RUNTIME_NODE_CLASS)));
286N/A il.append(new GETFIELD(cpg.addFieldref(RUNTIME_NODE_CLASS,
286N/A NODE_FIELD,
286N/A NODE_FIELD_SIG)));
286N/A }
286N/A
286N/A /**
286N/A * Returns the class name of an internal type's external representation.
286N/A */
286N/A public String getClassName() {
286N/A return(RUNTIME_NODE_CLASS);
286N/A }
286N/A
286N/A public Instruction LOAD(int slot) {
286N/A return new ILOAD(slot);
286N/A }
286N/A
286N/A public Instruction STORE(int slot) {
286N/A return new ISTORE(slot);
286N/A }
286N/A}