Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 74fa591eb3fa13650079c09f037fd37d9afe314c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2012 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jaxb.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jpt.common.core.internal.JptCommonCoreMessages;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.jaxb.core.internal.plugin.JptJaxbCorePlugin;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
 * Public access to the Dali JAXB preferences.
 * <p>
 * Preferences are a cross between public, model-related state and private,
 * plug-in-related state; thus this public facade to state that is
 * (traditionally) scoped by the source code's plug-in location.
 * Another complication is that preferences must be available even when a
 * model is not (yet) present (e.g. for a "creation" wizard).
 * <p>
 * Provisional API: This class is part of an interim API that is still
 * under development and expected to change significantly before reaching
 * stability. It is available at this early stage to solicit feedback from
 * pioneering adopters on the understanding that any code that uses this API
 * will almost certainly be broken (repeatedly) as the API evolves.
 * 
 * @version 3.3
 * @since 3.3
 */
public class JaxbPreferences {

	// ********** project JAXB platform ID **********

	public static String getJaxbPlatformID(IProject project) {
		return getJaxbPlatformID(project, GenericJaxbPlatform.VERSION_2_1.getId());
	}

	private static String getJaxbPlatformID(IProject project, String def) {
		Preferences prefs = getJaxbPlatformPreferences(project);
		return (prefs == null) ? def : prefs.get(JAXB_PLATFORM_ID, def);
	}

	public static void setJaxbPlatformID(IProject project, String jaxbPlatformID) {
		Preferences prefs = getJaxbPlatformPreferences(project);
		if (prefs != null) {
			prefs.put(JAXB_PLATFORM_ID, jaxbPlatformID);
			flushPreferences(prefs);
		}
	}

	private static Preferences getJaxbPlatformPreferences(IProject project) {
		Preferences prefs = getPreferences(project);
		return (prefs == null) ? null : prefs.node(JAXB_PLATFORM_NODE);
	}

	private static final String JAXB_PLATFORM_NODE = "platform";  //$NON-NLS-1$
	private static final String JAXB_PLATFORM_ID = "platform-id";  //$NON-NLS-1$


	// ********** schema locations **********

	public static List<String> getSchemaLocations(IProject project) {
		return getSchemaLocations(project, Collections.<String>emptyList());
	}

	private static List<String> getSchemaLocations(IProject project, List<String> def) {
		Preferences prefs = getSchemasPreferences(project);
		if (prefs == null) {
			return def;
		}

		try {
			ArrayList<String> schemaLocations = new ArrayList<String>();
			int i = 1;
			String nodeName = null;
			while (prefs.nodeExists(nodeName = buildSchemaNodeName(i))) {
				Preferences schemaPrefs = prefs.node(nodeName);
				String location = schemaPrefs.get(SCHEMA_LOCATION, null);
				if (location != null) {
					schemaLocations.add(location);
				}
				i++;
			}
			return schemaLocations;
		} catch (BackingStoreException ex) {
			JptJaxbCorePlugin.instance().logError(ex);
			return def;
		}
	}

	public static void setSchemaLocations(IProject project, List<String> schemaLocations) {
		Preferences prefs = getSchemasPreferences(project);
		if (prefs == null) {
			return;
		}

		try {
			int i = 1;
			String nodeName = null;
			for (String location : schemaLocations) {
				nodeName = buildSchemaNodeName(i);
				Preferences schemaPref = prefs.node(nodeName);
				schemaPref.put(SCHEMA_LOCATION, location);
				i++;
			}
			while (prefs.nodeExists(nodeName = buildSchemaNodeName(i))) {
				prefs.node(nodeName).removeNode();
				i++;
			}
			flushPreferences(prefs);
		} catch (BackingStoreException ex) {
			JptJaxbCorePlugin.instance().logError(ex);
		}
	}

	private static Preferences getSchemasPreferences(IProject project) {
		Preferences prefs = getPreferences(project);
		return (prefs == null) ? null : prefs.node(SCHEMAS_NODE);
	}

	private static String buildSchemaNodeName(int i) {
		return SCHEMA_NODE_PREFIX + String.valueOf(i);
	}

	private static final String SCHEMAS_NODE = "schemas";  //$NON-NLS-1$
	/**
	 * Converted into <code>"schema-1"</code>, <code>"schema-2"</code>, etc.
	 */
	private static final String SCHEMA_NODE_PREFIX = "schema-";  //$NON-NLS-1$
	private static final String SCHEMA_LOCATION = "location";  //$NON-NLS-1$


	// ********** class gen package **********

	public static String getClassGenPackage(IProject project) {
		return getClassGenPackage(project, StringTools.EMPTY_STRING);
	}

	private static String getClassGenPackage(IProject project, String def) {
		Preferences prefs = getClassGenPreferences(project);
		return (prefs == null) ? def : prefs.get(PACKAGE, def);
	}

	public static void setClassGenPackage(IProject project, String packageName) {
		Preferences prefs = getClassGenPreferences(project);
		if (prefs != null) {
			prefs.put(PACKAGE, packageName);
			flushPreferences(prefs);
		}
	}

	private static Preferences getClassGenPreferences(IProject project) {
		Preferences prefs = getPreferences(project);
		return (prefs == null) ? null : prefs.node(CLASS_GEN_NODE);
	}

	private static final String CLASS_GEN_NODE = "classgen";  //$NON-NLS-1$
	private static final String PACKAGE = "package";  //$NON-NLS-1$


	// ********** flush preferences **********

	private static void flushPreferences(Preferences prefs) {
		new FlushPreferencesJob(prefs).schedule();
	}

	/* CU private */ static class FlushPreferencesJob
		extends Job
	{
		private final Preferences prefs;

		FlushPreferencesJob(Preferences prefs) {
			super(NLS.bind(JptCommonCoreMessages.PREFERENCES_FLUSH_JOB_NAME, prefs.absolutePath()));
			this.prefs = prefs;
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			try {
				this.prefs.flush();
			} catch(BackingStoreException ex) {
				return JptJaxbCorePlugin.instance().logError(ex);
			}
			return Status.OK_STATUS;
		}
	}


	// ********** misc **********

	/**
	 * <strong>NB:</strong> Return the <em>faceted project</em> preferences.
	 */
	private static Preferences getPreferences(IProject project) {
		IFacetedProject facetedProject = getFacetedProject(project);
		try {
			return (facetedProject == null) ? null : facetedProject.getPreferences(JaxbFacet.FACET);
		} catch (BackingStoreException ex) {
			JptJaxbCorePlugin.instance().logError(ex);
			return null;
		}
	}

	private static IFacetedProject getFacetedProject(IProject project) {
		try {
			return ProjectFacetsManager.create(project);
		} catch (CoreException ex) {
			JptJaxbCorePlugin.instance().logError(ex);
			return null;
		}
	}

	private JaxbPreferences() {
		throw new UnsupportedOperationException();
	}
}

Back to the top