1 16 package org.apache.cocoon.generation.asciiart; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import org.apache.regexp.RE; 22 import org.apache.regexp.RESyntaxException; 23 24 31 public class AsciiArtPad { 32 33 private int width; 34 private int height; 35 36 39 private List pad; 40 41 private double xGrid; 42 private double yGrid; 43 44 45 48 public AsciiArtPad() { 49 pad = new ArrayList (); 50 } 51 52 53 59 public AsciiArtPad(int w, int h) { 60 width = w; 61 height = h; 62 63 pad = new ArrayList (); 64 } 65 66 67 72 public void setWidth(int width) { 73 this.width = width; 74 } 75 76 77 82 public void setHeight(int height) { 83 this.height = height; 84 } 85 86 87 92 public void setXGrid(double xGrid) { 93 this.xGrid = xGrid; 94 } 95 96 97 102 public void setYGrid(double yGrid) { 103 this.yGrid = yGrid; 104 } 105 106 107 112 public int getWidth() { 113 return width; 114 } 115 116 117 122 public int getHeight() { 123 return height; 124 } 125 126 127 132 public double getXGrid() { 133 return xGrid; 134 } 135 136 137 142 public double getYGrid() { 143 return yGrid; 144 } 145 146 147 152 public void add(Object o) { 153 pad.add(o); 154 } 155 156 157 162 public Iterator iterator() { 163 return pad.iterator(); 164 } 165 166 167 171 public static class AsciiArtLine implements AsciiArtElement { 172 double xStart; 173 double yStart; 174 double xEnd; 175 double yEnd; 176 177 178 184 public AsciiArtLine(AsciiArtCoordinate start, AsciiArtCoordinate end) { 185 xStart = start.getXDouble(); 186 yStart = start.getYDouble(); 187 xEnd = end.getXDouble(); 188 yEnd = end.getYDouble(); 189 } 190 191 192 197 public void setXStart(double xStart) { 198 this.xStart = xStart; 199 } 200 201 202 207 public void setYStart(double yStart) { 208 this.yStart = yStart; 209 } 210 211 212 217 public void setXEnd(double xEnd) { 218 this.xEnd = xEnd; 219 } 220 221 222 227 public void setYEnd(double yEnd) { 228 this.yEnd = yEnd; 229 } 230 231 232 237 public double getXStart() { 238 return xStart; 239 } 240 241 242 247 public double getYStart() { 248 return yStart; 249 } 250 251 252 257 public double getXEnd() { 258 return xEnd; 259 } 260 261 262 267 public double getYEnd() { 268 return yEnd; 269 } 270 271 272 277 public String toString() { 278 String s = 279 "[xStart:" + String.valueOf(xStart) + "]" + 280 "[yStart:" + String.valueOf(yStart) + "]" + 281 "[xEnd:" + String.valueOf(xEnd) + "]" + 282 "[yEnd:" + String.valueOf(yEnd) + "]"; 283 return s; 284 } 285 } 286 287 288 292 public static class AsciiArtRect implements AsciiArtElement { 293 double xUpperLeft; 294 double yUpperLeft; 295 double xLowerRight; 296 double yLowerRight; 297 298 299 305 public AsciiArtRect(AsciiArtCoordinate upperLeft, AsciiArtCoordinate lowerRight) { 306 xUpperLeft = upperLeft.getXDouble(); 307 yUpperLeft = upperLeft.getYDouble(); 308 xLowerRight = lowerRight.getXDouble(); 309 yLowerRight = lowerRight.getYDouble(); 310 } 311 312 313 318 public void setXUpperLeft(double xUpperLeft) { 319 this.xUpperLeft = xUpperLeft; 320 } 321 322 323 328 public void setYUpperLeft(double yUpperLeft) { 329 this.yUpperLeft = yUpperLeft; 330 } 331 332 333 338 public void setXLowerRight(double xLowerRight) { 339 this.xLowerRight = xLowerRight; 340 } 341 342 343 348 public void setYLowerRight(double yLowerRight) { 349 this.yLowerRight = yLowerRight; 350 } 351 352 353 358 public double getXUpperLeft() { 359 return xUpperLeft; 360 } 361 362 363 368 public double getYUpperLeft() { 369 return yUpperLeft; 370 } 371 372 373 378 public double getXLowerRight() { 379 return xLowerRight; 380 } 381 382 383 388 public double getYLowerRight() { 389 return yLowerRight; 390 } 391 392 393 398 public double getWidth() { 399 return Math.abs(xUpperLeft - xLowerRight); 400 } 401 402 403 408 public double getHeight() { 409 return Math.abs(yUpperLeft - yLowerRight); 410 } 411 412 413 418 public String toString() { 419 String s = 420 "[xUpperLeft:" + String.valueOf(xUpperLeft) + "]" + 421 "[yUpperLeft:" + String.valueOf(yUpperLeft) + "]" + 422 "[xLowerRight:" + String.valueOf(xLowerRight) + "]" + 423 "[yLowerRight:" + String.valueOf(yLowerRight) + "]"; 424 return s; 425 } 426 } 427 428 429 433 public static class AsciiArtString implements AsciiArtElement { 434 private double x; 435 private double y; 436 private String s; 437 438 439 445 public AsciiArtString(AsciiArtCoordinate aac, String s) { 446 this.x = aac.getXDouble(); 447 this.y = aac.getYDouble(); 448 this.s = s; 449 } 450 451 452 457 public void setX(double x) { 458 this.x = x; 459 } 460 461 462 467 public void setY(double y) { 468 this.y = y; 469 } 470 471 472 477 public void setS(String s) { 478 this.s = s; 479 } 480 481 482 487 public double getX() { 488 return x; 489 } 490 491 492 497 public double getY() { 498 return y; 499 } 500 501 502 507 public String getS() { 508 return s; 509 } 510 511 512 517 public String toString() { 518 String s = 519 "[x:" + String.valueOf(x) + "]" + 520 "[y:" + String.valueOf(y) + "]" + 521 "[s:" + String.valueOf(this.s) + "]"; 522 return s; 523 } 524 } 525 526 527 531 public static class AsciiArtCoordinate { 532 int x, y; 533 AsciiArtPad asciiArtPad; 534 double tx, ty; 535 536 537 540 public AsciiArtCoordinate() { } 541 542 543 548 public AsciiArtCoordinate(AsciiArtPad asciiArtPad) { 549 setAsciiArtPad(asciiArtPad); 550 } 551 552 553 559 public AsciiArtCoordinate(int x, int y) { 560 setXY(x, y); 561 } 562 563 564 569 public void setAsciiArtPad(AsciiArtPad asciiArtPad) { 570 this.asciiArtPad = asciiArtPad; 571 } 572 573 574 580 public void setTransXY(double tx, double ty) { 581 this.tx = tx; 582 this.ty = ty; 583 } 584 585 586 592 public void setXY(int x, int y) { 593 this.x = x; 594 this.y = y; 595 } 596 597 598 603 public double getXDouble() { 604 return x * asciiArtPad.getXGrid() + tx; 605 } 606 607 608 613 public double getYDouble() { 614 return y * asciiArtPad.getYGrid() + ty; 615 } 616 } 617 618 619 624 public static class AsciiArt { 625 private String [] s; 626 private int w; 627 private int h; 628 629 630 635 public AsciiArt(String [] s) { 636 this.s = s; 637 int length = s.length; 638 h = length; 639 w = 0; 640 for (int i = 0; i < length; i++) { 641 String line = s[i]; 642 if (line != null && line.length() > w) { 643 w = line.length(); 644 } 645 } 646 } 647 648 649 654 public int getW() { 655 return w; 656 } 657 658 659 664 public int getH() { 665 return h; 666 } 667 668 669 675 public String getRow(int r) { 676 String row = this.s[r]; 677 return row; 678 } 679 680 681 687 public String getColumn(int c) { 688 StringBuffer column = new StringBuffer (); 689 690 final String EMPTY_CHAR = " "; 691 for (int i = 0; i < s.length; i++) { 692 if (s[i] != null && c < s[i].length()) { 693 column.append(s[i].charAt(c)); 694 } else { 695 column.append(EMPTY_CHAR); 696 } 697 } 698 return column.toString(); 699 } 700 } 701 702 703 707 public static class AsciiArtPadBuilder { 708 private AsciiArtPad asciiArtPad; 709 private AsciiArt aa; 710 711 final static String EDGE_GROUP = "[+\\\\/]"; 712 final static String HLINE_GROUP = "[\\-~=+]"; 713 final static String VLINE_GROUP = "[|+]"; 714 715 final static String STRING_SUFFIX_GROUP = "[^\\-|~=\\/+ \\\\]"; 716 final static String STRING_PREFIX_GROUP = STRING_SUFFIX_GROUP; 718 719 720 725 public AsciiArtPadBuilder(AsciiArtPad asciiArtPad) { 726 this.asciiArtPad = asciiArtPad; 727 } 728 729 730 735 public void build(String [] asciiArt) { 736 aa = new AsciiArt(asciiArt); 737 asciiArtPad.setWidth(aa.getW()); 738 asciiArtPad.setHeight(aa.getH()); 739 740 findRectPattern(); 742 findCornerPattern(); 743 findLinePattern(); 744 findStringPattern(); 745 } 746 747 748 752 protected void findRectPattern() { 753 } 754 755 756 759 protected void findCornerPattern() { 760 AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); 761 aacStart.setTransXY(0, asciiArtPad.getYGrid() / 2); 762 AsciiArtCoordinate aacEnd = new AsciiArtCoordinate(this.asciiArtPad); 763 aacEnd.setTransXY(0, asciiArtPad.getYGrid() / 2); 764 765 try { 767 final RE reCorner = new RE(EDGE_GROUP); 768 for (int r = 0; r < aa.getH(); r++) { 769 String row = aa.getRow(r); 770 int startIndex = 0; 771 while (reCorner.match(row, startIndex)) { 772 String s = reCorner.getParen(0); 773 int mStart = reCorner.getParenStart(0); 774 int mEnd = reCorner.getParenEnd(0); 775 776 if (s.equals("\\")) { 777 aacStart.setXY(mStart, r - 1); 778 aacEnd.setXY(mStart + 1, r); 779 } else if (s.equals("/")) { 780 aacStart.setXY(mStart + 1, r - 1); 781 aacEnd.setXY(mStart, r); 782 } else { 783 aacStart.setXY(mStart, r); 784 aacEnd.setXY(mStart, r); 785 } 786 AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); 787 this.asciiArtPad.add(aal); 788 789 if (startIndex >= mEnd) { 790 break; 791 } 792 startIndex = mEnd; 793 } 794 } 795 } catch (RESyntaxException rese) { 796 rese.printStackTrace(); 797 } 798 799 } 800 801 802 805 protected void findLinePattern() { 806 AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); 807 aacStart.setTransXY(0, asciiArtPad.getYGrid() / 2); 808 AsciiArtCoordinate aacEnd = new AsciiArtCoordinate(this.asciiArtPad); 809 aacEnd.setTransXY(0, asciiArtPad.getYGrid() / 2); 810 811 try { 813 final RE reHorLine = new RE(HLINE_GROUP + "+"); 814 for (int r = 0; r < aa.getH(); r++) { 815 String row = aa.getRow(r); 816 int startIndex = 0; 817 while (reHorLine.match(row, startIndex)) { 818 int mStart = reHorLine.getParenStart(0); 819 int mEnd = reHorLine.getParenEnd(0); 820 821 aacStart.setXY(mStart, r); 822 aacEnd.setXY(mEnd - 1, r); 823 AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); 824 this.asciiArtPad.add(aal); 825 826 if (startIndex >= mEnd) { 827 break; 828 } 829 startIndex = mEnd; 830 } 831 } 832 } catch (RESyntaxException rese) { 833 rese.printStackTrace(); 834 } 835 836 try { 838 RE reVerLine = new RE(VLINE_GROUP + "+"); 839 for (int c = 0; c < aa.getW(); c++) { 840 String col = aa.getColumn(c); 841 int startIndex = 0; 842 while (reVerLine.match(col, startIndex)) { 843 int mStart = reVerLine.getParenStart(0); 844 int mEnd = reVerLine.getParenEnd(0); 845 846 aacStart.setXY(c, mStart); 847 aacEnd.setXY(c, mEnd - 1); 848 AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); 849 this.asciiArtPad.add(aal); 850 851 if (startIndex >= mEnd) { 852 break; 853 } 854 startIndex = mEnd; 855 } 856 } 857 } catch (RESyntaxException rese) { 858 rese.printStackTrace(); 859 } 860 } 861 862 863 866 protected void findStringPattern() { 867 AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); 868 aacStart.setTransXY(0, 3 * asciiArtPad.getYGrid() / 4); 869 try { 871 final RE reString = new RE(STRING_PREFIX_GROUP + STRING_SUFFIX_GROUP + "*"); 872 for (int r = 0; r < aa.getH(); r++) { 873 String row = aa.getRow(r); 874 int startIndex = 0; 875 while (reString.match(row, startIndex)) { 876 String s = reString.getParen(0); 877 int mStart = reString.getParenStart(0); 878 int mEnd = reString.getParenEnd(0); 879 880 aacStart.setXY(mStart, r); 881 AsciiArtString aas = new AsciiArtString(aacStart, s); 882 this.asciiArtPad.add(aas); 883 884 if (startIndex >= mEnd) { 885 break; 886 } 887 startIndex = mEnd; 888 } 889 } 890 } catch (RESyntaxException rese) { 891 rese.printStackTrace(); 892 } 893 } 894 895 } 896 897 898 902 public static interface AsciiArtElement { 903 } 904 } 905 906 | Popular Tags |