1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import org.apache.bcel.generic.ConstantPoolGen; 23 import org.apache.bcel.generic.GETSTATIC; 24 import org.apache.bcel.generic.INVOKEINTERFACE; 25 import org.apache.bcel.generic.InstructionList; 26 import org.apache.bcel.generic.PUSH; 27 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 28 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 29 import org.apache.xalan.xsltc.compiler.util.Util; 30 31 36 final class Text extends Instruction { 37 38 private String _text; 39 private boolean _escaping = true; 40 private boolean _ignore = false; 41 private boolean _textElement = false; 42 43 46 public Text() { 47 _textElement = true; 48 } 49 50 54 public Text(String text) { 55 _text = text; 56 } 57 58 62 protected String getText() { 63 return _text; 64 } 65 66 71 protected void setText(String text) { 72 if (_text == null) 73 _text = text; 74 else 75 _text = _text + text; 76 } 77 78 public void display(int indent) { 79 indent(indent); 80 Util.println("Text"); 81 indent(indent + IndentIncrement); 82 Util.println(_text); 83 } 84 85 public void parseContents(Parser parser) { 86 final String str = getAttribute("disable-output-escaping"); 87 if ((str != null) && (str.equals("yes"))) _escaping = false; 88 89 parseChildren(parser); 90 91 if (_text == null) { 92 if (_textElement) { 93 _text = EMPTYSTRING; 94 } 95 else { 96 _ignore = true; 97 } 98 } 99 else if (_textElement) { 100 if (_text.length() == 0) _ignore = true; 101 } 102 else if (getParent() instanceof LiteralElement) { 103 LiteralElement element = (LiteralElement)getParent(); 104 String space = element.getAttribute("xml:space"); 105 if ((space == null) || (!space.equals("preserve"))) 106 if (_text.trim().length() == 0) _ignore = true; 107 } 108 else { 109 if (_text.trim().length() == 0) _ignore = true; 110 } 111 } 112 113 public void ignore() { 114 _ignore = true; 115 } 116 117 public boolean isIgnore() { 118 return _ignore; 119 } 120 121 public boolean isTextElement() { 122 return _textElement; 123 } 124 125 protected boolean contextDependent() { 126 return false; 127 } 128 129 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 130 final ConstantPoolGen cpg = classGen.getConstantPool(); 131 final InstructionList il = methodGen.getInstructionList(); 132 133 if (!_ignore) { 134 final int esc = cpg.addInterfaceMethodref(OUTPUT_HANDLER, 136 "setEscaping", "(Z)Z"); 137 if (!_escaping) { 138 il.append(methodGen.loadHandler()); 139 il.append(new PUSH(cpg, false)); 140 il.append(new INVOKEINTERFACE(esc, 2)); 141 } 142 143 il.append(methodGen.loadHandler()); 144 145 if (!canLoadAsArrayOffsetLength()) { 148 final int characters = cpg.addInterfaceMethodref(OUTPUT_HANDLER, 149 "characters", 150 "("+STRING_SIG+")V"); 151 il.append(new PUSH(cpg, _text)); 152 il.append(new INVOKEINTERFACE(characters, 2)); 153 } else { 154 final int characters = cpg.addInterfaceMethodref(OUTPUT_HANDLER, 155 "characters", 156 "([CII)V"); 157 loadAsArrayOffsetLength(classGen, methodGen); 158 il.append(new INVOKEINTERFACE(characters, 4)); 159 } 160 161 if (!_escaping) { 164 il.append(methodGen.loadHandler()); 165 il.append(SWAP); 166 il.append(new INVOKEINTERFACE(esc, 2)); 167 il.append(POP); 168 } 169 } 170 translateContents(classGen, methodGen); 171 } 172 173 179 public boolean canLoadAsArrayOffsetLength() { 180 188 return (_text.length() <= 21845); 189 } 190 191 200 public void loadAsArrayOffsetLength(ClassGenerator classGen, 201 MethodGenerator methodGen) { 202 final ConstantPoolGen cpg = classGen.getConstantPool(); 203 final InstructionList il = methodGen.getInstructionList(); 204 final XSLTC xsltc = classGen.getParser().getXSLTC(); 205 206 final int offset = xsltc.addCharacterData(_text); 209 final int length = _text.length(); 210 String charDataFieldName = 211 STATIC_CHAR_DATA_FIELD + (xsltc.getCharacterDataCount()-1); 212 213 il.append(new GETSTATIC(cpg.addFieldref(xsltc.getClassName(), 214 charDataFieldName, 215 STATIC_CHAR_DATA_FIELD_SIG))); 216 il.append(new PUSH(cpg, offset)); 217 il.append(new PUSH(cpg, _text.length())); 218 } 219 } 220 | Popular Tags |