View Javadoc

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   * Example program to pretty-print an HTML document.
16   * Part of the Quiotix Html Parser package.  
17   * See http://www.quiotix.com/opensource/html-parser for more information
18   */
19  
20  public class HtmlFormat {
21  
22    /**
23     * Runnable.
24     */
25    public static void main (String args[]) throws IOException {
26      boolean compress=false, format=false, quote=false;
27      int i, rightMargin=-1, indentIncrement=-1;
28      InputStream r;
29      HtmlDocument document;
30      HtmlFormatter v;
31  
32      for (i=0; i<args.length; i++) {
33        if (!args[i].startsWith("-"))
34          break;
35        if (args[i].equals("-compress")) { 
36          compress = true;
37          format = false;
38        }
39        else if (args[i].equals("-format")) {
40          compress = false;
41          format = true;
42        }
43        else if (args[i].equals("-quote")) {
44          compress = false;
45          format = true;
46          quote = true;
47        }
48        else if (args[i].equals("-indent")
49                 && i+1 < args.length) {
50          compress = false;
51          format = true;
52          indentIncrement = (int) Integer.parseInt(args[i+1]);
53          i++;
54        }
55        else if (args[i].equals("-margin")
56                 && i+1 < args.length) {
57          rightMargin = (int) Integer.parseInt(args[i+1]);
58          i++;
59        }
60      }
61  
62      int scrubberFlags = HtmlScrubber.DEFAULT_OPTIONS 
63                          | HtmlScrubber.TRIM_SPACES;
64      for (; i < args.length; i++) { 
65        r = new FileInputStream(args[i]);
66      
67        try { 
68          document = new HtmlParser(r).HtmlDocument();
69          if (compress) {
70            document.accept(new HtmlScrubber(scrubberFlags));
71            document.accept(new HtmlDumper(System.out));
72          }
73          else if (format) {
74            document.accept(new HtmlCollector());
75            if (quote) {
76              scrubberFlags = scrubberFlags | HtmlScrubber.QUOTE_ATTRS;
77            }
78            document.accept(new HtmlScrubber(scrubberFlags));
79            v = new HtmlFormatter(System.out);
80            if (rightMargin != -1)     v.setRightMargin(rightMargin);
81            if (indentIncrement != -1) v.setIndent(indentIncrement);
82            document.accept(v);
83          }
84          else {
85            document.accept(new HtmlCollector());
86            document.accept(new HtmlScrubber(scrubberFlags));
87            v = new HtmlFormatter(System.out);
88            v.setRightMargin(1024);
89            v.setIndent(0);
90            document.accept(v);
91          }
92        }
93        catch (Exception e) {
94          e.printStackTrace();
95        }
96        finally {
97          r.close();
98        }
99      }
100     
101   }
102 }