huber 2003/01/06 13:52:49 Added: src/scratchpad/src/org/apache/cocoon/generation/asciiart AsciiArtPad.java AsciiArtPadTestCase.java AsciiArtSVGGenerator.java Log: initial version of asciiart generator Revision Changes Path 1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/generation/asciiart/AsciiArtPad.java Index: AsciiArtPad.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.generation.asciiart; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; /** * A drawing ascii art pad. * *@author [EMAIL PROTECTED] *@created 18. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public class AsciiArtPad { private int width; private int height; /** * List of AsciiArt elements */ private List pad; private double xGrid; private double yGrid; /** *Constructor for the AsciiArtPad object */ public AsciiArtPad() { pad = new ArrayList(); } /** *Constructor for the AsciiArtPad object * *@param w Description of the Parameter *@param h Description of the Parameter */ public AsciiArtPad(int w, int h) { width = w; height = h; pad = new ArrayList(); } /** * Sets the width attribute of the AsciiArtPad object * *@param width The new width value */ public void setWidth(int width) { this.width = width; } /** * Sets the height attribute of the AsciiArtPad object * *@param height The new height value */ public void setHeight(int height) { this.height = height; } /** * Sets the xGrid attribute of the AsciiArtPad object * *@param xGrid The new xGrid value */ public void setXGrid(double xGrid) { this.xGrid = xGrid; } /** * Sets the yGrid attribute of the AsciiArtPad object * *@param yGrid The new yGrid value */ public void setYGrid(double yGrid) { this.yGrid = yGrid; } /** * Gets the width attribute of the AsciiArtPad object * *@return The width value */ public int getWidth() { return width; } /** * Gets the height attribute of the AsciiArtPad object * *@return The height value */ public int getHeight() { return height; } /** * Gets the xGrid attribute of the AsciiArtPad object * *@return The xGrid value */ public double getXGrid() { return xGrid; } /** * Gets the yGrid attribute of the AsciiArtPad object * *@return The yGrid value */ public double getYGrid() { return yGrid; } /** * Add a AsciiArtElement * *@param o the AsciiArtElement object */ public void add(Object o) { pad.add(o); } /** * Iterator of AsciiArtPad * *@return Iterator iterating over all AsciiArtElements */ public Iterator iterator() { return pad.iterator(); } /** * An AsciiArtElement describing a line. * *@author [EMAIL PROTECTED] *@created 18. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArtLine implements AsciiArtElement { double xStart; double yStart; double xEnd; double yEnd; /** *Constructor for the AsciiArtLine object * *@param start Description of the Parameter *@param end Description of the Parameter */ public AsciiArtLine(AsciiArtCoordinate start, AsciiArtCoordinate end) { xStart = start.getXDouble(); yStart = start.getYDouble(); xEnd = end.getXDouble(); yEnd = end.getYDouble(); } /** * Sets the xStart attribute of the AsciiArtLine object * *@param xStart The new xStart value */ public void setXStart(double xStart) { this.xStart = xStart; } /** * Sets the yStart attribute of the AsciiArtLine object * *@param yStart The new yStart value */ public void setYStart(double yStart) { this.yStart = yStart; } /** * Sets the xEnd attribute of the AsciiArtLine object * *@param xEnd The new xEnd value */ public void setXEnd(double xEnd) { this.xEnd = xEnd; } /** * Sets the yEnd attribute of the AsciiArtLine object * *@param yEnd The new yEnd value */ public void setYEnd(double yEnd) { this.yEnd = yEnd; } /** * Gets the xStart attribute of the AsciiArtLine object * *@return The xStart value */ public double getXStart() { return xStart; } /** * Gets the yStart attribute of the AsciiArtLine object * *@return The yStart value */ public double getYStart() { return yStart; } /** * Gets the xEnd attribute of the AsciiArtLine object * *@return The xEnd value */ public double getXEnd() { return xEnd; } /** * Gets the yEnd attribute of the AsciiArtLine object * *@return The yEnd value */ public double getYEnd() { return yEnd; } /** * Descriptive string * *@return String */ public String toString() { String s = "[xStart:" + String.valueOf(xStart) + "]" + "[yStart:" + String.valueOf(yStart) + "]" + "[xEnd:" + String.valueOf(xEnd) + "]" + "[yEnd:" + String.valueOf(yEnd) + "]"; return s; } } /** * An AsciiArtElement describing a rectangle. * *@author [EMAIL PROTECTED] *@created 21. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArtRect implements AsciiArtElement { double xUpperLeft; double yUpperLeft; double xLowerRight; double yLowerRight; /** *Constructor for the AsciiArtRect object * *@param upperLeft Description of the Parameter *@param lowerRight Description of the Parameter */ public AsciiArtRect(AsciiArtCoordinate upperLeft, AsciiArtCoordinate lowerRight) { xUpperLeft = upperLeft.getXDouble(); yUpperLeft = upperLeft.getYDouble(); xLowerRight = lowerRight.getXDouble(); yLowerRight = lowerRight.getYDouble(); } /** * Sets the xUpperLeft attribute of the AsciiArtRect object * *@param xUpperLeft The new xUpperLeft value */ public void setXUpperLeft(double xUpperLeft) { this.xUpperLeft = xUpperLeft; } /** * Sets the yUpperLeft attribute of the AsciiArtRect object * *@param yUpperLeft The new yUpperLeft value */ public void setYUpperLeft(double yUpperLeft) { this.yUpperLeft = yUpperLeft; } /** * Sets the xLowerRight attribute of the AsciiArtRect object * *@param xLowerRight The new xLowerRight value */ public void setXLowerRight(double xLowerRight) { this.xLowerRight = xLowerRight; } /** * Sets the yLowerRight attribute of the AsciiArtRect object * *@param yLowerRight The new yLowerRight value */ public void setYLowerRight(double yLowerRight) { this.yLowerRight = yLowerRight; } /** * Gets the xUpperLeft attribute of the AsciiArtRect object * *@return The xUpperLeft value */ public double getXUpperLeft() { return xUpperLeft; } /** * Gets the yUpperLeft attribute of the AsciiArtRect object * *@return The yUpperLeft value */ public double getYUpperLeft() { return yUpperLeft; } /** * Gets the xLowerRight attribute of the AsciiArtRect object * *@return The xLowerRight value */ public double getXLowerRight() { return xLowerRight; } /** * Gets the yLowerRight attribute of the AsciiArtRect object * *@return The yLowerRight value */ public double getYLowerRight() { return yLowerRight; } /** * Gets the width attribute of the AsciiArtRect object * *@return The width value */ public double getWidth() { return Math.abs(xUpperLeft - xLowerRight); } /** * Gets the height attribute of the AsciiArtRect object * *@return The height value */ public double getHeight() { return Math.abs(yUpperLeft - yLowerRight); } /** * Descriptive string * *@return String */ public String toString() { String s = "[xUpperLeft:" + String.valueOf(xUpperLeft) + "]" + "[yUpperLeft:" + String.valueOf(yUpperLeft) + "]" + "[xLowerRight:" + String.valueOf(xLowerRight) + "]" + "[yLowerRight:" + String.valueOf(yLowerRight) + "]"; return s; } } /** * An AsciiArtElement describing a string of text. * *@author [EMAIL PROTECTED] *@created 21. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArtString implements AsciiArtElement { private double x; private double y; private String s; /** *Constructor for the AsciiArtString object * *@param s Description of the Parameter *@param aac Description of the Parameter */ public AsciiArtString(AsciiArtCoordinate aac, String s) { this.x = aac.getXDouble(); this.y = aac.getYDouble(); this.s = s; } /** * Sets the x attribute of the AsciiArtString object * *@param x The new x value */ public void setX(double x) { this.x = x; } /** * Sets the y attribute of the AsciiArtString object * *@param y The new y value */ public void setY(double y) { this.y = y; } /** * Sets the s attribute of the AsciiArtString object * *@param s The new s value */ public void setS(String s) { this.s = s; } /** * Gets the x attribute of the AsciiArtString object * *@return The x value */ public double getX() { return x; } /** * Gets the y attribute of the AsciiArtString object * *@return The y value */ public double getY() { return y; } /** * Gets the s attribute of the AsciiArtString object * *@return The s value */ public String getS() { return s; } /** * Descriptive string * *@return String */ public String toString() { String s = "[x:" + String.valueOf(x) + "]" + "[y:" + String.valueOf(y) + "]" + "[s:" + String.valueOf(this.s) + "]"; return s; } } /** * Helper class describing a coordinate of AsciiArtPad elements. * *@author [EMAIL PROTECTED] *@created 21. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArtCoordinate { int x, y; AsciiArtPad asciiArtPad; double tx, ty; /** *Constructor for the AsciiArtCoordinate object */ public AsciiArtCoordinate() { } /** *Constructor for the AsciiArtCoordinate object * *@param asciiArtPad Description of the Parameter */ public AsciiArtCoordinate(AsciiArtPad asciiArtPad) { setAsciiArtPad(asciiArtPad); } /** *Constructor for the AsciiArtCoordinate object * *@param x Description of the Parameter *@param y Description of the Parameter */ public AsciiArtCoordinate(int x, int y) { setXY(x, y); } /** * Sets the asciiArtPad attribute of the AsciiArtCoordinate object * *@param asciiArtPad The new asciiArtPad value */ public void setAsciiArtPad(AsciiArtPad asciiArtPad) { this.asciiArtPad = asciiArtPad; } /** * Sets the xY attribute of the AsciiArtCoordinate object * *@param tx The new transXY value *@param ty The new transXY value */ public void setTransXY(double tx, double ty) { this.tx = tx; this.ty = ty; } /** * Sets the xY attribute of the AsciiArtCoordinate object * *@param x The new xY value *@param y The new xY value */ public void setXY(int x, int y) { this.x = x; this.y = y; } /** * Gets the xDouble attribute of the AsciiArtCoordinate object * *@return The xDouble value */ public double getXDouble() { return x * asciiArtPad.getXGrid() + tx; } /** * Gets the yDouble attribute of the AsciiArtCoordinate object * *@return The yDouble value */ public double getYDouble() { return y * asciiArtPad.getYGrid() + ty; } } /** * Helper class containing the ascii text data, * acting as input of an AsciiArtPad * *@author [EMAIL PROTECTED] *@created 21. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArt { private String[] s; private int w; private int h; /** *Constructor for the AsciiArt object * *@param s Description of the Parameter */ public AsciiArt(String[] s) { this.s = s; int length = s.length; h = length; w = 0; for (int i = 0; i < length; i++) { String line = s[i]; if (line != null && line.length() > w) { w = line.length(); } } } /** * Gets the w attribute of the AsciiArt object * *@return The w value */ public int getW() { return w; } /** * Gets the h attribute of the AsciiArt object * *@return The h value */ public int getH() { return h; } /** * Gets the row attribute of the AsciiArt object * *@param r Description of the Parameter *@return The row value */ public String getRow(int r) { String row = this.s[r]; return row; } /** * Gets the column attribute of the AsciiArt object * *@param c Description of the Parameter *@return The column value */ public String getColumn(int c) { StringBuffer column = new StringBuffer(); final String EMPTY_CHAR = " "; for (int i = 0; i < s.length; i++) { if (s[i] != null && c < s[i].length()) { column.append(s[i].charAt(c)); } else { column.append(EMPTY_CHAR); } } return column.toString(); } } /** * Builder of AsciiArtElements from an AsciiArt input. * *@author [EMAIL PROTECTED] *@created 21. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static class AsciiArtPadBuilder { private AsciiArtPad asciiArtPad; private AsciiArt aa; final String EDGE_GROUP = "[+\\\\/]"; final String HLINE_GROUP = "[\\-~=+]"; final String VLINE_GROUP = "[|+]"; final String STRING_SUFFIX_GROUP = "[^\\-|+ \\\\]"; //final String STRING_PREFIX_GROUP = "[a-zA-Z0-9_\\*;\\.#]"; final String STRING_PREFIX_GROUP = STRING_SUFFIX_GROUP; /** *Constructor for the AsciiArtPadBuilder object * *@param asciiArtPad Description of the Parameter */ public AsciiArtPadBuilder(AsciiArtPad asciiArtPad) { this.asciiArtPad = asciiArtPad; } /** * Build AsciiArtElement from an asciiArt * *@param asciiArt Description of the Parameter */ public void build(String[] asciiArt) { aa = new AsciiArt(asciiArt); asciiArtPad.setWidth(aa.getW()); asciiArtPad.setHeight(aa.getH()); // find asciiArt patterns findRectPattern(); findCornerPattern(); findLinePattern(); findStringPattern(); } /** * Find rectangles in the AsciiArt. * not implemented yet. */ protected void findRectPattern() { } /** * Find corners in the AsciiArt */ protected void findCornerPattern() { AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); aacStart.setTransXY(0, asciiArtPad.getYGrid() / 2); AsciiArtCoordinate aacEnd = new AsciiArtCoordinate(this.asciiArtPad); aacEnd.setTransXY(0, asciiArtPad.getYGrid() / 2); // hor line try { final RE reCorner = new RE(EDGE_GROUP); for (int r = 0; r < aa.getH(); r++) { String row = aa.getRow(r); int startIndex = 0; while (reCorner.match(row, startIndex)) { String s = reCorner.getParen(0); int mStart = reCorner.getParenStart(0); int mEnd = reCorner.getParenEnd(0); if (s.equals("\\")) { aacStart.setXY(mStart, r - 1); aacEnd.setXY(mStart + 1, r); } else if (s.equals("/")) { aacStart.setXY(mStart + 1, r - 1); aacEnd.setXY(mStart, r); } else { aacStart.setXY(mStart, r); aacEnd.setXY(mStart, r); } AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); this.asciiArtPad.add(aal); if (startIndex >= mEnd) { break; } startIndex = mEnd; } } } catch (RESyntaxException rese) { rese.printStackTrace(); } } /** * Find lines in the AsciiArt */ protected void findLinePattern() { AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); aacStart.setTransXY(0, asciiArtPad.getYGrid() / 2); AsciiArtCoordinate aacEnd = new AsciiArtCoordinate(this.asciiArtPad); aacEnd.setTransXY(0, asciiArtPad.getYGrid() / 2); // hor line try { final RE reHorLine = new RE(HLINE_GROUP + "+"); for (int r = 0; r < aa.getH(); r++) { String row = aa.getRow(r); int startIndex = 0; while (reHorLine.match(row, startIndex)) { int mStart = reHorLine.getParenStart(0); int mEnd = reHorLine.getParenEnd(0); aacStart.setXY(mStart, r); aacEnd.setXY(mEnd - 1, r); AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); this.asciiArtPad.add(aal); if (startIndex >= mEnd) { break; } startIndex = mEnd; } } } catch (RESyntaxException rese) { rese.printStackTrace(); } // ver line try { RE reVerLine = new RE(VLINE_GROUP + "+"); for (int c = 0; c < aa.getW(); c++) { String col = aa.getColumn(c); int startIndex = 0; while (reVerLine.match(col, startIndex)) { int mStart = reVerLine.getParenStart(0); int mEnd = reVerLine.getParenEnd(0); aacStart.setXY(c, mStart); aacEnd.setXY(c, mEnd - 1); AsciiArtLine aal = new AsciiArtLine(aacStart, aacEnd); this.asciiArtPad.add(aal); if (startIndex >= mEnd) { break; } startIndex = mEnd; } } } catch (RESyntaxException rese) { rese.printStackTrace(); } } /** * Find string text in the AsciiArt. */ protected void findStringPattern() { AsciiArtCoordinate aacStart = new AsciiArtCoordinate(this.asciiArtPad); aacStart.setTransXY(0, 3 * asciiArtPad.getYGrid() / 4); // string try { final RE reString = new RE(STRING_PREFIX_GROUP + STRING_SUFFIX_GROUP + "*"); for (int r = 0; r < aa.getH(); r++) { String row = aa.getRow(r); int startIndex = 0; while (reString.match(row, startIndex)) { String s = reString.getParen(0); int mStart = reString.getParenStart(0); int mEnd = reString.getParenEnd(0); aacStart.setXY(mStart, r); AsciiArtString aas = new AsciiArtString(aacStart, s); this.asciiArtPad.add(aas); if (startIndex >= mEnd) { break; } startIndex = mEnd; } } } catch (RESyntaxException rese) { rese.printStackTrace(); } } } /** * Marker interface of objects addable to the AsciiArtPad * *@author [EMAIL PROTECTED] *@created 22. Dezember 2002 *@version CVS Version: $Id: AsciiArtPad.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public static interface AsciiArtElement { } } 1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/generation/asciiart/AsciiArtPadTestCase.java Index: AsciiArtPadTestCase.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.generation.asciiart; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Iterator; import java.util.Locale; import junit.framework.TestCase; import junit.framework.TestCase; import junit.textui.TestRunner; /** * A Simple testcase of the AsciiArtPad. * *@author huber.at.apache.org *@created 18. Dezember 2002 *@version CVS Version: $Id: AsciiArtPadTestCase.java,v 1.1 2003/01/06 21:52:49 huber Exp $ */ public class AsciiArtPadTestCase extends TestCase { private AsciiArtPad asciiArtPad; /** *Constructor for the AsciiArtPadTestCase object * *@param name Description of Parameter *@since */ public AsciiArtPadTestCase(String name) { super(name); } /** * The main program for the AsciiArtPadTestCase class * *@param args The command line arguments *@exception Exception Description of the Exception */ public static void main(final String[] args) throws Exception { final String[] testCaseName = {AsciiArtPadTestCase.class.getName()}; TestRunner.main(testCaseName); } /** * A unit test for JUnit */ public void test0() { } /** * A unit test for JUnit * *@exception Exception Description of the Exception */ public void test1() throws Exception { System.out.println("test1()"); String[] asciiArt = new String[]{ // 1 //01234567890123456789 "= =", " +--------------+ ", " | 1st quarter | ", " +------------+-+ ", " | 2nd | ", " +-----+------+ ", " | 3rd | ", " +-----+ ", "= =", " +-------------+ ", " | container | ", " | | ", " | +-------+ | ", " | | part | | ", " | +-------+ | ", " | | ", " +-------------+ ", "= =", " +==============+ ", " | Mail_Header | ", " | +========+ | ", " | | Body | | ", " | +========+ | ", " | |a |a |a | | ", " | |1 |2 |3 | | ", " | +========+ | ", " +==============+ ", "= =", " +----------------+", " | header |", " +----------------+", " + c | col2 | c +", " + o | | o +", " + l | | l +", " + 1 | | 3 +", " +----------------+", " | footer |", " +----------------+", "= =", " +---stylesheets ", " +---docs ", " | +---top_col_1 ", " | +---mid_col_1 ", " | +---mid_col_2 ", " | +---mid_col_3 ", " | +---mid_col_4 ", " | \\---bottom_col_1", " \\---resources", " |---styles", " \\---images", }; asciiArtPad = new AsciiArtPad(); asciiArtPad.setXGrid(10); asciiArtPad.setYGrid(12); AsciiArtPad.AsciiArtPadBuilder aapb = new AsciiArtPad.AsciiArtPadBuilder(asciiArtPad); aapb.build(asciiArt); Iterator i = asciiArtPad.iterator(); NumberFormat nf = NumberFormat.getInstance(Locale.US); DecimalFormat df = new DecimalFormat("#0.0##"); StringBuffer svg_lines = new StringBuffer(); StringBuffer svg_text = new StringBuffer(); while (i.hasNext()) { Object o = i.next(); double x; double y; if (o instanceof AsciiArtPad.AsciiArtLine) { AsciiArtPad.AsciiArtLine aal = (AsciiArtPad.AsciiArtLine) o; x = aal.getXStart(); y = aal.getYStart(); svg_lines.append("<path d=\""); svg_lines.append("M " + nf.format(x) + " " + nf.format(y)); x = aal.getXEnd(); y = aal.getYEnd(); svg_lines.append("L " + nf.format(x) + " " + nf.format(y)); svg_lines.append("\"/>\n"); } else if (o instanceof AsciiArtPad.AsciiArtString) { AsciiArtPad.AsciiArtString aas = (AsciiArtPad.AsciiArtString) o; x = aas.getX(); y = aas.getY(); svg_text.append("<text "); svg_text.append("x=\"" + nf.format(x) + "\" y=\"" + nf.format(y) + "\">"); svg_text.append("<![CDATA[" + aas.getS() + "]]>"); svg_text.append("</text>\n"); } else { System.out.println("o " + o.toString()); } } System.out.println("<!-- lines --> "); System.out.println(svg_lines.toString()); System.out.println("<!-- text --> "); System.out.println(svg_text.toString()); } /** *The JUnit setup method * *@exception Exception Description of Exception *@since */ protected void setUp() throws Exception { asciiArtPad = null; } /** * The teardown method for JUnit * *@exception Exception Description of the Exception */ protected void tearDown() throws Exception { asciiArtPad = null; } } 1.1 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/generation/asciiart/AsciiArtSVGGenerator.java Index: AsciiArtSVGGenerator.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.generation.asciiart; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.CascadingIOException; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.caching.CacheableProcessingComponent; import org.apache.cocoon.components.source.SourceUtil; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.generation.ComposerGenerator; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; import org.apache.excalibur.source.SourceValidity; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; /** * A simple AsciiArt text SVG XML generator. * *@author <a href="mailto:[EMAIL PROTECTED]">Bernhard Huber</a> *@created 22. Dezember 2002 *@version CVS $Id: AsciiArtSVGGenerator.java,v 1.1 2003/01/06 21:52:49 huber Exp $ *@since Cocoon 2.1 */ public class AsciiArtSVGGenerator extends ComposerGenerator implements CacheableProcessingComponent { /** * The input source */ protected Source inputSource; private AttributesImpl attributes = null; private AsciiArtPad asciiArtPad; //private String PREFIX = "svg"; private String PREFIX = ""; private String URI = "http://www.w3.org/2000/svg"; /** default SVG line attributes */ private final String DEFAULT_LINE_ATTRIBUTE = "stroke:black; stroke-width:1.5"; /** default SVG text attribute */ private final String DEFAULT_TEXT_ATTRIBUTE = "font-size: 12; font-family:Times Roman; fill:blue;"; private String lineAttribute = DEFAULT_LINE_ATTRIBUTE; private String textAttribute = DEFAULT_TEXT_ATTRIBUTE; final int DEFAULT_X_GRID = 10; final int DEFAULT_Y_GRID = 12; private int xGrid = DEFAULT_X_GRID; private int yGrid = DEFAULT_Y_GRID; /** * Setup the AsciiArtSVG generator. * Try to get the last modification date of the source for caching. * *@param resolver Cocoon's resolver *@param objectModel Cocoon's objectModel *@param src generator's src attribute *@param par sitemap parameters *@exception ProcessingException setup fails *@exception SAXException sax generation fails *@exception IOException general io fails */ public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException { super.setup(resolver, objectModel, src, par); try { this.inputSource = resolver.resolveURI(src); } catch (SourceException se) { throw SourceUtil.handle("Error during resolving of '" + src + "'.", se); } // setup lineAttribute lineAttribute = par.getParameter( "line-attribute", DEFAULT_LINE_ATTRIBUTE ); // setup textAttribute textAttribute = par.getParameter( "text-attribute", DEFAULT_TEXT_ATTRIBUTE ); xGrid = par.getParameterAsInteger( "x-grid", DEFAULT_X_GRID ); yGrid = par.getParameterAsInteger( "y-grid", DEFAULT_Y_GRID ); } /** * Recycle this component. * All instance variables are set to <code>null</code>. */ public void recycle() { if (null != this.inputSource) { super.resolver.release(this.inputSource); this.inputSource = null; } super.recycle(); } /** * Generate the unique key. * This key must be unique inside the space of this component. * *@return The generated key hashes the src */ public java.io.Serializable generateKey() { return this.inputSource.getSystemId(); } /** * Generate the validity object. * *@return The generated validity object or <code>null</code> if the * component is currently not cacheable. */ public SourceValidity generateValidity() { return this.inputSource.getValidity(); } /** * Generate XML data. * *@exception IOException Description of the Exception *@exception SAXException Description of the Exception *@exception ProcessingException Description of the Exception */ public void generate() throws IOException, SAXException, ProcessingException { try { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("processing asciiart file " + super.source); this.getLogger().debug("asciiart file resolved to " + this.inputSource.getSystemId()); } // read the ascii art String[] asciiArt = readAsciiArt(); // setup ascii art pad asciiArtPad = new AsciiArtPad(); asciiArtPad.setXGrid(this.xGrid); asciiArtPad.setYGrid(this.yGrid); // build the ascii art AsciiArtPad.AsciiArtPadBuilder builder = new AsciiArtPad.AsciiArtPadBuilder(asciiArtPad); builder.build(asciiArt); attributes = new AttributesImpl(); // start the document this.contentHandler.startDocument(); this.contentHandler.startPrefixMapping(PREFIX, URI); // generate root element attributes.clear(); // set svg attributes addAttribute("width", String.valueOf(asciiArtPad.getXGrid() * asciiArtPad.getWidth())); addAttribute("height", String.valueOf(asciiArtPad.getYGrid() * asciiArtPad.getHeight())); startElement("svg", attributes); // generate svg g, and path elements attributes.clear(); // set line attributes addAttribute("style", this.lineAttribute); startElement("g", attributes); generateSVGLineElements(); endElement("g"); // generate svg g, and text elements attributes.clear(); // set text attributes addAttribute("style", this.textAttribute); startElement("g", attributes); generateSVGTextElements(); endElement("g"); // end root element, document endElement("svg"); this.contentHandler.endPrefixMapping(PREFIX); this.contentHandler.endDocument(); } catch (SAXException e) { final Exception cause = e.getException(); if (cause != null) { if (cause instanceof ProcessingException) { throw (ProcessingException) cause; } if (cause instanceof IOException) { throw (IOException) cause; } if (cause instanceof SAXException) { throw (SAXException) cause; } throw new ProcessingException("Could not read resource " + this.inputSource.getSystemId(), cause); } throw e; } } /** * Read the ascii art from the input source. * *@return String[] describing the ascii art *@exception IOException reading the ascii art fails */ protected String[] readAsciiArt() throws IOException { InputStream is = null; BufferedReader br = null; try { is = this.inputSource.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); String line; List lines = new ArrayList(); while ((line = br.readLine()) != null) { lines.add(line); } String[] asciiArt = (String[]) lines.toArray(new String[0]); return asciiArt; } catch (SourceException se) { throw new CascadingIOException("Cannot get input stream", se); } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } } /** * Generate SVG path elements. * The SVG path elements are generated from ascii art lines. * *@throws SAXException iff SAX generation fails. */ protected void generateSVGLineElements() throws SAXException { //NumberFormat nf = NumberFormat.getInstance(Locale.US); //nf.setGroupingIsUsed( false ); DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("##0.0##", dfs); Iterator i = asciiArtPad.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof AsciiArtPad.AsciiArtLine) { AsciiArtPad.AsciiArtLine aal = (AsciiArtPad.AsciiArtLine) o; double mx = aal.getXStart(); double my = aal.getYStart(); double lx = aal.getXEnd(); double ly = aal.getYEnd(); attributes.clear(); addAttribute("d", "M " + df.format(mx) + " " + df.format(my) + " " + "L " + df.format(lx) + " " + df.format(ly)); startElement("path", attributes); endElement("path"); } } } /** * Generate SVG text elements. * The SVG text elements are generated from ascii art string. * *@throws SAXException iff SAX generation fails. */ protected void generateSVGTextElements() throws SAXException { //NumberFormat nf = NumberFormat.getInstance(Locale.US); DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("##0.0##", dfs); Iterator i = asciiArtPad.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof AsciiArtPad.AsciiArtString) { AsciiArtPad.AsciiArtString aas = (AsciiArtPad.AsciiArtString) o; double x = aas.getX(); double y = aas.getY(); attributes.clear(); addAttribute("x", df.format(x)); addAttribute("y", df.format(y)); startElement("text", attributes); characters(aas.getS()); endElement("text"); } } } /** * SAX startElement helper * *@param nodeName name of the element's name *@param attributes of the node *@throws SAXException iff SAX generation fails */ protected void startElement(String nodeName, Attributes attributes) throws SAXException { if (PREFIX.length() > 0) { this.contentHandler.startElement(URI, nodeName, PREFIX + ":" + nodeName, attributes); } else { this.contentHandler.startElement(URI, nodeName, nodeName, attributes); } } /** * SAX character helper * *@param s Description of the Parameter *@throws SAXException iff SAX generation fails */ protected void characters(String s) throws SAXException { if (s != null) { char[] stringCharacters = s.toCharArray(); this.contentHandler.characters(stringCharacters, 0, stringCharacters.length); } } /** * SAX endElement helper * *@param nodeName name of the element's name *@throws SAXException iff SAX generation fails */ protected void endElement(String nodeName) throws SAXException { if (PREFIX.length() > 0) { this.contentHandler.endElement(URI, nodeName, PREFIX + ":" + nodeName); } else { this.contentHandler.endElement(URI, nodeName, nodeName); } } /** * Adds a feature to the Attribute attribute of the MailXMLSerializer * object * *@param nodeName name of the attriute's name *@param nodeValue value of the attribute */ protected void addAttribute(String nodeName, String nodeValue) { attributes.addAttribute("", nodeName, nodeName, "CDATA", nodeValue); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]