1 21 22 package nu.xom; 23 24 import java.io.UnsupportedEncodingException ; 25 26 37 public class Text extends Node { 38 39 40 private byte[] data; 41 42 43 60 public Text(String data) { 61 _setValue(data); 62 } 63 64 65 72 public Text(Text text) { 73 this.data = text.data; 77 } 78 79 80 private Text() {} 81 82 83 static Text build(String data) { 84 85 Text result = new Text(); 86 try { 87 result.data = data.getBytes("UTF8"); 88 } 89 catch (UnsupportedEncodingException ex) { 90 throw new RuntimeException ( 91 "Bad VM! Does not support UTF-8" 92 ); 93 } 94 return result; 95 96 } 97 98 99 116 public void setValue(String data) { 117 _setValue(data); 118 } 119 120 121 private void _setValue(String data) { 122 123 if (data == null) data = ""; 124 else Verifier.checkPCDATA(data); 125 try { 126 this.data = data.getBytes("UTF8"); 127 } 128 catch (UnsupportedEncodingException ex) { 129 throw new RuntimeException ( 130 "Bad VM! Does not support UTF-8" 131 ); 132 } 133 134 } 135 136 145 public final String getValue() { 146 147 try { 148 return new String (data, "UTF8"); 149 } 150 catch (UnsupportedEncodingException ex) { 151 throw new RuntimeException ( 152 "Bad VM! Does not support UTF-8" 153 ); 154 } 155 156 } 157 158 159 173 public final Node getChild(int position) { 174 throw new IndexOutOfBoundsException ( 175 "LeafNodes do not have children"); 176 } 177 178 179 186 public final int getChildCount() { 187 return 0; 188 } 189 190 191 199 public Node copy() { 200 201 if (isCDATASection()) { 202 return new CDATASection(this); 203 } 204 else { 205 return new Text(this); 206 } 207 208 } 209 210 211 222 public final String toXML() { 223 return escapeText(getValue()); 224 } 225 226 227 private static String escapeText(String s) { 228 229 int length = s.length(); 230 StringBuffer result = new StringBuffer (length+12); 232 for (int i = 0; i < length; i++) { 233 char c = s.charAt(i); 234 switch (c) { 235 case '\r': 236 result.append("
"); 237 break; 238 case 14: 239 break; 241 case 15: 242 break; 244 case 16: 245 break; 247 case 17: 248 break; 250 case 18: 251 break; 253 case 19: 254 break; 256 case 20: 257 break; 259 case 21: 260 break; 262 case 22: 263 break; 265 case 23: 266 break; 268 case 24: 269 break; 271 case 25: 272 break; 274 case 26: 275 break; 277 case 27: 278 break; 280 case 28: 281 break; 283 case 29: 284 break; 286 case 30: 287 break; 289 case 31: 290 break; 292 case ' ': 293 result.append(' '); 294 break; 295 case '!': 296 result.append('!'); 297 break; 298 case '"': 299 result.append('"'); 300 break; 301 case '#': 302 result.append('#'); 303 break; 304 case '$': 305 result.append('$'); 306 break; 307 case '%': 308 result.append('%'); 309 break; 310 case '&': 311 result.append("&"); 312 break; 313 case '\'': 314 result.append('\''); 315 break; 316 case '(': 317 result.append('('); 318 break; 319 case ')': 320 result.append(')'); 321 break; 322 case '*': 323 result.append('*'); 324 break; 325 case '+': 326 result.append('+'); 327 break; 328 case ',': 329 result.append(','); 330 break; 331 case '-': 332 result.append('-'); 333 break; 334 case '.': 335 result.append('.'); 336 break; 337 case '/': 338 result.append('/'); 339 break; 340 case '0': 341 result.append('0'); 342 break; 343 case '1': 344 result.append('1'); 345 break; 346 case '2': 347 result.append('2'); 348 break; 349 case '3': 350 result.append('3'); 351 break; 352 case '4': 353 result.append('4'); 354 break; 355 case '5': 356 result.append('5'); 357 break; 358 case '6': 359 result.append('6'); 360 break; 361 case '7': 362 result.append('7'); 363 break; 364 case '8': 365 result.append('8'); 366 break; 367 case '9': 368 result.append('9'); 369 break; 370 case ':': 371 result.append(':'); 372 break; 373 case ';': 374 result.append(';'); 375 break; 376 case '<': 377 result.append("<"); 378 break; 379 case '=': 380 result.append('='); 381 break; 382 case '>': 383 result.append(">"); 384 break; 385 default: 386 result.append(c); 387 } 388 } 389 390 return result.toString(); 391 392 } 393 394 395 boolean isText() { 396 return true; 397 } 398 399 400 410 public final String toString() { 411 412 return "[" + getClass().getName() + ": " 413 + escapeLineBreaksAndTruncate(getValue()) + "]"; 414 415 } 416 417 418 static String escapeLineBreaksAndTruncate(String s) { 419 420 int length = s.length(); 421 boolean tooLong = length > 40; 422 if (length > 40) { 423 length = 35; 424 s = s.substring(0, 35); 425 } 426 427 StringBuffer result = new StringBuffer (length); 428 for (int i = 0; i < length; i++) { 429 char c = s.charAt(i); 430 switch (c) { 431 case '\n': 432 result.append("\\n"); 433 break; 434 case '\r': 435 result.append("\\r"); 436 break; 437 case '\t': 438 result.append("\\t"); 439 break; 440 default: 441 result.append(c); 442 } 443 } 444 if (tooLong) result.append("..."); 445 446 return result.toString(); 447 448 } 449 450 451 boolean isCDATASection() { 452 return false; 453 } 454 455 456 boolean isEmpty() { 457 return this.data.length == 0; 458 } 459 460 461 } | Popular Tags |