1 7 8 package javax.swing.text; 9 10 import java.io.Writer ; 11 import java.io.IOException ; 12 import java.util.Enumeration ; 13 14 24 25 public abstract class AbstractWriter { 26 27 private ElementIterator it; 28 private Writer out; 29 private int indentLevel = 0; 30 private int indentSpace = 2; 31 private Document doc = null; 32 private int maxLineLength = 100; 33 private int currLength = 0; 34 private int startOffset = 0; 35 private int endOffset = 0; 36 private int offsetIndent = 0; 40 41 47 private String lineSeparator; 48 49 54 private boolean canWrapLines; 55 56 60 private boolean isLineEmpty; 61 62 65 private char[] indentChars; 66 67 70 private char[] tempChars; 71 72 78 private char[] newlineChars; 79 80 83 private Segment segment; 84 85 89 protected static final char NEWLINE = '\n'; 90 91 92 100 protected AbstractWriter(Writer w, Document doc) { 101 this(w, doc, 0, doc.getLength()); 102 } 103 104 115 protected AbstractWriter(Writer w, Document doc, int pos, int len) { 116 this.doc = doc; 117 it = new ElementIterator (doc.getDefaultRootElement()); 118 out = w; 119 startOffset = pos; 120 endOffset = pos + len; 121 Object docNewline = doc.getProperty(DefaultEditorKit. 122 EndOfLineStringProperty); 123 if (docNewline instanceof String ) { 124 setLineSeparator((String )docNewline); 125 } 126 else { 127 String newline = null; 128 try { 129 newline = System.getProperty("line.separator"); 130 } catch (SecurityException se) {} 131 if (newline == null) { 132 newline = "\n"; 135 } 136 setLineSeparator(newline); 137 } 138 canWrapLines = true; 139 } 140 141 149 protected AbstractWriter(Writer w, Element root) { 150 this(w, root, 0, root.getEndOffset()); 151 } 152 153 164 protected AbstractWriter(Writer w, Element root, int pos, int len) { 165 this.doc = root.getDocument(); 166 it = new ElementIterator (root); 167 out = w; 168 startOffset = pos; 169 endOffset = pos + len; 170 canWrapLines = true; 171 } 172 173 178 public int getStartOffset() { 179 return startOffset; 180 } 181 182 187 public int getEndOffset() { 188 return endOffset; 189 } 190 191 196 protected ElementIterator getElementIterator() { 197 return it; 198 } 199 200 205 protected Writer getWriter() { 206 return out; 207 } 208 209 214 protected Document getDocument() { 215 return doc; 216 } 217 218 229 protected boolean inRange(Element next) { 230 int startOffset = getStartOffset(); 231 int endOffset = getEndOffset(); 232 if ((next.getStartOffset() >= startOffset && 233 next.getStartOffset() < endOffset) || 234 (startOffset >= next.getStartOffset() && 235 startOffset < next.getEndOffset())) { 236 return true; 237 } 238 return false; 239 } 240 241 247 abstract protected void write() throws IOException , BadLocationException ; 248 249 260 protected String getText(Element elem) throws BadLocationException { 261 return doc.getText(elem.getStartOffset(), 262 elem.getEndOffset() - elem.getStartOffset()); 263 } 264 265 266 276 protected void text(Element elem) throws BadLocationException , 277 IOException { 278 int start = Math.max(getStartOffset(), elem.getStartOffset()); 279 int end = Math.min(getEndOffset(), elem.getEndOffset()); 280 if (start < end) { 281 if (segment == null) { 282 segment = new Segment (); 283 } 284 getDocument().getText(start, end - start, segment); 285 if (segment.count > 0) { 286 write(segment.array, segment.offset, segment.count); 287 } 288 } 289 } 290 291 297 protected void setLineLength(int l) { 298 maxLineLength = l; 299 } 300 301 306 protected int getLineLength() { 307 return maxLineLength; 308 } 309 310 315 protected void setCurrentLineLength(int length) { 316 currLength = length; 317 isLineEmpty = (currLength == 0); 318 } 319 320 325 protected int getCurrentLineLength() { 326 return currLength; 327 } 328 329 336 protected boolean isLineEmpty() { 337 return isLineEmpty; 338 } 339 340 347 protected void setCanWrapLines(boolean newValue) { 348 canWrapLines = newValue; 349 } 350 351 357 protected boolean getCanWrapLines() { 358 return canWrapLines; 359 } 360 361 368 protected void setIndentSpace(int space) { 369 indentSpace = space; 370 } 371 372 377 protected int getIndentSpace() { 378 return indentSpace; 379 } 380 381 388 public void setLineSeparator(String value) { 389 lineSeparator = value; 390 } 391 392 397 public String getLineSeparator() { 398 return lineSeparator; 399 } 400 401 406 protected void incrIndent() { 407 if (offsetIndent > 0) { 409 offsetIndent++; 410 } 411 else { 412 if (++indentLevel * getIndentSpace() >= getLineLength()) { 413 offsetIndent++; 414 --indentLevel; 415 } 416 } 417 } 418 419 422 protected void decrIndent() { 423 if (offsetIndent > 0) { 424 --offsetIndent; 425 } 426 else { 427 indentLevel--; 428 } 429 } 430 431 438 protected int getIndentLevel() { 439 return indentLevel; 440 } 441 442 450 protected void indent() throws IOException { 451 int max = getIndentLevel() * getIndentSpace(); 452 if (indentChars == null || max > indentChars.length) { 453 indentChars = new char[max]; 454 for (int counter = 0; counter < max; counter++) { 455 indentChars[counter] = ' '; 456 } 457 } 458 int length = getCurrentLineLength(); 459 boolean wasEmpty = isLineEmpty(); 460 output(indentChars, 0, max); 461 if (wasEmpty && length == 0) { 462 isLineEmpty = true; 463 } 464 } 465 466 473 protected void write(char ch) throws IOException { 474 if (tempChars == null) { 475 tempChars = new char[128]; 476 } 477 tempChars[0] = ch; 478 write(tempChars, 0, 1); 479 } 480 481 488 protected void write(String content) throws IOException { 489 if (content == null) { 490 return; 491 } 492 int size = content.length(); 493 if (tempChars == null || tempChars.length < size) { 494 tempChars = new char[size]; 495 } 496 content.getChars(0, size, tempChars, 0); 497 write(tempChars, 0, size); 498 } 499 500 506 protected void writeLineSeparator() throws IOException { 507 String newline = getLineSeparator(); 508 int length = newline.length(); 509 if (newlineChars == null || newlineChars.length < length) { 510 newlineChars = new char[length]; 511 } 512 newline.getChars(0, length, newlineChars, 0); 513 output(newlineChars, 0, length); 514 setCurrentLineLength(0); 515 } 516 517 530 protected void write(char[] chars, int startIndex, int length) 531 throws IOException { 532 if (!getCanWrapLines()) { 533 int lastIndex = startIndex; 536 int endIndex = startIndex + length; 537 int newlineIndex = indexOf(chars, NEWLINE, startIndex, endIndex); 538 while (newlineIndex != -1) { 539 if (newlineIndex > lastIndex) { 540 output(chars, lastIndex, newlineIndex - lastIndex); 541 } 542 writeLineSeparator(); 543 lastIndex = newlineIndex + 1; 544 newlineIndex = indexOf(chars, '\n', lastIndex, endIndex); 545 } 546 if (lastIndex < endIndex) { 547 output(chars, lastIndex, endIndex - lastIndex); 548 } 549 } 550 else { 551 int lastIndex = startIndex; 553 int endIndex = startIndex + length; 554 int lineLength = getCurrentLineLength(); 555 int maxLength = getLineLength(); 556 557 while (lastIndex < endIndex) { 558 int newlineIndex = indexOf(chars, NEWLINE, lastIndex, 559 endIndex); 560 boolean needsNewline = false; 561 boolean forceNewLine = false; 562 563 lineLength = getCurrentLineLength(); 564 if (newlineIndex != -1 && (lineLength + 565 (newlineIndex - lastIndex)) < maxLength) { 566 if (newlineIndex > lastIndex) { 567 output(chars, lastIndex, newlineIndex - lastIndex); 568 } 569 lastIndex = newlineIndex + 1; 570 forceNewLine = true; 571 } 572 else if (newlineIndex == -1 && (lineLength + 573 (endIndex - lastIndex)) < maxLength) { 574 if (endIndex > lastIndex) { 575 output(chars, lastIndex, endIndex - lastIndex); 576 } 577 lastIndex = endIndex; 578 } 579 else { 580 int breakPoint = -1; 584 int maxBreak = Math.min(endIndex - lastIndex, 585 maxLength - lineLength - 1); 586 int counter = 0; 587 while (counter < maxBreak) { 588 if (Character.isWhitespace(chars[counter + 589 lastIndex])) { 590 breakPoint = counter; 591 } 592 counter++; 593 } 594 if (breakPoint != -1) { 595 breakPoint += lastIndex + 1; 597 output(chars, lastIndex, breakPoint - lastIndex); 598 lastIndex = breakPoint; 599 needsNewline = true; 600 } 601 else { 602 604 counter = Math.max(0, maxBreak); 609 maxBreak = endIndex - lastIndex; 610 while (counter < maxBreak) { 611 if (Character.isWhitespace(chars[counter + 612 lastIndex])) { 613 breakPoint = counter; 614 break; 615 } 616 counter++; 617 } 618 if (breakPoint == -1) { 619 output(chars, lastIndex, endIndex - lastIndex); 620 breakPoint = endIndex; 621 } 622 else { 623 breakPoint += lastIndex; 624 if (chars[breakPoint] == NEWLINE) { 625 output(chars, lastIndex, breakPoint++ - 626 lastIndex); 627 forceNewLine = true; 628 } 629 else { 630 output(chars, lastIndex, ++breakPoint - 631 lastIndex); 632 needsNewline = true; 633 } 634 } 635 lastIndex = breakPoint; 636 } 637 } 638 if (forceNewLine || needsNewline || lastIndex < endIndex) { 639 writeLineSeparator(); 640 if (lastIndex < endIndex || !forceNewLine) { 641 indent(); 642 } 643 } 644 } 645 } 646 } 647 648 655 protected void writeAttributes(AttributeSet attr) throws IOException { 656 657 Enumeration names = attr.getAttributeNames(); 658 while (names.hasMoreElements()) { 659 Object name = names.nextElement(); 660 write(" " + name + "=" + attr.getAttribute(name)); 661 } 662 } 663 664 677 protected void output(char[] content, int start, int length) 678 throws IOException { 679 getWriter().write(content, start, length); 680 setCurrentLineLength(getCurrentLineLength() + length); 681 } 682 683 686 private int indexOf(char[] chars, char sChar, int startIndex, 687 int endIndex) { 688 while(startIndex < endIndex) { 689 if (chars[startIndex] == sChar) { 690 return startIndex; 691 } 692 startIndex++; 693 } 694 return -1; 695 } 696 } 697 | Popular Tags |