2 Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.pipeline;
40 import java.io.IOException;
42 import java.util.Enumeration;
43 import java.util.Vector;
45 import org.xml.sax.Attributes;
46 import org.xml.sax.Locator;
47 import org.xml.sax.SAXException;
51 * Pipeline filter to remember XHTML links found in a document,
52 * so they can later be crawled. Fragments are not counted, and duplicates
53 * are ignored. Callers are responsible for filtering out URLs they aren't
54 * interested in. Events are passed through unmodified.
56 * <p> Input MUST include a setDocumentLocator() call, as it's used to
57 * resolve relative links in the absence of a "base" element. Input MUST
58 * also include namespace identifiers, since it is the XHTML namespace
59 * identifier which is used to identify the relevant elements.
61 * <p><em>FIXME:</em> handle xml:base attribute ... in association with
62 * a stack of base URIs. Similarly, recognize/support XLink data.
64 * @author David Brownell
66 public class LinkFilter extends EventFilter
69 private Vector vector = new Vector ();
71 // struct for "full" link record (tbd)
72 // these for troubleshooting original source:
74 // uri as resolved (base, relative, etc)
75 // URI of originating doc
77 // original element + attrs (img src, desc, etc)
79 // XLink model of the link ... for inter-site pairups ?
81 private String baseURI;
83 private boolean siteRestricted = false;
86 // XXX leverage blacklist info (like robots.txt)
88 // XXX constructor w/param ... pipeline for sending link data
89 // probably XHTML --> XLink, providing info as sketched above
94 * Constructs a new event filter, which collects links in private data
95 * structure for later enumeration.
97 // constructor used by PipelineFactory
100 super.setContentHandler (this);
105 * Constructs a new event filter, which collects links in private data
106 * structure for later enumeration and passes all events, unmodified,
107 * to the next consumer.
109 // constructor used by PipelineFactory
110 public LinkFilter (EventConsumer next)
113 super.setContentHandler (this);
118 * Returns an enumeration of the links found since the filter
119 * was constructed, or since removeAllLinks() was called.
121 * @return enumeration of strings.
123 public Enumeration getLinks ()
125 return vector.elements ();
129 * Removes records about all links reported to the event
130 * stream, as if the filter were newly created.
132 public void removeAllLinks ()
134 vector = new Vector ();
139 * Collects URIs for (X)HTML content from elements which hold them.
141 public void startElement (
146 ) throws SAXException
150 // Recognize XHTML links.
151 if ("http://www.w3.org/1999/xhtml".equals (uri)) {
153 if ("a".equals (localName) || "base".equals (localName)
154 || "area".equals (localName))
155 link = atts.getValue ("href");
156 else if ("iframe".equals (localName) || "frame".equals (localName))
157 link = atts.getValue ("src");
158 else if ("blockquote".equals (localName) || "q".equals (localName)
159 || "ins".equals (localName) || "del".equals (localName))
160 link = atts.getValue ("cite");
163 link = maybeAddLink (link);
165 // "base" modifies designated baseURI
166 if ("base".equals (localName) && link != null)
169 if ("iframe".equals (localName) || "img".equals (localName))
170 maybeAddLink (atts.getValue ("longdesc"));
173 super.startElement (uri, localName, qName, atts);
176 private String maybeAddLink (String link)
180 // ignore empty links and fragments inside docs
183 if ((index = link.indexOf ("#")) >= 0)
184 link = link.substring (0, index);
185 if (link.equals (""))
190 URL base = new URL ((baseURI != null)
192 : getDocumentLocator ().getSystemId ());
193 URL url = new URL (base, link);
195 link = url.toString ();
198 if (vector.contains (link))
201 // other than what "base" does, stick to original site:
202 if (siteRestricted) {
203 // don't switch protocols
204 if (!base.getProtocol ().equals (url.getProtocol ()))
206 // don't switch servers
207 if (base.getHost () != null
208 && !base.getHost ().equals (url.getHost ()))
212 vector.addElement (link);
216 } catch (IOException e) {
217 // bad URLs we don't want
223 * Reports an error if no Locator has been made available.
225 public void startDocument ()
228 if (getDocumentLocator () == null)
229 throw new SAXException ("no Locator!");
233 * Forgets about any base URI information that may be recorded.
234 * Applications will often want to call removeAllLinks(), likely
235 * after examining the links which were reported.
237 public void endDocument ()
241 super.endDocument ();