⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webgraph.java

📁 一个用java语言编写的网络爬虫程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                n.fixed = true;                placeNodeOnGraph (n, 0, 0);            }            else {                // root node of an additional tree -- drop it randomly                // within window                                Dimension d = size ();                                int x = (int)(Math.random() * d.width);                int y = (int)(Math.random() * d.height);                placeNodeOnScreen (n, x, y);            }        }        else {            // check if parent's filter allows this link to be displayed            if (!shouldDisplay (parent.filter, link.getStatus ()))                return;            // hang the node off its parent in a random direction            double len = getRestLength();            double angle = Math.random () * 2 * Math.PI;            double x = parent.x + len*Math.cos(angle);            double y = parent.y + len*Math.sin(angle);            update (n);            addNode (n);            placeNodeOnGraph (n, x, y);            WebEdge e = new WebEdge (link, parent, n);            links.put (link, e);            update (e);            addEdge (e);        }    }    void update (WebEdge e) {        e.color = Colors.parseColor (e.link.getLabel ("Workbench.color"));        e.thick = e.link.hasLabel ("Workbench.thick");    }    void update (WebNode n) {        Page page = n.link.getPage ();        int status = n.link.getStatus ();        if (page == null) {            // not downloaded yet            switch (n.rendering) {            case ICON:                n.name = null;                n.icon = getIcon (LinkEvent.eventName[status]);                break;            case TITLE:            case ABSOLUTE_URL:                n.name = n.link.getURL().toString();                n.icon = null;                break;            case RELATIVE_URL: {                Link origin = n.link.getSource().getOrigin();                if (origin != null)                    n.name = Link.relativeTo (origin.getURL(), n.link.getURL());                else                    n.name = n.link.getURL().toString();                n.icon = null;                break;            }            }        }        else {            switch (n.rendering) {            case ICON:                n.name = null;                n.icon = getIcon (page.getLabel ("Workbench.icon",                                                  LinkEvent.eventName[status]));                break;            case TITLE:                n.name = page.getTitle ();                if (n.name == null)                    n.name = "[" + n.link.getURL().toString() + "]";                n.icon = null;                break;            case ABSOLUTE_URL:                n.name = n.link.getURL().toString();                n.icon = null;                break;            case RELATIVE_URL: {                Link origin = n.link.getSource().getOrigin();                if (origin != null)                    n.name = Link.relativeTo (origin.getURL(), n.link.getURL());                else                    n.name = n.link.getURL().toString();                n.icon = null;                break;            }            }            n.color = Colors.parseColor (page.getLabel ("Workbench.color"));            n.scale = page.getNumericLabel ("Workbench.size",                             new Integer (1)).doubleValue ();        }        if (n.icon == null) {            FontMetrics fm = getFontMetrics ();            n.width = fm.stringWidth (n.name) + 10;            n.height = fm.getHeight () + 4;        }        else {            n.width = (int)(n.icon.getWidth(this) * n.scale);            n.height = (int)(n.icon.getHeight(this) * n.scale);        }    }    WebEdge findEdge (Link l) {        if (l == null)                return null;            Object obj = links.get (l);            if (obj instanceof WebEdge)                return (WebEdge)obj;            else                return null;    }            WebNode findNode (Link l) {        if (l == null)            return null;        Object obj = links.get (l);        if (obj instanceof WebEdge)            return (WebNode)((WebEdge)obj).to;        else            return (WebNode)obj;    }        WebNode findParent (Link l) {        if (l == null)            return null;        Page source = l.getSource ();        Link origin	= source.getOrigin ();        return findNode (origin);    }    /*     * LinkView listeners     */    private Vector listeners = new Vector ();    /**     * Add a listener for LinkViewEvents.  A LinkViewEvent is sent every time a     * node or edge in the graph is double-clicked.     * @param listener Object that wants to receive LinkViewEvents      */    public void addLinkViewListener (LinkViewListener listener) {        if (!listeners.contains (listener))            listeners.addElement (listener);    }    /**     * Removes a listener from the set of LinkViewEvent listeners.  If it is not found in the set,     * does nothing.     *     * @param listen a listener     */    public void removeLinkViewListener (CrawlListener listener) {        listeners.removeElement (listener);    }    void fireEvent (Link link) {        LinkViewEvent event = new LinkViewEvent (this, link);        for (int j=0, len=listeners.size(); j<len; ++j) {            LinkViewListener listen = (LinkViewListener)listeners.elementAt(j);            listen.viewLink (event);        }    }    void doubleClick (int x, int y) {        Object over = pick (x, y);        if (over != null) {            Link link;            if (over instanceof WebNode)                link = ((WebNode)over).link;            else                link = ((WebEdge)over).link;            fireEvent (link);        }    }            public boolean handleEvent (Event event) {        if (event.id == Event.MOUSE_DOWN && event.clickCount == 2) {            doubleClick (event.x, event.y);        }        else            return super.handleEvent (event);        return true;    }        public Link getSelectedLink () {        WebNode n = (WebNode)getSelectedNode();        if (n != null)            return n.link;                    WebEdge e = (WebEdge)getSelectedEdge();        if (e != null)            return e.link;                    return null;    }        /**     * Create a new Frame containing a WebGraph connected to a crawler.     */     public static Frame monitor (Crawler crawler) {        Frame win = new ClosableFrame ("Graph: " + crawler.getName ());        WebGraph g = new WebGraph ();        crawler.addCrawlListener (g);        crawler.addLinkListener (g);        g.setNodeCharge (1000);        g.setRestLength (50);        win.add ("Center", g);        win.pack ();        win.show ();        return win;    }    static String[] getTip (Link link) {        Vector result = new Vector ();        String exception = link.getLabel ("exception");        if (exception != null && exception.length() > 0)            result.addElement ("*** " + exception);        String anchor = link.toText ();        if (anchor != null && anchor.length() > 0)            result.addElement (anchor);        String url = link.getURL ().toString ();        if (url != null && url.length() > 0)            result.addElement (url);        String labels = link.getObjectLabels ();        if (labels != null && labels.length() > 0)            result.addElement (labels);        String[] tip = new String[result.size ()];        result.copyInto (tip);        return tip;    }    static String[] getTip (Page page) {        Vector result = new Vector ();        String title = page.getTitle ();        if (title != null && title.length() > 0)            result.addElement (title);        String url = page.getURL ().toString();        if (url != null && url.length() > 0)            result.addElement (url);        String labels = page.getObjectLabels ();        if (labels != null && labels.length() > 0)            result.addElement (labels);        String[] tip = new String[result.size ()];        result.copyInto (tip);        return tip;    }    Hashtable icons = new Hashtable ();       // maps String (CrawlEvent name or user-defined icon name) to Image    Image pageIcon;    Image linkIcon;    Image retrievingIcon;

⌨️ 快捷键说明

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