001: /*--
002:
003: $Id: ContentFilter.java,v 1.14 2004/08/31 04:56:07 jhunter Exp $
004:
005: Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions
010: are met:
011:
012: 1. Redistributions of source code must retain the above copyright
013: notice, this list of conditions, and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright
016: notice, this list of conditions, and the disclaimer that follows
017: these conditions in the documentation and/or other materials
018: provided with the distribution.
019:
020: 3. The name "JDOM" must not be used to endorse or promote products
021: derived from this software without prior written permission. For
022: written permission, please contact <request_AT_jdom_DOT_org>.
023:
024: 4. Products derived from this software may not be called "JDOM", nor
025: may "JDOM" appear in their name, without prior written permission
026: from the JDOM Project Management <request_AT_jdom_DOT_org>.
027:
028: In addition, we request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by the
032: JDOM Project (http://www.jdom.org/)."
033: Alternatively, the acknowledgment may be graphical using the logos
034: available at http://www.jdom.org/images/logos.
035:
036: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
040: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: SUCH DAMAGE.
048:
049: This software consists of voluntary contributions made by many
050: individuals on behalf of the JDOM Project and was originally
051: created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
052: Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
053: on the JDOM Project, please see <http://www.jdom.org/>.
054:
055: */
056:
057: package org.jdom.filter;
058:
059: import org.jdom.*;
060:
061: /**
062: * A general purpose Filter able to represent all legal JDOM objects or a
063: * specific subset. Filtering is accomplished by way of a filtering mask in
064: * which each bit represents whether a JDOM object is visible or not.
065: * For example to view all Text and CDATA nodes in the content of element x.
066: * <pre><code>
067: * Filter filter = new ContentFilter(ContentFilter.TEXT |
068: * ContentFilter.CDATA);
069: * List content = x.getContent(filter);
070: * </code></pre>
071: * <p>
072: * For those who don't like bit-masking, set methods are provided as an
073: * alternative. For example to allow everything except Comment nodes.
074: * <pre><code>
075: * Filter filter = new ContentFilter();
076: * filter.setCommentVisible(false);
077: * List content = x.getContent(filter);
078: * </code></pre>
079: * <p>
080: * The default is to allow all valid JDOM objects.
081: *
082: * @version $Revision: 1.14 $, $Date: 2004/08/31 04:56:07 $
083: * @author Bradley S. Huffman
084: */
085: public class ContentFilter extends AbstractFilter {
086:
087: private static final String CVS_ID = "@(#) $RCSfile: ContentFilter.java,v $ $Revision: 1.14 $ $Date: 2004/08/31 04:56:07 $ $Name: jdom_1_0 $";
088:
089: /** Mask for JDOM {@link Element} objects */
090: public static final int ELEMENT = 1;
091:
092: /** Mask for JDOM {@link CDATA} objects */
093: public static final int CDATA = 2;
094:
095: /** Mask for JDOM {@link Text} objects */
096: public static final int TEXT = 4;
097:
098: /** Mask for JDOM {@link Comment} objects */
099: public static final int COMMENT = 8;
100:
101: /** Mask for JDOM {@link ProcessingInstruction} objects */
102: public static final int PI = 16;
103:
104: /** Mask for JDOM {@link EntityRef} objects */
105: public static final int ENTITYREF = 32;
106:
107: /** Mask for JDOM {@link Document} object */
108: public static final int DOCUMENT = 64;
109:
110: /** Mask for JDOM {@link DocType} object */
111: public static final int DOCTYPE = 128;
112:
113: /** The JDOM object mask */
114: private int filterMask;
115:
116: /**
117: * Default constructor that allows any legal JDOM objects.
118: */
119: public ContentFilter() {
120: setDefaultMask();
121: }
122:
123: /**
124: * Set whether all JDOM objects are visible or not.
125: *
126: * @param allVisible <code>true</code> all JDOM objects are visible,
127: * <code>false</code> all JDOM objects are hidden.
128: */
129: public ContentFilter(boolean allVisible) {
130: if (allVisible) {
131: setDefaultMask();
132: } else {
133: filterMask &= ~filterMask;
134: }
135: }
136:
137: /**
138: * Filter out JDOM objects according to a filtering mask.
139: *
140: * @param mask Mask of JDOM objects to allow.
141: */
142: public ContentFilter(int mask) {
143: setFilterMask(mask);
144: }
145:
146: /**
147: * Return current filtering mask.
148: *
149: * @return the current filtering mask
150: */
151: public int getFilterMask() {
152: return filterMask;
153: }
154:
155: /**
156: * Set filtering mask.
157: *
158: * @param mask the new filtering mask
159: */
160: public void setFilterMask(int mask) {
161: setDefaultMask();
162: filterMask &= mask;
163: }
164:
165: /**
166: * Set this filter to allow all legal JDOM objects.
167: */
168: public void setDefaultMask() {
169: filterMask = ELEMENT | CDATA | TEXT | COMMENT | PI | ENTITYREF
170: | DOCUMENT | DOCTYPE;
171: }
172:
173: /**
174: * Set filter to match only JDOM objects that are legal
175: * document content.
176: */
177: public void setDocumentContent() {
178: filterMask = ELEMENT | COMMENT | PI | DOCTYPE;
179: }
180:
181: /**
182: * Set filter to match only JDOM objects that are legal
183: * element content.
184: */
185: public void setElementContent() {
186: filterMask = ELEMENT | CDATA | TEXT | COMMENT | PI | ENTITYREF;
187: }
188:
189: /**
190: * Set visiblity of <code>Element</code> objects.
191: *
192: * @param visible whether Elements are visible, <code>true</code>
193: * if yes, <code>false</code> if not
194: */
195: public void setElementVisible(boolean visible) {
196: if (visible) {
197: filterMask |= ELEMENT;
198: } else {
199: filterMask &= ~ELEMENT;
200: }
201: }
202:
203: /**
204: * Set visiblity of <code>CDATA</code> objects.
205: *
206: * @param visible whether CDATA nodes are visible, <code>true</code>
207: * if yes, <code>false</code> if not
208: */
209: public void setCDATAVisible(boolean visible) {
210: if (visible) {
211: filterMask |= CDATA;
212: } else {
213: filterMask &= ~CDATA;
214: }
215: }
216:
217: /**
218: * Set visiblity of <code>Text</code> objects.
219: *
220: * @param visible whether Text nodes are visible, <code>true</code>
221: * if yes, <code>false</code> if not
222: */
223: public void setTextVisible(boolean visible) {
224: if (visible) {
225: filterMask |= TEXT;
226: } else {
227: filterMask &= ~TEXT;
228: }
229: }
230:
231: /**
232: * Set visiblity of <code>Comment</code> objects.
233: *
234: * @param visible whether Comments are visible, <code>true</code>
235: * if yes, <code>false</code> if not
236: */
237: public void setCommentVisible(boolean visible) {
238: if (visible) {
239: filterMask |= COMMENT;
240: } else {
241: filterMask &= ~COMMENT;
242: }
243: }
244:
245: /**
246: * Set visiblity of <code>ProcessingInstruction</code> objects.
247: *
248: * @param visible whether ProcessingInstructions are visible,
249: * <code>true</code> if yes, <code>false</code> if not
250: */
251: public void setPIVisible(boolean visible) {
252: if (visible) {
253: filterMask |= PI;
254: } else {
255: filterMask &= ~PI;
256: }
257: }
258:
259: /**
260: * Set visiblity of <code>EntityRef</code> objects.
261: *
262: * @param visible whether EntityRefs are visible, <code>true</code>
263: * if yes, <code>false</code> if not
264: */
265: public void setEntityRefVisible(boolean visible) {
266: if (visible) {
267: filterMask |= ENTITYREF;
268: } else {
269: filterMask &= ~ENTITYREF;
270: }
271: }
272:
273: /**
274: * Set visiblity of <code>DocType</code> objects.
275: *
276: * @param visible whether the DocType is visible, <code>true</code>
277: * if yes, <code>false</code> if not
278: */
279: public void setDocTypeVisible(boolean visible) {
280: if (visible) {
281: filterMask |= DOCTYPE;
282: } else {
283: filterMask &= ~DOCTYPE;
284: }
285: }
286:
287: /**
288: * Check to see if the object matches according to the filter mask.
289: *
290: * @param obj The object to verify.
291: * @return <code>true</code> if the objected matched a predfined
292: * set of rules.
293: */
294: public boolean matches(Object obj) {
295: if (obj instanceof Element) {
296: return (filterMask & ELEMENT) != 0;
297: } else if (obj instanceof CDATA) { // must come before Text check
298: return (filterMask & CDATA) != 0;
299: } else if (obj instanceof Text) {
300: return (filterMask & TEXT) != 0;
301: } else if (obj instanceof Comment) {
302: return (filterMask & COMMENT) != 0;
303: } else if (obj instanceof ProcessingInstruction) {
304: return (filterMask & PI) != 0;
305: } else if (obj instanceof EntityRef) {
306: return (filterMask & ENTITYREF) != 0;
307: } else if (obj instanceof Document) {
308: return (filterMask & DOCUMENT) != 0;
309: } else if (obj instanceof DocType) {
310: return (filterMask & DOCTYPE) != 0;
311: }
312:
313: return false;
314: }
315:
316: /**
317: * Returns whether the two filters are equivalent (i.e. the
318: * matching mask values are identical).
319: *
320: * @param obj the object to compare against
321: * @return whether the two filters are equal
322: */
323: public boolean equals(Object obj) {
324: // Generated by IntelliJ
325: if (this == obj)
326: return true;
327: if (!(obj instanceof ContentFilter))
328: return false;
329:
330: final ContentFilter filter = (ContentFilter) obj;
331:
332: if (filterMask != filter.filterMask)
333: return false;
334:
335: return true;
336: }
337:
338: public int hashCode() {
339: // Generated by IntelliJ
340: return filterMask;
341: }
342: }
|