KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.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.core.natures.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_BUILD = "compilers.p.build"; //$NON-NLS-1$
66

67     public static final String JavaDoc P_INCOMPATIBLE_ENV = "compilers.incompatible-environment"; //$NON-NLS-1$
68

69     public static final String JavaDoc P_MISSING_EXPORT_PKGS = "compilers.p.missing-packages"; //$NON-NLS-1$
70

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

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

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

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

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

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

83     private static final String JavaDoc[][] fFlags = {
84             { P_UNRESOLVED_IMPORTS,
85               P_INCOMPATIBLE_ENV,
86               P_UNRESOLVED_EX_POINTS,
87               P_NO_REQUIRED_ATT,
88               P_UNKNOWN_ELEMENT,
89               P_UNKNOWN_ATTRIBUTE,
90               P_DEPRECATED,
91               P_UNKNOWN_CLASS,
92               P_UNKNOWN_RESOURCE,
93               P_NOT_EXTERNALIZED,
94               P_BUILD,
95               P_MISSING_EXPORT_PKGS},
96             { S_CREATE_DOCS,
97               S_DOC_FOLDER,
98               S_OPEN_TAGS },
99             { F_UNRESOLVED_PLUGINS,
100               F_UNRESOLVED_FEATURES }};
101
102     public static int getFlagType(String JavaDoc flagId) {
103         if (flagId.equals(S_CREATE_DOCS))
104             return BOOLEAN;
105         if (flagId.equals(S_DOC_FOLDER))
106             return STRING;
107         return MARKER;
108     }
109
110     public static int getFlag(IProject project, String JavaDoc flagId) {
111         try {
112             return Integer.parseInt(getString(project, flagId));
113         } catch (NumberFormatException JavaDoc nfe) {
114             return 0;
115         }
116     }
117
118     public static boolean getBoolean(IProject project, String JavaDoc flagId) {
119         return Boolean.valueOf(getString(project, flagId)).booleanValue();
120     }
121
122     /**
123      *
124      * @param project
125      * project to use PROJECT,INSTANCE,DEFAULT scope or null to use
126      * only INSTANCE,DEFAULT scope
127      * @param flagId
128      * @return value or ""
129      */

130     public static String JavaDoc getString(IProject project, String JavaDoc flagId) {
131         IPreferencesService service = Platform.getPreferencesService();
132         IScopeContext[] contexts = project == null ? null
133                 : new IScopeContext[] { new ProjectScope(project) };
134         return service.getString(PDE.PLUGIN_ID, flagId, "", //$NON-NLS-1$
135
project == null ? null : contexts);
136     }
137
138     public static int getDefaultFlag(String JavaDoc flagId) {
139         return new DefaultScope().getNode(PDE.PLUGIN_ID).getInt(flagId, 0);
140     }
141
142     public static String JavaDoc getDefaultString(String JavaDoc flagId) {
143         return new DefaultScope().getNode(PDE.PLUGIN_ID).get(flagId, ""); //$NON-NLS-1$
144
}
145
146     public static boolean getDefaultBoolean(String JavaDoc flagId) {
147         return new DefaultScope().getNode(PDE.PLUGIN_ID).getBoolean(flagId,
148                 false);
149     }
150
151     public static void setFlag(String JavaDoc flagId, int value) {
152         if (getDefaultFlag(flagId) == value)
153             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
154         else
155             new InstanceScope().getNode(PDE.PLUGIN_ID).putInt(flagId, value);
156     }
157
158     public static void setFlag(IProject project, String JavaDoc flagId, int value) {
159         setString(project, flagId, Integer.toString(value));
160     }
161
162     public static void setBoolean(String JavaDoc flagId, boolean value) {
163         if (getDefaultBoolean(flagId) == value)
164             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
165         else
166             new InstanceScope().getNode(PDE.PLUGIN_ID)
167                     .putBoolean(flagId, value);
168     }
169
170     public static void setBoolean(IProject project, String JavaDoc flagId, boolean value) {
171         setString(project, flagId, Boolean.toString(value));
172     }
173
174     public static void setString(String JavaDoc flagId, String JavaDoc value) {
175         if (getDefaultString(flagId).equals(value))
176             new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
177         else
178             new InstanceScope().getNode(PDE.PLUGIN_ID).put(flagId, value);
179     }
180
181     /**
182      * Sets preference on PROJECT scope
183      *
184      * @param project
185      * @param flagId
186      * @param value
187      */

188     public static void setString(IProject project, String JavaDoc flagId, String JavaDoc value) {
189         if (project == null)
190             return;
191         Preferences preferences = new ProjectScope(project)
192                 .getNode(PDE.PLUGIN_ID);
193         preferences.put(flagId, value);
194         try {
195             preferences.flush();
196         } catch (BackingStoreException bse) {
197         }
198     }
199
200     /**
201      * Clears preference from Project scope
202      *
203      * @param project
204      * @param flagId
205      */

206     public static void clear(IProject project, String JavaDoc flagId) {
207         if (project == null)
208             return;
209         Preferences preferences = new ProjectScope(project)
210                 .getNode(PDE.PLUGIN_ID);
211         preferences.remove(flagId);
212         try {
213             preferences.flush();
214         } catch (BackingStoreException bse) {
215         }
216     }
217
218     public static void initializeDefaults() {
219         Preferences node = new DefaultScope().getNode(PDE.PLUGIN_ID);
220         node.putInt(P_UNRESOLVED_IMPORTS, ERROR);
221         node.putInt(P_UNRESOLVED_EX_POINTS, ERROR);
222         node.putInt(P_NO_REQUIRED_ATT, ERROR);
223         node.putInt(P_UNKNOWN_ELEMENT, WARNING);
224         node.putInt(P_UNKNOWN_ATTRIBUTE, WARNING);
225         node.putInt(P_DEPRECATED, WARNING);
226         node.putInt(P_UNKNOWN_CLASS, WARNING);
227         node.putInt(P_UNKNOWN_RESOURCE, WARNING);
228         node.putInt(P_NOT_EXTERNALIZED, IGNORE);
229         node.putInt(P_BUILD, WARNING);
230         node.putInt(P_INCOMPATIBLE_ENV, WARNING);
231         node.putInt(P_MISSING_EXPORT_PKGS, IGNORE);
232
233         node.putBoolean(S_CREATE_DOCS, false);
234         node.put(S_DOC_FOLDER, "doc"); //$NON-NLS-1$
235
node.putInt(S_OPEN_TAGS, WARNING);
236
237         node.putInt(F_UNRESOLVED_PLUGINS, WARNING);
238         node.putInt(F_UNRESOLVED_FEATURES, WARNING);
239     }
240
241     public static String JavaDoc[] getFlags(int group) {
242         return fFlags[group];
243     }
244
245     /**
246      * Saves INSTANCE preferences
247      */

248     public static void save() {
249         try {
250             new InstanceScope().getNode(PDE.PLUGIN_ID).flush();
251         } catch (BackingStoreException e) {
252         }
253     }
254 }
255
Popular Tags