KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > asciiart > AsciiArtPad


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.generation.asciiart;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import org.apache.regexp.RE;
22 import org.apache.regexp.RESyntaxException;
23
24 /**
25  * A drawing ascii art pad.
26  *
27  * @author huber@apache.org
28  * @since 18. Dezember 2002
29  * @version CVS $Id: AsciiArtPad.java 123810 2004-12-31 17:07:31Z antonio $
30  */

31 public class AsciiArtPad {
32
33     private int width;
34     private int height;
35
36     /**
37      * List of AsciiArt elements
38      */

39     private List JavaDoc pad;
40
41     private double xGrid;
42     private double yGrid;
43
44
45     /**
46      *Constructor for the AsciiArtPad object
47      */

48     public AsciiArtPad() {
49         pad = new ArrayList JavaDoc();
50     }
51
52
53     /**
54      *Constructor for the AsciiArtPad object
55      *
56      *@param w Description of the Parameter
57      *@param h Description of the Parameter
58      */

59     public AsciiArtPad(int w, int h) {
60         width = w;
61         height = h;
62
63         pad = new ArrayList JavaDoc();
64     }
65
66
67     /**
68      * Sets the width attribute of the AsciiArtPad object
69      *
70      *@param width The new width value
71      */

72     public void setWidth(int width) {
73         this.width = width;
74     }
75
76
77     /**
78      * Sets the height attribute of the AsciiArtPad object
79      *
80      *@param height The new height value
81      */

82     public void setHeight(int height) {
83         this.height = height;
84     }
85
86
87     /**
88      * Sets the xGrid attribute of the AsciiArtPad object
89      *
90      *@param xGrid The new xGrid value
91      */

92     public void setXGrid(double xGrid) {
93         this.xGrid = xGrid;
94     }
95
96
97     /**
98      * Sets the yGrid attribute of the AsciiArtPad object
99      *
100      *@param yGrid The new yGrid value
101      */

102     public void setYGrid(double yGrid) {
103         this.yGrid = yGrid;
104     }
105
106
107     /**
108      * Gets the width attribute of the AsciiArtPad object
109      *
110      *@return The width value
111      */

112     public int getWidth() {
113         return width;
114     }
115
116
117     /**
118      * Gets the height attribute of the AsciiArtPad object
119      *
120      *@return The height value
121      */

122     public int getHeight() {
123         return height;
124     }
125
126
127     /**
128      * Gets the xGrid attribute of the AsciiArtPad object
129      *
130      *@return The xGrid value
131      */

132     public double getXGrid() {
133         return xGrid;
134     }
135
136
137     /**
138      * Gets the yGrid attribute of the AsciiArtPad object
139      *
140      *@return The yGrid value
141      */

142     public double getYGrid() {
143         return yGrid;
144     }
145
146
147     /**
148      * Add a AsciiArtElement
149      *
150      *@param o the AsciiArtElement object
151      */

152     public void add(Object JavaDoc o) {
153         pad.add(o);
154     }
155
156
157     /**
158      * Iterator of AsciiArtPad
159      *
160      *@return Iterator iterating over all AsciiArtElements
161      */

162     public Iterator JavaDoc iterator() {
163         return pad.iterator();
164     }
165
166
167     /**
168      * An AsciiArtElement describing a line.
169      *
170      */

171     public static class AsciiArtLine implements AsciiArtElement {
172         double xStart;
173         double yStart;
174         double xEnd;
175         double yEnd;
176
177
178         /**
179          *Constructor for the AsciiArtLine object
180          *
181          *@param start Description of the Parameter
182          *@param end Description of the Parameter
183          */

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         /**
193          * Sets the xStart attribute of the AsciiArtLine object
194          *
195          *@param xStart The new xStart value
196          */

197         public void setXStart(double xStart) {
198             this.xStart = xStart;
199         }
200
201
202         /**
203          * Sets the yStart attribute of the AsciiArtLine object
204          *
205          *@param yStart The new yStart value
206          */

207         public void setYStart(double yStart) {
208             this.yStart = yStart;
209         }
210
211
212         /**
213          * Sets the xEnd attribute of the AsciiArtLine object
214          *
215          *@param xEnd The new xEnd value
216          */

217         public void setXEnd(double xEnd) {
218             this.xEnd = xEnd;
219         }
220
221
222         /**
223          * Sets the yEnd attribute of the AsciiArtLine object
224          *
225          *@param yEnd The new yEnd value
226          */

227         public void setYEnd(double yEnd) {
228             this.yEnd = yEnd;
229         }
230
231
232         /**
233          * Gets the xStart attribute of the AsciiArtLine object
234          *
235          *@return The xStart value
236          */

237         public double getXStart() {
238             return xStart;
239         }
240
241
242         /**
243          * Gets the yStart attribute of the AsciiArtLine object
244          *
245          *@return The yStart value
246          */

247         public double getYStart() {
248             return yStart;
249         }
250
251
252         /**
253          * Gets the xEnd attribute of the AsciiArtLine object
254          *
255          *@return The xEnd value
256          */

257         public double getXEnd() {
258             return xEnd;
259         }
260
261
262         /**
263          * Gets the yEnd attribute of the AsciiArtLine object
264          *
265          *@return The yEnd value
266          */

267         public double getYEnd() {
268             return yEnd;
269         }
270
271
272         /**
273          * Descriptive string
274          *
275          *@return String
276          */

277         public String JavaDoc toString() {
278             String JavaDoc 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     /**
289      * An AsciiArtElement describing a rectangle.
290      *
291      */

292     public static class AsciiArtRect implements AsciiArtElement {
293         double xUpperLeft;
294         double yUpperLeft;
295         double xLowerRight;
296         double yLowerRight;
297
298
299         /**
300          *Constructor for the AsciiArtRect object
301          *
302          *@param upperLeft Description of the Parameter
303          *@param lowerRight Description of the Parameter
304          */

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         /**
314          * Sets the xUpperLeft attribute of the AsciiArtRect object
315          *
316          *@param xUpperLeft The new xUpperLeft value
317          */

318         public void setXUpperLeft(double xUpperLeft) {
319             this.xUpperLeft = xUpperLeft;
320         }
321
322
323         /**
324          * Sets the yUpperLeft attribute of the AsciiArtRect object
325          *
326          *@param yUpperLeft The new yUpperLeft value
327          */

328         public void setYUpperLeft(double yUpperLeft) {
329             this.yUpperLeft = yUpperLeft;
330         }
331
332
333         /**
334          * Sets the xLowerRight attribute of the AsciiArtRect object
335          *
336          *@param xLowerRight The new xLowerRight value
337          */

338         public void setXLowerRight(double xLowerRight) {
339             this.xLowerRight = xLowerRight;
340         }
341
342
343         /**
344          * Sets the yLowerRight attribute of the AsciiArtRect object
345          *
346          *@param yLowerRight The new yLowerRight value
347          */

348         public void setYLowerRight(double yLowerRight) {
349             this.yLowerRight = yLowerRight;
350         }
351
352
353         /**
354          * Gets the xUpperLeft attribute of the AsciiArtRect object
355          *
356          *@return The xUpperLeft value
357          */

358         public double getXUpperLeft() {
359             return xUpperLeft;
360         }
361
362
363         /**
364          * Gets the yUpperLeft attribute of the AsciiArtRect object
365          *
366          *@return The yUpperLeft value
367          */

368         public double getYUpperLeft() {
369             return yUpperLeft;
370         }
371
372
373         /**
374          * Gets the xLowerRight attribute of the AsciiArtRect object
375          *
376          *@return The xLowerRight value
377          */

378         public double getXLowerRight() {
379             return xLowerRight;
380         }
381
382
383         /**
384          * Gets the yLowerRight attribute of the AsciiArtRect object
385          *
386          *@return The yLowerRight value
387          */

388         public double getYLowerRight() {
389             return yLowerRight;
390         }
391
392
393         /**
394          * Gets the width attribute of the AsciiArtRect object
395          *
396          *@return The width value
397          */

398         public double getWidth() {
399             return Math.abs(xUpperLeft - xLowerRight);
400         }
401
402
403         /**
404          * Gets the height attribute of the AsciiArtRect object
405          *
406          *@return The height value
407          */

408         public double getHeight() {
409             return Math.abs(yUpperLeft - yLowerRight);
410         }
411
412
413         /**
414          * Descriptive string
415          *
416          *@return String
417          */

418         public String JavaDoc toString() {
419             String JavaDoc 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     /**
430      * An AsciiArtElement describing a string of text.
431      *
432      */

433     public static class AsciiArtString implements AsciiArtElement {
434         private double x;
435         private double y;
436         private String JavaDoc s;
437
438
439         /**
440          *Constructor for the AsciiArtString object
441          *
442          *@param s Description of the Parameter
443          *@param aac Description of the Parameter
444          */

445         public AsciiArtString(AsciiArtCoordinate aac, String JavaDoc s) {
446             this.x = aac.getXDouble();
447             this.y = aac.getYDouble();
448             this.s = s;
449         }
450
451
452         /**
453          * Sets the x attribute of the AsciiArtString object
454          *
455          *@param x The new x value
456          */

457         public void setX(double x) {
458             this.x = x;
459         }
460
461
462         /**
463          * Sets the y attribute of the AsciiArtString object
464          *
465          *@param y The new y value
466          */

467         public void setY(double y) {
468             this.y = y;
469         }
470
471
472         /**
473          * Sets the s attribute of the AsciiArtString object
474          *
475          *@param s The new s value
476          */

477         public void setS(String JavaDoc s) {
478             this.s = s;
479         }
480
481
482         /**
483          * Gets the x attribute of the AsciiArtString object
484          *
485          *@return The x value
486          */

487         public double getX() {
488             return x;
489         }
490
491
492         /**
493          * Gets the y attribute of the AsciiArtString object
494          *
495          *@return The y value
496          */

497         public double getY() {
498             return y;
499         }
500
501
502         /**
503          * Gets the s attribute of the AsciiArtString object
504          *
505          *@return The s value
506          */

507         public String JavaDoc getS() {
508             return s;
509         }
510
511
512         /**
513          * Descriptive string
514          *
515          *@return String
516          */

517         public String JavaDoc toString() {
518             String JavaDoc 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     /**
528      * Helper class describing a coordinate of AsciiArtPad elements.
529      *
530      */

531     public static class AsciiArtCoordinate {
532         int x, y;
533         AsciiArtPad asciiArtPad;
534         double tx, ty;
535
536
537         /**
538          *Constructor for the AsciiArtCoordinate object
539          */

540         public AsciiArtCoordinate() { }
541
542
543         /**
544          *Constructor for the AsciiArtCoordinate object
545          *
546          *@param asciiArtPad Description of the Parameter
547          */

548         public AsciiArtCoordinate(AsciiArtPad asciiArtPad) {
549             setAsciiArtPad(asciiArtPad);
550         }
551
552
553         /**
554          *Constructor for the AsciiArtCoordinate object
555          *
556          *@param x Description of the Parameter
557          *@param y Description of the Parameter
558          */

559         public AsciiArtCoordinate(int x, int y) {
560             setXY(x, y);
561         }
562
563
564         /**
565          * Sets the asciiArtPad attribute of the AsciiArtCoordinate object
566          *
567          *@param asciiArtPad The new asciiArtPad value
568          */

569         public void setAsciiArtPad(AsciiArtPad asciiArtPad) {
570             this.asciiArtPad = asciiArtPad;
571         }
572
573
574         /**
575          * Sets the xY attribute of the AsciiArtCoordinate object
576          *
577          *@param tx The new transXY value
578          *@param ty The new transXY value
579          */

580         public void setTransXY(double tx, double ty) {
581             this.tx = tx;
582             this.ty = ty;
583         }
584
585
586         /**
587          * Sets the xY attribute of the AsciiArtCoordinate object
588          *
589          *@param x The new xY value
590          *@param y The new xY value
591          */

592         public void setXY(int x, int y) {
593             this.x = x;
594             this.y = y;
595         }
596
597
598         /**
599          * Gets the xDouble attribute of the AsciiArtCoordinate object
600          *
601          *@return The xDouble value
602          */

603         public double getXDouble() {
604             return x * asciiArtPad.getXGrid() + tx;
605         }
606
607
608         /**
609          * Gets the yDouble attribute of the AsciiArtCoordinate object
610          *
611          *@return The yDouble value
612          */

613         public double getYDouble() {
614             return y * asciiArtPad.getYGrid() + ty;
615         }
616     }
617
618
619     /**
620      * Helper class containing the ascii text data,
621      * acting as input of an AsciiArtPad
622      *
623      */

624     public static class AsciiArt {
625         private String JavaDoc[] s;
626         private int w;
627         private int h;
628
629
630         /**
631          *Constructor for the AsciiArt object
632          *
633          *@param s Description of the Parameter
634          */

635         public AsciiArt(String JavaDoc[] 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 JavaDoc line = s[i];
642                 if (line != null && line.length() > w) {
643                     w = line.length();
644                 }
645             }
646         }
647
648
649         /**
650          * Gets the w attribute of the AsciiArt object
651          *
652          *@return The w value
653          */

654         public int getW() {
655             return w;
656         }
657
658
659         /**
660          * Gets the h attribute of the AsciiArt object
661          *
662          *@return The h value
663          */

664         public int getH() {
665             return h;
666         }
667
668
669         /**
670          * Gets the row attribute of the AsciiArt object
671          *
672          *@param r Description of the Parameter
673          *@return The row value
674          */

675         public String JavaDoc getRow(int r) {
676             String JavaDoc row = this.s[r];
677             return row;
678         }
679
680
681         /**
682          * Gets the column attribute of the AsciiArt object
683          *
684          *@param c Description of the Parameter
685          *@return The column value
686          */

687         public String JavaDoc getColumn(int c) {
688             StringBuffer JavaDoc column = new StringBuffer JavaDoc();
689
690             final String JavaDoc 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     /**
704      * Builder of AsciiArtElements from an AsciiArt input.
705      *
706      */

707     public static class AsciiArtPadBuilder {
708         private AsciiArtPad asciiArtPad;
709         private AsciiArt aa;
710
711         final static String JavaDoc EDGE_GROUP = "[+\\\\/]";
712         final static String JavaDoc HLINE_GROUP = "[\\-~=+]";
713         final static String JavaDoc VLINE_GROUP = "[|+]";
714
715         final static String JavaDoc STRING_SUFFIX_GROUP = "[^\\-|~=\\/+ \\\\]";
716         //final static String STRING_PREFIX_GROUP = "[a-zA-Z0-9_\\*;\\.#]";
717
final static String JavaDoc STRING_PREFIX_GROUP = STRING_SUFFIX_GROUP;
718
719
720         /**
721          *Constructor for the AsciiArtPadBuilder object
722          *
723          *@param asciiArtPad Description of the Parameter
724          */

725         public AsciiArtPadBuilder(AsciiArtPad asciiArtPad) {
726             this.asciiArtPad = asciiArtPad;
727         }
728
729
730         /**
731          * Build AsciiArtElement from an asciiArt
732          *
733          *@param asciiArt Description of the Parameter
734          */

735         public void build(String JavaDoc[] asciiArt) {
736             aa = new AsciiArt(asciiArt);
737             asciiArtPad.setWidth(aa.getW());
738             asciiArtPad.setHeight(aa.getH());
739
740             // find asciiArt patterns
741
findRectPattern();
742             findCornerPattern();
743             findLinePattern();
744             findStringPattern();
745         }
746
747
748         /**
749          * Find rectangles in the AsciiArt.
750          * not implemented yet.
751          */

752         protected void findRectPattern() {
753         }
754
755
756         /**
757          * Find corners in the AsciiArt
758          */

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             // hor line
766
try {
767                 final RE reCorner = new RE(EDGE_GROUP);
768                 for (int r = 0; r < aa.getH(); r++) {
769                     String JavaDoc row = aa.getRow(r);
770                     int startIndex = 0;
771                     while (reCorner.match(row, startIndex)) {
772                         String JavaDoc 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         /**
803          * Find lines in the AsciiArt
804          */

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             // hor line
812
try {
813                 final RE reHorLine = new RE(HLINE_GROUP + "+");
814                 for (int r = 0; r < aa.getH(); r++) {
815                     String JavaDoc 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             // ver line
837
try {
838                 RE reVerLine = new RE(VLINE_GROUP + "+");
839                 for (int c = 0; c < aa.getW(); c++) {
840                     String JavaDoc 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         /**
864          * Find string text in the AsciiArt.
865          */

866         protected void findStringPattern() {
867             AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad);
868             aacStart.setTransXY(0, 3 * asciiArtPad.getYGrid() / 4);
869             // string
870
try {
871                 final RE reString = new RE(STRING_PREFIX_GROUP + STRING_SUFFIX_GROUP + "*");
872                 for (int r = 0; r < aa.getH(); r++) {
873                     String JavaDoc row = aa.getRow(r);
874                     int startIndex = 0;
875                     while (reString.match(row, startIndex)) {
876                         String JavaDoc 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     /**
899      * Marker interface of objects addable to the AsciiArtPad
900      *
901      */

902     public static interface AsciiArtElement {
903     }
904 }
905
906
Popular Tags