KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > wrappers > OrFilterWrapper


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/parserapplications/filterbuilder/wrappers/OrFilterWrapper.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:43:06 $
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.parserapplications.filterbuilder.wrappers;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.NodeFilter;
31 import org.htmlparser.Parser;
32 import org.htmlparser.filters.OrFilter;
33 import org.htmlparser.parserapplications.filterbuilder.Filter;
34 import org.htmlparser.parserapplications.filterbuilder.SubFilterList;
35
36 /**
37  * Wrapper for OrFilters.
38  */

39 public class OrFilterWrapper
40     extends
41         Filter
42 {
43     /**
44      * The drop target container.
45      */

46     protected SubFilterList mContainer;
47     
48     /**
49      * The underlying filter.
50      */

51     protected OrFilter mFilter;
52
53     /**
54      * Create a wrapper over a new OrFilter.
55      */

56     public OrFilterWrapper ()
57     {
58         mFilter = new OrFilter ();
59
60         // add the subfilter container
61
mContainer = new SubFilterList (this, "Predicates", 0);
62         add (mContainer);
63     }
64
65     //
66
// Filter overrides and concrete implementations
67
//
68

69     public String JavaDoc getDescription ()
70     {
71         return ("Or");
72     }
73
74     public String JavaDoc getIconSpec ()
75     {
76         return ("images/OrFilter.gif");
77     }
78
79     public NodeFilter getNodeFilter ()
80     {
81         NodeFilter[] predicates;
82         NodeFilter[] temp;
83         OrFilter ret;
84         
85         ret = new OrFilter ();
86
87         predicates = mFilter.getPredicates ();
88         temp = new NodeFilter[predicates.length];
89         for (int i = 0; i < predicates.length; i++)
90             temp[i] = ((Filter)predicates[i]).getNodeFilter ();
91         ret.setPredicates (temp);
92             
93         return (ret);
94     }
95
96     public void setNodeFilter (NodeFilter filter, Parser context)
97     {
98         mFilter = (OrFilter)filter;
99     }
100
101     public NodeFilter[] getSubNodeFilters ()
102     {
103         return (mFilter.getPredicates ());
104     }
105
106     public void setSubNodeFilters (NodeFilter[] filters)
107     {
108         mFilter.setPredicates (filters);
109     }
110
111     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
112     {
113         String JavaDoc array;
114         NodeFilter[] predicates;
115         String JavaDoc[] names;
116         String JavaDoc ret;
117         
118         predicates = mFilter.getPredicates ();
119         array = null; // stoopid Java compiler
120
if (0 != predicates.length)
121         {
122             names = new String JavaDoc[predicates.length];
123             for (int i = 0; i < predicates.length; i++)
124             {
125                 names[i] = ((Filter)predicates[i]).toJavaCode (out, context);
126             }
127             array = "array" + context[2]++;
128             spaces (out, context[0]);
129             out.append ("NodeFilter[] ");
130             out.append (array);
131             out.append (" = new NodeFilter[");
132             out.append (predicates.length);
133             out.append ("];");
134             newline (out);
135             for (int i = 0; i < predicates.length; i++)
136             {
137                 spaces (out, context[0]);
138                 out.append (array);
139                 out.append ("[");
140                 out.append (i);
141                 out.append ("] = ");
142                 out.append (names[i]);
143                 out.append (";");
144                 newline (out);
145             }
146         }
147         ret = "filter" + context[1]++;
148         spaces (out, context[0]);
149         out.append ("OrFilter ");
150         out.append (ret);
151         out.append (" = new OrFilter ();");
152         newline (out);
153         if (0 != predicates.length)
154         {
155             spaces (out, context[0]);
156             out.append (ret);
157             out.append (".setPredicates (");
158             out.append (array);
159             out.append (");");
160             newline (out);
161         }
162         
163         return (ret);
164     }
165
166     //
167
// NodeFilter interface
168
//
169

170     public boolean accept (Node node)
171     {
172         return (mFilter.accept (node));
173     }
174 }
175
Popular Tags