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., 51 Franklin Street, Fifth Floor, 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
.SAXException
;
50 * Pipeline filter to remember XHTML links found in a document,
51 * so they can later be crawled. Fragments are not counted, and duplicates
52 * are ignored. Callers are responsible for filtering out URLs they aren't
53 * interested in. Events are passed through unmodified.
55 * <p> Input MUST include a setDocumentLocator() call, as it's used to
56 * resolve relative links in the absence of a "base" element. Input MUST
57 * also include namespace identifiers, since it is the XHTML namespace
58 * identifier which is used to identify the relevant elements.
60 * <p><em>FIXME:</em> handle xml:base attribute ... in association with
61 * a stack of base URIs. Similarly, recognize/support XLink data.
63 * @author David Brownell
65 public class LinkFilter
extends EventFilter
68 private Vector vector
= new Vector ();
70 // struct for "full" link record (tbd)
71 // these for troubleshooting original source:
73 // uri as resolved (base, relative, etc)
74 // URI of originating doc
76 // original element + attrs (img src, desc, etc)
78 // XLink model of the link ... for inter-site pairups ?
80 private String baseURI
;
82 private boolean siteRestricted
= false;
85 // XXX leverage blacklist info (like robots.txt)
87 // XXX constructor w/param ... pipeline for sending link data
88 // probably XHTML --> XLink, providing info as sketched above
93 * Constructs a new event filter, which collects links in private data
94 * structure for later enumeration.
96 // constructor used by PipelineFactory
99 super.setContentHandler (this);
104 * Constructs a new event filter, which collects links in private data
105 * structure for later enumeration and passes all events, unmodified,
106 * to the next consumer.
108 // constructor used by PipelineFactory
109 public LinkFilter (EventConsumer next
)
112 super.setContentHandler (this);
117 * Returns an enumeration of the links found since the filter
118 * was constructed, or since removeAllLinks() was called.
120 * @return enumeration of strings.
122 public Enumeration
getLinks ()
124 return vector
.elements ();
128 * Removes records about all links reported to the event
129 * stream, as if the filter were newly created.
131 public void removeAllLinks ()
133 vector
= new Vector ();
138 * Collects URIs for (X)HTML content from elements which hold them.
140 public void startElement (
145 ) throws SAXException
149 // Recognize XHTML links.
150 if ("http://www.w3.org/1999/xhtml".equals (uri
)) {
152 if ("a".equals (localName
) || "base".equals (localName
)
153 || "area".equals (localName
))
154 link
= atts
.getValue ("href");
155 else if ("iframe".equals (localName
) || "frame".equals (localName
))
156 link
= atts
.getValue ("src");
157 else if ("blockquote".equals (localName
) || "q".equals (localName
)
158 || "ins".equals (localName
) || "del".equals (localName
))
159 link
= atts
.getValue ("cite");
162 link
= maybeAddLink (link
);
164 // "base" modifies designated baseURI
165 if ("base".equals (localName
) && link
!= null)
168 if ("iframe".equals (localName
) || "img".equals (localName
))
169 maybeAddLink (atts
.getValue ("longdesc"));
172 super.startElement (uri
, localName
, qName
, atts
);
175 private String
maybeAddLink (String link
)
179 // ignore empty links and fragments inside docs
182 if ((index
= link
.indexOf ("#")) >= 0)
183 link
= link
.substring (0, index
);
184 if (link
.equals (""))
189 URL base
= new URL ((baseURI
!= null)
191 : getDocumentLocator ().getSystemId ());
192 URL url
= new URL (base
, link
);
194 link
= url
.toString ();
197 if (vector
.contains (link
))
200 // other than what "base" does, stick to original site:
201 if (siteRestricted
) {
202 // don't switch protocols
203 if (!base
.getProtocol ().equals (url
.getProtocol ()))
205 // don't switch servers
206 if (base
.getHost () != null
207 && !base
.getHost ().equals (url
.getHost ()))
211 vector
.addElement (link
);
215 } catch (IOException e
) {
216 // bad URLs we don't want
222 * Reports an error if no Locator has been made available.
224 public void startDocument ()
227 if (getDocumentLocator () == null)
228 throw new SAXException ("no Locator!");
232 * Forgets about any base URI information that may be recorded.
233 * Applications will often want to call removeAllLinks(), likely
234 * after examining the links which were reported.
236 public void endDocument ()
240 super.endDocument ();