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: Text.java,v 1.17 2004/02/16 22:25:10 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  21. import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
  22. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  24. import com.sun.org.apache.bcel.internal.generic.PUSH;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  28. /**
  29. * @author Jacek Ambroziak
  30. * @author Santiago Pericas-Geertsen
  31. * @author Morten Jorgensen
  32. */
  33. final class Text extends Instruction {
  34. private String _text;
  35. private boolean _escaping = true;
  36. private boolean _ignore = false;
  37. private boolean _textElement = false;
  38. /**
  39. * Create a blank Text syntax tree node.
  40. */
  41. public Text() {
  42. _textElement = true;
  43. }
  44. /**
  45. * Create text syntax tree node.
  46. * @param text is the text to put in the node.
  47. */
  48. public Text(String text) {
  49. _text = text;
  50. }
  51. /**
  52. * Returns the text wrapped inside this node
  53. * @return The text wrapped inside this node
  54. */
  55. protected String getText() {
  56. return _text;
  57. }
  58. /**
  59. * Set the text for this node. Appends the given text to any already
  60. * existing text (using string concatenation, so use only when needed).
  61. * @param text is the text to wrap inside this node.
  62. */
  63. protected void setText(String text) {
  64. if (_text == null)
  65. _text = text;
  66. else
  67. _text = _text + text;
  68. }
  69. public void display(int indent) {
  70. indent(indent);
  71. Util.println("Text");
  72. indent(indent + IndentIncrement);
  73. Util.println(_text);
  74. }
  75. public void parseContents(Parser parser) {
  76. final String str = getAttribute("disable-output-escaping");
  77. if ((str != null) && (str.equals("yes"))) _escaping = false;
  78. parseChildren(parser);
  79. if (_text == null) {
  80. if (_textElement) {
  81. _text = EMPTYSTRING;
  82. }
  83. else {
  84. _ignore = true;
  85. }
  86. }
  87. else if (_textElement) {
  88. if (_text.length() == 0) _ignore = true;
  89. }
  90. else if (getParent() instanceof LiteralElement) {
  91. LiteralElement element = (LiteralElement)getParent();
  92. String space = element.getAttribute("xml:space");
  93. if ((space == null) || (!space.equals("preserve")))
  94. if (_text.trim().length() == 0) _ignore = true;
  95. }
  96. else {
  97. if (_text.trim().length() == 0) _ignore = true;
  98. }
  99. }
  100. public void ignore() {
  101. _ignore = true;
  102. }
  103. public boolean isIgnore() {
  104. return _ignore;
  105. }
  106. public boolean isTextElement() {
  107. return _textElement;
  108. }
  109. protected boolean contextDependent() {
  110. return false;
  111. }
  112. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  113. final ConstantPoolGen cpg = classGen.getConstantPool();
  114. final InstructionList il = methodGen.getInstructionList();
  115. if (!_ignore) {
  116. // Turn off character escaping if so is wanted.
  117. final int esc = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
  118. "setEscaping", "(Z)Z");
  119. if (!_escaping) {
  120. il.append(methodGen.loadHandler());
  121. il.append(new PUSH(cpg, false));
  122. il.append(new INVOKEINTERFACE(esc, 2));
  123. }
  124. il.append(methodGen.loadHandler());
  125. // Call characters(String) or characters(char[],int,int), as
  126. // appropriate.
  127. if (!canLoadAsArrayOffsetLength()) {
  128. final int characters = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
  129. "characters",
  130. "("+STRING_SIG+")V");
  131. il.append(new PUSH(cpg, _text));
  132. il.append(new INVOKEINTERFACE(characters, 2));
  133. } else {
  134. final int characters = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
  135. "characters",
  136. "([CII)V");
  137. loadAsArrayOffsetLength(classGen, methodGen);
  138. il.append(new INVOKEINTERFACE(characters, 4));
  139. }
  140. // Restore character escaping setting to whatever it was.
  141. // Note: setEscaping(bool) returns the original (old) value
  142. if (!_escaping) {
  143. il.append(methodGen.loadHandler());
  144. il.append(SWAP);
  145. il.append(new INVOKEINTERFACE(esc, 2));
  146. il.append(POP);
  147. }
  148. }
  149. translateContents(classGen, methodGen);
  150. }
  151. /**
  152. * Check whether this Text node can be stored in a char[] in the translet.
  153. * Calling this is precondition to calling loadAsArrayOffsetLength.
  154. * @see #loadAsArrayOffsetLength(ClassGenerator,MethodGenerator)
  155. * @return true if this Text node can be
  156. */
  157. public boolean canLoadAsArrayOffsetLength() {
  158. // Magic number! 21845*3 == 65535. BCEL uses a DataOutputStream to
  159. // serialize class files. The Java run-time places a limit on the size
  160. // of String data written using a DataOutputStream - it cannot require
  161. // more than 64KB when represented as UTF-8. The number of bytes
  162. // required to represent a Java string as UTF-8 cannot be greater
  163. // than three times the number of char's in the string, hence the
  164. // check for 21845.
  165. return (_text.length() <= 21845);
  166. }
  167. /**
  168. * Generates code that loads the array that will contain the character
  169. * data represented by this Text node, followed by the offset of the
  170. * data from the start of the array, and then the length of the data.
  171. *
  172. * The pre-condition to calling this method is that
  173. * canLoadAsArrayOffsetLength() returns true.
  174. * @see #canLoadArrayOffsetLength()
  175. */
  176. public void loadAsArrayOffsetLength(ClassGenerator classGen,
  177. MethodGenerator methodGen) {
  178. final ConstantPoolGen cpg = classGen.getConstantPool();
  179. final InstructionList il = methodGen.getInstructionList();
  180. final XSLTC xsltc = classGen.getParser().getXSLTC();
  181. // The XSLTC object keeps track of character data
  182. // that is to be stored in char arrays.
  183. final int offset = xsltc.addCharacterData(_text);
  184. final int length = _text.length();
  185. String charDataFieldName =
  186. STATIC_CHAR_DATA_FIELD + (xsltc.getCharacterDataCount()-1);
  187. il.append(new GETSTATIC(cpg.addFieldref(xsltc.getClassName(),
  188. charDataFieldName,
  189. STATIC_CHAR_DATA_FIELD_SIG)));
  190. il.append(new PUSH(cpg, offset));
  191. il.append(new PUSH(cpg, _text.length()));
  192. }
  193. }