2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
* $Id: ValueOf.java,v 1.2 2009/12/10 03:18:18 matthewoliver Exp $
22
package org.apache.xalan.xsltc.compiler;
24
import org.apache.bcel.generic.ConstantPoolGen;
25
import org.apache.bcel.generic.INVOKEINTERFACE;
26
import org.apache.bcel.generic.INVOKEVIRTUAL;
27
import org.apache.bcel.generic.InstructionList;
28
import org.apache.bcel.generic.PUSH;
29
import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
30
import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
31
import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
32
import org.apache.xalan.xsltc.compiler.util.Type;
33
import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
34
import org.apache.xalan.xsltc.compiler.util.Util;
37
* @author Jacek Ambroziak
38
* @author Santiago Pericas-Geertsen
39
* @author Morten Jorgensen
41
final class ValueOf extends Instruction {
42
private Expression _select;
43
private boolean _escaping = true;
44
private boolean _isString = false;
46
public void display(int indent) {
48
Util.println("ValueOf");
49
indent(indent + IndentIncrement);
50
Util.println("select " + _select.toString());
53
public void parseContents(Parser parser) {
54
_select = parser.parseExpression(this, "select", null);
56
// make sure required attribute(s) have been set
57
if (_select.isDummy()) {
58
reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
61
final String str = getAttribute("disable-output-escaping");
62
if ((str != null) && (str.equals("yes"))) _escaping = false;
65
public Type typeCheck(SymbolTable stable) throws TypeCheckError {
66
Type type = _select.typeCheck(stable);
68
// Prefer to handle the value as a node; fall back to String, otherwise
69
if (type != null && !type.identicalTo(Type.Node)) {
71
*** %HZ% Would like to treat result-tree fragments in the same
72
*** %HZ% way as node sets for value-of, but that's running into
73
*** %HZ% some snags. Instead, they'll be converted to String
74
if (type.identicalTo(Type.ResultTree)) {
75
_select = new CastExpr(new CastExpr(_select, Type.NodeSet),
79
if (type.identicalTo(Type.NodeSet)) {
80
_select = new CastExpr(_select, Type.Node);
83
if (!type.identicalTo(Type.String)) {
84
_select = new CastExpr(_select, Type.String);
92
public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
93
final ConstantPoolGen cpg = classGen.getConstantPool();
94
final InstructionList il = methodGen.getInstructionList();
95
final int setEscaping = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
96
"setEscaping","(Z)Z");
98
// Turn off character escaping if so is wanted.
100
il.append(methodGen.loadHandler());
101
il.append(new PUSH(cpg,false));
102
il.append(new INVOKEINTERFACE(setEscaping,2));
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.
111
final int characters = cpg.addMethodref(TRANSLET_CLASS,
115
il.append(classGen.loadTranslet());
116
_select.translate(classGen, methodGen);
117
il.append(methodGen.loadHandler());
118
il.append(new INVOKEVIRTUAL(characters));
120
final int characters = cpg.addInterfaceMethodref(DOM_INTF,
124
il.append(methodGen.loadDOM());
125
_select.translate(classGen, methodGen);
126
il.append(methodGen.loadHandler());
127
il.append(new INVOKEINTERFACE(characters, 3));
130
// Restore character escaping setting to whatever it was.
132
il.append(methodGen.loadHandler());
134
il.append(new INVOKEINTERFACE(setEscaping,2));