1 18 19 package org.apache.batik.apps.svgpp; 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.batik.i18n.LocalizableSupport; 26 import org.apache.batik.transcoder.Transcoder; 27 import org.apache.batik.transcoder.TranscoderInput; 28 import org.apache.batik.transcoder.TranscoderOutput; 29 import org.apache.batik.transcoder.svg2svg.SVGTranscoder; 30 31 39 public class Main { 40 41 45 public static void main(String [] args) { 46 new Main(args).run(); 47 } 48 49 52 public final static String BUNDLE_CLASSNAME = 53 "org.apache.batik.apps.svgpp.resources.Messages"; 54 55 58 protected static LocalizableSupport localizableSupport = 59 new LocalizableSupport(BUNDLE_CLASSNAME, Main.class.getClassLoader()); 60 61 64 protected String [] arguments; 65 66 69 protected int index; 70 71 74 protected Map handlers = new HashMap (); 75 { 76 handlers.put("-doctype", new DoctypeHandler()); 77 handlers.put("-doc-width", new DocWidthHandler()); 78 handlers.put("-newline", new NewlineHandler()); 79 handlers.put("-public-id", new PublicIdHandler()); 80 handlers.put("-no-format", new NoFormatHandler()); 81 handlers.put("-system-id", new SystemIdHandler()); 82 handlers.put("-tab-width", new TabWidthHandler()); 83 handlers.put("-xml-decl", new XMLDeclHandler()); 84 } 85 86 89 protected Transcoder transcoder = new SVGTranscoder(); 90 91 95 public Main(String [] args) { 96 arguments = args; 97 } 98 99 102 public void run() { 103 if (arguments.length == 0) { 104 printUsage(); 105 return; 106 } 107 try { 108 for (;;) { 109 OptionHandler oh = (OptionHandler)handlers.get(arguments[index]); 110 if (oh == null) { 111 break; 112 } 113 oh.handleOption(); 114 } 115 TranscoderInput in; 116 in = new TranscoderInput(new java.io.FileReader (arguments[index++])); 117 TranscoderOutput out; 118 if (index < arguments.length) { 119 out = new TranscoderOutput(new java.io.FileWriter (arguments[index])); 120 } else { 121 out = new TranscoderOutput(new java.io.OutputStreamWriter (System.out)); 122 } 123 transcoder.transcode(in, out); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 printUsage(); 127 } 128 } 129 130 133 protected void printUsage() { 134 printHeader(); 135 System.out.println(localizableSupport.formatMessage("syntax", null)); 136 System.out.println(); 137 System.out.println(localizableSupport.formatMessage("options", null)); 138 Iterator it = handlers.keySet().iterator(); 139 while (it.hasNext()) { 140 String s = (String )it.next(); 141 System.out.println(((OptionHandler)handlers.get(s)).getDescription()); 142 } 143 } 144 145 148 protected void printHeader() { 149 System.out.println(localizableSupport.formatMessage("header", null)); 150 } 151 152 155 protected interface OptionHandler { 156 159 void handleOption(); 160 161 164 String getDescription(); 165 } 166 167 170 protected class DoctypeHandler implements OptionHandler { 171 protected final Map values = new HashMap (6); 172 { 173 values.put("remove", SVGTranscoder.VALUE_DOCTYPE_REMOVE); 174 values.put("change", SVGTranscoder.VALUE_DOCTYPE_CHANGE); 175 } 176 public void handleOption() { 177 index++; 178 if (index >= arguments.length) { 179 throw new IllegalArgumentException (); 180 } 181 Object val = values.get(arguments[index++]); 182 if (val == null) { 183 throw new IllegalArgumentException (); 184 } 185 transcoder.addTranscodingHint(SVGTranscoder.KEY_DOCTYPE, val); 186 } 187 188 public String getDescription() { 189 return localizableSupport.formatMessage("doctype.description", null); 190 } 191 } 192 193 196 protected class NewlineHandler implements OptionHandler { 197 protected final Map values = new HashMap (6); 198 { 199 values.put("cr", SVGTranscoder.VALUE_NEWLINE_CR); 200 values.put("cr-lf", SVGTranscoder.VALUE_NEWLINE_CR_LF); 201 values.put("lf", SVGTranscoder.VALUE_NEWLINE_LF); 202 } 203 public void handleOption() { 204 index++; 205 if (index >= arguments.length) { 206 throw new IllegalArgumentException (); 207 } 208 Object val = values.get(arguments[index++]); 209 if (val == null) { 210 throw new IllegalArgumentException (); 211 } 212 transcoder.addTranscodingHint(SVGTranscoder.KEY_NEWLINE, val); 213 } 214 215 public String getDescription() { 216 return localizableSupport.formatMessage("newline.description", null); 217 } 218 } 219 220 223 protected class NoFormatHandler implements OptionHandler { 224 public void handleOption() { 225 index++; 226 transcoder.addTranscodingHint(SVGTranscoder.KEY_FORMAT, Boolean.FALSE); 227 } 228 229 public String getDescription() { 230 return localizableSupport.formatMessage("no-format.description", null); 231 } 232 } 233 234 237 protected class PublicIdHandler implements OptionHandler { 238 public void handleOption() { 239 index++; 240 if (index >= arguments.length) { 241 throw new IllegalArgumentException (); 242 } 243 String s = arguments[index++]; 244 transcoder.addTranscodingHint(SVGTranscoder.KEY_PUBLIC_ID, s); 245 } 246 247 public String getDescription() { 248 return localizableSupport.formatMessage("public-id.description", null); 249 } 250 } 251 252 255 protected class SystemIdHandler implements OptionHandler { 256 public void handleOption() { 257 index++; 258 if (index >= arguments.length) { 259 throw new IllegalArgumentException (); 260 } 261 String s = arguments[index++]; 262 transcoder.addTranscodingHint(SVGTranscoder.KEY_SYSTEM_ID, s); 263 } 264 265 public String getDescription() { 266 return localizableSupport.formatMessage("system-id.description", null); 267 } 268 } 269 270 273 protected class XMLDeclHandler implements OptionHandler { 274 public void handleOption() { 275 index++; 276 if (index >= arguments.length) { 277 throw new IllegalArgumentException (); 278 } 279 String s = arguments[index++]; 280 transcoder.addTranscodingHint(SVGTranscoder.KEY_XML_DECLARATION, s); 281 } 282 283 public String getDescription() { 284 return localizableSupport.formatMessage("xml-decl.description", null); 285 } 286 } 287 288 291 protected class TabWidthHandler implements OptionHandler { 292 public void handleOption() { 293 index++; 294 if (index >= arguments.length) { 295 throw new IllegalArgumentException (); 296 } 297 transcoder.addTranscodingHint(SVGTranscoder.KEY_TABULATION_WIDTH, 298 new Integer (arguments[index++])); 299 } 300 301 public String getDescription() { 302 return localizableSupport.formatMessage("tab-width.description", null); 303 } 304 } 305 306 309 protected class DocWidthHandler implements OptionHandler { 310 public void handleOption() { 311 index++; 312 if (index >= arguments.length) { 313 throw new IllegalArgumentException (); 314 } 315 transcoder.addTranscodingHint(SVGTranscoder.KEY_DOCUMENT_WIDTH, 316 new Integer (arguments[index++])); 317 } 318 319 public String getDescription() { 320 return localizableSupport.formatMessage("doc-width.description", null); 321 } 322 } 323 } 324 | Popular Tags |