001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.builders;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.ProjectScope;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.core.runtime.preferences.DefaultScope;
016: import org.eclipse.core.runtime.preferences.IPreferencesService;
017: import org.eclipse.core.runtime.preferences.IScopeContext;
018: import org.eclipse.core.runtime.preferences.InstanceScope;
019: import org.eclipse.pde.internal.core.natures.PDE;
020: import org.osgi.service.prefs.BackingStoreException;
021: import org.osgi.service.prefs.Preferences;
022:
023: public class CompilerFlags {
024: public static final int ERROR = 0;
025:
026: public static final int WARNING = 1;
027:
028: public static final int IGNORE = 2;
029:
030: public static final int MARKER = 0;
031:
032: public static final int BOOLEAN = 1;
033:
034: public static final int STRING = 2;
035:
036: public static final int PLUGIN_FLAGS = 0;
037:
038: public static final int SCHEMA_FLAGS = 1;
039:
040: public static final int FEATURE_FLAGS = 2;
041:
042: public static final int SITE_FLAGS = 3;
043:
044: // Project or Instance preferences
045: public static final String USE_PROJECT_PREF = "compilers.use-project"; //$NON-NLS-1$
046:
047: // Manifest compiler flags
048: public static final String P_UNRESOLVED_IMPORTS = "compilers.p.unresolved-import"; //$NON-NLS-1$
049:
050: public static final String P_UNRESOLVED_EX_POINTS = "compilers.p.unresolved-ex-points"; //$NON-NLS-1$
051:
052: public static final String P_UNKNOWN_ELEMENT = "compilers.p.unknown-element"; //$NON-NLS-1$
053:
054: public static final String P_UNKNOWN_ATTRIBUTE = "compilers.p.unknown-attribute"; //$NON-NLS-1$
055:
056: public static final String P_UNKNOWN_CLASS = "compilers.p.unknown-class"; //$NON-NLS-1$
057:
058: public static final String P_UNKNOWN_RESOURCE = "compilers.p.unknown-resource"; //$NON-NLS-1$
059:
060: public static final String P_NO_REQUIRED_ATT = "compilers.p.no-required-att"; //$NON-NLS-1$
061:
062: public static final String P_NOT_EXTERNALIZED = "compilers.p.not-externalized-att"; //$NON-NLS-1$
063:
064: public static final String P_BUILD = "compilers.p.build"; //$NON-NLS-1$
065:
066: public static final String P_INCOMPATIBLE_ENV = "compilers.incompatible-environment"; //$NON-NLS-1$
067:
068: public static final String P_MISSING_EXPORT_PKGS = "compilers.p.missing-packages"; //$NON-NLS-1$
069:
070: public static final String P_DEPRECATED = "compilers.p.deprecated"; //$NON-NLS-1$
071:
072: public static final String S_CREATE_DOCS = "compilers.s.create-docs"; //$NON-NLS-1$
073:
074: public static final String S_DOC_FOLDER = "compilers.s.doc-folder"; //$NON-NLS-1$
075:
076: public static final String S_OPEN_TAGS = "compilers.s.open-tags"; //$NON-NLS-1$
077:
078: public static final String F_UNRESOLVED_PLUGINS = "compilers.f.unresolved-plugins"; //$NON-NLS-1$
079:
080: public static final String F_UNRESOLVED_FEATURES = "compilers.f.unresolved-features"; //$NON-NLS-1$
081:
082: private static final String[][] fFlags = {
083: { P_UNRESOLVED_IMPORTS, P_INCOMPATIBLE_ENV,
084: P_UNRESOLVED_EX_POINTS, P_NO_REQUIRED_ATT,
085: P_UNKNOWN_ELEMENT, P_UNKNOWN_ATTRIBUTE,
086: P_DEPRECATED, P_UNKNOWN_CLASS, P_UNKNOWN_RESOURCE,
087: P_NOT_EXTERNALIZED, P_BUILD, P_MISSING_EXPORT_PKGS },
088: { S_CREATE_DOCS, S_DOC_FOLDER, S_OPEN_TAGS },
089: { F_UNRESOLVED_PLUGINS, F_UNRESOLVED_FEATURES } };
090:
091: public static int getFlagType(String flagId) {
092: if (flagId.equals(S_CREATE_DOCS))
093: return BOOLEAN;
094: if (flagId.equals(S_DOC_FOLDER))
095: return STRING;
096: return MARKER;
097: }
098:
099: public static int getFlag(IProject project, String flagId) {
100: try {
101: return Integer.parseInt(getString(project, flagId));
102: } catch (NumberFormatException nfe) {
103: return 0;
104: }
105: }
106:
107: public static boolean getBoolean(IProject project, String flagId) {
108: return Boolean.valueOf(getString(project, flagId))
109: .booleanValue();
110: }
111:
112: /**
113: *
114: * @param project
115: * project to use PROJECT,INSTANCE,DEFAULT scope or null to use
116: * only INSTANCE,DEFAULT scope
117: * @param flagId
118: * @return value or ""
119: */
120: public static String getString(IProject project, String flagId) {
121: IPreferencesService service = Platform.getPreferencesService();
122: IScopeContext[] contexts = project == null ? null
123: : new IScopeContext[] { new ProjectScope(project) };
124: return service.getString(PDE.PLUGIN_ID, flagId, "", //$NON-NLS-1$
125: project == null ? null : contexts);
126: }
127:
128: public static int getDefaultFlag(String flagId) {
129: return new DefaultScope().getNode(PDE.PLUGIN_ID).getInt(flagId,
130: 0);
131: }
132:
133: public static String getDefaultString(String flagId) {
134: return new DefaultScope().getNode(PDE.PLUGIN_ID)
135: .get(flagId, ""); //$NON-NLS-1$
136: }
137:
138: public static boolean getDefaultBoolean(String flagId) {
139: return new DefaultScope().getNode(PDE.PLUGIN_ID).getBoolean(
140: flagId, false);
141: }
142:
143: public static void setFlag(String flagId, int value) {
144: if (getDefaultFlag(flagId) == value)
145: new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
146: else
147: new InstanceScope().getNode(PDE.PLUGIN_ID).putInt(flagId,
148: value);
149: }
150:
151: public static void setFlag(IProject project, String flagId,
152: int value) {
153: setString(project, flagId, Integer.toString(value));
154: }
155:
156: public static void setBoolean(String flagId, boolean value) {
157: if (getDefaultBoolean(flagId) == value)
158: new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
159: else
160: new InstanceScope().getNode(PDE.PLUGIN_ID).putBoolean(
161: flagId, value);
162: }
163:
164: public static void setBoolean(IProject project, String flagId,
165: boolean value) {
166: setString(project, flagId, Boolean.toString(value));
167: }
168:
169: public static void setString(String flagId, String value) {
170: if (getDefaultString(flagId).equals(value))
171: new InstanceScope().getNode(PDE.PLUGIN_ID).remove(flagId);
172: else
173: new InstanceScope().getNode(PDE.PLUGIN_ID).put(flagId,
174: value);
175: }
176:
177: /**
178: * Sets preference on PROJECT scope
179: *
180: * @param project
181: * @param flagId
182: * @param value
183: */
184: public static void setString(IProject project, String flagId,
185: String value) {
186: if (project == null)
187: return;
188: Preferences preferences = new ProjectScope(project)
189: .getNode(PDE.PLUGIN_ID);
190: preferences.put(flagId, value);
191: try {
192: preferences.flush();
193: } catch (BackingStoreException bse) {
194: }
195: }
196:
197: /**
198: * Clears preference from Project scope
199: *
200: * @param project
201: * @param flagId
202: */
203: public static void clear(IProject project, String flagId) {
204: if (project == null)
205: return;
206: Preferences preferences = new ProjectScope(project)
207: .getNode(PDE.PLUGIN_ID);
208: preferences.remove(flagId);
209: try {
210: preferences.flush();
211: } catch (BackingStoreException bse) {
212: }
213: }
214:
215: public static void initializeDefaults() {
216: Preferences node = new DefaultScope().getNode(PDE.PLUGIN_ID);
217: node.putInt(P_UNRESOLVED_IMPORTS, ERROR);
218: node.putInt(P_UNRESOLVED_EX_POINTS, ERROR);
219: node.putInt(P_NO_REQUIRED_ATT, ERROR);
220: node.putInt(P_UNKNOWN_ELEMENT, WARNING);
221: node.putInt(P_UNKNOWN_ATTRIBUTE, WARNING);
222: node.putInt(P_DEPRECATED, WARNING);
223: node.putInt(P_UNKNOWN_CLASS, WARNING);
224: node.putInt(P_UNKNOWN_RESOURCE, WARNING);
225: node.putInt(P_NOT_EXTERNALIZED, IGNORE);
226: node.putInt(P_BUILD, WARNING);
227: node.putInt(P_INCOMPATIBLE_ENV, WARNING);
228: node.putInt(P_MISSING_EXPORT_PKGS, IGNORE);
229:
230: node.putBoolean(S_CREATE_DOCS, false);
231: node.put(S_DOC_FOLDER, "doc"); //$NON-NLS-1$
232: node.putInt(S_OPEN_TAGS, WARNING);
233:
234: node.putInt(F_UNRESOLVED_PLUGINS, WARNING);
235: node.putInt(F_UNRESOLVED_FEATURES, WARNING);
236: }
237:
238: public static String[] getFlags(int group) {
239: return fFlags[group];
240: }
241:
242: /**
243: * Saves INSTANCE preferences
244: */
245: public static void save() {
246: try {
247: new InstanceScope().getNode(PDE.PLUGIN_ID).flush();
248: } catch (BackingStoreException e) {
249: }
250: }
251: }
|