KickJava   Java API By Example, From Geeks To Geeks.

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


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: Comment.java,v 1.8 2004/02/16 22:24:29 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.GETFIELD;
24 import org.apache.bcel.generic.INVOKEINTERFACE;
25 import org.apache.bcel.generic.INVOKEVIRTUAL;
26 import org.apache.bcel.generic.PUSH;
27 import org.apache.bcel.generic.InstructionList;
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 Comment extends Instruction {
39
40     public void parseContents(Parser parser) {
41     parseChildren(parser);
42     }
43
44     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
45     typeCheckContents(stable);
46     return Type.String;
47     }
48
49     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
50     final ConstantPoolGen cpg = classGen.getConstantPool();
51     final InstructionList il = methodGen.getInstructionList();
52
53         // Shortcut for literal strings
54
Text rawText = null;
55         if (elementCount() == 1) {
56             Object JavaDoc content = elementAt(0);
57             if (content instanceof Text) {
58                 rawText = (Text) content;
59             }
60         }
61
62         // If the content is literal text, call comment(char[],int,int) or
63
// comment(String), as appropriate. Otherwise, use a
64
// StringValueHandler to gather the textual content of the xsl:comment
65
// and call comment(String) with the result.
66
if (rawText != null) {
67             il.append(methodGen.loadHandler());
68
69             if (rawText.canLoadAsArrayOffsetLength()) {
70                 rawText.loadAsArrayOffsetLength(classGen, methodGen);
71                 final int comment =
72                         cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
73                                                   "comment",
74                                                   "([CII)V");
75                 il.append(new INVOKEINTERFACE(comment, 4));
76             } else {
77                 il.append(new PUSH(cpg, rawText.getText()));
78                 final int comment =
79                         cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
80                                                   "comment",
81                                                   "(" + STRING_SIG + ")V");
82                 il.append(new INVOKEINTERFACE(comment, 2));
83             }
84         } else {
85             // Save the current handler base on the stack
86
il.append(methodGen.loadHandler());
87             il.append(DUP); // first arg to "comment" call
88

89             // Get the translet's StringValueHandler
90
il.append(classGen.loadTranslet());
91             il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
92                                                    "stringValueHandler",
93                                                    STRING_VALUE_HANDLER_SIG)));
94             il.append(DUP);
95             il.append(methodGen.storeHandler());
96
97             // translate contents with substituted handler
98
translateContents(classGen, methodGen);
99
100             // get String out of the handler
101
il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
102                                                          "getValue",
103                                                          "()" + STRING_SIG)));
104             // call "comment"
105
final int comment =
106                         cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
107                                                   "comment",
108                                                   "(" + STRING_SIG + ")V");
109             il.append(new INVOKEINTERFACE(comment, 2));
110             // Restore old handler base from stack
111
il.append(methodGen.storeHandler());
112         }
113     }
114 }
115
Popular Tags