Source code: org/apache/batik/apps/svgpp/Main.java
1 /*
2
3 Copyright 1999-2003 The Apache Software Foundation
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
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 /**
32 * This class is the main class of the svgpp application.
33 * <p>
34 * svgpp is a pretty-printer for SVG source files.
35 *
36 * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
37 * @version $Id: Main.java,v 1.8 2004/10/30 18:38:04 deweese Exp $
38 */
39 public class Main {
40
41 /**
42 * The application main method.
43 * @param args The command-line arguments.
44 */
45 public static void main(String[] args) {
46 new Main(args).run();
47 }
48
49 /**
50 * The default resource bundle base name.
51 */
52 public final static String BUNDLE_CLASSNAME =
53 "org.apache.batik.apps.svgpp.resources.Messages";
54
55 /**
56 * The localizable support.
57 */
58 protected static LocalizableSupport localizableSupport =
59 new LocalizableSupport(BUNDLE_CLASSNAME, Main.class.getClassLoader());
60
61 /**
62 * The arguments.
63 */
64 protected String[] arguments;
65
66 /**
67 * The current index.
68 */
69 protected int index;
70
71 /**
72 * The option handlers.
73 */
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 /**
87 * The transcoder.
88 */
89 protected Transcoder transcoder = new SVGTranscoder();
90
91 /**
92 * Initializes the application.
93 * @param args The command-line arguments.
94 */
95 public Main(String[] args) {
96 arguments = args;
97 }
98
99 /**
100 * Runs the pretty printer.
101 */
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 /**
131 * Prints the command usage.
132 */
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 /**
146 * Prints the command header.
147 */
148 protected void printHeader() {
149 System.out.println(localizableSupport.formatMessage("header", null));
150 }
151
152 /**
153 * This interface represents an option handler.
154 */
155 protected interface OptionHandler {
156 /**
157 * Handles the current option.
158 */
159 void handleOption();
160
161 /**
162 * Returns the option description.
163 */
164 String getDescription();
165 }
166
167 /**
168 * To handle the '-doctype' option.
169 */
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 /**
194 * To handle the '-newline' option.
195 */
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 /**
221 * To handle the '-no-format' option.
222 */
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 /**
235 * To handle the '-public-id' option.
236 */
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 /**
253 * To handle the '-system-id' option.
254 */
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 /**
271 * To handle the '-xml-decl' option.
272 */
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 /**
289 * To handle the '-tab-width' option.
290 */
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 /**
307 * To handle the '-doc-width' option.
308 */
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 }