1 56 57 package org.jdom.filter; 58 59 import org.jdom.*; 60 61 85 public class ContentFilter extends AbstractFilter { 86 87 private static final String CVS_ID = 88 "@(#) $RCSfile: ContentFilter.java,v $ $Revision: 1.14 $ $Date: 2004/08/31 04:56:07 $ $Name: $"; 89 90 91 public static final int ELEMENT = 1; 92 93 94 public static final int CDATA = 2; 95 96 97 public static final int TEXT = 4; 98 99 100 public static final int COMMENT = 8; 101 102 103 public static final int PI = 16; 104 105 106 public static final int ENTITYREF = 32; 107 108 109 public static final int DOCUMENT = 64; 110 111 112 public static final int DOCTYPE = 128; 113 114 115 private int filterMask; 116 117 120 public ContentFilter() { 121 setDefaultMask(); 122 } 123 124 130 public ContentFilter(boolean allVisible) { 131 if (allVisible) { 132 setDefaultMask(); 133 } 134 else { 135 filterMask &= ~filterMask; 136 } 137 } 138 139 144 public ContentFilter(int mask) { 145 setFilterMask(mask); 146 } 147 148 153 public int getFilterMask() { 154 return filterMask; 155 } 156 157 162 public void setFilterMask(int mask) { 163 setDefaultMask(); 164 filterMask &= mask; 165 } 166 167 170 public void setDefaultMask() { 171 filterMask = ELEMENT | CDATA | TEXT | COMMENT | 172 PI | ENTITYREF | DOCUMENT | DOCTYPE; 173 } 174 175 179 public void setDocumentContent() { 180 filterMask = ELEMENT | COMMENT | PI | DOCTYPE; 181 } 182 183 187 public void setElementContent() { 188 filterMask = ELEMENT | CDATA | TEXT | 189 COMMENT | PI | ENTITYREF; 190 } 191 192 198 public void setElementVisible(boolean visible) { 199 if (visible) { 200 filterMask |= ELEMENT; 201 } 202 else { 203 filterMask &= ~ELEMENT; 204 } 205 } 206 207 213 public void setCDATAVisible(boolean visible) { 214 if (visible) { 215 filterMask |= CDATA; 216 } 217 else { 218 filterMask &= ~CDATA; 219 } 220 } 221 222 228 public void setTextVisible(boolean visible) { 229 if (visible) { 230 filterMask |= TEXT; 231 } 232 else { 233 filterMask &= ~TEXT; 234 } 235 } 236 237 243 public void setCommentVisible(boolean visible) { 244 if (visible) { 245 filterMask |= COMMENT; 246 } 247 else { 248 filterMask &= ~COMMENT; 249 } 250 } 251 252 258 public void setPIVisible(boolean visible) { 259 if (visible) { 260 filterMask |= PI; 261 } 262 else { 263 filterMask &= ~PI; 264 } 265 } 266 267 273 public void setEntityRefVisible(boolean visible) { 274 if (visible) { 275 filterMask |= ENTITYREF; 276 } 277 else { 278 filterMask &= ~ENTITYREF; 279 } 280 } 281 282 288 public void setDocTypeVisible(boolean visible) { 289 if (visible) { 290 filterMask |= DOCTYPE; 291 } 292 else { 293 filterMask &= ~DOCTYPE; 294 } 295 } 296 297 304 public boolean matches(Object obj) { 305 if (obj instanceof Element) { 306 return (filterMask & ELEMENT) != 0; 307 } 308 else if (obj instanceof CDATA) { 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 340 public boolean equals(Object obj) { 341 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 return filterMask; 355 } 356 } 357 | Popular Tags |