myhtmlhrefhandler.java

来自「Simple Web Spider - This spider can fetc」· Java 代码 · 共 43 行

JAVA
43
字号
// MyHtmlHrefHandler.java
// Written by Wei Sun @ 2006 Semester A; For Course CS5286
// This should always be used together with
//      LinkExtractor.java


import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class MyHtmlHrefHandler extends HTMLEditorKit.ParserCallback {
    public Set extLinks=new HashSet();
    public Set localLinks=new HashSet();

    public void reset(){
        extLinks.clear();
        localLinks.clear();
    }

    // This method is inherited from the super class
    //     HTMLEditorKit.ParserCallback
    public void handleStartTag(
            HTML.Tag tag, MutableAttributeSet attributes, int pos) {
        //If the element is not a link, i.e.: Not <A HREF="...">...</A>
        //Then ignore it
        if(!(tag.equals(HTML.Tag.A))){
            return;
        }
        // If the element is <A HREF="hello.html">Hello</a>
        // Then link="hello.html"
        String link=(String) attributes.getAttribute(HTML.Attribute.HREF);
        if(link==null||link.length()==0)return;

        if(link.length()>=4&&link.substring(0,4).equalsIgnoreCase("http")){
            // An external link is like "http://www.google.com/"
            extLinks.add(link);
        }else{
            // An internal link is like "/~cs5286/index.html"
            localLinks.add(link);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?