KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > Message


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: Message.java,v 1.11 2004/02/16 22:24:28 minchau Exp $
18  */

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

38 final class Message extends Instruction {
39     private boolean _terminate = false;
40
41     public void parseContents(Parser parser) {
42     String JavaDoc termstr = getAttribute("terminate");
43     if (termstr != null) {
44             _terminate = termstr.equals("yes");
45     }
46     parseChildren(parser);
47     }
48
49     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
50     typeCheckContents(stable);
51     return Type.Void;
52     }
53
54     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
55     final ConstantPoolGen cpg = classGen.getConstantPool();
56     final InstructionList il = methodGen.getInstructionList();
57
58     // Load the translet (for call to displayMessage() function)
59
il.append(classGen.loadTranslet());
60
61         switch (elementCount()) {
62             case 0:
63                 il.append(new PUSH(cpg, ""));
64             break;
65             case 1:
66                 SyntaxTreeNode child = (SyntaxTreeNode) elementAt(0);
67                 if (child instanceof Text) {
68                     il.append(new PUSH(cpg, ((Text) child).getText()));
69                     break;
70                 }
71                 // falls through
72
default:
73                 // Push current output handler onto the stack
74
il.append(methodGen.loadHandler());
75
76                 // Replace the current output handler by a ToXMLStream
77
il.append(new NEW(cpg.addClass(STREAM_XML_OUTPUT)));
78                 il.append(methodGen.storeHandler());
79
80                 // Push a reference to a StringWriter
81
il.append(new NEW(cpg.addClass(STRING_WRITER)));
82                 il.append(DUP);
83                 il.append(DUP);
84                 il.append(new INVOKESPECIAL(
85                     cpg.addMethodref(STRING_WRITER, "<init>", "()V")));
86
87                 // Load ToXMLStream
88
il.append(methodGen.loadHandler());
89                 il.append(new INVOKESPECIAL(
90                     cpg.addMethodref(STREAM_XML_OUTPUT, "<init>",
91                                      "()V")));
92
93                 // Invoke output.setWriter(STRING_WRITER)
94
il.append(methodGen.loadHandler());
95                 il.append(SWAP);
96                 il.append(new INVOKEVIRTUAL(
97                     cpg.addMethodref(OUTPUT_BASE, "setWriter",
98                                      "("+WRITER_SIG+")V")));
99
100                 // Invoke output.setEncoding("UTF-8")
101
il.append(methodGen.loadHandler());
102                 il.append(new PUSH(cpg, "UTF-8")); // other encodings?
103
il.append(new INVOKEVIRTUAL(
104                     cpg.addMethodref(OUTPUT_BASE, "setEncoding",
105                                      "("+STRING_SIG+")V")));
106
107                 // Invoke output.setOmitXMLDeclaration(true)
108
il.append(methodGen.loadHandler());
109                 il.append(ICONST_1);
110                 il.append(new INVOKEVIRTUAL(
111                     cpg.addMethodref(OUTPUT_BASE, "setOmitXMLDeclaration",
112                                      "(Z)V")));
113
114                 il.append(methodGen.loadHandler());
115                 il.append(new INVOKEVIRTUAL(
116                     cpg.addMethodref(OUTPUT_BASE, "startDocument",
117                                      "()V")));
118
119                 // Inline translation of contents
120
translateContents(classGen, methodGen);
121
122                 il.append(methodGen.loadHandler());
123                 il.append(new INVOKEVIRTUAL(
124                     cpg.addMethodref(OUTPUT_BASE, "endDocument",
125                                      "()V")));
126
127                 // Call toString() on StringWriter
128
il.append(new INVOKEVIRTUAL(
129                     cpg.addMethodref(STRING_WRITER, "toString",
130                                      "()" + STRING_SIG)));
131
132                 // Restore old output handler
133
il.append(SWAP);
134                 il.append(methodGen.storeHandler());
135             break;
136         }
137
138     // Send the resulting string to the message handling method
139
il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
140                              "displayMessage",
141                              "("+STRING_SIG+")V")));
142
143     // If 'terminate' attribute is set to 'yes': Instanciate a
144
// RunTimeException, but it on the stack and throw an exception
145
if (_terminate == true) {
146         // Create a new instance of RunTimeException
147
final int einit = cpg.addMethodref("java.lang.RuntimeException",
148                            "<init>",
149                            "(Ljava/lang/String;)V");
150         il.append(new NEW(cpg.addClass("java.lang.RuntimeException")));
151         il.append(DUP);
152         il.append(new PUSH(cpg,"Termination forced by an " +
153                        "xsl:message instruction"));
154         il.append(new INVOKESPECIAL(einit));
155         il.append(ATHROW);
156     }
157     }
158
159 }
160
Popular Tags