Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Filters |
|
| 1.0;1 |
1 | /*-- | |
2 | ||
3 | Copyright (C) 2011-2012 Jason Hunter & Brett McLaughlin. | |
4 | All rights reserved. | |
5 | ||
6 | Redistribution and use in source and binary forms, with or without | |
7 | modification, are permitted provided that the following conditions | |
8 | are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright | |
11 | notice, this list of conditions, and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright | |
14 | notice, this list of conditions, and the disclaimer that follows | |
15 | these conditions in the documentation and/or other materials | |
16 | provided with the distribution. | |
17 | ||
18 | 3. The name "JDOM" must not be used to endorse or promote products | |
19 | derived from this software without prior written permission. For | |
20 | written permission, please contact <request_AT_jdom_DOT_org>. | |
21 | ||
22 | 4. Products derived from this software may not be called "JDOM", nor | |
23 | may "JDOM" appear in their name, without prior written permission | |
24 | from the JDOM Project Management <request_AT_jdom_DOT_org>. | |
25 | ||
26 | In addition, we request (but do not require) that you include in the | |
27 | end-user documentation provided with the redistribution and/or in the | |
28 | software itself an acknowledgement equivalent to the following: | |
29 | "This product includes software developed by the | |
30 | JDOM Project (http://www.jdom.org/)." | |
31 | Alternatively, the acknowledgment may be graphical using the logos | |
32 | available at http://www.jdom.org/images/logos. | |
33 | ||
34 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |
35 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
36 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
37 | DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT | |
38 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
39 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
40 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
41 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
42 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
43 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
44 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
45 | SUCH DAMAGE. | |
46 | ||
47 | This software consists of voluntary contributions made by many | |
48 | individuals on behalf of the JDOM Project and was originally | |
49 | created by Jason Hunter <jhunter_AT_jdom_DOT_org> and | |
50 | Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information | |
51 | on the JDOM Project, please see <http://www.jdom.org/>. | |
52 | ||
53 | */ | |
54 | ||
55 | package org.jdom2.filter; | |
56 | ||
57 | import org.jdom2.Attribute; | |
58 | import org.jdom2.CDATA; | |
59 | import org.jdom2.Comment; | |
60 | import org.jdom2.Content; | |
61 | import org.jdom2.DocType; | |
62 | import org.jdom2.Document; | |
63 | import org.jdom2.Element; | |
64 | import org.jdom2.EntityRef; | |
65 | import org.jdom2.Namespace; | |
66 | import org.jdom2.ProcessingInstruction; | |
67 | import org.jdom2.Text; | |
68 | ||
69 | /** | |
70 | * Factory class of convenience methods to create Filter instances of common | |
71 | * types. Methods that return Filters that act on core JDOM classes (Element, | |
72 | * Text, etc.) are simply named after the content they return. | |
73 | * <p> | |
74 | * Filters that | |
75 | * match non-core classes (Boolean, Object, etc.) are all prefixed with the | |
76 | * letter 'f' (for <strong>f</strong>ilter). | |
77 | * <p> | |
78 | * The Filter returned by {@link #fpassthrough()} is not really a filter in the | |
79 | * sense that it will never filter anything out - everything matches. This can | |
80 | * be useful to accomplish some tasks, for example the JDOM XPath API uses it | |
81 | * extensively. | |
82 | * | |
83 | * @author Rolf Lear | |
84 | * | |
85 | */ | |
86 | public final class Filters { | |
87 | ||
88 | 1 | private static final Filter<Content> fcontent = |
89 | new ClassFilter<Content>(Content.class); | |
90 | ||
91 | 1 | private static final Filter<Attribute> fattribute = |
92 | new AttributeFilter(); | |
93 | ||
94 | 1 | private static final Filter<Comment> fcomment = |
95 | new ClassFilter<Comment>(Comment.class); | |
96 | ||
97 | 1 | private static final Filter<CDATA> fcdata = |
98 | new ClassFilter<CDATA>(CDATA.class); | |
99 | ||
100 | 1 | private static final Filter<DocType> fdoctype = |
101 | new ClassFilter<DocType>(DocType.class); | |
102 | ||
103 | 1 | private static final Filter<EntityRef> fentityref = |
104 | new ClassFilter<EntityRef>(EntityRef.class); | |
105 | ||
106 | 1 | private static final Filter<ProcessingInstruction> fpi = |
107 | new ClassFilter<ProcessingInstruction>(ProcessingInstruction.class); | |
108 | ||
109 | 1 | private static final Filter<Text> ftext = |
110 | new ClassFilter<Text>(Text.class); | |
111 | ||
112 | 1 | private static final Filter<Text> ftextonly = new TextOnlyFilter(); |
113 | ||
114 | 1 | private static final Filter<Element> felement = |
115 | new ClassFilter<Element>(Element.class); | |
116 | ||
117 | 1 | private static final Filter<Document> fdocument = |
118 | new ClassFilter<Document>(Document.class); | |
119 | ||
120 | 1 | private static final Filter<Double> fdouble = |
121 | new ClassFilter<Double>(Double.class); | |
122 | ||
123 | 1 | private static final Filter<Boolean> fboolean = |
124 | new ClassFilter<Boolean>(Boolean.class); | |
125 | ||
126 | 1 | private static final Filter<String> fstring = |
127 | new ClassFilter<String>(String.class); | |
128 | ||
129 | 1 | private static final Filter<Object> fpassthrough = |
130 | new PassThroughFilter(); | |
131 | ||
132 | ||
133 | 0 | private Filters() { |
134 | // do nothing... make instances impossible. | |
135 | 0 | } |
136 | ||
137 | /** | |
138 | * Return a Filter that matches any {@link Content} data. | |
139 | * | |
140 | * @return a Filter that matches any {@link Content} data. | |
141 | */ | |
142 | public static final Filter<Content> content() { | |
143 | 398 | return fcontent; |
144 | } | |
145 | ||
146 | /** | |
147 | * Return a Filter that matches any {@link Attribute} data. | |
148 | * | |
149 | * @return a Filter that matches any {@link Attribute} data. | |
150 | */ | |
151 | public static final Filter<Attribute> attribute() { | |
152 | 37 | return fattribute; |
153 | } | |
154 | ||
155 | /** | |
156 | * Return a Filter that matches any {@link Attribute} data with the | |
157 | * specified name. | |
158 | * | |
159 | * @param name The name for all the Attributes to have (these can be in any | |
160 | * Namespace). | |
161 | * @return a Filter that matches any {@link Attribute} data with the | |
162 | * specified name. | |
163 | */ | |
164 | public static final Filter<Attribute> attribute(String name) { | |
165 | 1 | return new AttributeFilter(name); |
166 | } | |
167 | ||
168 | /** | |
169 | * Return a Filter that matches any {@link Attribute} data with the | |
170 | * specified name and namespace. | |
171 | * | |
172 | * @param name The name for all the Attributes to have. | |
173 | * @param ns The Namespace for all the Attributes to have. | |
174 | * @return a Filter that matches any {@link Attribute} data with the | |
175 | * specified name and namespace. | |
176 | */ | |
177 | public static final Filter<Attribute> attribute(String name, Namespace ns) { | |
178 | 7 | return new AttributeFilter(name, ns); |
179 | } | |
180 | ||
181 | /** | |
182 | * Return a Filter that matches any {@link Attribute} data with the | |
183 | * specified namespace. | |
184 | * | |
185 | * @param ns The Namespace for all the Attributes to have. | |
186 | * @return a Filter that matches any {@link Attribute} data with the | |
187 | * specified namespace. | |
188 | */ | |
189 | public static final Filter<Attribute> attribute(Namespace ns) { | |
190 | 2 | return new AttributeFilter(ns); |
191 | } | |
192 | ||
193 | /** | |
194 | * Return a Filter that matches any {@link Comment} data. | |
195 | * | |
196 | * @return a Filter that matches any {@link Comment} data. | |
197 | */ | |
198 | public static final Filter<Comment> comment() { | |
199 | 326 | return fcomment; |
200 | } | |
201 | ||
202 | /** | |
203 | * Return a Filter that matches any {@link CDATA} data. | |
204 | * | |
205 | * @return a Filter that matches any {@link CDATA} data. | |
206 | */ | |
207 | public static final Filter<CDATA> cdata() { | |
208 | 3 | return fcdata; |
209 | } | |
210 | ||
211 | /** | |
212 | * Return a Filter that matches any {@link DocType} data. | |
213 | * | |
214 | * @return a Filter that matches any {@link DocType} data. | |
215 | */ | |
216 | public static final Filter<DocType> doctype() { | |
217 | 1 | return fdoctype; |
218 | } | |
219 | ||
220 | /** | |
221 | * Return a Filter that matches any {@link EntityRef} data. | |
222 | * | |
223 | * @return a Filter that matches any {@link EntityRef} data. | |
224 | */ | |
225 | public static final Filter<EntityRef> entityref() { | |
226 | 1 | return fentityref; |
227 | } | |
228 | ||
229 | /** | |
230 | * Return a Filter that matches any {@link Element} data. | |
231 | * | |
232 | * @return a Filter that matches any {@link Element} data. | |
233 | */ | |
234 | public static final Filter<Element> element() { | |
235 | 90 | return felement; |
236 | } | |
237 | ||
238 | /** | |
239 | * Return a Filter that matches any {@link Document} data. | |
240 | * | |
241 | * @return a Filter that matches any {@link Document} data. | |
242 | */ | |
243 | public static final Filter<Document> document() { | |
244 | 1 | return fdocument; |
245 | } | |
246 | ||
247 | /** | |
248 | * Return a Filter that matches any {@link Element} data with the specified | |
249 | * name. | |
250 | * | |
251 | * @param name The name of Elements to match. | |
252 | * @return a Filter that matches any {@link Element} data with the specified | |
253 | * name. | |
254 | */ | |
255 | public static final Filter<Element> element(String name) { | |
256 | 9 | return new ElementFilter(name, Namespace.NO_NAMESPACE); |
257 | } | |
258 | ||
259 | /** | |
260 | * Return a Filter that matches any {@link Element} data with the specified | |
261 | * name and Namespace. | |
262 | * | |
263 | * @param name The name of Elements to match. | |
264 | * @param ns The Namespace to match | |
265 | * @return a Filter that matches any {@link Element} data with the specified | |
266 | * name and Namespace. | |
267 | */ | |
268 | public static final Filter<Element> element(String name, Namespace ns) { | |
269 | 4 | return new ElementFilter(name, ns); |
270 | } | |
271 | ||
272 | /** | |
273 | * Return a Filter that matches any {@link Element} data with the specified | |
274 | * Namespace. | |
275 | * | |
276 | * @param ns The Namespace to match | |
277 | * @return a Filter that matches any {@link Element} data with the specified | |
278 | * Namespace. | |
279 | */ | |
280 | public static final Filter<Element> element(Namespace ns) { | |
281 | 3 | return new ElementFilter(null, ns); |
282 | } | |
283 | ||
284 | /** | |
285 | * Return a Filter that matches any {@link ProcessingInstruction} data. | |
286 | * | |
287 | * @return a Filter that matches any {@link ProcessingInstruction} data. | |
288 | */ | |
289 | public static final Filter<ProcessingInstruction> processinginstruction() { | |
290 | 217 | return fpi; |
291 | } | |
292 | ||
293 | /** | |
294 | * Return a Filter that matches any {@link Text} data (which includes | |
295 | * {@link CDATA} since that is a subclass of Text). | |
296 | * | |
297 | * @return a Filter that matches any {@link Text} data (which includes | |
298 | * {@link CDATA} since that is a subclass of Text). | |
299 | */ | |
300 | public static final Filter<Text> text() { | |
301 | 2578 | return ftext; |
302 | } | |
303 | ||
304 | /** | |
305 | * Return a Filter that matches any {@link Text} data (excludes | |
306 | * {@link CDATA} instances). | |
307 | * | |
308 | * @return a Filter that matches any {@link Text} data (which excludes | |
309 | * {@link CDATA} instances). | |
310 | */ | |
311 | public static final Filter<Text> textOnly() { | |
312 | 1 | return ftextonly; |
313 | } | |
314 | ||
315 | /** | |
316 | * Return a Filter that matches any Boolean data. | |
317 | * | |
318 | * @return a Filter that matches any Boolean data. | |
319 | */ | |
320 | public static final Filter<Boolean> fboolean() { | |
321 | 1 | return fboolean; |
322 | } | |
323 | ||
324 | /** | |
325 | * Return a Filter that matches any String data. | |
326 | * | |
327 | * @return a Filter that matches any String data. | |
328 | */ | |
329 | public static final Filter<String> fstring() { | |
330 | 43 | return fstring; |
331 | } | |
332 | ||
333 | /** | |
334 | * Return a Filter that matches any Double data. | |
335 | * | |
336 | * @return a Filter that matches any Double data. | |
337 | */ | |
338 | public static final Filter<Double> fdouble() { | |
339 | 11 | return fdouble; |
340 | } | |
341 | ||
342 | /** | |
343 | * Return a Filter that matches any data of the specified Class. | |
344 | * | |
345 | * @param <F> The generic type of the content returned by this Filter | |
346 | * @param clazz the Class type to match in the filter | |
347 | * @return a Filter that matches any data of the specified Class. | |
348 | */ | |
349 | public static final <F> Filter<F> fclass(Class<F> clazz) { | |
350 | 1 | return new ClassFilter<F>(clazz); |
351 | } | |
352 | ||
353 | /** | |
354 | * Return a filter that does no filtering at all - everything matches. | |
355 | * @return A Pass-Through Filter. | |
356 | */ | |
357 | public static final Filter<Object> fpassthrough() { | |
358 | 6163 | return fpassthrough; |
359 | } | |
360 | ||
361 | } |