KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > LinkChecker


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: LinkChecker.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.dom4j.Attribute;
16 import org.dom4j.Document;
17
18 /**
19  * A sample program to demonstrate the use of XPath in DOM4J to find all the
20  * hypertext links in a source file.
21  *
22  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
23  * @version $Revision: 1.4 $
24  */

25 public class LinkChecker extends SAXDemo {
26
27     public static void main(String JavaDoc[] args) {
28         run(new LinkChecker(), args);
29     }
30
31     public LinkChecker() {
32     }
33
34     protected void process(Document document) throws Exception JavaDoc {
35
36         List JavaDoc list = document.selectNodes("//*[local-name()='a']/@href");
37
38         System.out.println("Found: " + list.size() + " links(s)");
39
40         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
41             Attribute attribute = (Attribute) iter.next();
42             System.out.println("href = " + attribute.getValue());
43         }
44     }
45 }
46
47 /*
48  * Redistribution and use of this software and associated documentation
49  * ("Software"), with or without modification, are permitted provided that the
50  * following conditions are met:
51  *
52  * 1. Redistributions of source code must retain copyright statements and
53  * notices. Redistributions must also contain a copy of this document.
54  *
55  * 2. Redistributions in binary form must reproduce the above copyright notice,
56  * this list of conditions and the following disclaimer in the documentation
57  * and/or other materials provided with the distribution.
58  *
59  * 3. The name "DOM4J" must not be used to endorse or promote products derived
60  * from this Software without prior written permission of MetaStuff, Ltd. For
61  * written permission, please contact dom4j-info@metastuff.com.
62  *
63  * 4. Products derived from this Software may not be called "DOM4J" nor may
64  * "DOM4J" appear in their names without prior written permission of MetaStuff,
65  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
66  *
67  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
68  *
69  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
70  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
71  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
72  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
73  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
74  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
75  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
77  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
78  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
79  * POSSIBILITY OF SUCH DAMAGE.
80  *
81  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
82  *
83  * $Id: LinkChecker.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
84  */

85
Popular Tags