1 18 19 package org.apache.batik.transcoder.svg2svg; 20 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 import java.io.StringWriter ; 25 import java.io.Writer ; 26 27 import org.apache.batik.dom.util.DOMUtilities; 28 import org.apache.batik.transcoder.AbstractTranscoder; 29 import org.apache.batik.transcoder.ErrorHandler; 30 import org.apache.batik.transcoder.TranscoderException; 31 import org.apache.batik.transcoder.TranscoderInput; 32 import org.apache.batik.transcoder.TranscoderOutput; 33 import org.apache.batik.transcoder.TranscodingHints; 34 import org.apache.batik.transcoder.keys.BooleanKey; 35 import org.apache.batik.transcoder.keys.IntegerKey; 36 import org.apache.batik.transcoder.keys.StringKey; 37 import org.w3c.dom.Document ; 38 39 45 public class SVGTranscoder extends AbstractTranscoder { 46 47 50 public final static ErrorHandler DEFAULT_ERROR_HANDLER = new ErrorHandler() { 51 public void error(TranscoderException ex) throws TranscoderException { 52 throw ex; 53 } 54 public void fatalError(TranscoderException ex) throws TranscoderException { 55 throw ex; 56 } 57 public void warning(TranscoderException ex) throws TranscoderException { 58 } 60 }; 61 62 65 public final static TranscodingHints.Key KEY_NEWLINE = new NewlineKey(); 66 67 70 public final static NewlineValue VALUE_NEWLINE_CR = new NewlineValue("\r"); 71 72 75 public final static NewlineValue VALUE_NEWLINE_CR_LF = new NewlineValue("\r\n"); 76 77 80 public final static NewlineValue VALUE_NEWLINE_LF = new NewlineValue("\n"); 81 82 85 public final static TranscodingHints.Key KEY_FORMAT = new BooleanKey(); 86 87 90 public final static Boolean VALUE_FORMAT_ON = Boolean.TRUE; 91 92 95 public final static Boolean VALUE_FORMAT_OFF = Boolean.FALSE; 96 97 100 public final static TranscodingHints.Key KEY_TABULATION_WIDTH 101 = new IntegerKey(); 102 103 106 public final static TranscodingHints.Key KEY_DOCUMENT_WIDTH 107 = new IntegerKey(); 108 109 112 public final static TranscodingHints.Key KEY_DOCTYPE 113 = new DoctypeKey(); 114 115 118 public final static DoctypeValue VALUE_DOCTYPE_CHANGE = 119 new DoctypeValue(PrettyPrinter.DOCTYPE_CHANGE); 120 121 124 public final static DoctypeValue VALUE_DOCTYPE_REMOVE = 125 new DoctypeValue(PrettyPrinter.DOCTYPE_REMOVE); 126 127 130 public final static DoctypeValue VALUE_DOCTYPE_KEEP_UNCHANGED = 131 new DoctypeValue(PrettyPrinter.DOCTYPE_KEEP_UNCHANGED); 132 133 136 public final static TranscodingHints.Key KEY_PUBLIC_ID 137 = new StringKey(); 138 139 142 public final static TranscodingHints.Key KEY_SYSTEM_ID 143 = new StringKey(); 144 145 148 public final static TranscodingHints.Key KEY_XML_DECLARATION 149 = new StringKey(); 150 151 154 public SVGTranscoder() { 155 setErrorHandler(DEFAULT_ERROR_HANDLER); 156 } 157 158 164 public void transcode(TranscoderInput input, TranscoderOutput output) 165 throws TranscoderException { 166 Reader r = input.getReader(); 167 Writer w = output.getWriter(); 168 169 if (r == null) { 170 Document d = input.getDocument(); 171 if (d == null) { 172 throw new Error ("Reader or Document expected"); 173 } 174 StringWriter sw = new StringWriter (); 175 try { 176 DOMUtilities.writeDocument(d, sw); 177 } catch (IOException e) { 178 throw new Error ("IO"); 179 } 180 r = new StringReader (sw.toString()); 181 } 182 if (w == null) { 183 throw new Error ("Writer expected"); 184 } 185 prettyPrint(r, w); 186 } 187 188 189 192 protected void prettyPrint(Reader in, Writer out) throws TranscoderException { 193 try { 194 PrettyPrinter pp = new PrettyPrinter(); 195 NewlineValue nlv = (NewlineValue)hints.get(KEY_NEWLINE); 196 if (nlv != null) { 197 pp.setNewline(nlv.getValue()); 198 } 199 Boolean b = (Boolean )hints.get(KEY_FORMAT); 200 if (b != null) { 201 pp.setFormat(b.booleanValue()); 202 } 203 Integer i = (Integer )hints.get(KEY_TABULATION_WIDTH); 204 if (i != null) { 205 pp.setTabulationWidth(i.intValue()); 206 } 207 i = (Integer )hints.get(KEY_DOCUMENT_WIDTH); 208 if (i != null) { 209 pp.setDocumentWidth(i.intValue()); 210 } 211 DoctypeValue dtv = (DoctypeValue)hints.get(KEY_DOCTYPE); 212 if (dtv != null) { 213 pp.setDoctypeOption(dtv.getValue()); 214 } 215 String s = (String )hints.get(KEY_PUBLIC_ID); 216 if (s != null) { 217 pp.setPublicId(s); 218 } 219 s = (String )hints.get(KEY_SYSTEM_ID); 220 if (s != null) { 221 pp.setSystemId(s); 222 } 223 224 s = (String )hints.get(KEY_XML_DECLARATION); 225 if (s != null) { 226 pp.setXMLDeclaration(s); 227 } 228 229 pp.print(in, out); 230 out.flush(); 231 } catch (IOException e) { 232 getErrorHandler().fatalError(new TranscoderException(e.getMessage())); 233 } 234 } 235 236 239 protected static class NewlineKey extends TranscodingHints.Key { 240 public boolean isCompatibleValue(Object v) { 241 return v instanceof NewlineValue; 242 } 243 } 244 245 248 protected static class NewlineValue { 249 protected String value; 250 public NewlineValue(String val) { 251 value = val; 252 } 253 public String getValue() { 254 return value; 255 } 256 } 257 258 261 protected static class DoctypeKey extends TranscodingHints.Key { 262 public boolean isCompatibleValue(Object v) { 263 return v instanceof DoctypeValue; 264 } 265 } 266 267 270 protected static class DoctypeValue { 271 int value; 272 public DoctypeValue(int value) { 273 this.value = value; 274 } 275 public int getValue() { 276 return value; 277 } 278 } 279 } 280 | Popular Tags |