KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.text.DecimalFormat JavaDoc;
23 import java.text.DecimalFormatSymbols JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.apache.avalon.framework.parameters.Parameters;
30 import org.apache.cocoon.CascadingIOException;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.caching.CacheableProcessingComponent;
33 import org.apache.cocoon.components.source.SourceUtil;
34 import org.apache.cocoon.environment.SourceResolver;
35 import org.apache.cocoon.generation.AbstractGenerator;
36 import org.apache.excalibur.source.Source;
37 import org.apache.excalibur.source.SourceException;
38 import org.apache.excalibur.source.SourceValidity;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.helpers.AttributesImpl JavaDoc;
42
43 /**
44  * A simple AsciiArt text SVG XML generator.
45  *
46  * @author <a HREF="mailto:huber@apache.org">Bernhard Huber</a>
47  * @version CVS $Id: AsciiArtSVGGenerator.java 123810 2004-12-31 17:07:31Z antonio $
48  * @since Cocoon 2.1, 22 December 2002
49  */

50 public class AsciiArtSVGGenerator
51     extends AbstractGenerator
52     implements CacheableProcessingComponent {
53
54     /**
55      * The input source
56      */

57     protected Source inputSource;
58
59     private AttributesImpl JavaDoc attributes = null;
60     private AsciiArtPad asciiArtPad;
61
62     //private String static PREFIX = "svg";
63
private String JavaDoc PREFIX = "";
64     private String JavaDoc URI = "http://www.w3.org/2000/svg";
65
66     /** default SVG line attributes
67      */

68     private final static String JavaDoc DEFAULT_LINE_ATTRIBUTE = "stroke:black; stroke-width:1.5";
69
70     /** default SVG text attribute
71      */

72     private final static String JavaDoc DEFAULT_TEXT_ATTRIBUTE = "font-size: 12; font-family:Times Roman; fill:blue;";
73
74     private String JavaDoc lineAttribute = DEFAULT_LINE_ATTRIBUTE;
75     private String JavaDoc textAttribute = DEFAULT_TEXT_ATTRIBUTE;
76
77
78     final static int DEFAULT_X_GRID = 10;
79     final static int DEFAULT_Y_GRID = 12;
80     private int xGrid = DEFAULT_X_GRID;
81     private int yGrid = DEFAULT_Y_GRID;
82
83     /**
84      * Setup the AsciiArtSVG generator.
85      * Try to get the last modification date of the source for caching.
86      *
87      *@param resolver Cocoon's resolver
88      *@param objectModel Cocoon's objectModel
89      *@param src generator's src attribute
90      *@param par sitemap parameters
91      *@exception ProcessingException setup fails
92      *@exception SAXException sax generation fails
93      *@exception IOException general io fails
94      */

95     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
96     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
97         super.setup(resolver, objectModel, src, par);
98
99         try {
100             this.inputSource = resolver.resolveURI(src);
101         } catch (SourceException se) {
102             throw SourceUtil.handle("Error during resolving of '" + src + "'.", se);
103         }
104
105         // Setup lineAttribute
106
lineAttribute = par.getParameter("line-attribute", DEFAULT_LINE_ATTRIBUTE);
107         // Setup textAttribute
108
textAttribute = par.getParameter("text-attribute", DEFAULT_TEXT_ATTRIBUTE);
109
110         xGrid = par.getParameterAsInteger("x-grid", DEFAULT_X_GRID);
111         yGrid = par.getParameterAsInteger("y-grid", DEFAULT_Y_GRID);
112     }
113
114
115     /**
116      * Recycle this component.
117      * All instance variables are set to <code>null</code>.
118      */

119     public void recycle() {
120         if (null != this.inputSource) {
121             super.resolver.release(this.inputSource);
122             this.inputSource = null;
123         }
124         super.recycle();
125     }
126
127
128     /**
129      * Generate the unique key.
130      * This key must be unique inside the space of this component.
131      *
132      * @return The generated key hashes the src
133      */

134     public java.io.Serializable JavaDoc getKey() {
135         return this.inputSource.getURI();
136     }
137
138
139     /**
140      * Generate the validity object.
141      *
142      * @return The generated validity object or <code>null</code> if the
143      * component is currently not cacheable.
144      */

145     public SourceValidity getValidity() {
146         return this.inputSource.getValidity();
147     }
148
149
150     /**
151      * Generate XML data.
152      */

153     public void generate()
154     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
155         try {
156             if (getLogger().isDebugEnabled()) {
157                 getLogger().debug("Source " + super.source +
158                                   " resolved to " + this.inputSource.getURI());
159             }
160
161             // read the ascii art
162
String JavaDoc[] asciiArt = readAsciiArt();
163             // setup ascii art pad
164
asciiArtPad = new AsciiArtPad();
165             asciiArtPad.setXGrid(this.xGrid);
166             asciiArtPad.setYGrid(this.yGrid);
167
168             // build the ascii art
169
AsciiArtPad.AsciiArtPadBuilder builder = new AsciiArtPad.AsciiArtPadBuilder(asciiArtPad);
170             builder.build(asciiArt);
171             attributes = new AttributesImpl JavaDoc();
172
173             // start the document
174
this.contentHandler.startDocument();
175             this.contentHandler.startPrefixMapping(PREFIX, URI);
176
177             // generate root element
178
attributes.clear();
179             // set svg attributes
180
addAttribute("width", String.valueOf(asciiArtPad.getXGrid() * asciiArtPad.getWidth()));
181             addAttribute("height", String.valueOf(asciiArtPad.getYGrid() * asciiArtPad.getHeight()));
182             startElement("svg", attributes);
183
184             // generate svg g, and path elements
185
attributes.clear();
186             // set line attributes
187
addAttribute("style", this.lineAttribute);
188             startElement("g", attributes);
189             generateSVGLineElements();
190             endElement("g");
191
192             // generate svg g, and text elements
193
attributes.clear();
194             // set text attributes
195
addAttribute("style", this.textAttribute);
196             startElement("g", attributes);
197             generateSVGTextElements();
198             endElement("g");
199
200             // end root element, document
201
endElement("svg");
202             this.contentHandler.endPrefixMapping(PREFIX);
203             this.contentHandler.endDocument();
204         } catch (SAXException JavaDoc e) {
205             SourceUtil.handleSAXException(this.inputSource.getURI(), e);
206         }
207     }
208
209
210     /**
211      * Read the ascii art from the input source.
212      *
213      *@return String[] describing the ascii art
214      *@exception IOException reading the ascii art fails
215      */

216     protected String JavaDoc[] readAsciiArt() throws IOException JavaDoc {
217         InputStream JavaDoc is = null;
218         BufferedReader JavaDoc br = null;
219         try {
220             is = this.inputSource.getInputStream();
221             br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
222             String JavaDoc line;
223             List JavaDoc lines = new ArrayList JavaDoc();
224             while ((line = br.readLine()) != null) {
225                 lines.add(line);
226             }
227             String JavaDoc[] asciiArt = (String JavaDoc[]) lines.toArray(new String JavaDoc[0]);
228             return asciiArt;
229         } catch (SourceException se) {
230             throw new CascadingIOException("Cannot get input stream", se);
231         } finally {
232             if (is != null) {
233                 is.close();
234             }
235             if (br != null) {
236                 br.close();
237             }
238         }
239     }
240
241
242     /**
243      * Generate SVG path elements.
244      * The SVG path elements are generated from ascii art lines.
245      *
246      *@throws SAXException iff SAX generation fails.
247      */

248     protected void generateSVGLineElements() throws SAXException JavaDoc {
249         //NumberFormat nf = NumberFormat.getInstance(Locale.US);
250
//nf.setGroupingIsUsed( false );
251
DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc(Locale.US);
252         DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("##0.0##", dfs);
253         Iterator JavaDoc i = asciiArtPad.iterator();
254         while (i.hasNext()) {
255             Object JavaDoc o = i.next();
256             if (o instanceof AsciiArtPad.AsciiArtLine) {
257                 AsciiArtPad.AsciiArtLine aal = (AsciiArtPad.AsciiArtLine) o;
258                 double mx = aal.getXStart();
259                 double my = aal.getYStart();
260                 double lx = aal.getXEnd();
261                 double ly = aal.getYEnd();
262
263                 attributes.clear();
264                 addAttribute("d",
265                         "M " + df.format(mx) + " " + df.format(my) + " " +
266                         "L " + df.format(lx) + " " + df.format(ly));
267                 startElement("path", attributes);
268                 endElement("path");
269             }
270         }
271     }
272
273
274     /**
275      * Generate SVG text elements.
276      * The SVG text elements are generated from ascii art string.
277      *
278      *@throws SAXException iff SAX generation fails.
279      */

280     protected void generateSVGTextElements() throws SAXException JavaDoc {
281         //NumberFormat nf = NumberFormat.getInstance(Locale.US);
282
DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc(Locale.US);
283         DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("##0.0##", dfs);
284         Iterator JavaDoc i = asciiArtPad.iterator();
285         while (i.hasNext()) {
286             Object JavaDoc o = i.next();
287             if (o instanceof AsciiArtPad.AsciiArtString) {
288                 AsciiArtPad.AsciiArtString aas = (AsciiArtPad.AsciiArtString) o;
289                 double x = aas.getX();
290                 double y = aas.getY();
291                 attributes.clear();
292                 addAttribute("x", df.format(x));
293                 addAttribute("y", df.format(y));
294                 startElement("text", attributes);
295                 characters(aas.getS());
296                 endElement("text");
297             }
298         }
299     }
300
301
302     /**
303      * SAX startElement helper
304      *
305      *@param nodeName name of the element's name
306      *@param attributes of the node
307      *@throws SAXException iff SAX generation fails
308      */

309     protected void startElement(String JavaDoc nodeName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
310         if (PREFIX.length() > 0) {
311             this.contentHandler.startElement(URI, nodeName, PREFIX + ":" + nodeName, attributes);
312         } else {
313             this.contentHandler.startElement(URI, nodeName, nodeName, attributes);
314         }
315     }
316
317
318     /**
319      * SAX character helper
320      *
321      *@param s Description of the Parameter
322      *@throws SAXException iff SAX generation fails
323      */

324     protected void characters(String JavaDoc s) throws SAXException JavaDoc {
325         if (s != null) {
326             char[] stringCharacters = s.toCharArray();
327             this.contentHandler.characters(stringCharacters, 0, stringCharacters.length);
328         }
329     }
330
331
332     /**
333      * SAX endElement helper
334      *
335      *@param nodeName name of the element's name
336      *@throws SAXException iff SAX generation fails
337      */

338     protected void endElement(String JavaDoc nodeName) throws SAXException JavaDoc {
339         if (PREFIX.length() > 0) {
340             this.contentHandler.endElement(URI, nodeName, PREFIX + ":" + nodeName);
341         } else {
342             this.contentHandler.endElement(URI, nodeName, nodeName);
343         }
344     }
345
346
347     /**
348      * Adds a feature to the Attribute attribute of the MailXMLSerializer
349      * object
350      *
351      *@param nodeName name of the attriute's name
352      *@param nodeValue value of the attribute
353      */

354     protected void addAttribute(String JavaDoc nodeName, String JavaDoc nodeValue) {
355         attributes.addAttribute("", nodeName, nodeName, "CDATA", nodeValue);
356     }
357 }
358
359
Popular Tags