www.pudn.com > htmlsaver.rar > DealHTMLStream.java


import javax.swing.text.*; 
import javax.swing.text.html.*; 
import javax.swing.text.html.parser.*; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
 
class ParserGetter extends HTMLEditorKit 
{ 
  public HTMLEditorKit.Parser getParser() 
  { 
    return super.getParser(); 
  } 
} 
 
class DealHTMLCallback extends HTMLEditorKit.ParserCallback 
{ 
  private Writer out; 
  private DealHTMLTag deal; 
   
  DealHTMLCallback(Writer out, DealHTMLTag deal) 
  { 
    this.out = out; 
    this.deal = deal; 
  } 
   
  public void handleStartTag(HTML.Tag tag, 
    MutableAttributeSet attributes, int position) 
  { 
    if (!deal.dealTag(tag, attributes) && (out == null)) 
      return; 
     
    String str = new String("<" + tag.toString()); 
    if (attributes.getAttributeCount() != 0) 
    { 
      int i; 
      Enumeration e = attributes.getAttributeNames(); 
      while (e.hasMoreElements()) 
      { 
        Object name = e.nextElement(); 
        Object value = attributes.getAttribute(name); 
        str += " " + name.toString() + "=\"" + value.toString() + "\""; 
      } 
    } 
    str += ">"; 
    if (tag.isBlock() && (tag != HTML.Tag.TD)) 
      str += "\r\n"; 
     
    try { 
      out.write(str); 
    } 
    catch (IOException e) {} 
  } 
   
  public void handleEndTag(HTML.Tag tag, int position) 
  { 
    if (!deal.dealEndTag(tag) && (out == null)) 
      return; 
     
    String str = new String(""); 
    if (tag.isBlock() || tag.breaksFlow()) 
      str += "\r\n"; 
       
    try { 
      out.write(str); 
      out.flush(); 
    } 
    catch (IOException e) {} 
  } 
   
  public void handleSimpleTag(HTML.Tag tag, 
    MutableAttributeSet attributes, int position) 
  { 
    if (!deal.dealTag(tag, attributes) && (out == null)) 
      return; 
     
    String str = new String("<" + tag.toString()); 
    if (attributes.getAttributeCount() != 0) 
    { 
      int i; 
      Enumeration e = attributes.getAttributeNames(); 
      while (e.hasMoreElements()) 
      { 
        Object name = e.nextElement(); 
        Object value = attributes.getAttribute(name); 
        str += " " + name.toString() + "=\"" + value.toString() + "\""; 
      } 
    } 
    str += ">"; 
    if (tag.breaksFlow()) 
      str += "\r\n"; 
     
    try { 
      out.write(str); 
      out.flush(); 
    } 
    catch (IOException e) {} 
  } 
       
  public void handleText(char[] text, int position) 
  { 
    if (!deal.dealText(text) && (out == null)) 
      return; 
     
    try { 
      out.write(text); 
    } 
    catch (IOException e) {} 
  } 
} 
 
public class DealHTMLStream 
{ 
  public static boolean dealHTMLStream(Reader in, Writer out, DealHTMLTag op) 
  { 
    ParserGetter kit = new ParserGetter(); 
    HTMLEditorKit.Parser parser = kit.getParser(); 
     
    HTMLEditorKit.ParserCallback callback = new DealHTMLCallback(out, op); 
    try { 
      parser.parse(in, callback, true); 
    } 
    catch (IOException e) { 
      return false; 
    } 
    /* 
    try { 
      if (out != null) 
        out.close(); 
      in.close(); 
    } 
    catch (IOException e) { 
    } 
    */ 
    return true; 
  } 
   
  public static void main(String[] args) 
  { 
    File f = new File("temp.html"); 
    if (f.exists()) 
      f.delete(); 
       
    try { 
      f.createNewFile(); 
      OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f)); 
       
      URL url = new URL(new SystemIn().readLine()); 
      InputStreamReader in = new InputStreamReader(url.openStream()); 
       
      dealHTMLStream(in, out, new DealHTMLTagNothing()); 
    } 
    catch (IOException e) { 
    } 
  } 
}