1 16 package org.apache.cocoon.generation.asciiart; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.text.DecimalFormat ; 23 import java.text.DecimalFormatSymbols ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 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 ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.helpers.AttributesImpl ; 42 43 50 public class AsciiArtSVGGenerator 51 extends AbstractGenerator 52 implements CacheableProcessingComponent { 53 54 57 protected Source inputSource; 58 59 private AttributesImpl attributes = null; 60 private AsciiArtPad asciiArtPad; 61 62 private String PREFIX = ""; 64 private String URI = "http://www.w3.org/2000/svg"; 65 66 68 private final static String DEFAULT_LINE_ATTRIBUTE = "stroke:black; stroke-width:1.5"; 69 70 72 private final static String DEFAULT_TEXT_ATTRIBUTE = "font-size: 12; font-family:Times Roman; fill:blue;"; 73 74 private String lineAttribute = DEFAULT_LINE_ATTRIBUTE; 75 private String 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 95 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 96 throws ProcessingException, SAXException , IOException { 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 lineAttribute = par.getParameter("line-attribute", DEFAULT_LINE_ATTRIBUTE); 107 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 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 134 public java.io.Serializable getKey() { 135 return this.inputSource.getURI(); 136 } 137 138 139 145 public SourceValidity getValidity() { 146 return this.inputSource.getValidity(); 147 } 148 149 150 153 public void generate() 154 throws IOException , SAXException , ProcessingException { 155 try { 156 if (getLogger().isDebugEnabled()) { 157 getLogger().debug("Source " + super.source + 158 " resolved to " + this.inputSource.getURI()); 159 } 160 161 String [] asciiArt = readAsciiArt(); 163 asciiArtPad = new AsciiArtPad(); 165 asciiArtPad.setXGrid(this.xGrid); 166 asciiArtPad.setYGrid(this.yGrid); 167 168 AsciiArtPad.AsciiArtPadBuilder builder = new AsciiArtPad.AsciiArtPadBuilder(asciiArtPad); 170 builder.build(asciiArt); 171 attributes = new AttributesImpl (); 172 173 this.contentHandler.startDocument(); 175 this.contentHandler.startPrefixMapping(PREFIX, URI); 176 177 attributes.clear(); 179 addAttribute("width", String.valueOf(asciiArtPad.getXGrid() * asciiArtPad.getWidth())); 181 addAttribute("height", String.valueOf(asciiArtPad.getYGrid() * asciiArtPad.getHeight())); 182 startElement("svg", attributes); 183 184 attributes.clear(); 186 addAttribute("style", this.lineAttribute); 188 startElement("g", attributes); 189 generateSVGLineElements(); 190 endElement("g"); 191 192 attributes.clear(); 194 addAttribute("style", this.textAttribute); 196 startElement("g", attributes); 197 generateSVGTextElements(); 198 endElement("g"); 199 200 endElement("svg"); 202 this.contentHandler.endPrefixMapping(PREFIX); 203 this.contentHandler.endDocument(); 204 } catch (SAXException e) { 205 SourceUtil.handleSAXException(this.inputSource.getURI(), e); 206 } 207 } 208 209 210 216 protected String [] readAsciiArt() throws IOException { 217 InputStream is = null; 218 BufferedReader br = null; 219 try { 220 is = this.inputSource.getInputStream(); 221 br = new BufferedReader (new InputStreamReader (is)); 222 String line; 223 List lines = new ArrayList (); 224 while ((line = br.readLine()) != null) { 225 lines.add(line); 226 } 227 String [] asciiArt = (String []) lines.toArray(new String [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 248 protected void generateSVGLineElements() throws SAXException { 249 DecimalFormatSymbols dfs = new DecimalFormatSymbols (Locale.US); 252 DecimalFormat df = new DecimalFormat ("##0.0##", dfs); 253 Iterator i = asciiArtPad.iterator(); 254 while (i.hasNext()) { 255 Object 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 280 protected void generateSVGTextElements() throws SAXException { 281 DecimalFormatSymbols dfs = new DecimalFormatSymbols (Locale.US); 283 DecimalFormat df = new DecimalFormat ("##0.0##", dfs); 284 Iterator i = asciiArtPad.iterator(); 285 while (i.hasNext()) { 286 Object 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 309 protected void startElement(String nodeName, Attributes attributes) throws SAXException { 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 324 protected void characters(String s) throws SAXException { 325 if (s != null) { 326 char[] stringCharacters = s.toCharArray(); 327 this.contentHandler.characters(stringCharacters, 0, stringCharacters.length); 328 } 329 } 330 331 332 338 protected void endElement(String nodeName) throws SAXException { 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 354 protected void addAttribute(String nodeName, String nodeValue) { 355 attributes.addAttribute("", nodeName, nodeName, "CDATA", nodeValue); 356 } 357 } 358 359 | Popular Tags |