View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: Comment.java,v 1.2.4.1 2005/09/01 12:02:37 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
27  import com.sun.org.apache.bcel.internal.generic.GETFIELD;
28  import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
29  import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
30  import com.sun.org.apache.bcel.internal.generic.PUSH;
31  import com.sun.org.apache.bcel.internal.generic.InstructionList;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
33  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
36  
37  /**
38   * @author Jacek Ambroziak
39   * @author Santiago Pericas-Geertsen
40   * @author Morten Jorgensen
41   */
42  final class Comment extends Instruction {
43  
44      public void parseContents(Parser parser) {
45          parseChildren(parser);
46      }
47  
48      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
49          typeCheckContents(stable);
50          return Type.String;
51      }
52  
53      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
54          final ConstantPoolGen cpg = classGen.getConstantPool();
55          final InstructionList il = methodGen.getInstructionList();
56  
57          // Shortcut for literal strings
58          Text rawText = null;
59          if (elementCount() == 1) {
60              Object content = elementAt(0);
61              if (content instanceof Text) {
62                  rawText = (Text) content;
63              }
64          }
65  
66          // If the content is literal text, call comment(char[],int,int) or
67          // comment(String), as appropriate.  Otherwise, use a
68          // StringValueHandler to gather the textual content of the xsl:comment
69          // and call comment(String) with the result.
70          if (rawText != null) {
71              il.append(methodGen.loadHandler());
72  
73              if (rawText.canLoadAsArrayOffsetLength()) {
74                  rawText.loadAsArrayOffsetLength(classGen, methodGen);
75                  final int comment =
76                          cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
77                                                    "comment",
78                                                    "([CII)V");
79                  il.append(new INVOKEINTERFACE(comment, 4));
80              } else {
81                  il.append(new PUSH(cpg, rawText.getText()));
82                  final int comment =
83                          cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
84                                                    "comment",
85                                                    "(" + STRING_SIG + ")V");
86                  il.append(new INVOKEINTERFACE(comment, 2));
87              }
88          } else {
89              // Save the current handler base on the stack
90              il.append(methodGen.loadHandler());
91              il.append(DUP);             // first arg to "comment" call
92  
93              // Get the translet's StringValueHandler
94              il.append(classGen.loadTranslet());
95              il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
96                                                     "stringValueHandler",
97                                                     STRING_VALUE_HANDLER_SIG)));
98              il.append(DUP);
99              il.append(methodGen.storeHandler());
100 
101             // translate contents with substituted handler
102             translateContents(classGen, methodGen);
103 
104             // get String out of the handler
105             il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
106                                                          "getValue",
107                                                          "()" + STRING_SIG)));
108             // call "comment"
109             final int comment =
110                         cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
111                                                   "comment",
112                                                   "(" + STRING_SIG + ")V");
113             il.append(new INVOKEINTERFACE(comment, 2));
114             // Restore old handler base from stack
115             il.append(methodGen.storeHandler());
116         }
117     }
118 }