1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2001-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 /* 21 * $Id: ValueOf.java,v 1.2.4.1 2005/09/05 09:30:04 pvedula Exp $ 22 */ 23 24 package com.sun.org.apache.xalan.internal.xsltc.compiler; 25 26 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 27 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE; 28 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL; 29 import com.sun.org.apache.bcel.internal.generic.InstructionList; 30 import com.sun.org.apache.bcel.internal.generic.PUSH; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; 37 38 /** 39 * @author Jacek Ambroziak 40 * @author Santiago Pericas-Geertsen 41 * @author Morten Jorgensen 42 */ 43 final class ValueOf extends Instruction { 44 private Expression _select; 45 private boolean _escaping = true; 46 private boolean _isString = false; 47 48 public void display(int indent) { 49 indent(indent); 50 Util.println("ValueOf"); 51 indent(indent + IndentIncrement); 52 Util.println("select " + _select.toString()); 53 } 54 55 public void parseContents(Parser parser) { 56 _select = parser.parseExpression(this, "select", null); 57 58 // make sure required attribute(s) have been set 59 if (_select.isDummy()) { 60 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select"); 61 return; 62 } 63 final String str = getAttribute("disable-output-escaping"); 64 if ((str != null) && (str.equals("yes"))) _escaping = false; 65 } 66 67 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 68 Type type = _select.typeCheck(stable); 69 70 // Prefer to handle the value as a node; fall back to String, otherwise 71 if (type != null && !type.identicalTo(Type.Node)) { 72 /*** 73 *** %HZ% Would like to treat result-tree fragments in the same 74 *** %HZ% way as node sets for value-of, but that's running into 75 *** %HZ% some snags. Instead, they'll be converted to String 76 if (type.identicalTo(Type.ResultTree)) { 77 _select = new CastExpr(new CastExpr(_select, Type.NodeSet), 78 Type.Node); 79 } else 80 ***/ 81 if (type.identicalTo(Type.NodeSet)) { 82 _select = new CastExpr(_select, Type.Node); 83 } else { 84 _isString = true; 85 if (!type.identicalTo(Type.String)) { 86 _select = new CastExpr(_select, Type.String); 87 } 88 _isString = true; 89 } 90 } 91 return Type.Void; 92 } 93 94 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 95 final ConstantPoolGen cpg = classGen.getConstantPool(); 96 final InstructionList il = methodGen.getInstructionList(); 97 final int setEscaping = cpg.addInterfaceMethodref(OUTPUT_HANDLER, 98 "setEscaping","(Z)Z"); 99 100 // Turn off character escaping if so is wanted. 101 if (!_escaping) { 102 il.append(methodGen.loadHandler()); 103 il.append(new PUSH(cpg,false)); 104 il.append(new INVOKEINTERFACE(setEscaping,2)); 105 } 106 107 // Translate the contents. If the value is a string, use the 108 // translet.characters(String, TranslatOutputHandler) method. 109 // Otherwise, the value is a node, and the 110 // dom.characters(int node, TransletOutputHandler) method can dispatch 111 // the string value of the node to the output handler more efficiently. 112 if (_isString) { 113 final int characters = cpg.addMethodref(TRANSLET_CLASS, 114 CHARACTERSW, 115 CHARACTERSW_SIG); 116 117 il.append(classGen.loadTranslet()); 118 _select.translate(classGen, methodGen); 119 il.append(methodGen.loadHandler()); 120 il.append(new INVOKEVIRTUAL(characters)); 121 } else { 122 final int characters = cpg.addInterfaceMethodref(DOM_INTF, 123 CHARACTERS, 124 CHARACTERS_SIG); 125 126 il.append(methodGen.loadDOM()); 127 _select.translate(classGen, methodGen); 128 il.append(methodGen.loadHandler()); 129 il.append(new INVOKEINTERFACE(characters, 3)); 130 } 131 132 // Restore character escaping setting to whatever it was. 133 if (!_escaping) { 134 il.append(methodGen.loadHandler()); 135 il.append(SWAP); 136 il.append(new INVOKEINTERFACE(setEscaping,2)); 137 il.append(POP); 138 } 139 } 140 }