001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: ValueOf.java 468650 2006-10-28 07:03:30Z minchau $
020 */
021
022 package org.apache.xalan.xsltc.compiler;
023
024 import org.apache.bcel.generic.ConstantPoolGen;
025 import org.apache.bcel.generic.INVOKEINTERFACE;
026 import org.apache.bcel.generic.INVOKEVIRTUAL;
027 import org.apache.bcel.generic.InstructionList;
028 import org.apache.bcel.generic.PUSH;
029 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
030 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
031 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
032 import org.apache.xalan.xsltc.compiler.util.Type;
033 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
034 import org.apache.xalan.xsltc.compiler.util.Util;
035
036 /**
037 * @author Jacek Ambroziak
038 * @author Santiago Pericas-Geertsen
039 * @author Morten Jorgensen
040 */
041 final class ValueOf extends Instruction {
042 private Expression _select;
043 private boolean _escaping = true;
044 private boolean _isString = false;
045
046 public void display(int indent) {
047 indent(indent);
048 Util.println("ValueOf");
049 indent(indent + IndentIncrement);
050 Util.println("select " + _select.toString());
051 }
052
053 public void parseContents(Parser parser) {
054 _select = parser.parseExpression(this, "select", null);
055
056 // make sure required attribute(s) have been set
057 if (_select.isDummy()) {
058 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
059 return;
060 }
061 final String str = getAttribute("disable-output-escaping");
062 if ((str != null) && (str.equals("yes"))) _escaping = false;
063 }
064
065 public Type typeCheck(SymbolTable stable) throws TypeCheckError {
066 Type type = _select.typeCheck(stable);
067
068 // Prefer to handle the value as a node; fall back to String, otherwise
069 if (type != null && !type.identicalTo(Type.Node)) {
070 /***
071 *** %HZ% Would like to treat result-tree fragments in the same
072 *** %HZ% way as node sets for value-of, but that's running into
073 *** %HZ% some snags. Instead, they'll be converted to String
074 if (type.identicalTo(Type.ResultTree)) {
075 _select = new CastExpr(new CastExpr(_select, Type.NodeSet),
076 Type.Node);
077 } else
078 ***/
079 if (type.identicalTo(Type.NodeSet)) {
080 _select = new CastExpr(_select, Type.Node);
081 } else {
082 _isString = true;
083 if (!type.identicalTo(Type.String)) {
084 _select = new CastExpr(_select, Type.String);
085 }
086 _isString = true;
087 }
088 }
089 return Type.Void;
090 }
091
092 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
093 final ConstantPoolGen cpg = classGen.getConstantPool();
094 final InstructionList il = methodGen.getInstructionList();
095 final int setEscaping = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
096 "setEscaping","(Z)Z");
097
098 // Turn off character escaping if so is wanted.
099 if (!_escaping) {
100 il.append(methodGen.loadHandler());
101 il.append(new PUSH(cpg,false));
102 il.append(new INVOKEINTERFACE(setEscaping,2));
103 }
104
105 // Translate the contents. If the value is a string, use the
106 // translet.characters(String, TranslatOutputHandler) method.
107 // Otherwise, the value is a node, and the
108 // dom.characters(int node, TransletOutputHandler) method can dispatch
109 // the string value of the node to the output handler more efficiently.
110 if (_isString) {
111 final int characters = cpg.addMethodref(TRANSLET_CLASS,
112 CHARACTERSW,
113 CHARACTERSW_SIG);
114
115 il.append(classGen.loadTranslet());
116 _select.translate(classGen, methodGen);
117 il.append(methodGen.loadHandler());
118 il.append(new INVOKEVIRTUAL(characters));
119 } else {
120 final int characters = cpg.addInterfaceMethodref(DOM_INTF,
121 CHARACTERS,
122 CHARACTERS_SIG);
123
124 il.append(methodGen.loadDOM());
125 _select.translate(classGen, methodGen);
126 il.append(methodGen.loadHandler());
127 il.append(new INVOKEINTERFACE(characters, 3));
128 }
129
130 // Restore character escaping setting to whatever it was.
131 if (!_escaping) {
132 il.append(methodGen.loadHandler());
133 il.append(SWAP);
134 il.append(new INVOKEINTERFACE(setEscaping,2));
135 il.append(POP);
136 }
137 }
138 }