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: Comment.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.GETFIELD;
026 import org.apache.bcel.generic.INVOKEINTERFACE;
027 import org.apache.bcel.generic.INVOKEVIRTUAL;
028 import org.apache.bcel.generic.PUSH;
029 import org.apache.bcel.generic.InstructionList;
030 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
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
035 /**
036 * @author Jacek Ambroziak
037 * @author Santiago Pericas-Geertsen
038 * @author Morten Jorgensen
039 */
040 final class Comment extends Instruction {
041
042 public void parseContents(Parser parser) {
043 parseChildren(parser);
044 }
045
046 public Type typeCheck(SymbolTable stable) throws TypeCheckError {
047 typeCheckContents(stable);
048 return Type.String;
049 }
050
051 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
052 final ConstantPoolGen cpg = classGen.getConstantPool();
053 final InstructionList il = methodGen.getInstructionList();
054
055 // Shortcut for literal strings
056 Text rawText = null;
057 if (elementCount() == 1) {
058 Object content = elementAt(0);
059 if (content instanceof Text) {
060 rawText = (Text) content;
061 }
062 }
063
064 // If the content is literal text, call comment(char[],int,int) or
065 // comment(String), as appropriate. Otherwise, use a
066 // StringValueHandler to gather the textual content of the xsl:comment
067 // and call comment(String) with the result.
068 if (rawText != null) {
069 il.append(methodGen.loadHandler());
070
071 if (rawText.canLoadAsArrayOffsetLength()) {
072 rawText.loadAsArrayOffsetLength(classGen, methodGen);
073 final int comment =
074 cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
075 "comment",
076 "([CII)V");
077 il.append(new INVOKEINTERFACE(comment, 4));
078 } else {
079 il.append(new PUSH(cpg, rawText.getText()));
080 final int comment =
081 cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
082 "comment",
083 "(" + STRING_SIG + ")V");
084 il.append(new INVOKEINTERFACE(comment, 2));
085 }
086 } else {
087 // Save the current handler base on the stack
088 il.append(methodGen.loadHandler());
089 il.append(DUP); // first arg to "comment" call
090
091 // Get the translet's StringValueHandler
092 il.append(classGen.loadTranslet());
093 il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
094 "stringValueHandler",
095 STRING_VALUE_HANDLER_SIG)));
096 il.append(DUP);
097 il.append(methodGen.storeHandler());
098
099 // translate contents with substituted handler
100 translateContents(classGen, methodGen);
101
102 // get String out of the handler
103 il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
104 "getValue",
105 "()" + STRING_SIG)));
106 // call "comment"
107 final int comment =
108 cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
109 "comment",
110 "(" + STRING_SIG + ")V");
111 il.append(new INVOKEINTERFACE(comment, 2));
112 // Restore old handler base from stack
113 il.append(methodGen.storeHandler());
114 }
115 }
116 }