001: /*******************************************************************************
002: * Copyright (c) 2005, 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.target;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.Set;
017: import java.util.StringTokenizer;
018: import java.util.TreeMap;
019:
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.pde.core.IModelChangedEvent;
022: import org.eclipse.pde.internal.core.ifeature.IEnvironment;
023: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
024: import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
025: import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
026: import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
027: import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
028: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
029: import org.eclipse.pde.internal.core.itarget.ITarget;
030: import org.eclipse.pde.internal.core.itarget.ITargetFeature;
031: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
032: import org.eclipse.pde.internal.core.itarget.ITargetModel;
033: import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
034: import org.eclipse.pde.internal.core.itarget.ITargetObject;
035: import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039:
040: public class Target extends TargetObject implements ITarget {
041:
042: private static final long serialVersionUID = 1L;
043: private String fName;
044: private TreeMap fPlugins = new TreeMap();
045: private TreeMap fFeatures = new TreeMap();
046: private IArgumentsInfo fArgsInfo;
047: private IEnvironmentInfo fEnvInfo;
048: private ITargetJRE fRuntimeInfo;
049: private ILocationInfo fLocationInfo;
050: private IImplicitDependenciesInfo fImplicitInfo;
051: private boolean fUseAllTargetPlatform = false;
052: private Set fAdditionalDirectories = new HashSet();
053: private String fDescription = null;
054:
055: public Target(ITargetModel model) {
056: super (model);
057: }
058:
059: public void reset() {
060: fArgsInfo = null;
061: fEnvInfo = null;
062: fRuntimeInfo = null;
063: fLocationInfo = null;
064: fImplicitInfo = null;
065: fPlugins.clear();
066: fFeatures.clear();
067: fUseAllTargetPlatform = false;
068: fAdditionalDirectories.clear();
069: }
070:
071: public void parse(Node node) {
072: if (node.getNodeType() == Node.ELEMENT_NODE
073: && node.getNodeName().equals("target")) { //$NON-NLS-1$
074: Element element = (Element) node;
075: fName = element.getAttribute(P_NAME);
076: NodeList children = node.getChildNodes();
077: ITargetModelFactory factory = getModel().getFactory();
078: for (int i = 0; i < children.getLength(); i++) {
079: Node child = children.item(i);
080: if (child.getNodeType() == Node.ELEMENT_NODE) {
081: String name = child.getNodeName();
082: if (name.equals("launcherArgs")) { //$NON-NLS-1$
083: fArgsInfo = factory.createArguments();
084: fArgsInfo.parse(child);
085: } else if (name.equals("content")) { //$NON-NLS-1$
086: parseContent((Element) child);
087: } else if (name.equals("environment")) { //$NON-NLS-1$
088: fEnvInfo = factory.createEnvironment();
089: fEnvInfo.parse(child);
090: } else if (name.equals("targetJRE")) { //$NON-NLS-1$
091: fRuntimeInfo = factory.createJREInfo();
092: fRuntimeInfo.parse(child);
093: } else if (name.equals("location")) { //$NON-NLS-1$
094: fLocationInfo = factory.createLocation();
095: fLocationInfo.parse(child);
096: } else if (name.equals("implicitDependencies")) { //$NON-NLS-1$
097: fImplicitInfo = factory
098: .createImplicitPluginInfo();
099: fImplicitInfo.parse(child);
100: }
101: }
102: }
103: }
104: }
105:
106: private void parseContent(Element content) {
107: fUseAllTargetPlatform = "true".equals(content.getAttribute("useAllPlugins")); //$NON-NLS-1$ //$NON-NLS-2$
108: NodeList children = content.getChildNodes();
109: for (int i = 0; i < children.getLength(); i++) {
110: Node node = children.item(i);
111: if ("plugins".equals(node.getNodeName())) { //$NON-NLS-1$
112: parsePlugins(node.getChildNodes());
113: } else if ("features".equals(node.getNodeName())) { //$NON-NLS-1$
114: parseFeatures(node.getChildNodes());
115: } else if ("extraLocations".equals(node.getNodeName())) { //$NON-NLS-1$
116: parseLocations(node.getChildNodes());
117: }
118: }
119: }
120:
121: private void parsePlugins(NodeList children) {
122: for (int i = 0; i < children.getLength(); i++) {
123: Node child = children.item(i);
124: if (child.getNodeType() == Node.ELEMENT_NODE) {
125: if (child.getNodeName().equals("plugin")) { //$NON-NLS-1$
126: ITargetPlugin plugin = getModel().getFactory()
127: .createPlugin();
128: plugin.parse(child);
129: fPlugins.put(plugin.getId(), plugin);
130: }
131: }
132: }
133: }
134:
135: private void parseFeatures(NodeList children) {
136: for (int i = 0; i < children.getLength(); i++) {
137: Node child = children.item(i);
138: if (child.getNodeType() == Node.ELEMENT_NODE) {
139: if (child.getNodeName().equals("feature")) { //$NON-NLS-1$
140: ITargetFeature feature = getModel().getFactory()
141: .createFeature();
142: feature.parse(child);
143: fFeatures.put(feature.getId(), feature);
144: }
145: }
146: }
147: }
148:
149: private void parseLocations(NodeList children) {
150: for (int i = 0; i < children.getLength(); i++) {
151: Node child = children.item(i);
152: if (child.getNodeType() == Node.ELEMENT_NODE) {
153: if (child.getNodeName().equals("location")) { //$NON-NLS-1$
154: IAdditionalLocation loc = getModel().getFactory()
155: .createAdditionalLocation();
156: loc.parse(child);
157: fAdditionalDirectories.add(loc);
158: }
159: }
160: }
161: }
162:
163: public void write(String indent, PrintWriter writer) {
164: writer.print(indent + "<target"); //$NON-NLS-1$
165: if (fName != null && fName.length() > 0)
166: writer
167: .print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
168: writer.println(">"); //$NON-NLS-1$
169: if (fArgsInfo != null) {
170: fArgsInfo.write(indent + " ", writer); //$NON-NLS-1$
171: }
172: if (fEnvInfo != null) {
173: fEnvInfo.write(indent + " ", writer); //$NON-NLS-1$
174: }
175: if (fRuntimeInfo != null) {
176: fRuntimeInfo.write(indent + " ", writer); //$NON-NLS-1$
177: }
178: if (fLocationInfo != null) {
179: fLocationInfo.write(indent + " ", writer); //$NON-NLS-1$
180: }
181:
182: writer.println();
183: if (fUseAllTargetPlatform) {
184: writer.println(indent
185: + " <content useAllPlugins=\"true\">"); //$NON-NLS-1$
186: } else {
187: writer.println(indent + " <content>"); //$NON-NLS-1$
188: }
189:
190: writer.println(indent + " <plugins>"); //$NON-NLS-1$
191: Iterator iter = fPlugins.values().iterator();
192: while (iter.hasNext()) {
193: ITargetPlugin plugin = (ITargetPlugin) iter.next();
194: plugin.write(indent + " ", writer); //$NON-NLS-1$
195: }
196: writer.println(indent + " </plugins>"); //$NON-NLS-1$
197: writer.println(indent + " <features>"); //$NON-NLS-1$
198: iter = fFeatures.values().iterator();
199: while (iter.hasNext()) {
200: ITargetFeature feature = (ITargetFeature) iter.next();
201: feature.write(indent + " ", writer); //$NON-NLS-1$
202: }
203: writer.println(indent + " </features>"); //$NON-NLS-1$
204: if (!fAdditionalDirectories.isEmpty()) {
205: writer.println(indent + " <extraLocations>"); //$NON-NLS-1$
206: iter = fAdditionalDirectories.iterator();
207: while (iter.hasNext()) {
208: IAdditionalLocation location = (IAdditionalLocation) iter
209: .next();
210: location.write(indent + " ", writer); //$NON-NLS-1$
211: }
212: writer.println(indent + " </extraLocations>"); //$NON-NLS-1$
213: }
214: writer.println(indent + " </content>"); //$NON-NLS-1$
215: if (fImplicitInfo != null) {
216: fImplicitInfo.write(indent + " ", writer); //$NON-NLS-1$
217: }
218: writer.println();
219: writer.println(indent + "</target>"); //$NON-NLS-1$
220: }
221:
222: public IArgumentsInfo getArguments() {
223: return fArgsInfo;
224: }
225:
226: public void setArguments(IArgumentsInfo info) {
227: fArgsInfo = info;
228: }
229:
230: public IEnvironmentInfo getEnvironment() {
231: return fEnvInfo;
232: }
233:
234: public void setEnvironment(IEnvironmentInfo info) {
235: fEnvInfo = info;
236: }
237:
238: public ITargetJRE getTargetJREInfo() {
239: return fRuntimeInfo;
240: }
241:
242: public void setTargetJREInfo(ITargetJRE info) {
243: fRuntimeInfo = info;
244:
245: }
246:
247: public String getName() {
248: return fName;
249: }
250:
251: public void setName(String name) {
252: String oldValue = fName;
253: fName = name;
254: firePropertyChanged(P_NAME, oldValue, fName);
255: }
256:
257: public ILocationInfo getLocationInfo() {
258: return fLocationInfo;
259: }
260:
261: public void setLocationInfo(ILocationInfo info) {
262: fLocationInfo = info;
263: }
264:
265: public void addPlugin(ITargetPlugin plugin) {
266: addPlugins(new ITargetPlugin[] { plugin });
267: }
268:
269: public void addPlugins(ITargetPlugin[] plugins) {
270: ArrayList list = new ArrayList();
271: for (int i = 0; i < plugins.length; i++) {
272: String id = plugins[i].getId();
273: if (fPlugins.containsKey(id))
274: continue;
275: list.add(plugins[i]);
276: plugins[i].setModel(getModel());
277: fPlugins.put(id, plugins[i]);
278: }
279: if (isEditable() && list.size() > 0) {
280: fireStructureChanged((ITargetObject[]) list
281: .toArray(new ITargetObject[list.size()]),
282: IModelChangedEvent.INSERT);
283: }
284: }
285:
286: public void addFeature(ITargetFeature feature) {
287: addFeatures(new ITargetFeature[] { feature });
288: }
289:
290: public void addFeatures(ITargetFeature[] features) {
291: ArrayList list = new ArrayList();
292: for (int i = 0; i < features.length; i++) {
293: String id = features[i].getId();
294: if (fFeatures.containsKey(id))
295: continue;
296: list.add(features[i]);
297: features[i].setModel(getModel());
298: fFeatures.put(id, features[i]);
299: }
300: if (isEditable() && list.size() > 0)
301: fireStructureChanged((ITargetObject[]) list
302: .toArray(new ITargetObject[list.size()]),
303: IModelChangedEvent.INSERT);
304: }
305:
306: public void removePlugin(ITargetPlugin plugin) {
307: removePlugins(new ITargetPlugin[] { plugin });
308: }
309:
310: public void removePlugins(ITargetPlugin[] plugins) {
311: boolean modify = false;
312: for (int i = 0; i < plugins.length; i++)
313: modify = ((fPlugins.remove(plugins[i].getId()) != null) || modify);
314: if (isEditable() && modify)
315: fireStructureChanged(plugins, IModelChangedEvent.REMOVE);
316: }
317:
318: public void removeFeature(ITargetFeature feature) {
319: removeFeatures(new ITargetFeature[] { feature });
320: }
321:
322: public void removeFeatures(ITargetFeature[] features) {
323: boolean modify = false;
324: for (int i = 0; i < features.length; i++) {
325: modify = ((fFeatures.remove(features[i].getId()) != null) || modify);
326: }
327: if (isEditable() && modify)
328: fireStructureChanged(features, IModelChangedEvent.REMOVE);
329: }
330:
331: public ITargetPlugin[] getPlugins() {
332: return (ITargetPlugin[]) fPlugins.values().toArray(
333: new ITargetPlugin[fPlugins.size()]);
334: }
335:
336: public ITargetFeature[] getFeatures() {
337: return (ITargetFeature[]) fFeatures.values().toArray(
338: new ITargetFeature[fFeatures.size()]);
339: }
340:
341: public boolean containsPlugin(String id) {
342: return fPlugins.containsKey(id);
343: }
344:
345: public boolean containsFeature(String id) {
346: return fFeatures.containsKey(id);
347: }
348:
349: public boolean useAllPlugins() {
350: return fUseAllTargetPlatform;
351: }
352:
353: public void setUseAllPlugins(boolean value) {
354: boolean oldValue = fUseAllTargetPlatform;
355: fUseAllTargetPlatform = value;
356: if (isEditable())
357: firePropertyChanged(P_ALL_PLUGINS, new Boolean(oldValue),
358: new Boolean(fUseAllTargetPlatform));
359: }
360:
361: public void setImplicitPluginsInfo(IImplicitDependenciesInfo info) {
362: fImplicitInfo = info;
363: }
364:
365: public IImplicitDependenciesInfo getImplicitPluginsInfo() {
366: return fImplicitInfo;
367: }
368:
369: public IAdditionalLocation[] getAdditionalDirectories() {
370: return (IAdditionalLocation[]) fAdditionalDirectories
371: .toArray(new IAdditionalLocation[fAdditionalDirectories
372: .size()]);
373: }
374:
375: public void addAdditionalDirectories(IAdditionalLocation[] dirs) {
376: for (int i = 0; i < dirs.length; i++) {
377: fAdditionalDirectories.add(dirs[i]);
378: }
379: fireStructureChanged(dirs, IModelChangedEvent.INSERT);
380: }
381:
382: public void removeAdditionalDirectories(IAdditionalLocation[] dirs) {
383: for (int i = 0; i < dirs.length; i++) {
384: fAdditionalDirectories.remove(dirs[i]);
385: }
386: fireStructureChanged(dirs, IModelChangedEvent.REMOVE);
387: }
388:
389: public void setDescription(String desc) {
390: fDescription = desc;
391: }
392:
393: public String getDescription() {
394: return fDescription;
395: }
396:
397: // Would be better implemented if IFeaturePlugin extends IEnvironment. Look at doing this post 3.2
398: public boolean isValidFeatureObject(Object featureObj) {
399: IEnvironment env = null;
400: IFeaturePlugin plugin = null;
401: if (featureObj instanceof IEnvironment)
402: env = (IEnvironment) featureObj;
403: else
404: plugin = (IFeaturePlugin) featureObj;
405: boolean result = true;
406:
407: String value = (env != null) ? env.getArch() : plugin.getArch();
408: if (value != null && result) {
409: String arch = (fEnvInfo != null) ? fEnvInfo.getArch()
410: : null;
411: if (arch == null || arch.length() == 0)
412: arch = Platform.getOSArch();
413: result = containsProperty(arch, value);
414: }
415:
416: value = (env != null) ? env.getOS() : plugin.getOS();
417: if (value != null && result) {
418: String os = (fEnvInfo != null) ? fEnvInfo.getOS() : null;
419: if (os == null || os.length() == 0)
420: os = Platform.getOS();
421: result = containsProperty(os, value);
422: }
423:
424: value = (env != null) ? env.getWS() : plugin.getWS();
425: if (value != null && result) {
426: String ws = (fEnvInfo != null) ? fEnvInfo.getWS() : null;
427: if (ws == null || ws.length() == 0)
428: ws = Platform.getWS();
429: result = containsProperty(ws, value);
430: }
431:
432: value = (env != null) ? env.getNL() : plugin.getNL();
433: if (value != null && result) {
434: String nl = (fEnvInfo != null) ? fEnvInfo.getNL() : null;
435: if (nl == null || nl.length() == 0)
436: nl = Platform.getNL();
437: result = containsProperty(nl, value);
438: }
439: return result;
440: }
441:
442: private boolean containsProperty(String property, String value) {
443: if (value == null || property == null)
444: return false;
445: StringTokenizer tokenizer = new StringTokenizer(value, ","); //$NON-NLS-1$
446: while (tokenizer.hasMoreTokens()) {
447: if (property.equals(tokenizer.nextToken().trim()))
448: return true;
449: }
450: return false;
451: }
452: }
|