KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > builders > CompilerFlags


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.pde.internal.builders;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.ProjectScope;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.preferences.DefaultScope;
17 import org.eclipse.core.runtime.preferences.IPreferencesService;
18 import org.eclipse.core.runtime.preferences.IScopeContext;
19 import org.eclipse.core.runtime.preferences.InstanceScope;
20 import org.eclipse.pde.internal.PDE;
21 import org.osgi.service.prefs.BackingStoreException;
22 import org.osgi.service.prefs.Preferences;
23
24 public class CompilerFlags {
25     public static final int ERROR = 0;
26
27     public static final int WARNING = 1;
28
29     public static final int IGNORE = 2;
30
31     public static final int MARKER = 0;
32
33     public static final int BOOLEAN = 1;
34
35     public static final int STRING = 2;
36
37     public static final int PLUGIN_FLAGS = 0;
38
39     public static final int SCHEMA_FLAGS = 1;
40
41     public static final int FEATURE_FLAGS = 2;
42
43     public static final int SITE_FLAGS = 3;
44
45     // Project or Instance preferences
46
public static final String JavaDoc USE_PROJECT_PREF = "compilers.use-project"; //$NON-NLS-1$
47

48     // Manifest compiler flags
49
public static final String JavaDoc P_UNRESOLVED_IMPORTS = "compilers.p.unresolved-import"; //$NON-NLS-1$
50

51     public static final String JavaDoc P_UNRESOLVED_EX_POINTS = "compilers.p.unresolved-ex-points"; //$NON-NLS-1$
52

53     public static final String JavaDoc P_UNKNOWN_ELEMENT = "compilers.p.unknown-element"; //$NON-NLS-1$
54

55     public static final String JavaDoc P_UNKNOWN_ATTRIBUTE = "compilers.p.unknown-attribute"; //$NON-NLS-1$
56

57     public static final String JavaDoc P_UNKNOWN_CLASS = "compilers.p.unknown-class"; //$NON-NLS-1$
58

59     public static final String JavaDoc P_UNKNOWN_RESOURCE = "compilers.p.unknown-resource"; //$NON-NLS-1$
60

61     public static final String JavaDoc P_NO_REQUIRED_ATT = "compilers.p.no-required-att"; //$NON-NLS-1$
62

63     public static final String JavaDoc P_NOT_EXTERNALIZED = "compilers.p.not-externalized-att"; //$NON-NLS-1$
64

65     public static final String JavaDoc P_DEPRECATED = "compilers.p.deprecated"; //$NON-NLS-1$
66

67     public static final String JavaDoc S_CREATE_DOCS = "compilers.s.create-docs"; //$NON-NLS-1$
68

69     public static final String JavaDoc S_DOC_FOLDER = "compilers.s.doc-folder"; //$NON-NLS-1$
70

71     public static final String JavaDoc S_OPEN_TAGS = "compilers.s.open-tags"; //$NON-NLS-1$
72

73     public static final String JavaDoc F_UNRESOLVED_PLUGINS = "compilers.f.unresolved-plugins"; //$NON-NLS-1$
74

75     public static final String JavaDoc F_UNRESOLVED_FEATURES = "compilers.f.unresolved-features"; //$NON-NLS-1$
76

77     private static final String JavaDoc[][] fFlags = {
78             { P_UNRESOLVED_IMPORTS, P_UNRESOLVED_EX_POINTS, P_NO_REQUIRED_ATT,
79                     P_UNKNOWN_ELEMENT, P_UNKNOWN_ATTRIBUTE, P_DEPRECATED,
80                     P_UNKNOWN_CLASS, P_UNKNOWN_RESOURCE, P_NOT_EXTERNALIZED },
81             { S_CREATE_DOCS, S_DOC_FOLDER, S_OPEN_TAGS },
82             { F_UNRESOLVED_PLUGINS, F_UNRESOLVED_FEATURES }, {} };
83
84     public static int getFlagType(String JavaDoc flagId) {
85         if (flagId.equals(S_CREATE_DOCS))
86             return BOOLEAN;
87         if (flagId.equals(S_DOC_FOLDER))
88             return STRING;
89         return MARKER;
90     }
91
92     public static int getFlag(IProject project, String JavaDoc flagId) {
93         try {
94             return Integer.parseInt(getString(project, flagId));
95         } catch (NumberFormatException JavaDoc nfe) {
96             return 0;
97         }
98     }
99
100     public static boolean getBoolean(IProject project, String JavaDoc flagId) {
101         return Boolean.valueOf(getString(project, flagId)).booleanValue();
102     }
103
104     /**
105      *
106      * @param project
107      * project to use PROJECT,INSTANCE,DEFAULT scope or null to use
108      * only INSTANCE,DEFAULT scope
109      * @param flagId
110      * @return value or ""
111      */

112     public static String JavaDoc getString(IProject project, String JavaDoc flagId) {
113         IPreferencesService service = Platform.getPreferencesService();
114         IScopeContext[] contexts = project == null ? null
115                 : new IScopeContext[] { new ProjectScope(project) };
116         return service.getString(PDE.PLUGIN_ID, flagId, "", //$NON-NLS-1$
117
project == null ? null : contexts);
118     }
119
120     public static int getDefaultFlag(String JavaDoc flagId) {
121         return new DefaultScope().getNode(PDE.PLUGIN_ID).getInt(flagId, 0);
122     }
123
124     public static String JavaDoc getDefaultString(String JavaDoc flagId) {
125         return new DefaultScope().getNode(PDE.PLUGIN_ID).get(flagId, ""); //$NON-NLS-1$
126
}
127
128     public static boolean getDefaultBoolean(String JavaDoc flagId) {
129         return new DefaultScope().getNode(PDE.PLUGIN_ID).getBoolean(flagId,
130                 false);
131     }
132
133     public static void setFlag(String JavaDoc flagId, int value) {
134         if (getDefaultFlag(flagId) == value)
135             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
136         else
137             new InstanceScope().getNode(PDE.PLUGIN_ID).putInt(flagId, value);
138     }
139
140     public static void setFlag(IProject project, String JavaDoc flagId, int value) {
141         setString(project, flagId, Integer.toString(value));
142     }
143
144     public static void setBoolean(String JavaDoc flagId, boolean value) {
145         if (getDefaultBoolean(flagId) == value)
146             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
147         else
148             new InstanceScope().getNode(PDE.PLUGIN_ID)
149                     .putBoolean(flagId, value);
150     }
151
152     public static void setBoolean(IProject project, String JavaDoc flagId, boolean value) {
153         setString(project, flagId, Boolean.toString(value));
154     }
155
156     public static void setString(String JavaDoc flagId, String JavaDoc value) {
157         if (getDefaultString(flagId).equals(value))
158             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
159         else
160             new InstanceScope().getNode(PDE.PLUGIN_ID).put(flagId, value);
161     }
162
163     /**
164      * Sets preference on PROJECT scope
165      *
166      * @param project
167      * @param flagId
168      * @param value
169      */

170     public static void setString(IProject project, String JavaDoc flagId, String JavaDoc value) {
171         if (project == null)
172             return;
173         Preferences preferences = new ProjectScope(project)
174                 .getNode(PDE.PLUGIN_ID);
175         preferences.put(flagId, value);
176         try {
177             preferences.flush();
178         } catch (BackingStoreException bse) {
179         }
180     }
181
182     /**
183      * Clears preference from Project scope
184      *
185      * @param project
186      * @param flagId
187      */

188     public static void clear(IProject project, String JavaDoc flagId) {
189         if (project == null)
190             return;
191         Preferences preferences = new ProjectScope(project)
192                 .getNode(PDE.PLUGIN_ID);
193         preferences.remove(flagId);
194         try {
195             preferences.flush();
196         } catch (BackingStoreException bse) {
197         }
198     }
199
200     public static void initializeDefaults() {
201         Preferences node = new DefaultScope().getNode(PDE.PLUGIN_ID);
202         node.putInt(P_UNRESOLVED_IMPORTS, ERROR);
203         node.putInt(P_UNRESOLVED_EX_POINTS, ERROR);
204         node.putInt(P_NO_REQUIRED_ATT, ERROR);
205         node.putInt(P_UNKNOWN_ELEMENT, WARNING);
206         node.putInt(P_UNKNOWN_ATTRIBUTE, WARNING);
207         node.putInt(P_DEPRECATED, WARNING);
208         node.putInt(P_UNKNOWN_CLASS, IGNORE);
209         node.putInt(P_UNKNOWN_RESOURCE, IGNORE);
210         node.putInt(P_NOT_EXTERNALIZED, IGNORE);
211
212         node.putBoolean(S_CREATE_DOCS, false);
213         node.put(S_DOC_FOLDER, "doc"); //$NON-NLS-1$
214
node.putInt(S_OPEN_TAGS, WARNING);
215
216         node.putInt(F_UNRESOLVED_PLUGINS, WARNING);
217         node.putInt(F_UNRESOLVED_FEATURES, WARNING);
218     }
219
220     public static String JavaDoc[] getFlags(int group) {
221         return fFlags[group];
222     }
223
224     /**
225      * Saves INSTANCE preferences
226      */

227     public static void save() {
228         try {
229             new InstanceScope().getNode(PDE.PLUGIN_ID).flush();
230         } catch (BackingStoreException e) {
231         }
232     }
233 }
234
Popular Tags