KickJava   Java API By Example, From Geeks To Geeks.

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


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2005 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasSiblingFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.1 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.filters;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.NodeFilter;
31 import org.htmlparser.Tag;
32 import org.htmlparser.util.NodeList;
33
34 /**
35  * This class accepts all tags that have a sibling acceptable to another filter.
36  * End tags are not considered to be siblings of any tag.
37  */

38 public class HasSiblingFilter
39     implements
40         NodeFilter
41 {
42     /**
43      * The filter to apply to the sibling.
44      */

45     public NodeFilter mSiblingFilter;
46
47     /**
48      * Creates a new instance of HasSiblingFilter.
49      * With no sibling filter, this would always return <code>false</code>
50      * from {@link #accept}.
51      */

52     public HasSiblingFilter ()
53     {
54         this (null);
55     }
56
57     /**
58      * Creates a new instance of HasSiblingFilter that accepts nodes with sibling acceptable to the filter.
59      * @param filter The filter to apply to the sibling.
60      */

61     public HasSiblingFilter (NodeFilter filter)
62     {
63         setSiblingFilter (filter);
64     }
65
66     /**
67      * Get the filter used by this HasSiblingFilter.
68      * @return The filter to apply to siblings.
69      */

70     public NodeFilter getSiblingFilter ()
71     {
72         return (mSiblingFilter);
73     }
74     
75     /**
76      * Set the filter for this HasSiblingFilter.
77      * @param filter The filter to apply to siblings in {@link #accept}.
78      */

79     public void setSiblingFilter (NodeFilter filter)
80     {
81         mSiblingFilter = filter;
82     }
83
84     /**
85      * Accept tags with a sibling acceptable to the filter.
86      * @param node The node to check.
87      */

88     public boolean accept (Node node)
89     {
90         Node parent;
91         NodeList siblings;
92         int count;
93         boolean ret;
94
95         ret = false;
96         if (!(node instanceof Tag) || !((Tag)node).isEndTag ())
97         {
98             parent = node.getParent ();
99             if (null != parent)
100             {
101                 siblings = parent.getChildren ();
102                 if (null != siblings)
103                 {
104                     count = siblings.size ();
105                     for (int i = 0; !ret && (i < count); i++)
106                         if (getSiblingFilter ().accept (siblings.elementAt (i)))
107                             ret = true;
108                 }
109                 else
110                     System.out.println("gotcha");
111             }
112         }
113
114         return (ret);
115     }
116 }
117
Popular Tags