Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3de74e6216d4b2b09c0f1e33f0ebf12154dedabe (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.core.target;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
import org.eclipse.pde.internal.core.itarget.ILocationInfo;
import org.eclipse.pde.internal.core.itarget.ITarget;
import org.eclipse.pde.internal.core.itarget.ITargetFeature;
import org.eclipse.pde.internal.core.itarget.ITargetJRE;
import org.eclipse.pde.internal.core.itarget.ITargetModel;
import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
import org.eclipse.pde.internal.core.itarget.ITargetObject;
import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Target extends TargetObject implements ITarget {

	private static final long serialVersionUID = 1L;
	private String fName;
	private TreeMap fPlugins = new TreeMap();
	private TreeMap fFeatures = new TreeMap();
	private IArgumentsInfo fArgsInfo;
	private IEnvironmentInfo fEnvInfo;
	private ITargetJRE fRuntimeInfo;
	private ILocationInfo fLocationInfo;
	private IImplicitDependenciesInfo fImplicitInfo;
	private boolean fUseAllTargetPlatform = false;
	private Set fAdditionalDirectories = new HashSet();
	private String fDescription = null;
	
	public Target(ITargetModel model) {
		super(model);
	}

	public void reset() {
		fArgsInfo = null;
		fEnvInfo = null;
		fRuntimeInfo = null;
		fLocationInfo = null;
		fImplicitInfo = null;
		fPlugins.clear();
		fFeatures.clear();
		fUseAllTargetPlatform = false;
		fAdditionalDirectories.clear();
	}

	public void parse(Node node) {
		if (node.getNodeType() == Node.ELEMENT_NODE 
				&& node.getNodeName().equals("target")) { //$NON-NLS-1$
			Element element = (Element)node; 
			fName = element.getAttribute(P_NAME); 
			NodeList children = node.getChildNodes();
			ITargetModelFactory factory = getModel().getFactory();
			for (int i = 0; i < children.getLength(); i++) {
				Node child = children.item(i);
				if (child.getNodeType() == Node.ELEMENT_NODE) {
					String name = child.getNodeName();
					if (name.equals("launcherArgs")) { //$NON-NLS-1$
						fArgsInfo = factory.createArguments();
						fArgsInfo.parse(child);
					} else if (name.equals("content")) { //$NON-NLS-1$
						parseContent((Element)child);
					} else if (name.equals("environment")) { //$NON-NLS-1$
						fEnvInfo = factory.createEnvironment();
						fEnvInfo.parse(child);
					} else if (name.equals("targetJRE")) { //$NON-NLS-1$
						fRuntimeInfo = factory.createJREInfo();
						fRuntimeInfo.parse(child);
					} else if (name.equals("location")) { //$NON-NLS-1$
						fLocationInfo = factory.createLocation();
						fLocationInfo.parse(child);
					} else if (name.equals("implicitDependencies")) { //$NON-NLS-1$
						fImplicitInfo = factory.createImplicitPluginInfo();
						fImplicitInfo.parse(child);
					}
				}
			}
		}
	}
	
	private void parseContent(Element content) {
		fUseAllTargetPlatform =
			"true".equals(content.getAttribute("useAllPlugins")); //$NON-NLS-1$ //$NON-NLS-2$
		NodeList children = content.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node node = children.item(i);
			if ("plugins".equals(node.getNodeName())) { //$NON-NLS-1$
				parsePlugins(node.getChildNodes());
			} else if ("features".equals(node.getNodeName())) { //$NON-NLS-1$
				parseFeatures(node.getChildNodes());
			} else if ("extraLocations".equals(node.getNodeName())) { //$NON-NLS-1$
				parseLocations(node.getChildNodes());
			}
		}	
	}
	
	private void parsePlugins(NodeList children) {
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("plugin")) { //$NON-NLS-1$
					ITargetPlugin plugin = getModel().getFactory().createPlugin();
					plugin.parse(child);
					fPlugins.put(plugin.getId(), plugin);
				}
			}
		}
	}
	
	private void parseFeatures(NodeList children) {
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("feature")) { //$NON-NLS-1$
					ITargetFeature feature = getModel().getFactory().createFeature();
					feature.parse(child);
					fFeatures.put(feature.getId(), feature);
				}
			}
		}
	}
	
	private void parseLocations(NodeList children) {
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("location")) { //$NON-NLS-1$
					IAdditionalLocation loc = getModel().getFactory().createAdditionalLocation();
					loc.parse(child);
					fAdditionalDirectories.add(loc);
				}
			}
		}
	}

	public void write(String indent, PrintWriter writer) {
		writer.print(indent + "<target"); //$NON-NLS-1$
		if (fName != null && fName.length() > 0)
			writer.print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		writer.println(">"); //$NON-NLS-1$
		if (fArgsInfo != null) {
			fArgsInfo.write(indent + "   ", writer); //$NON-NLS-1$
		}
		if (fEnvInfo != null) {
			fEnvInfo.write(indent + "   ", writer); //$NON-NLS-1$
		}
		if (fRuntimeInfo != null) {
			fRuntimeInfo.write(indent + "   ", writer); //$NON-NLS-1$
		}
		if (fLocationInfo != null) {
			fLocationInfo.write(indent + "   ", writer); //$NON-NLS-1$
		}
		
		writer.println();
		if (fUseAllTargetPlatform) {
			writer.println(indent + "   <content useAllPlugins=\"true\">"); //$NON-NLS-1$
		} else {
			writer.println(indent + "   <content>"); //$NON-NLS-1$
		}
		
		writer.println(indent + "      <plugins>"); //$NON-NLS-1$
		Iterator iter = fPlugins.values().iterator();
		while (iter.hasNext()) {
			ITargetPlugin plugin = (ITargetPlugin) iter.next();
			plugin.write(indent + "         ", writer); //$NON-NLS-1$
		}
		writer.println(indent + "      </plugins>"); //$NON-NLS-1$		
		writer.println(indent + "      <features>"); //$NON-NLS-1$
		iter = fFeatures.values().iterator();
		while (iter.hasNext()) {
			ITargetFeature feature = (ITargetFeature) iter.next();
			feature.write(indent + "         ", writer); //$NON-NLS-1$
		}
		writer.println(indent + "      </features>"); //$NON-NLS-1$
		if (!fAdditionalDirectories.isEmpty()) {
			writer.println(indent + "      <extraLocations>"); //$NON-NLS-1$
			iter = fAdditionalDirectories.iterator();
			while (iter.hasNext()) {
				IAdditionalLocation location = (IAdditionalLocation) iter.next();
				location.write(indent + "         ", writer); //$NON-NLS-1$
			}
			writer.println(indent + "      </extraLocations>"); //$NON-NLS-1$
		}
		writer.println(indent + "   </content>"); //$NON-NLS-1$
		if (fImplicitInfo != null) {
			fImplicitInfo.write(indent + "   ", writer); //$NON-NLS-1$
		}
		writer.println();
		writer.println(indent + "</target>"); //$NON-NLS-1$
	}
	
	public IArgumentsInfo getArguments() {
		return fArgsInfo;
	}
	
	public void setArguments(IArgumentsInfo info) {
		fArgsInfo = info;
	}

	public IEnvironmentInfo getEnvironment() {
		return fEnvInfo;
	}

	public void setEnvironment(IEnvironmentInfo info) {
		fEnvInfo = info;
	}

	public ITargetJRE getTargetJREInfo() {
		return fRuntimeInfo;
	}

	public void setTargetJREInfo(ITargetJRE info) {
		fRuntimeInfo = info;
		
	}

	public String getName() {
		return fName;
	}

	public void setName(String name) {
		String oldValue = fName;
		fName = name;
		firePropertyChanged(P_NAME, oldValue, fName);
	}

	public ILocationInfo getLocationInfo() {
		return fLocationInfo;
	}

	public void setLocationInfo(ILocationInfo info) {
		fLocationInfo = info;
	}

	public void addPlugin(ITargetPlugin plugin) {
		addPlugins(new ITargetPlugin[] {plugin});
	}
	
	public void addPlugins(ITargetPlugin[] plugins) {
		ArrayList list = new ArrayList();
		for (int i = 0; i < plugins.length; i ++ ) {
			String id = plugins[i].getId();
			if (fPlugins.containsKey(id))
				continue;
			list.add(plugins[i]);
			plugins[i].setModel(getModel());
			fPlugins.put(id, plugins[i]);
		}
		if (isEditable() && list.size() > 0) {
			fireStructureChanged((ITargetObject[])list.toArray(new ITargetObject[list.size()]), 
								IModelChangedEvent.INSERT);
		}
	}

	public void addFeature(ITargetFeature feature) {
		addFeatures(new ITargetFeature[] {feature});
	}
	
	public void addFeatures(ITargetFeature[] features) {
		ArrayList list = new ArrayList();
		for (int i = 0; i < features.length; i++) {
			String id = features[i].getId();
			if (fFeatures.containsKey(id))
				continue;
			list.add(features[i]);
			features[i].setModel(getModel());
			fFeatures.put(id, features[i]);
		}
		if (isEditable() && list.size() > 0)
			fireStructureChanged((ITargetObject[])list.toArray(new ITargetObject[list.size()]), 
					IModelChangedEvent.INSERT);
	}

	public void removePlugin(ITargetPlugin plugin) {
		removePlugins(new ITargetPlugin[] {plugin});
	}
	
	public void removePlugins(ITargetPlugin[] plugins) {
		boolean modify = false;
		for (int i =0; i < plugins.length; i++) 
			modify = ((fPlugins.remove(plugins[i].getId()) != null) || modify);
		if (isEditable() && modify)
			fireStructureChanged(plugins, IModelChangedEvent.REMOVE);
	}

	public void removeFeature(ITargetFeature feature) {
		removeFeatures(new ITargetFeature[] {feature});
	}
	
	public void removeFeatures(ITargetFeature[] features) {
		boolean modify = false;
		for (int i = 0; i < features.length; i++) {
			modify = ((fFeatures.remove(features[i].getId()) != null) || modify);
		}
		if (isEditable() && modify)
			fireStructureChanged(features, IModelChangedEvent.REMOVE);	
	}

	public ITargetPlugin[] getPlugins() {
		return (ITargetPlugin[]) fPlugins.values().toArray(new ITargetPlugin[fPlugins.size()]);
	}

	public ITargetFeature[] getFeatures() {
		return (ITargetFeature[]) fFeatures.values().toArray(new ITargetFeature[fFeatures.size()]);
	}

	public boolean containsPlugin(String id) {
		return fPlugins.containsKey(id);
	}

	public boolean containsFeature(String id) {
		return fFeatures.containsKey(id);
	}

	public boolean useAllPlugins() {
		return fUseAllTargetPlatform;
	}

	public void setUseAllPlugins(boolean value) {
		boolean oldValue = fUseAllTargetPlatform;
		fUseAllTargetPlatform = value;
		if (isEditable())
			firePropertyChanged(P_ALL_PLUGINS, new Boolean(oldValue), new Boolean(fUseAllTargetPlatform));
	}

	public void setImplicitPluginsInfo(IImplicitDependenciesInfo info) {
		fImplicitInfo = info;
	}

	public IImplicitDependenciesInfo getImplicitPluginsInfo() {
		return fImplicitInfo;
	}

	public IAdditionalLocation[] getAdditionalDirectories() {
		return (IAdditionalLocation[])
			fAdditionalDirectories.toArray(new IAdditionalLocation[fAdditionalDirectories.size()]);
	}

	public void addAdditionalDirectories(IAdditionalLocation[] dirs) {
		for (int i = 0; i < dirs.length; i++) {
			fAdditionalDirectories.add(dirs[i]);
		}
		fireStructureChanged(dirs, IModelChangedEvent.INSERT);
	}
	
	public void removeAdditionalDirectories(IAdditionalLocation[] dirs) {
		for (int i = 0; i < dirs.length; i++) {
			fAdditionalDirectories.remove(dirs[i]);
		}
		fireStructureChanged(dirs, IModelChangedEvent.REMOVE);
	}

	public void setDescription(String desc) {
		fDescription = desc;
	}

	public String getDescription() {
		return fDescription;
	}
}

Back to the top