FileDocCategorySizeDatePackage
Outliner.javaAPI DocExample3865Sun Dec 12 10:52:42 GMT 2004None

Outliner

public class Outliner extends HTMLEditorKit$ParserCallback

Fields Summary
private Writer
out
private int
level
private boolean
inHeader
private static String
lineSeparator
Constructors Summary
public Outliner(Writer out)

  
     
    this.out = out;
  
Methods Summary
public voidflush()

    try {
      while (this.level-- > 0) {
        out.write(lineSeparator + "</ul>");   
      } 
      out.flush();
    }
    catch (IOException e) {
      System.err.println(e);
    }
  
public voidhandleEndTag(javax.swing.text.html.HTML$Tag tag, int position)


    if (tag == HTML.Tag.H1 || tag == HTML.Tag.H2 
     || tag == HTML.Tag.H3 || tag == HTML.Tag.H4
     || tag == HTML.Tag.H5 || tag == HTML.Tag.H6) {
      inHeader = false;
    }
    
    // work around bug in the parser that fails to call flush
    if (tag == HTML.Tag.HTML) this.flush();
    
  
public voidhandleStartTag(javax.swing.text.html.HTML$Tag tag, javax.swing.text.MutableAttributeSet attributes, int position)

    
    int newLevel = 0;
    if (tag == HTML.Tag.H1) newLevel = 1;
    else if (tag == HTML.Tag.H2) newLevel = 2;
    else if (tag == HTML.Tag.H3) newLevel = 3;
    else if (tag == HTML.Tag.H4) newLevel = 4;
    else if (tag == HTML.Tag.H5) newLevel = 5;
    else if (tag == HTML.Tag.H6) newLevel = 6;
    else return;
    
    this.inHeader = true;
    try {
      if (newLevel > this.level) {
        for (int i =0; i < newLevel-this.level; i++) {
          out.write("<ul>" + lineSeparator + "<li>");
        }
      }
      else if (newLevel < this.level) {
        for (int i =0; i < this.level-newLevel; i++) {
          out.write(lineSeparator + "</ul>" + lineSeparator);
        }
        out.write(lineSeparator + "<li>");
      }
      else {
        out.write(lineSeparator + "<li>"); 
      }
      this.level = newLevel;
      out.flush();
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
    
  
public voidhandleText(char[] text, int position)

 
    
    if (inHeader) {
      try { 
        out.write(text);
        out.flush();
      }
      catch (IOException ex) {
        System.err.println(ex);
      }
    }
    
  
public static voidmain(java.lang.String[] args)

 

    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();    
  
    String encoding = "ISO-8859-1";
    URL url = null;
    try {
      url = new URL(args[0]);
      InputStream in = url.openStream();
      InputStreamReader r = new InputStreamReader(in, encoding);
      // parse once just to detect the encoding 
      HTMLEditorKit.ParserCallback doNothing 
       = new HTMLEditorKit.ParserCallback();
      parser.parse(r, doNothing, false);
    }
    catch (MalformedURLException ex) {
      System.out.println("Usage: java Outliner url"); 
      return;
    }
    catch (ChangedCharSetException ex) {
      String mimeType = ex.getCharSetSpec();
      encoding = mimeType.substring(mimeType.indexOf("=") + 1).trim();
    }
    catch (IOException ex) {
      System.err.println(ex); 
    }
    catch (ArrayIndexOutOfBoundsException ex) {
      System.out.println("Usage: java Outliner url"); 
      return;
    }
    
    try {
      parse(url, encoding);
    }
    catch(IOException ex) {
      System.err.println(ex);
    }
          
  
private static voidparse(java.net.URL url, java.lang.String encoding)

      ParserGetter kit = new ParserGetter();
      HTMLEditorKit.Parser parser = kit.getParser();
      InputStream in = url.openStream();
      InputStreamReader r = new InputStreamReader(in, encoding);
      HTMLEditorKit.ParserCallback callback = new Outliner
       (new OutputStreamWriter(System.out));
      parser.parse(r, callback, true);