Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9437512334707ff1bdd79e0642b7ff15e6e85bd (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Saadia DHOUIB (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.export.util;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.core.build.IBuild;
import org.eclipse.pde.core.build.IBuildEntry;
import org.eclipse.pde.core.build.IBuildModel;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;

// TODO: Auto-generated Javadoc
/**
 * The Class BuildProperties.
 */
@SuppressWarnings("restriction")
public class BuildProperties {

	/** The Constant OVERWRITE_BUILD_PROPERTY. */
	public static final String OVERWRITE_BUILD_PROPERTY = "overwrite build property?";

	/** The Constant NL. */
	public static final String NL = System.getProperty("line.separator");

	/** The project. */
	private final IProject project;

	/** The its build model. */
	private IBuildModel itsBuildModel;

	/**
	 * Instantiates a new builds the properties.
	 * 
	 * @param project
	 *        the project
	 */
	public BuildProperties(IProject project) {
		this.project = project;
	}

	/**
	 * adds or overwrites an entry in the 'build.properties'.<br>
	 * returns <b>true</b> if adding was successful,<br>
	 * <b>false</b> is dialog did not confirm adding
	 * 
	 * @param key
	 *        the build property's name
	 * @param value
	 *        the build property's value
	 * @return <b>true</b> if adding was successful,<br>
	 *         <b>false</b> is dialog did not confirm adding
	 * @throws CoreException
	 *         the core exception
	 */
	public boolean addBuildEntry(String key, String value) throws CoreException {
		Assert.isNotNull(key);
		Assert.isNotNull(value);
		boolean writeEntry = true;
		if(hasBuildEntry(key)) {
			if(getBuild().getEntry(key).contains(value)) {
				return true;
			}


			removeExistingBuildEntry(key);

		}

		if(writeEntry) {
			IBuildModel buildModel = getBuildModel();
			IBuildEntry buildEntry = buildModel.getFactory().createEntry(key);
			buildEntry.addToken(value);
			getBuild().add(buildEntry);

			((WorkspaceBuildModel)buildModel).save();
		}
		return writeEntry;
	}



	/**
	 * removes an entry with the given name from the 'build.properties'
	 * 
	 * @param key
	 *        the build property's name
	 * @throws CoreException
	 *         the core exception
	 */
	public void removeExistingBuildEntry(String key) throws CoreException {
		IBuildEntry entry = getBuild().getEntry(key);
		if(entry != null) {
			getBuild().remove(entry);
		}
	}

	/**
	 * returns whether an entry exists for the given key.
	 * 
	 * @param key
	 *        the build property's name
	 * @return true, if successful
	 */
	public boolean hasBuildEntry(final String key) {
		return getBuild().getEntry(key) != null;
	}



	/**
	 * Gets the builds the.
	 * 
	 * @return the builds the
	 */
	private IBuild getBuild() {
		IBuildModel buildModel = getBuildModel();
		if(buildModel == null) {
			return null;
		}
		return buildModel.getBuild();
	}


	/**
	 * Gets the properties.
	 * 
	 * @param key
	 *        the key
	 * @return the properties
	 */
	public String[] getProperties(String key) {
		return getBuild().getEntry(key).getTokens();
	}



	/**
	 * returns the Eclipse BuildModel.
	 * 
	 * @return the Eclipse BuildModel
	 */
	private IBuildModel getBuildModel() {
		if(itsBuildModel == null) {
			IPluginModelBase pluginModel = PluginRegistry.findModel(this.project);
			itsBuildModel = createBuildModel(pluginModel);
		}
		return itsBuildModel;
	}


	/**
	 * see org.eclipse.pde.internal.core.ClasspathUtilCore.getBuild
	 * 
	 * @param model
	 *        the model
	 * @return the i build model
	 */
	private IBuildModel createBuildModel(final IPluginModelBase model) {
		IProject project = model.getUnderlyingResource().getProject();
		IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
		if(!buildFile.exists()) {
			return null;
		}
		WorkspaceBuildModel buildModel = new WorkspaceBuildModel(buildFile);
		buildModel.load();
		return buildModel;
	}

}

Back to the top