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


import javax.swing.text.*; 
import javax.swing.text.html.*; 
import java.util.*; 
import java.net.*; 
import java.io.*; 
 
public class DealHTMLTagGetURL implements DealHTMLTag 
{ 
  private Vector urls; 
  private boolean saveImage; 
  private boolean saveScript; 
   
  private boolean tagA; 
  private boolean tagScript; 
   
  private Link link; 
   
  DealHTMLTagGetURL(Project project, Vector urls) 
  { 
    this.urls = urls; 
    this.urls.clear(); 
     
    this.saveImage = project.sImage; 
    this.saveScript = project.sScript; 
     
    tagA = false; 
     
    link = new Link("", "the BASE is null"); 
    urls.add(link); 
  } 
   
  public String getBase() 
  { 
    return ((Link)urls.elementAt(0)).url; 
  } 
   
  public boolean dealTag(HTML.Tag tag, MutableAttributeSet attributes) 
  { 
    if (tag == HTML.Tag.A) 
    { 
      if (attributes.isDefined(HTML.Attribute.HREF)) 
      { 
        Object url = attributes.getAttribute(HTML.Attribute.HREF); 
        link = new Link(url.toString(), new String("")); 
        urls.add(link); 
         
        tagA = true; 
      } 
    } 
    else if (tag == HTML.Tag.IMG) 
    { 
      Object url = attributes.getAttribute(HTML.Attribute.SRC); 
 
      if (tagA) 
      { 
        link.text = new String("%% IMAGE: " + url.toString()); 
        tagA = false; 
      } 
       
      if (saveImage) 
      { 
        link = new Link(url.toString(), new String("%% This is an image URL")); 
        urls.add(link); 
      } 
    } 
    else if (tag == HTML.Tag.SCRIPT) 
    { 
      if (attributes.isDefined(HTML.Attribute.SRC)) 
      { 
        Object url = attributes.getAttribute(HTML.Attribute.SRC); 
        link = new Link(url.toString(), new String("%% This is a script file")); 
        urls.add(link); 
      } 
    } 
    else if (tag == HTML.Tag.FRAME) 
    { 
      if (attributes.isDefined(HTML.Attribute.SRC)) 
      { 
        Object url = attributes.getAttribute(HTML.Attribute.SRC); 
        link = new Link(url.toString(), new String("%% This is a frame src")); 
        urls.add(link); 
      } 
    } 
    else if (tag == HTML.Tag.BASE) 
    { 
      Object url = attributes.getAttribute(HTML.Attribute.HREF); 
      link = new Link(url.toString(), new String("%% This is the BASE")); 
      urls.set(0, link); 
    } 
     
    return false; 
  } 
   
  public boolean dealEndTag(HTML.Tag tag) 
  { 
    return false; 
  } 
   
  public boolean dealText(char[] text) 
  { 
    if (tagA == true) 
    { 
      link.text = new String(text); 
       
      tagA = false; 
    } 
     
    return false; 
  } 
   
  public static void main(String[] str) 
  { 
    Vector urls = new Vector(); 
    DealHTMLTagGetURL get = new DealHTMLTagGetURL(new Project(), urls); 
     
    try { 
      URL url = new URL((new SystemIn()).readLine()); 
      InputStreamReader in = new InputStreamReader(url.openStream()); 
       
      DealHTMLStream.dealHTMLStream(in, null, get); 
    } 
    catch (IOException e) { 
      System.err.println("IO error!"); 
      return; 
    } 
     
    int i; 
    for (i = 0; i < urls.size(); i++) 
    { 
      Link link = (Link)urls.elementAt(i); 
      System.out.println(link.url + " : " + link.text); 
    } 
     
    System.out.println("\nBase is>>>>>>>> : " + get.getBase()); 
  } 
}