KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ccil > cowan > tagsoup > CommandLine


1 // This file is part of TagSoup.
2
//
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version. You may also distribute
7
// and/or modify it under version 2.1 of the Academic Free License.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
//
13
//
14
// The TagSoup command line UI
15

16 package org.ccil.cowan.tagsoup;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.io.*;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import org.xml.sax.*;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
24 import org.xml.sax.ext.LexicalHandler JavaDoc;
25
26
27 /**
28 The stand-alone TagSoup program.
29 **/

30 public class CommandLine {
31
32     static Hashtable JavaDoc options = new Hashtable JavaDoc(); static {
33         options.put("--nocdata", Boolean.FALSE); // CDATA elements are normal
34
options.put("--files", Boolean.FALSE); // process arguments as separate files
35
options.put("--reuse", Boolean.FALSE); // reuse a single Parser
36
options.put("--nons", Boolean.FALSE); // no namespaces
37
options.put("--nobogons", Boolean.FALSE); // suppress unknown elements
38
options.put("--any", Boolean.FALSE); // unknowns have ANY content model
39
options.put("--pyxin", Boolean.FALSE); // input is PYX
40
options.put("--lexical", Boolean.FALSE); // output comments
41
options.put("--pyx", Boolean.FALSE); // output is PYX
42
options.put("--html", Boolean.FALSE); // output is HTML
43
options.put("--method=", Boolean.FALSE); // output method
44
options.put("--output-encoding=", Boolean.FALSE); // output encoding
45
options.put("--omit-xml-declaration", Boolean.FALSE); // omit XML decl
46
options.put("--encoding=", Boolean.FALSE); // specify encoding
47
options.put("--help", Boolean.FALSE); // display help
48
options.put("--version", Boolean.FALSE); // display version
49
options.put("--nodefaults", Boolean.FALSE); // no default attrs
50
options.put("--nocolons", Boolean.FALSE); // colon to underscore
51
options.put("--norestart", Boolean.FALSE); // no restartable elements
52
options.put("--ignorable", Boolean.FALSE); // return ignorable whitespace
53
}
54
55     /**
56     Main method. Processes specified files or standard input.
57     **/

58
59     public static void main(String JavaDoc[] argv) throws IOException, SAXException {
60         int optind = getopts(options, argv);
61         if (hasOption(options, "--help")) {
62             doHelp();
63             return;
64             }
65         if (hasOption(options, "--version")) {
66             System.err.println("TagSoup version 1.0.4");
67             return;
68             }
69         if (argv.length == optind) {
70             process("", System.out);
71             }
72         else if (hasOption(options, "--files")) {
73             for (int i = optind; i < argv.length; i++) {
74                 String JavaDoc src = argv[i];
75                 String JavaDoc dst;
76                 int j = src.lastIndexOf('.');
77                 if (j == -1)
78                     dst = src + ".xhtml";
79                 else if (src.endsWith(".xhtml"))
80                     dst = src + "_";
81                 else
82                     dst = src.substring(0, j) + ".xhtml";
83                 System.err.println("src: " + src + " dst: " + dst);
84                 OutputStream os = new FileOutputStream(dst);
85                 process(src, os);
86                 }
87             }
88         else {
89             for (int i = optind; i < argv.length; i++) {
90                 System.err.println("src: " + argv[i]);
91                 process(argv[i], System.out);
92                 }
93             }
94         }
95
96     // Print the help message
97

98     private static void doHelp() {
99         System.err.print("usage: java -jar tagsoup-*.jar ");
100         System.err.print(" [ ");
101         boolean first = true;
102         for (Enumeration JavaDoc e = options.keys(); e.hasMoreElements(); ) {
103             if (!first) {
104                 System.err.print("| ");
105                 }
106             first = false;
107             String JavaDoc key = (String JavaDoc)(e.nextElement());
108             System.err.print(key);
109             if (key.endsWith("="))
110                 System.err.print("?");
111                 System.err.print(" ");
112             }
113         System.err.println("]*");
114     }
115
116     private static Parser theParser = null;
117     private static HTMLSchema theSchema = null;
118     private static String JavaDoc theOutputEncoding = null;
119
120     // Process one source onto an output stream.
121

122     private static void process(String JavaDoc src, OutputStream os)
123             throws IOException, SAXException {
124         XMLReader r;
125         if (hasOption(options, "--reuse")) {
126             if (theParser == null) theParser = new Parser();
127             r = theParser;
128             }
129         else {
130             r = new Parser();
131             }
132         theSchema = new HTMLSchema();
133         r.setProperty(Parser.schemaProperty, theSchema);
134
135         if (hasOption(options, "--nocdata")) {
136             ElementType script = theSchema.getElementType("script");
137             script.setFlags(0);
138             ElementType style = theSchema.getElementType("style");
139             style.setFlags(0);
140             }
141
142         if (hasOption(options, "--nons") || hasOption(options, "--html")) {
143             r.setFeature(Parser.namespacesFeature, false);
144             }
145
146         if (hasOption(options, "--nobogons")) {
147             r.setFeature(Parser.ignoreBogonsFeature, true);
148             }
149
150         if (hasOption(options, "--any")) {
151             r.setFeature(Parser.bogonsEmptyFeature, false);
152             }
153
154         if (hasOption(options, "--nodefaults")) {
155             r.setFeature(Parser.defaultAttributesFeature, false);
156             }
157         if (hasOption(options, "--nocolons")) {
158             r.setFeature(Parser.translateColonsFeature, true);
159             }
160
161         if (hasOption(options, "--norestart")) {
162             r.setFeature(Parser.restartElementsFeature, false);
163             }
164
165         if (hasOption(options, "--ignorable")) {
166             r.setFeature(Parser.ignorableWhitespaceFeature, true);
167             }
168
169         if (hasOption(options, "--pyxin")) {
170             r.setProperty(Parser.scannerProperty, new PYXScanner());
171             }
172
173         Writer w;
174         if (theOutputEncoding == null) {
175             w = new OutputStreamWriter(os);
176             }
177         else {
178             w = new OutputStreamWriter(os, theOutputEncoding);
179             }
180         ContentHandler h = chooseContentHandler(w);
181         r.setContentHandler(h);
182         if (hasOption(options, "--lexical") && h instanceof LexicalHandler JavaDoc) {
183             r.setProperty(Parser.lexicalHandlerProperty, h);
184             }
185         InputSource s = new InputSource();
186         if (src != "") {
187             s.setSystemId(src);
188             }
189         else {
190             s.setByteStream(System.in);
191             }
192         if (hasOption(options, "--encoding=")) {
193 // System.out.println("%% Found --encoding");
194
String JavaDoc encoding = (String JavaDoc)options.get("--encoding=");
195             if (encoding != null) s.setEncoding(encoding);
196             }
197         r.parse(s);
198         }
199
200     // Pick a content handler to generate the desired format.
201

202     private static ContentHandler chooseContentHandler(Writer w) {
203         XMLWriter x;
204         if (hasOption(options, "--pyx")) {
205             return new PYXWriter(w);
206             }
207
208         x = new XMLWriter(w);
209         if (hasOption(options, "--html")) {
210             x.setOutputProperty(XMLWriter.METHOD, "html");
211             x.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
212             }
213         if (hasOption(options, "--method=")) {
214             String JavaDoc method = (String JavaDoc)options.get("--method=");
215             if (method != null) {
216                 x.setOutputProperty(XMLWriter.METHOD, method);
217                 }
218             }
219         if (hasOption(options, "--output-encoding=")) {
220             theOutputEncoding = (String JavaDoc)options.get("--output-encoding=");
221 // System.err.println("%%%% Output encoding is " + theOutputEncoding);
222
if (theOutputEncoding != null) {
223                 x.setOutputProperty(XMLWriter.ENCODING, theOutputEncoding);
224                 }
225             }
226         if (hasOption(options, "--omit-xml-declaration")) {
227             x.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
228             }
229         x.setPrefix(theSchema.getURI(), "");
230         return x;
231         }
232
233     // Options processing
234

235     private static int getopts(Hashtable JavaDoc options, String JavaDoc[] argv) {
236         int optind;
237         for (optind = 0; optind < argv.length; optind++) {
238             String JavaDoc arg = argv[optind];
239             String JavaDoc value = null;
240             if (arg.charAt(0) != '-') break;
241             int eqsign = arg.indexOf('=');
242             if (eqsign != -1) {
243                 value = arg.substring(eqsign + 1, arg.length());
244                 arg = arg.substring(0, eqsign + 1);
245                 }
246             if (options.containsKey(arg)) {
247                 if (value == null) options.put(arg, Boolean.TRUE);
248                 else options.put(arg, value);
249 // System.out.println("%% Parsed [" + arg + "]=[" + value + "]");
250
}
251             else {
252                 System.err.print("Unknown option ");
253                 System.err.println(arg);
254                 System.exit(1);
255                 }
256             }
257         return optind;
258         }
259
260     // Return true if an option exists.
261

262     private static boolean hasOption(Hashtable JavaDoc options, String JavaDoc option) {
263         if (Boolean.getBoolean(option)) return true;
264         else if (options.get(option) != Boolean.FALSE) return true;
265         return false;
266         }
267
268     }
269
Popular Tags