KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > ValueOf


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ValueOf.java,v 1.11 2004/02/16 22:25:10 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
23 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
24 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
25 import com.sun.org.apache.bcel.internal.generic.InstructionList;
26 import com.sun.org.apache.bcel.internal.generic.PUSH;
27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
33
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  * @author Morten Jorgensen
38  */

39 final class ValueOf extends Instruction {
40     private Expression _select;
41     private boolean _escaping = true;
42     private boolean _isString = false;
43
44     public void display(int indent) {
45         indent(indent);
46         Util.println("ValueOf");
47         indent(indent + IndentIncrement);
48         Util.println("select " + _select.toString());
49     }
50
51     public void parseContents(Parser parser) {
52         _select = parser.parseExpression(this, "select", null);
53
54         // make sure required attribute(s) have been set
55
if (_select.isDummy()) {
56             reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
57             return;
58         }
59         final String JavaDoc str = getAttribute("disable-output-escaping");
60         if ((str != null) && (str.equals("yes"))) _escaping = false;
61     }
62
63     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
64         Type type = _select.typeCheck(stable);
65
66         // Prefer to handle the value as a node; fall back to String, otherwise
67
if (type != null && !type.identicalTo(Type.Node)) {
68             /***
69              *** %HZ% Would like to treat result-tree fragments in the same
70              *** %HZ% way as node sets for value-of, but that's running into
71              *** %HZ% some snags. Instead, they'll be converted to String
72             if (type.identicalTo(Type.ResultTree)) {
73                 _select = new CastExpr(new CastExpr(_select, Type.NodeSet),
74                                        Type.Node);
75             } else
76             ***/

77             if (type.identicalTo(Type.NodeSet)) {
78                 _select = new CastExpr(_select, Type.Node);
79             } else {
80                 _isString = true;
81                 if (!type.identicalTo(Type.String)) {
82                     _select = new CastExpr(_select, Type.String);
83                 }
84                 _isString = true;
85             }
86         }
87         return Type.Void;
88     }
89
90     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
91         final ConstantPoolGen cpg = classGen.getConstantPool();
92         final InstructionList il = methodGen.getInstructionList();
93         final int setEscaping = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
94                                                           "setEscaping","(Z)Z");
95
96         // Turn off character escaping if so is wanted.
97
if (!_escaping) {
98             il.append(methodGen.loadHandler());
99             il.append(new PUSH(cpg,false));
100             il.append(new INVOKEINTERFACE(setEscaping,2));
101         }
102
103         // Translate the contents. If the value is a string, use the
104
// translet.characters(String, TranslatOutputHandler) method.
105
// Otherwise, the value is a node, and the
106
// dom.characters(int node, TransletOutputHandler) method can dispatch
107
// the string value of the node to the output handler more efficiently.
108
if (_isString) {
109             final int characters = cpg.addMethodref(TRANSLET_CLASS,
110                                                     CHARACTERSW,
111                                                     CHARACTERSW_SIG);
112
113             il.append(classGen.loadTranslet());
114             _select.translate(classGen, methodGen);
115             il.append(methodGen.loadHandler());
116             il.append(new INVOKEVIRTUAL(characters));
117         } else {
118             final int characters = cpg.addInterfaceMethodref(DOM_INTF,
119                                                              CHARACTERS,
120                                                              CHARACTERS_SIG);
121
122             il.append(methodGen.loadDOM());
123             _select.translate(classGen, methodGen);
124             il.append(methodGen.loadHandler());
125             il.append(new INVOKEINTERFACE(characters, 3));
126         }
127
128         // Restore character escaping setting to whatever it was.
129
if (!_escaping) {
130             il.append(methodGen.loadHandler());
131             il.append(SWAP);
132             il.append(new INVOKEINTERFACE(setEscaping,2));
133             il.append(POP);
134         }
135     }
136 }
137
Popular Tags