1 56 57 package org.jdom; 58 59 69 public class Text extends Content { 70 71 private static final String CVS_ID = 72 "@(#) $RCSfile: Text.java,v $ $Revision: 1.24 $ $Date: 2004/02/27 11:32:57 $ $Name: $"; 73 74 static final String EMPTY_STRING = ""; 75 76 77 protected String value; 81 82 87 protected Text() { } 88 89 98 public Text(String str) { 99 setText(str); 100 } 101 102 108 public String getText() { 109 return value; 110 } 111 112 118 public String getTextTrim() { 119 return getText().trim(); 120 } 121 122 129 public String getTextNormalize() { 130 return normalizeString(getText()); 131 } 132 133 144 public static String normalizeString(String str) { 145 if (str == null) 146 return EMPTY_STRING; 147 148 char[] c = str.toCharArray(); 149 char[] n = new char[c.length]; 150 boolean white = true; 151 int pos = 0; 152 for (int i = 0; i < c.length; i++) { 153 if (" \t\n\r".indexOf(c[i]) != -1) { 154 if (!white) { 155 n[pos++] = ' '; 156 white = true; 157 } 158 } 159 else { 160 n[pos++] = c[i]; 161 white = false; 162 } 163 } 164 if (white && pos > 0) { 165 pos--; 166 } 167 return new String (n, 0, pos); 168 } 169 170 179 public Text setText(String str) { 180 String reason; 181 182 if (str == null) { 183 value = EMPTY_STRING; 184 return this; 185 } 186 187 if ((reason = Verifier.checkCharacterData(str)) != null) { 188 throw new IllegalDataException(str, "character content", reason); 189 } 190 value = str; 191 return this; 192 } 193 194 203 public void append(String str) { 204 String reason; 205 206 if (str == null) { 207 return; 208 } 209 if ((reason = Verifier.checkCharacterData(str)) != null) { 210 throw new IllegalDataException(str, "character content", reason); 211 } 212 213 if (str == EMPTY_STRING) 214 value = str; 215 else value += str; 216 } 217 218 224 public void append(Text text) { 225 if (text == null) { 226 return; 227 } 228 value += text.getText(); 229 } 230 231 237 public String getValue() { 238 return value; 239 } 240 241 251 public String toString() { 252 return new StringBuffer (64) 253 .append("[Text: ") 254 .append(getText()) 255 .append("]") 256 .toString(); 257 } 258 259 265 public Object clone() { 266 Text text = (Text)super.clone(); 267 text.value = value; 268 return text; 269 } 270 271 } 272 | Popular Tags |