| 1 | |
package com.quiotix.html.example; |
| 2 | |
import java.io.FileInputStream; |
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
|
| 6 | |
import com.quiotix.html.parser.HtmlCollector; |
| 7 | |
import com.quiotix.html.parser.HtmlDocument; |
| 8 | |
import com.quiotix.html.parser.HtmlDumper; |
| 9 | |
import com.quiotix.html.parser.HtmlFormatter; |
| 10 | |
import com.quiotix.html.parser.HtmlParser; |
| 11 | |
import com.quiotix.html.parser.HtmlScrubber; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 0 | public class HtmlFormat { |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
public static void main (String args[]) throws IOException { |
| 26 | 0 | boolean compress=false, format=false, quote=false; |
| 27 | 0 | int i, rightMargin=-1, indentIncrement=-1; |
| 28 | |
InputStream r; |
| 29 | |
HtmlDocument document; |
| 30 | |
HtmlFormatter v; |
| 31 | |
|
| 32 | 0 | for (i=0; i<args.length; i++) { |
| 33 | 0 | if (!args[i].startsWith("-")) |
| 34 | 0 | break; |
| 35 | 0 | if (args[i].equals("-compress")) { |
| 36 | 0 | compress = true; |
| 37 | 0 | format = false; |
| 38 | |
} |
| 39 | 0 | else if (args[i].equals("-format")) { |
| 40 | 0 | compress = false; |
| 41 | 0 | format = true; |
| 42 | |
} |
| 43 | 0 | else if (args[i].equals("-quote")) { |
| 44 | 0 | compress = false; |
| 45 | 0 | format = true; |
| 46 | 0 | quote = true; |
| 47 | |
} |
| 48 | 0 | else if (args[i].equals("-indent") |
| 49 | |
&& i+1 < args.length) { |
| 50 | 0 | compress = false; |
| 51 | 0 | format = true; |
| 52 | 0 | indentIncrement = (int) Integer.parseInt(args[i+1]); |
| 53 | 0 | i++; |
| 54 | |
} |
| 55 | 0 | else if (args[i].equals("-margin") |
| 56 | |
&& i+1 < args.length) { |
| 57 | 0 | rightMargin = (int) Integer.parseInt(args[i+1]); |
| 58 | 0 | i++; |
| 59 | |
} |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | int scrubberFlags = HtmlScrubber.DEFAULT_OPTIONS |
| 63 | |
| HtmlScrubber.TRIM_SPACES; |
| 64 | 0 | for (; i < args.length; i++) { |
| 65 | 0 | r = new FileInputStream(args[i]); |
| 66 | |
|
| 67 | |
try { |
| 68 | 0 | document = new HtmlParser(r).HtmlDocument(); |
| 69 | 0 | if (compress) { |
| 70 | 0 | document.accept(new HtmlScrubber(scrubberFlags)); |
| 71 | 0 | document.accept(new HtmlDumper(System.out)); |
| 72 | |
} |
| 73 | 0 | else if (format) { |
| 74 | 0 | document.accept(new HtmlCollector()); |
| 75 | 0 | if (quote) { |
| 76 | 0 | scrubberFlags = scrubberFlags | HtmlScrubber.QUOTE_ATTRS; |
| 77 | |
} |
| 78 | 0 | document.accept(new HtmlScrubber(scrubberFlags)); |
| 79 | 0 | v = new HtmlFormatter(System.out); |
| 80 | 0 | if (rightMargin != -1) v.setRightMargin(rightMargin); |
| 81 | 0 | if (indentIncrement != -1) v.setIndent(indentIncrement); |
| 82 | 0 | document.accept(v); |
| 83 | |
} |
| 84 | |
else { |
| 85 | 0 | document.accept(new HtmlCollector()); |
| 86 | 0 | document.accept(new HtmlScrubber(scrubberFlags)); |
| 87 | 0 | v = new HtmlFormatter(System.out); |
| 88 | 0 | v.setRightMargin(1024); |
| 89 | 0 | v.setIndent(0); |
| 90 | 0 | document.accept(v); |
| 91 | |
} |
| 92 | |
} |
| 93 | 0 | catch (Exception e) { |
| 94 | 0 | e.printStackTrace(); |
| 95 | |
} |
| 96 | |
finally { |
| 97 | 0 | r.close(); |
| 98 | 0 | } |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | } |
| 102 | |
} |