Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContentFilter.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*--
  2. $Id: ContentFilter.java,v 1.15 2007/11/10 05:29:00 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.filter;
  46. import org.jdom.*;
  47. /**
  48. * A general purpose Filter able to represent all legal JDOM objects or a
  49. * specific subset. Filtering is accomplished by way of a filtering mask in
  50. * which each bit represents whether a JDOM object is visible or not.
  51. * For example to view all Text and CDATA nodes in the content of element x.
  52. * <pre><code>
  53. * Filter filter = new ContentFilter(ContentFilter.TEXT |
  54. * ContentFilter.CDATA);
  55. * List content = x.getContent(filter);
  56. * </code></pre>
  57. * <p>
  58. * For those who don't like bit-masking, set methods are provided as an
  59. * alternative. For example to allow everything except Comment nodes.
  60. * <pre><code>
  61. * Filter filter = new ContentFilter();
  62. * filter.setCommentVisible(false);
  63. * List content = x.getContent(filter);
  64. * </code></pre>
  65. * <p>
  66. * The default is to allow all valid JDOM objects.
  67. *
  68. * @version $Revision: 1.15 $, $Date: 2007/11/10 05:29:00 $
  69. * @author Bradley S. Huffman
  70. */
  71. public class ContentFilter extends AbstractFilter {
  72. private static final String CVS_ID =
  73. "@(#) $RCSfile: ContentFilter.java,v $ $Revision: 1.15 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
  74. /** Mask for JDOM {@link Element} objects */
  75. public static final int ELEMENT = 1;
  76. /** Mask for JDOM {@link CDATA} objects */
  77. public static final int CDATA = 2;
  78. /** Mask for JDOM {@link Text} objects */
  79. public static final int TEXT = 4;
  80. /** Mask for JDOM {@link Comment} objects */
  81. public static final int COMMENT = 8;
  82. /** Mask for JDOM {@link ProcessingInstruction} objects */
  83. public static final int PI = 16;
  84. /** Mask for JDOM {@link EntityRef} objects */
  85. public static final int ENTITYREF = 32;
  86. /** Mask for JDOM {@link Document} object */
  87. public static final int DOCUMENT = 64;
  88. /** Mask for JDOM {@link DocType} object */
  89. public static final int DOCTYPE = 128;
  90. /** The JDOM object mask */
  91. private int filterMask;
  92. /**
  93. * Default constructor that allows any legal JDOM objects.
  94. */
  95. public ContentFilter() {
  96. setDefaultMask();
  97. }
  98. /**
  99. * Set whether all JDOM objects are visible or not.
  100. *
  101. * @param allVisible <code>true</code> all JDOM objects are visible,
  102. * <code>false</code> all JDOM objects are hidden.
  103. */
  104. public ContentFilter(boolean allVisible) {
  105. if (allVisible) {
  106. setDefaultMask();
  107. }
  108. else {
  109. filterMask &= ~filterMask;
  110. }
  111. }
  112. /**
  113. * Filter out JDOM objects according to a filtering mask.
  114. *
  115. * @param mask Mask of JDOM objects to allow.
  116. */
  117. public ContentFilter(int mask) {
  118. setFilterMask(mask);
  119. }
  120. /**
  121. * Return current filtering mask.
  122. *
  123. * @return the current filtering mask
  124. */
  125. public int getFilterMask() {
  126. return filterMask;
  127. }
  128. /**
  129. * Set filtering mask.
  130. *
  131. * @param mask the new filtering mask
  132. */
  133. public void setFilterMask(int mask) {
  134. setDefaultMask();
  135. filterMask &= mask;
  136. }
  137. /**
  138. * Set this filter to allow all legal JDOM objects.
  139. */
  140. public void setDefaultMask() {
  141. filterMask = ELEMENT | CDATA | TEXT | COMMENT |
  142. PI | ENTITYREF | DOCUMENT | DOCTYPE;
  143. }
  144. /**
  145. * Set filter to match only JDOM objects that are legal
  146. * document content.
  147. */
  148. public void setDocumentContent() {
  149. filterMask = ELEMENT | COMMENT | PI | DOCTYPE;
  150. }
  151. /**
  152. * Set filter to match only JDOM objects that are legal
  153. * element content.
  154. */
  155. public void setElementContent() {
  156. filterMask = ELEMENT | CDATA | TEXT |
  157. COMMENT | PI | ENTITYREF;
  158. }
  159. /**
  160. * Set visiblity of <code>Element</code> objects.
  161. *
  162. * @param visible whether Elements are visible, <code>true</code>
  163. * if yes, <code>false</code> if not
  164. */
  165. public void setElementVisible(boolean visible) {
  166. if (visible) {
  167. filterMask |= ELEMENT;
  168. }
  169. else {
  170. filterMask &= ~ELEMENT;
  171. }
  172. }
  173. /**
  174. * Set visiblity of <code>CDATA</code> objects.
  175. *
  176. * @param visible whether CDATA nodes are visible, <code>true</code>
  177. * if yes, <code>false</code> if not
  178. */
  179. public void setCDATAVisible(boolean visible) {
  180. if (visible) {
  181. filterMask |= CDATA;
  182. }
  183. else {
  184. filterMask &= ~CDATA;
  185. }
  186. }
  187. /**
  188. * Set visiblity of <code>Text</code> objects.
  189. *
  190. * @param visible whether Text nodes are visible, <code>true</code>
  191. * if yes, <code>false</code> if not
  192. */
  193. public void setTextVisible(boolean visible) {
  194. if (visible) {
  195. filterMask |= TEXT;
  196. }
  197. else {
  198. filterMask &= ~TEXT;
  199. }
  200. }
  201. /**
  202. * Set visiblity of <code>Comment</code> objects.
  203. *
  204. * @param visible whether Comments are visible, <code>true</code>
  205. * if yes, <code>false</code> if not
  206. */
  207. public void setCommentVisible(boolean visible) {
  208. if (visible) {
  209. filterMask |= COMMENT;
  210. }
  211. else {
  212. filterMask &= ~COMMENT;
  213. }
  214. }
  215. /**
  216. * Set visiblity of <code>ProcessingInstruction</code> objects.
  217. *
  218. * @param visible whether ProcessingInstructions are visible,
  219. * <code>true</code> if yes, <code>false</code> if not
  220. */
  221. public void setPIVisible(boolean visible) {
  222. if (visible) {
  223. filterMask |= PI;
  224. }
  225. else {
  226. filterMask &= ~PI;
  227. }
  228. }
  229. /**
  230. * Set visiblity of <code>EntityRef</code> objects.
  231. *
  232. * @param visible whether EntityRefs are visible, <code>true</code>
  233. * if yes, <code>false</code> if not
  234. */
  235. public void setEntityRefVisible(boolean visible) {
  236. if (visible) {
  237. filterMask |= ENTITYREF;
  238. }
  239. else {
  240. filterMask &= ~ENTITYREF;
  241. }
  242. }
  243. /**
  244. * Set visiblity of <code>DocType</code> objects.
  245. *
  246. * @param visible whether the DocType is visible, <code>true</code>
  247. * if yes, <code>false</code> if not
  248. */
  249. public void setDocTypeVisible(boolean visible) {
  250. if (visible) {
  251. filterMask |= DOCTYPE;
  252. }
  253. else {
  254. filterMask &= ~DOCTYPE;
  255. }
  256. }
  257. /**
  258. * Check to see if the object matches according to the filter mask.
  259. *
  260. * @param obj The object to verify.
  261. * @return <code>true</code> if the objected matched a predfined
  262. * set of rules.
  263. */
  264. public boolean matches(Object obj) {
  265. if (obj instanceof Element) {
  266. return (filterMask & ELEMENT) != 0;
  267. }
  268. else if (obj instanceof CDATA) { // must come before Text check
  269. return (filterMask & CDATA) != 0;
  270. }
  271. else if (obj instanceof Text) {
  272. return (filterMask & TEXT) != 0;
  273. }
  274. else if (obj instanceof Comment) {
  275. return (filterMask & COMMENT) != 0;
  276. }
  277. else if (obj instanceof ProcessingInstruction) {
  278. return (filterMask & PI) != 0;
  279. }
  280. else if (obj instanceof EntityRef) {
  281. return (filterMask & ENTITYREF) != 0;
  282. }
  283. else if (obj instanceof Document) {
  284. return (filterMask & DOCUMENT) != 0;
  285. }
  286. else if (obj instanceof DocType) {
  287. return (filterMask & DOCTYPE) != 0;
  288. }
  289. return false;
  290. }
  291. /**
  292. * Returns whether the two filters are equivalent (i&#46;e&#46; the
  293. * matching mask values are identical).
  294. *
  295. * @param obj the object to compare against
  296. * @return whether the two filters are equal
  297. */
  298. public boolean equals(Object obj) {
  299. // Generated by IntelliJ
  300. if (this == obj) return true;
  301. if (!(obj instanceof ContentFilter)) return false;
  302. final ContentFilter filter = (ContentFilter) obj;
  303. if (filterMask != filter.filterMask) return false;
  304. return true;
  305. }
  306. public int hashCode() {
  307. // Generated by IntelliJ
  308. return filterMask;
  309. }
  310. }