KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > UAContentFilterProcessor


1 /***************************************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM Corporation - initial API and implementation
8  **************************************************************************************************/

9
10 package org.eclipse.help.internal.xhtml;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.IProduct;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.help.internal.FilterableUAElement;
18 import org.eclipse.help.internal.util.StringUtil;
19 import org.osgi.framework.Bundle;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 /**
26  * Handles content manipulation. Filters filter content in as opposed to filtering out.
27  *
28  */

29 public class UAContentFilterProcessor {
30
31     public Document JavaDoc applyFilters(Document JavaDoc dom) {
32         Element body = DOMUtil.getBodyElement(dom);
33         NodeList JavaDoc allChildElements = body.getChildNodes();
34         for (int i = 0; i < allChildElements.getLength(); i++) {
35             Node JavaDoc node = (Node JavaDoc) allChildElements.item(i);
36             if (!(node instanceof Element))
37                 continue;
38             applyFilters((Element) node);
39
40         }
41         return dom;
42     }
43
44     public void applyFilters(Element element) {
45         boolean filteredIn = false;
46         if (hasFilterAttribute(element)) {
47             filteredIn = processFilterAttribute(element);
48             if (!filteredIn)
49                 return;
50         } else if (hasFiltersAsChildren(element)) {
51             Element[] filters = DOMUtil.getElementsByTagName(element, "filter"); //$NON-NLS-1$
52
filteredIn = processFilterChildren(element, filters);
53             if (!filteredIn)
54                 return;
55         }
56
57         NodeList JavaDoc allChildElements = element.getChildNodes();
58         for (int i = 0; i < allChildElements.getLength(); i++) {
59             Node JavaDoc node = (Node JavaDoc) allChildElements.item(i);
60             if (!(node instanceof Element))
61                 continue;
62             Element childElement = (Element) node;
63             applyFilters(childElement);
64         }
65     }
66
67     /**
68      * Returns the current value for the given single-value filterable property
69      * by the given name (e.g. "os"). For multi-value properties, returns null.
70      *
71      * @param filterName the filter name (e.g. "os")
72      * @return the current value (e.g. "win32") or null
73      */

74     public String JavaDoc getCurrentValue(String JavaDoc filterName) {
75         if (filterName.equals("ws")) { //$NON-NLS-1$
76
return Platform.getWS();
77         }
78         else if (filterName.equals("os")) { //$NON-NLS-1$
79
return Platform.getOS();
80         }
81         else if (filterName.equals("arch")) { //$NON-NLS-1$
82
return Platform.getOSArch();
83         }
84         else if (filterName.equals("product")) { //$NON-NLS-1$
85
IProduct product = Platform.getProduct();
86             if (product != null) {
87                 return product.getId();
88             }
89         }
90         return null;
91     }
92     
93     private static boolean hasFilterAttribute(Element element) {
94         if (element.getAttribute(DOMUtil.ATT_FILTER).equals("")) //$NON-NLS-1$
95
return false;
96         return true;
97     }
98
99     private static boolean hasFiltersAsChildren(Element element) {
100         Element[] filters = DOMUtil.getElementsByTagName(element, "filter"); //$NON-NLS-1$
101
if (filters != null && filters.length > 0)
102             return true;
103         return false;
104     }
105
106
107     /**
108      * Returns true is filter passes and Element is to be included.
109      *
110      * @param element the element whose filter attribute to check
111      * @return whether or not the element should be filtered in
112      */

113     private boolean processFilterAttribute(Element element) {
114         String JavaDoc filterString = element.getAttribute(DOMUtil.ATT_FILTER);
115         boolean filtered_in = isFilteredIn(filterString);
116
117         if (!filtered_in) {
118             element.getParentNode().removeChild(element);
119         }
120         return filtered_in;
121     }
122
123
124     private boolean processFilterChildren(Element parent, Element[] filters) {
125         boolean filtered_in = false;
126         for (int i = 0; i < filters.length; i++) {
127             String JavaDoc filter = filters[i].getAttribute("name"); //$NON-NLS-1$
128
String JavaDoc value = filters[i].getAttribute("value"); //$NON-NLS-1$
129
boolean isPositive = (value.length() == 0 || value.charAt(0) != '!');
130             if (!isPositive) {
131                 // strip the NOT symbol (!)
132
value = value.substring(1);
133             }
134             filtered_in = isFilteredIn(filter, value, isPositive);
135             if (!filtered_in) {
136                 parent.getParentNode().removeChild(parent);
137                 break;
138             }
139         }
140         return filtered_in;
141     }
142
143     /**
144      * Returns whether or not the object with the given filter attribute should be
145      * filtered in.
146      *
147      * @param filterAttribute the attribute to check, e.g. "os=win32"
148      * @return whether or not the element should be filtered in
149      */

150     public boolean isFilteredIn(String JavaDoc filterAttribute) {
151         String JavaDoc[] parsedFilterString = null;
152         boolean isPositive = (filterAttribute.indexOf("!=") == -1); //$NON-NLS-1$
153
// split at "=" or "!="
154
parsedFilterString = StringUtil.split(filterAttribute, "!?="); //$NON-NLS-1$
155
String JavaDoc filter = parsedFilterString[0];
156         String JavaDoc value = parsedFilterString[1];
157         return isFilteredIn(filter, value, isPositive);
158     }
159
160     /**
161      * Returns whether or not the object with the given filter should be filtered in.
162      * Can be overriden to provide additional filtering.
163      *
164      * @param filter the filter name (e.g. "os")
165      * @param value the filter value (e.g. "win32")
166      * @param isPositive whether the filter is a positive filter (as opposed to a NOT)
167      * @return whether or not to filter the element
168      */

169     public boolean isFilteredIn(String JavaDoc filter, String JavaDoc value, boolean isPositive) {
170         boolean filtered_in = false;
171         if (filter.equals("ws")) { //$NON-NLS-1$
172
filtered_in = filterByWS(value);
173         } else if (filter.equals("os")) { //$NON-NLS-1$
174
filtered_in = filterByOS(value);
175         } else if (filter.equals("arch")) { //$NON-NLS-1$
176
filtered_in = filterByARCH(value);
177         } else if (filter.equals("product")) { //$NON-NLS-1$
178
filtered_in = filterByProduct(value);
179         } else if (filter.equals("plugin")) { //$NON-NLS-1$
180
filtered_in = filterByPlugin(value);
181         } else
182             filtered_in = filterBySystemProperty(filter, value);
183
184         return isPositive ? filtered_in : !filtered_in;
185     }
186
187     /**
188      * Returns whether or not the given object should be filtered out.
189      * Can be overriden to provide additional filtering.
190      *
191      * @param element the element to check
192      * @return whether or not to filter the element
193      */

194     public boolean isFilteredIn(FilterableUAElement element) {
195         if (element != null) {
196             Map JavaDoc filters = ((FilterableUAElement)element).getFilters();
197             if (filters != null) {
198                 Iterator JavaDoc iter = filters.entrySet().iterator();
199                 while (iter.hasNext()) {
200                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
201                     String JavaDoc key = (String JavaDoc)entry.getKey();
202                     String JavaDoc value = (String JavaDoc)entry.getValue();
203                     boolean isPositive = (value.length() == 0 || value.charAt(0) != '!');
204                     if (!isPositive) {
205                         value = value.substring(1);
206                     }
207                     if (!isFilteredIn(key, value, isPositive)) {
208                         return false;
209                     }
210                 }
211             }
212         }
213         return true;
214     }
215     
216     /**
217      * Returns whether or not the given filter property allows multiple instances
218      * to be specified. For example, for os there can only ever be one os running,
219      * but for plugins there can be many.
220      *
221      * @param filterName the name of the filter (without value)
222      * @return whether or not the filter allows multiple values
223      */

224     public boolean isMultiValue(String JavaDoc filterName) {
225         return !(filterName.equals("os") //$NON-NLS-1$
226
|| filterName.equals("ws") //$NON-NLS-1$
227
|| filterName.equals("arch") //$NON-NLS-1$
228
|| filterName.equals("product")); //$NON-NLS-1$
229
}
230     
231     /**
232      * evaluates WS filter.
233      */

234     private static boolean filterByWS(String JavaDoc ws) {
235         String JavaDoc currentWS = Platform.getWS();
236         if (currentWS.equals(ws))
237             return true;
238         return false;
239     }
240
241     /**
242      * evaluates OS filter.
243      */

244     private static boolean filterByOS(String JavaDoc os) {
245         String JavaDoc currentOS = Platform.getOS();
246         if (currentOS.equals(os))
247             return true;
248         return false;
249     }
250
251     /**
252      * evaluates ARCH filter.
253      */

254     private static boolean filterByARCH(String JavaDoc arch) {
255         String JavaDoc currentArch = Platform.getOSArch();
256         if (currentArch.equals(arch))
257             return true;
258         return false;
259     }
260
261     /**
262      * evaluates product filter.
263      */

264     private static boolean filterByProduct(String JavaDoc productId) {
265         IProduct product = Platform.getProduct();
266         if (product == null)
267             return false;
268
269         String JavaDoc currentProductId = product.getId();
270         if (currentProductId.equals(productId))
271             return true;
272         return false;
273     }
274
275     /**
276      * evaluates plugin filter.
277      */

278     private static boolean filterByPlugin(String JavaDoc bundleId) {
279         Bundle bundle = Platform.getBundle(bundleId);
280         boolean bundleIsOK = checkBundleState(bundle);
281         if (bundleIsOK)
282             return true;
283
284         return false;
285     }
286
287
288     public static boolean checkBundleState(Bundle bundle) {
289         if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
290                 || bundle.getState() == Bundle.INSTALLED)
291             return false;
292
293         return true;
294     }
295
296
297     /**
298      * evaluates system property filter.
299      */

300     private static boolean filterBySystemProperty(String JavaDoc property, String JavaDoc value) {
301         String JavaDoc currentValue = System.getProperty(property);
302         if (currentValue != null && currentValue.equals(value))
303             return true;
304         return false;
305     }
306
307 }
308
Popular Tags