/
ContentFilter.java
364 lines (317 loc) · 9.79 KB
/
ContentFilter.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*--
Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom2.filter;
import org.jdom2.*;
/**
* A general purpose Filter able to represent all legal JDOM objects or a
* specific subset. Filtering is accomplished by way of a filtering mask in
* which each bit represents whether a JDOM object is visible or not.
* For example to view all Text and CDATA nodes in the content of element x.
* <pre><code>
* Filter filter = new ContentFilter(ContentFilter.TEXT |
* ContentFilter.CDATA);
* List content = x.getContent(filter);
* </code></pre>
* <p>
* For those who don't like bit-masking, set methods are provided as an
* alternative. For example to allow everything except Comment nodes.
* <pre><code>
* Filter filter = new ContentFilter();
* filter.setCommentVisible(false);
* List content = x.getContent(filter);
* </code></pre>
* <p>
* The default is to allow all valid JDOM objects.
*
* @author Bradley S. Huffman
*/
public class ContentFilter extends AbstractFilter<Content> {
/**
* JDOM2 Serialization: Default mechanism
*/
private static final long serialVersionUID = 200L;
/** Mask for JDOM {@link Element} objects */
public static final int ELEMENT = 1;
/** Mask for JDOM {@link CDATA} objects */
public static final int CDATA = 2;
/** Mask for JDOM {@link Text} objects */
public static final int TEXT = 4;
/** Mask for JDOM {@link Comment} objects */
public static final int COMMENT = 8;
/** Mask for JDOM {@link ProcessingInstruction} objects */
public static final int PI = 16;
/** Mask for JDOM {@link EntityRef} objects */
public static final int ENTITYREF = 32;
/** Mask for JDOM {@link Document} object */
public static final int DOCUMENT = 64;
/** Mask for JDOM {@link DocType} object */
public static final int DOCTYPE = 128;
/** The JDOM object mask */
private int filterMask;
/**
* Default constructor that allows any legal JDOM objects.
*/
public ContentFilter() {
setDefaultMask();
}
/**
* Set whether all JDOM objects are visible or not.
*
* @param allVisible <code>true</code> all JDOM objects are visible,
* <code>false</code> all JDOM objects are hidden.
*/
public ContentFilter(boolean allVisible) {
if (allVisible) {
setDefaultMask();
}
else {
filterMask &= ~filterMask;
}
}
/**
* Filter out JDOM objects according to a filtering mask.
*
* @param mask Mask of JDOM objects to allow.
*/
public ContentFilter(int mask) {
setFilterMask(mask);
}
/**
* Return current filtering mask.
*
* @return the current filtering mask
*/
public int getFilterMask() {
return filterMask;
}
/**
* Set filtering mask.
*
* @param mask the new filtering mask
*/
public void setFilterMask(int mask) {
setDefaultMask();
filterMask &= mask;
}
/**
* Set this filter to allow all legal JDOM objects.
*/
public void setDefaultMask() {
filterMask = ELEMENT | CDATA | TEXT | COMMENT |
PI | ENTITYREF | DOCUMENT | DOCTYPE;
}
/**
* Set filter to match only JDOM objects that are legal
* document content.
*/
public void setDocumentContent() {
filterMask = ELEMENT | COMMENT | PI | DOCTYPE;
}
/**
* Set filter to match only JDOM objects that are legal
* element content.
*/
public void setElementContent() {
filterMask = ELEMENT | CDATA | TEXT |
COMMENT | PI | ENTITYREF;
}
/**
* Set visiblity of <code>Element</code> objects.
*
* @param visible whether Elements are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setElementVisible(boolean visible) {
if (visible) {
filterMask |= ELEMENT;
}
else {
filterMask &= ~ELEMENT;
}
}
/**
* Set visiblity of <code>CDATA</code> objects.
*
* @param visible whether CDATA nodes are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setCDATAVisible(boolean visible) {
if (visible) {
filterMask |= CDATA;
}
else {
filterMask &= ~CDATA;
}
}
/**
* Set visiblity of <code>Text</code> objects.
*
* @param visible whether Text nodes are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setTextVisible(boolean visible) {
if (visible) {
filterMask |= TEXT;
}
else {
filterMask &= ~TEXT;
}
}
/**
* Set visiblity of <code>Comment</code> objects.
*
* @param visible whether Comments are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setCommentVisible(boolean visible) {
if (visible) {
filterMask |= COMMENT;
}
else {
filterMask &= ~COMMENT;
}
}
/**
* Set visiblity of <code>ProcessingInstruction</code> objects.
*
* @param visible whether ProcessingInstructions are visible,
* <code>true</code> if yes, <code>false</code> if not
*/
public void setPIVisible(boolean visible) {
if (visible) {
filterMask |= PI;
}
else {
filterMask &= ~PI;
}
}
/**
* Set visiblity of <code>EntityRef</code> objects.
*
* @param visible whether EntityRefs are visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setEntityRefVisible(boolean visible) {
if (visible) {
filterMask |= ENTITYREF;
}
else {
filterMask &= ~ENTITYREF;
}
}
/**
* Set visiblity of <code>DocType</code> objects.
*
* @param visible whether the DocType is visible, <code>true</code>
* if yes, <code>false</code> if not
*/
public void setDocTypeVisible(boolean visible) {
if (visible) {
filterMask |= DOCTYPE;
}
else {
filterMask &= ~DOCTYPE;
}
}
/**
* Check to see if the object matches according to the filter mask.
*
* @param obj The object to verify.
* @return <code>true</code> if the objected matched a predfined
* set of rules.
*/
@Override
public Content filter(Object obj) {
if (obj == null || !Content.class.isInstance(obj)) {
return null;
}
Content content = (Content)obj;
if (content instanceof Element) {
return (filterMask & ELEMENT) != 0 ? content : null;
}
else if (content instanceof CDATA) { // must come before Text check
return (filterMask & CDATA) != 0 ? content : null;
}
else if (content instanceof Text) {
return (filterMask & TEXT) != 0 ? content : null;
}
else if (content instanceof Comment) {
return (filterMask & COMMENT) != 0 ? content : null;
}
else if (content instanceof ProcessingInstruction) {
return (filterMask & PI) != 0 ? content : null;
}
else if (content instanceof EntityRef) {
return (filterMask & ENTITYREF) != 0 ? content : null;
}
// else if (obj instanceof Document) {
// return (filterMask & DOCUMENT) != 0 ? obj : null;
// }
else if (content instanceof DocType) {
return (filterMask & DOCTYPE) != 0 ? content : null;
}
return null;
}
/**
* Returns whether the two filters are equivalent (i.e. the
* matching mask values are identical).
*
* @param obj the object to compare against
* @return whether the two filters are equal
*/
@Override
public boolean equals(Object obj) {
// Generated by IntelliJ
if (this == obj) return true;
if (!(obj instanceof ContentFilter)) return false;
final ContentFilter filter = (ContentFilter) obj;
if (filterMask != filter.filterMask) return false;
return true;
}
@Override
public int hashCode() {
// Generated by IntelliJ
return filterMask;
}
}