KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > FilterResolver


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

11 package org.eclipse.help.internal.dynamic;
12
13 import org.eclipse.core.runtime.IProduct;
14 import org.eclipse.core.runtime.Platform;
15 import org.osgi.framework.Bundle;
16
17 /*
18  * Resolves filters by determining whether the element in question should
19  * or shouldn't be filtered.
20  */

21 public class FilterResolver {
22
23     private static final String JavaDoc NAME_OS = "os"; //$NON-NLS-1$
24
private static final String JavaDoc NAME_WS = "ws"; //$NON-NLS-1$
25
private static final String JavaDoc NAME_ARCH = "arch"; //$NON-NLS-1$
26
private static final String JavaDoc NAME_PRODUCT = "product"; //$NON-NLS-1$
27
private static final String JavaDoc NAME_PLUGIN = "plugin"; //$NON-NLS-1$
28

29     private static Extension extension;
30     private static FilterResolver instance;
31     
32     public static FilterResolver getInstance() {
33         if (instance == null) {
34             instance = new FilterResolver();
35         }
36         return instance;
37     }
38     
39     /*
40      * Returns whether or not the given filter expression gets rejected by the
41      * filter. e.g. "os=win32", "ws!=gtk"
42      */

43     public boolean isFiltered(String JavaDoc expression) {
44         String JavaDoc name = null;
45         String JavaDoc value = null;
46         boolean not = false;
47         int index = expression.indexOf("!="); //$NON-NLS-1$
48
if (index != -1) {
49             name = expression.substring(0, index).trim();
50             value = expression.substring(index + 2).trim();
51             not = true;
52         }
53         else {
54             index = expression.indexOf('=');
55             if (index != -1) {
56                 name = expression.substring(0, index).trim();
57                 value = expression.substring(index + 1).trim();
58                 not = false;
59             }
60         }
61         if (name != null && name.length() > 0 && value != null && value.length() > 0) {
62             return isFiltered(name, value, not);
63         }
64         // don't apply any invalid filters
65
return false;
66     }
67     
68     /*
69      * Returns whether or not the given filter name and value get rejected by
70      * the filter.
71      */

72     public boolean isFiltered(String JavaDoc name, String JavaDoc value, boolean not) {
73         boolean filtered;
74         if (name.equals(NAME_OS)) {
75             filtered = filterByWS(value);
76         }
77         else if (name.equals(NAME_WS)) {
78             filtered = filterByOS(value);
79         }
80         else if (name.equals(NAME_ARCH)) {
81             filtered = filterByARCH(value);
82         }
83         else if (name.equals(NAME_PRODUCT)) {
84             filtered = filterByProduct(value);
85         }
86         else if (name.equals(NAME_PLUGIN)) {
87             filtered = filterByPlugin(value);
88         }
89         else if (extension != null && extension.isHandled(name)) {
90             filtered = extension.isFiltered(name, value);
91         }
92         else {
93             filtered = filterBySystemProperty(name, value);
94         }
95         return not ? !filtered : filtered;
96     }
97     
98     /*
99      * Hack: We don't have access to UI classes from here; the activity
100      * and category filters are dropped in from org.eclipse.help.ui when it
101      * starts.
102      */

103     public static void setExtension(Extension extension) {
104         FilterResolver.extension = extension;
105     }
106
107     /*
108      * Evaluates the "ws" filter.
109      */

110     private boolean filterByWS(String JavaDoc ws) {
111         return !ws.equals(Platform.getWS());
112     }
113
114     /*
115      * Evaluates the "os" filter.
116      */

117     private boolean filterByOS(String JavaDoc os) {
118         return !os.equals(Platform.getOS());
119     }
120
121     /*
122      * Evaluates the "arch" filter.
123      */

124     private boolean filterByARCH(String JavaDoc arch) {
125         return !arch.equals(Platform.getOSArch());
126     }
127
128     /*
129      * Evaluates the "product" filter.
130      */

131     private boolean filterByProduct(String JavaDoc productId) {
132         IProduct product = Platform.getProduct();
133         if (product != null) {
134             return !productId.equals(product.getId());
135         }
136         return false;
137     }
138
139     /*
140      * Evaluates the "plugin" filter.
141      */

142     private boolean filterByPlugin(String JavaDoc bundleId) {
143         Bundle bundle = Platform.getBundle(bundleId);
144         return bundle == null;
145     }
146
147     /*
148      * Evaluates the system property filter (when filter name doesn't match
149      * any known filter).
150      */

151     private boolean filterBySystemProperty(String JavaDoc property, String JavaDoc value) {
152         try {
153             String JavaDoc systemValue = System.getProperty(property);
154             if (systemValue != null) {
155                 return !value.equals(systemValue);
156             }
157         }
158         catch (Throwable JavaDoc t) {
159             // skip
160
}
161         return true;
162     }
163     
164     /*
165      * Hack: A way for the org.eclipse.help.ui plugin to extend the filtering
166      * capability with UI-related filters (this is a core plugin).
167      */

168     public static interface Extension {
169         
170         /*
171          * Returns whether or not this extension handles the given filter.
172          */

173         public boolean isHandled(String JavaDoc name);
174         
175         /*
176          * Returns whether the given filter is rejected or not.
177          */

178         public boolean isFiltered(String JavaDoc name, String JavaDoc value);
179     }
180 }
181
Popular Tags