KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > filter > ContentFilter


1 /*--
2
3  $Id: ContentFilter.java,v 1.14 2004/08/31 04:56:07 jhunter Exp $
4
5  Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57 package org.jdom.filter;
58
59 import org.jdom.*;
60
61 /**
62  * A general purpose Filter able to represent all legal JDOM objects or a
63  * specific subset. Filtering is accomplished by way of a filtering mask in
64  * which each bit represents whether a JDOM object is visible or not.
65  * For example to view all Text and CDATA nodes in the content of element x.
66  * <pre><code>
67  * Filter filter = new ContentFilter(ContentFilter.TEXT |
68  * ContentFilter.CDATA);
69  * List content = x.getContent(filter);
70  * </code></pre>
71  * <p>
72  * For those who don't like bit-masking, set methods are provided as an
73  * alternative. For example to allow everything except Comment nodes.
74  * <pre><code>
75  * Filter filter = new ContentFilter();
76  * filter.setCommentVisible(false);
77  * List content = x.getContent(filter);
78  * </code></pre>
79  * <p>
80  * The default is to allow all valid JDOM objects.
81  *
82  * @version $Revision: 1.14 $, $Date: 2004/08/31 04:56:07 $
83  * @author Bradley S. Huffman
84  */

85 public class ContentFilter extends AbstractFilter {
86
87     private static final String JavaDoc CVS_ID =
88       "@(#) $RCSfile: ContentFilter.java,v $ $Revision: 1.14 $ $Date: 2004/08/31 04:56:07 $ $Name: $";
89
90     /** Mask for JDOM {@link Element} objects */
91     public static final int ELEMENT = 1;
92
93     /** Mask for JDOM {@link CDATA} objects */
94     public static final int CDATA = 2;
95
96     /** Mask for JDOM {@link Text} objects */
97     public static final int TEXT = 4;
98
99     /** Mask for JDOM {@link Comment} objects */
100     public static final int COMMENT = 8;
101
102     /** Mask for JDOM {@link ProcessingInstruction} objects */
103     public static final int PI = 16;
104
105     /** Mask for JDOM {@link EntityRef} objects */
106     public static final int ENTITYREF = 32;
107
108     /** Mask for JDOM {@link Document} object */
109     public static final int DOCUMENT = 64;
110
111     /** Mask for JDOM {@link DocType} object */
112     public static final int DOCTYPE = 128;
113
114     /** The JDOM object mask */
115     private int filterMask;
116
117     /**
118      * Default constructor that allows any legal JDOM objects.
119      */

120     public ContentFilter() {
121         setDefaultMask();
122     }
123
124     /**
125      * Set whether all JDOM objects are visible or not.
126      *
127      * @param allVisible <code>true</code> all JDOM objects are visible,
128      * <code>false</code> all JDOM objects are hidden.
129      */

130     public ContentFilter(boolean allVisible) {
131         if (allVisible) {
132             setDefaultMask();
133         }
134         else {
135             filterMask &= ~filterMask;
136         }
137     }
138
139     /**
140      * Filter out JDOM objects according to a filtering mask.
141      *
142      * @param mask Mask of JDOM objects to allow.
143      */

144     public ContentFilter(int mask) {
145         setFilterMask(mask);
146     }
147
148     /**
149      * Return current filtering mask.
150      *
151      * @return the current filtering mask
152      */

153     public int getFilterMask() {
154         return filterMask;
155     }
156
157     /**
158      * Set filtering mask.
159      *
160      * @param mask the new filtering mask
161      */

162     public void setFilterMask(int mask) {
163         setDefaultMask();
164         filterMask &= mask;
165     }
166
167     /**
168      * Set this filter to allow all legal JDOM objects.
169      */

170     public void setDefaultMask() {
171         filterMask = ELEMENT | CDATA | TEXT | COMMENT |
172                      PI | ENTITYREF | DOCUMENT | DOCTYPE;
173     }
174
175     /**
176      * Set filter to match only JDOM objects that are legal
177      * document content.
178      */

179     public void setDocumentContent() {
180         filterMask = ELEMENT | COMMENT | PI | DOCTYPE;
181     }
182
183     /**
184      * Set filter to match only JDOM objects that are legal
185      * element content.
186      */

187     public void setElementContent() {
188         filterMask = ELEMENT | CDATA | TEXT |
189                      COMMENT | PI | ENTITYREF;
190     }
191
192     /**
193      * Set visiblity of <code>Element</code> objects.
194      *
195      * @param visible whether Elements are visible, <code>true</code>
196      * if yes, <code>false</code> if not
197      */

198     public void setElementVisible(boolean visible) {
199         if (visible) {
200             filterMask |= ELEMENT;
201         }
202         else {
203             filterMask &= ~ELEMENT;
204         }
205     }
206
207     /**
208      * Set visiblity of <code>CDATA</code> objects.
209      *
210      * @param visible whether CDATA nodes are visible, <code>true</code>
211      * if yes, <code>false</code> if not
212      */

213     public void setCDATAVisible(boolean visible) {
214         if (visible) {
215             filterMask |= CDATA;
216         }
217         else {
218             filterMask &= ~CDATA;
219         }
220     }
221
222     /**
223      * Set visiblity of <code>Text</code> objects.
224      *
225      * @param visible whether Text nodes are visible, <code>true</code>
226      * if yes, <code>false</code> if not
227      */

228     public void setTextVisible(boolean visible) {
229         if (visible) {
230             filterMask |= TEXT;
231         }
232         else {
233             filterMask &= ~TEXT;
234         }
235     }
236
237     /**
238      * Set visiblity of <code>Comment</code> objects.
239      *
240      * @param visible whether Comments are visible, <code>true</code>
241      * if yes, <code>false</code> if not
242      */

243     public void setCommentVisible(boolean visible) {
244         if (visible) {
245             filterMask |= COMMENT;
246         }
247         else {
248             filterMask &= ~COMMENT;
249         }
250     }
251
252     /**
253      * Set visiblity of <code>ProcessingInstruction</code> objects.
254      *
255      * @param visible whether ProcessingInstructions are visible,
256      * <code>true</code> if yes, <code>false</code> if not
257      */

258     public void setPIVisible(boolean visible) {
259         if (visible) {
260             filterMask |= PI;
261         }
262         else {
263             filterMask &= ~PI;
264         }
265     }
266
267     /**
268      * Set visiblity of <code>EntityRef</code> objects.
269      *
270      * @param visible whether EntityRefs are visible, <code>true</code>
271      * if yes, <code>false</code> if not
272      */

273     public void setEntityRefVisible(boolean visible) {
274         if (visible) {
275             filterMask |= ENTITYREF;
276         }
277         else {
278             filterMask &= ~ENTITYREF;
279         }
280     }
281
282     /**
283      * Set visiblity of <code>DocType</code> objects.
284      *
285      * @param visible whether the DocType is visible, <code>true</code>
286      * if yes, <code>false</code> if not
287      */

288     public void setDocTypeVisible(boolean visible) {
289         if (visible) {
290             filterMask |= DOCTYPE;
291         }
292         else {
293             filterMask &= ~DOCTYPE;
294         }
295     }
296
297     /**
298      * Check to see if the object matches according to the filter mask.
299      *
300      * @param obj The object to verify.
301      * @return <code>true</code> if the objected matched a predfined
302      * set of rules.
303      */

304     public boolean matches(Object JavaDoc obj) {
305         if (obj instanceof Element) {
306             return (filterMask & ELEMENT) != 0;
307         }
308         else if (obj instanceof CDATA) { // must come before Text check
309
return (filterMask & CDATA) != 0;
310         }
311         else if (obj instanceof Text) {
312             return (filterMask & TEXT) != 0;
313         }
314         else if (obj instanceof Comment) {
315             return (filterMask & COMMENT) != 0;
316         }
317         else if (obj instanceof ProcessingInstruction) {
318             return (filterMask & PI) != 0;
319         }
320         else if (obj instanceof EntityRef) {
321             return (filterMask & ENTITYREF) != 0;
322         }
323         else if (obj instanceof Document) {
324             return (filterMask & DOCUMENT) != 0;
325         }
326         else if (obj instanceof DocType) {
327             return (filterMask & DOCTYPE) != 0;
328         }
329
330         return false;
331     }
332
333     /**
334      * Returns whether the two filters are equivalent (i&#46;e&#46; the
335      * matching mask values are identical).
336      *
337      * @param obj the object to compare against
338      * @return whether the two filters are equal
339      */

340     public boolean equals(Object JavaDoc obj) {
341         // Generated by IntelliJ
342
if (this == obj) return true;
343         if (!(obj instanceof ContentFilter)) return false;
344
345         final ContentFilter filter = (ContentFilter) obj;
346
347         if (filterMask != filter.filterMask) return false;
348
349         return true;
350     }
351
352     public int hashCode() {
353         // Generated by IntelliJ
354
return filterMask;
355     }
356 }
357
Popular Tags