KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > filters > LinkStringFilter


1 // HTMLParser Library - A java-based parser for HTML
2
// http://htmlparser.org
3
// Copyright (C) 2006 John Derrick
4
//
5
// Revision Control Information
6
//
7
// $URL: https://svn.sourceforge.net/svnroot/htmlparser/trunk/parser/src/main/java/org/htmlparser/filters/LinkStringFilter.java $
8
// $Author: derrickoswald $
9
// $Date: 2006-09-16 10:44:17 -0400 (Sat, 16 Sep 2006) $
10
// $Revision: 4 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the Common Public License; either
14
// version 1.0 of the License, or (at your option) any later version.
15
//
16
// This library is distributed in the hope that it will be useful,
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
// Common Public License for more details.
20
//
21
// You should have received a copy of the Common Public License
22
// along with this library; if not, the license is available from
23
// the Open Source Initiative (OSI) website:
24
// http://opensource.org/licenses/cpl1.0.php
25

26 package org.htmlparser.filters;
27
28 import org.htmlparser.Node;
29 import org.htmlparser.NodeFilter;
30 import org.htmlparser.tags.LinkTag;
31
32 /**
33  * This class accepts tags of class LinkTag that contain a link matching a given
34  * pattern string. Use this filter to extract LinkTag nodes with URLs containing
35  * the desired string.
36  */

37 public class LinkStringFilter implements NodeFilter
38 {
39     /**
40      * The pattern to search for in the link.
41      */

42     protected String JavaDoc mPattern;
43
44     /**
45      * Flag indicating case sensitive/insensitive search.
46      */

47     protected boolean mCaseSensitive;
48
49     /**
50      * Creates a LinkStringFilter that accepts LinkTag nodes containing
51      * a URL that matches the supplied pattern.
52      * The match is case insensitive.
53      * @param pattern The pattern to match.
54      */

55     public LinkStringFilter (String JavaDoc pattern)
56     {
57         this (pattern, false);
58     }
59
60     /**
61      * Creates a LinkStringFilter that accepts LinkTag nodes containing
62      * a URL that matches the supplied pattern.
63      * @param pattern The pattern to match.
64      * @param caseSensitive Specifies case sensitivity for the matching process.
65      */

66     public LinkStringFilter (String JavaDoc pattern, boolean caseSensitive)
67     {
68         mPattern = pattern;
69         mCaseSensitive = caseSensitive;
70     }
71
72     /**
73      * Accept nodes that are a LinkTag and
74      * have a URL that matches the pattern supplied in the constructor.
75      * @param node The node to check.
76      * @return <code>true</code> if the node is a link with the pattern.
77      */

78     public boolean accept (Node node)
79     {
80         boolean ret;
81
82         ret = false;
83         if (LinkTag.class.isAssignableFrom (node.getClass ()))
84         {
85             String JavaDoc link = ((LinkTag)node).getLink ();
86             if (mCaseSensitive)
87             {
88                 if (link.indexOf (mPattern) > -1)
89                     ret = true;
90             }
91             else
92             {
93                 if (link.toUpperCase ().indexOf (mPattern.toUpperCase ()) > -1)
94                     ret = true;
95             }
96         }
97
98         return (ret);
99     }
100 }
101
Popular Tags