1 16 package org.ccil.cowan.tagsoup; 17 import java.util.Hashtable ; 18 import java.util.Enumeration ; 19 import java.io.*; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 import org.xml.sax.*; 23 import org.xml.sax.helpers.DefaultHandler ; 24 import org.xml.sax.ext.LexicalHandler ; 25 26 27 30 public class CommandLine { 31 32 static Hashtable options = new Hashtable (); static { 33 options.put("--nocdata", Boolean.FALSE); options.put("--files", Boolean.FALSE); options.put("--reuse", Boolean.FALSE); options.put("--nons", Boolean.FALSE); options.put("--nobogons", Boolean.FALSE); options.put("--any", Boolean.FALSE); options.put("--pyxin", Boolean.FALSE); options.put("--lexical", Boolean.FALSE); options.put("--pyx", Boolean.FALSE); options.put("--html", Boolean.FALSE); options.put("--method=", Boolean.FALSE); options.put("--output-encoding=", Boolean.FALSE); options.put("--omit-xml-declaration", Boolean.FALSE); options.put("--encoding=", Boolean.FALSE); options.put("--help", Boolean.FALSE); options.put("--version", Boolean.FALSE); options.put("--nodefaults", Boolean.FALSE); options.put("--nocolons", Boolean.FALSE); options.put("--norestart", Boolean.FALSE); options.put("--ignorable", Boolean.FALSE); } 54 55 58 59 public static void main(String [] 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 src = argv[i]; 75 String 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 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 e = options.keys(); e.hasMoreElements(); ) { 103 if (!first) { 104 System.err.print("| "); 105 } 106 first = false; 107 String key = (String )(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 theOutputEncoding = null; 119 120 122 private static void process(String 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 ) { 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 String encoding = (String )options.get("--encoding="); 195 if (encoding != null) s.setEncoding(encoding); 196 } 197 r.parse(s); 198 } 199 200 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 method = (String )options.get("--method="); 215 if (method != null) { 216 x.setOutputProperty(XMLWriter.METHOD, method); 217 } 218 } 219 if (hasOption(options, "--output-encoding=")) { 220 theOutputEncoding = (String )options.get("--output-encoding="); 221 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 235 private static int getopts(Hashtable options, String [] argv) { 236 int optind; 237 for (optind = 0; optind < argv.length; optind++) { 238 String arg = argv[optind]; 239 String 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 } 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 262 private static boolean hasOption(Hashtable options, String 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 |