forked from shauvik/htmlparser
/
OrFilterWrapper.java
225 lines (203 loc) · 6.71 KB
/
OrFilterWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// HTMLParser Library - A java-based parser for HTML
// http://htmlparser.org
// Copyright (C) 2006 Derrick Oswald
//
// Revision Control Information
//
// $URL$
// $Author$
// $Date$
// $Revision$
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Common Public License; either
// version 1.0 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Common Public License for more details.
//
// You should have received a copy of the Common Public License
// along with this library; if not, the license is available from
// the Open Source Initiative (OSI) website:
// http://opensource.org/licenses/cpl1.0.php
package org.htmlparser.parserapplications.filterbuilder.wrappers;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.parserapplications.filterbuilder.Filter;
import org.htmlparser.parserapplications.filterbuilder.SubFilterList;
/**
* Wrapper for OrFilters.
*/
public class OrFilterWrapper
extends
Filter
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The drop target container.
*/
protected SubFilterList mContainer;
/**
* The underlying filter.
*/
protected OrFilter mFilter;
/**
* Create a wrapper over a new OrFilter.
*/
public OrFilterWrapper ()
{
mFilter = new OrFilter ();
// add the subfilter container
mContainer = new SubFilterList (this, "Predicates", 0);
add (mContainer);
}
//
// Filter overrides and concrete implementations
//
/**
* Get the name of the filter.
* @return A descriptive name for the filter.
*/
public String getDescription ()
{
return ("Or");
}
/**
* Get the resource name for the icon.
* @return The icon resource specification.
*/
public String getIconSpec ()
{
return ("images/OrFilter.gif");
}
/**
* Get the underlying node filter object.
* @return The node filter object suitable for serialization.
*/
public NodeFilter getNodeFilter ()
{
NodeFilter[] predicates;
NodeFilter[] temp;
OrFilter ret;
ret = new OrFilter ();
predicates = mFilter.getPredicates ();
temp = new NodeFilter[predicates.length];
for (int i = 0; i < predicates.length; i++)
temp[i] = ((Filter)predicates[i]).getNodeFilter ();
ret.setPredicates (temp);
return (ret);
}
/**
* Assign the underlying node filter for this wrapper.
* @param filter The filter to wrap.
* @param context The parser to use for conditioning this filter.
* Some filters need contextual information to provide to the user,
* i.e. for tag names or attribute names or values,
* so the Parser context is provided.
*/
public void setNodeFilter (NodeFilter filter, Parser context)
{
mFilter = (OrFilter)filter;
}
/**
* Get the underlying node filter's subordinate filters.
* @return The node filter object's contained filters.
*/
public NodeFilter[] getSubNodeFilters ()
{
return (mFilter.getPredicates ());
}
/**
* Assign the underlying node filter's subordinate filters.
* @param filters The filters to insert into the underlying node filter.
*/
public void setSubNodeFilters (NodeFilter[] filters)
{
mFilter.setPredicates (filters);
}
/**
* Convert this filter into Java code.
* Output whatever text necessary and return the variable name.
* @param out The output buffer.
* @param context Three integers as follows:
* <li>indent level - the number of spaces to insert at the beginning of each line</li>
* <li>filter number - the next available filter number</li>
* <li>filter array number - the next available array of filters number</li>
* @return The variable name to use when referencing this filter (usually "filter" + context[1]++)
*/
public String toJavaCode (StringBuffer out, int[] context)
{
String array;
NodeFilter[] predicates;
String[] names;
String ret;
predicates = mFilter.getPredicates ();
array = null; // stoopid Java compiler
if (0 != predicates.length)
{
names = new String[predicates.length];
for (int i = 0; i < predicates.length; i++)
{
names[i] = ((Filter)predicates[i]).toJavaCode (out, context);
}
array = "array" + context[2]++;
spaces (out, context[0]);
out.append ("NodeFilter[] ");
out.append (array);
out.append (" = new NodeFilter[");
out.append (predicates.length);
out.append ("];");
newline (out);
for (int i = 0; i < predicates.length; i++)
{
spaces (out, context[0]);
out.append (array);
out.append ("[");
out.append (i);
out.append ("] = ");
out.append (names[i]);
out.append (";");
newline (out);
}
}
ret = "filter" + context[1]++;
spaces (out, context[0]);
out.append ("OrFilter ");
out.append (ret);
out.append (" = new OrFilter ();");
newline (out);
if (0 != predicates.length)
{
spaces (out, context[0]);
out.append (ret);
out.append (".setPredicates (");
out.append (array);
out.append (");");
newline (out);
}
return (ret);
}
//
// NodeFilter interface
//
/**
* Predicate to determine whether or not to keep the given node.
* The behaviour based on this outcome is determined by the context
* in which it is called. It may lead to the node being added to a list
* or printed out. See the calling routine for details.
* @return <code>true</code> if the node is to be kept, <code>false</code>
* if it is to be discarded.
* @param node The node to test.
*/
public boolean accept (Node node)
{
return (mFilter.accept (node));
}
}