KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > target > Target


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.target;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.TreeMap JavaDoc;
20
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.pde.core.IModelChangedEvent;
23 import org.eclipse.pde.internal.core.ifeature.IEnvironment;
24 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
25 import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
26 import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
27 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
28 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
29 import org.eclipse.pde.internal.core.itarget.ILocationInfo;
30 import org.eclipse.pde.internal.core.itarget.ITarget;
31 import org.eclipse.pde.internal.core.itarget.ITargetFeature;
32 import org.eclipse.pde.internal.core.itarget.ITargetJRE;
33 import org.eclipse.pde.internal.core.itarget.ITargetModel;
34 import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
35 import org.eclipse.pde.internal.core.itarget.ITargetObject;
36 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 public class Target extends TargetObject implements ITarget {
42
43     private static final long serialVersionUID = 1L;
44     private String JavaDoc fName;
45     private TreeMap JavaDoc fPlugins = new TreeMap JavaDoc();
46     private TreeMap JavaDoc fFeatures = new TreeMap JavaDoc();
47     private IArgumentsInfo fArgsInfo;
48     private IEnvironmentInfo fEnvInfo;
49     private ITargetJRE fRuntimeInfo;
50     private ILocationInfo fLocationInfo;
51     private IImplicitDependenciesInfo fImplicitInfo;
52     private boolean fUseAllTargetPlatform = false;
53     private Set JavaDoc fAdditionalDirectories = new HashSet JavaDoc();
54     private String JavaDoc fDescription = null;
55     
56     public Target(ITargetModel model) {
57         super(model);
58     }
59
60     public void reset() {
61         fArgsInfo = null;
62         fEnvInfo = null;
63         fRuntimeInfo = null;
64         fLocationInfo = null;
65         fImplicitInfo = null;
66         fPlugins.clear();
67         fFeatures.clear();
68         fUseAllTargetPlatform = false;
69         fAdditionalDirectories.clear();
70     }
71
72     public void parse(Node JavaDoc node) {
73         if (node.getNodeType() == Node.ELEMENT_NODE
74                 && node.getNodeName().equals("target")) { //$NON-NLS-1$
75
Element JavaDoc element = (Element JavaDoc)node;
76             fName = element.getAttribute(P_NAME);
77             NodeList JavaDoc children = node.getChildNodes();
78             ITargetModelFactory factory = getModel().getFactory();
79             for (int i = 0; i < children.getLength(); i++) {
80                 Node JavaDoc child = children.item(i);
81                 if (child.getNodeType() == Node.ELEMENT_NODE) {
82                     String JavaDoc name = child.getNodeName();
83                     if (name.equals("launcherArgs")) { //$NON-NLS-1$
84
fArgsInfo = factory.createArguments();
85                         fArgsInfo.parse(child);
86                     } else if (name.equals("content")) { //$NON-NLS-1$
87
parseContent((Element JavaDoc)child);
88                     } else if (name.equals("environment")) { //$NON-NLS-1$
89
fEnvInfo = factory.createEnvironment();
90                         fEnvInfo.parse(child);
91                     } else if (name.equals("targetJRE")) { //$NON-NLS-1$
92
fRuntimeInfo = factory.createJREInfo();
93                         fRuntimeInfo.parse(child);
94                     } else if (name.equals("location")) { //$NON-NLS-1$
95
fLocationInfo = factory.createLocation();
96                         fLocationInfo.parse(child);
97                     } else if (name.equals("implicitDependencies")) { //$NON-NLS-1$
98
fImplicitInfo = factory.createImplicitPluginInfo();
99                         fImplicitInfo.parse(child);
100                     }
101                 }
102             }
103         }
104     }
105     
106     private void parseContent(Element JavaDoc content) {
107         fUseAllTargetPlatform =
108             "true".equals(content.getAttribute("useAllPlugins")); //$NON-NLS-1$ //$NON-NLS-2$
109
NodeList JavaDoc children = content.getChildNodes();
110         for (int i = 0; i < children.getLength(); i++) {
111             Node JavaDoc node = children.item(i);
112             if ("plugins".equals(node.getNodeName())) { //$NON-NLS-1$
113
parsePlugins(node.getChildNodes());
114             } else if ("features".equals(node.getNodeName())) { //$NON-NLS-1$
115
parseFeatures(node.getChildNodes());
116             } else if ("extraLocations".equals(node.getNodeName())) { //$NON-NLS-1$
117
parseLocations(node.getChildNodes());
118             }
119         }
120     }
121     
122     private void parsePlugins(NodeList JavaDoc children) {
123         for (int i = 0; i < children.getLength(); i++) {
124             Node JavaDoc child = children.item(i);
125             if (child.getNodeType() == Node.ELEMENT_NODE) {
126                 if (child.getNodeName().equals("plugin")) { //$NON-NLS-1$
127
ITargetPlugin plugin = getModel().getFactory().createPlugin();
128                     plugin.parse(child);
129                     fPlugins.put(plugin.getId(), plugin);
130                 }
131             }
132         }
133     }
134     
135     private void parseFeatures(NodeList JavaDoc children) {
136         for (int i = 0; i < children.getLength(); i++) {
137             Node JavaDoc 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().createFeature();
141                     feature.parse(child);
142                     fFeatures.put(feature.getId(), feature);
143                 }
144             }
145         }
146     }
147     
148     private void parseLocations(NodeList JavaDoc children) {
149         for (int i = 0; i < children.getLength(); i++) {
150             Node JavaDoc child = children.item(i);
151             if (child.getNodeType() == Node.ELEMENT_NODE) {
152                 if (child.getNodeName().equals("location")) { //$NON-NLS-1$
153
IAdditionalLocation loc = getModel().getFactory().createAdditionalLocation();
154                     loc.parse(child);
155                     fAdditionalDirectories.add(loc);
156                 }
157             }
158         }
159     }
160
161     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
162         writer.print(indent + "<target"); //$NON-NLS-1$
163
if (fName != null && fName.length() > 0)
164             writer.print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
165
writer.println(">"); //$NON-NLS-1$
166
if (fArgsInfo != null) {
167             fArgsInfo.write(indent + " ", writer); //$NON-NLS-1$
168
}
169         if (fEnvInfo != null) {
170             fEnvInfo.write(indent + " ", writer); //$NON-NLS-1$
171
}
172         if (fRuntimeInfo != null) {
173             fRuntimeInfo.write(indent + " ", writer); //$NON-NLS-1$
174
}
175         if (fLocationInfo != null) {
176             fLocationInfo.write(indent + " ", writer); //$NON-NLS-1$
177
}
178         
179         writer.println();
180         if (fUseAllTargetPlatform) {
181             writer.println(indent + " <content useAllPlugins=\"true\">"); //$NON-NLS-1$
182
} else {
183             writer.println(indent + " <content>"); //$NON-NLS-1$
184
}
185         
186         writer.println(indent + " <plugins>"); //$NON-NLS-1$
187
Iterator JavaDoc iter = fPlugins.values().iterator();
188         while (iter.hasNext()) {
189             ITargetPlugin plugin = (ITargetPlugin) iter.next();
190             plugin.write(indent + " ", writer); //$NON-NLS-1$
191
}
192         writer.println(indent + " </plugins>"); //$NON-NLS-1$
193
writer.println(indent + " <features>"); //$NON-NLS-1$
194
iter = fFeatures.values().iterator();
195         while (iter.hasNext()) {
196             ITargetFeature feature = (ITargetFeature) iter.next();
197             feature.write(indent + " ", writer); //$NON-NLS-1$
198
}
199         writer.println(indent + " </features>"); //$NON-NLS-1$
200
if (!fAdditionalDirectories.isEmpty()) {
201             writer.println(indent + " <extraLocations>"); //$NON-NLS-1$
202
iter = fAdditionalDirectories.iterator();
203             while (iter.hasNext()) {
204                 IAdditionalLocation location = (IAdditionalLocation) iter.next();
205                 location.write(indent + " ", writer); //$NON-NLS-1$
206
}
207             writer.println(indent + " </extraLocations>"); //$NON-NLS-1$
208
}
209         writer.println(indent + " </content>"); //$NON-NLS-1$
210
if (fImplicitInfo != null) {
211             fImplicitInfo.write(indent + " ", writer); //$NON-NLS-1$
212
}
213         writer.println();
214         writer.println(indent + "</target>"); //$NON-NLS-1$
215
}
216     
217     public IArgumentsInfo getArguments() {
218         return fArgsInfo;
219     }
220     
221     public void setArguments(IArgumentsInfo info) {
222         fArgsInfo = info;
223     }
224
225     public IEnvironmentInfo getEnvironment() {
226         return fEnvInfo;
227     }
228
229     public void setEnvironment(IEnvironmentInfo info) {
230         fEnvInfo = info;
231     }
232
233     public ITargetJRE getTargetJREInfo() {
234         return fRuntimeInfo;
235     }
236
237     public void setTargetJREInfo(ITargetJRE info) {
238         fRuntimeInfo = info;
239         
240     }
241
242     public String JavaDoc getName() {
243         return fName;
244     }
245
246     public void setName(String JavaDoc name) {
247         String JavaDoc oldValue = fName;
248         fName = name;
249         firePropertyChanged(P_NAME, oldValue, fName);
250     }
251
252     public ILocationInfo getLocationInfo() {
253         return fLocationInfo;
254     }
255
256     public void setLocationInfo(ILocationInfo info) {
257         fLocationInfo = info;
258     }
259
260     public void addPlugin(ITargetPlugin plugin) {
261         addPlugins(new ITargetPlugin[] {plugin});
262     }
263     
264     public void addPlugins(ITargetPlugin[] plugins) {
265         ArrayList JavaDoc list = new ArrayList JavaDoc();
266         for (int i = 0; i < plugins.length; i ++ ) {
267             String JavaDoc id = plugins[i].getId();
268             if (fPlugins.containsKey(id))
269                 continue;
270             list.add(plugins[i]);
271             plugins[i].setModel(getModel());
272             fPlugins.put(id, plugins[i]);
273         }
274         if (isEditable() && list.size() > 0) {
275             fireStructureChanged((ITargetObject[])list.toArray(new ITargetObject[list.size()]),
276                                 IModelChangedEvent.INSERT);
277         }
278     }
279
280     public void addFeature(ITargetFeature feature) {
281         addFeatures(new ITargetFeature[] {feature});
282     }
283     
284     public void addFeatures(ITargetFeature[] features) {
285         ArrayList JavaDoc list = new ArrayList JavaDoc();
286         for (int i = 0; i < features.length; i++) {
287             String JavaDoc id = features[i].getId();
288             if (fFeatures.containsKey(id))
289                 continue;
290             list.add(features[i]);
291             features[i].setModel(getModel());
292             fFeatures.put(id, features[i]);
293         }
294         if (isEditable() && list.size() > 0)
295             fireStructureChanged((ITargetObject[])list.toArray(new ITargetObject[list.size()]),
296                     IModelChangedEvent.INSERT);
297     }
298
299     public void removePlugin(ITargetPlugin plugin) {
300         removePlugins(new ITargetPlugin[] {plugin});
301     }
302     
303     public void removePlugins(ITargetPlugin[] plugins) {
304         boolean modify = false;
305         for (int i =0; i < plugins.length; i++)
306             modify = ((fPlugins.remove(plugins[i].getId()) != null) || modify);
307         if (isEditable() && modify)
308             fireStructureChanged(plugins, IModelChangedEvent.REMOVE);
309     }
310
311     public void removeFeature(ITargetFeature feature) {
312         removeFeatures(new ITargetFeature[] {feature});
313     }
314     
315     public void removeFeatures(ITargetFeature[] features) {
316         boolean modify = false;
317         for (int i = 0; i < features.length; i++) {
318             modify = ((fFeatures.remove(features[i].getId()) != null) || modify);
319         }
320         if (isEditable() && modify)
321             fireStructureChanged(features, IModelChangedEvent.REMOVE);
322     }
323
324     public ITargetPlugin[] getPlugins() {
325         return (ITargetPlugin[]) fPlugins.values().toArray(new ITargetPlugin[fPlugins.size()]);
326     }
327
328     public ITargetFeature[] getFeatures() {
329         return (ITargetFeature[]) fFeatures.values().toArray(new ITargetFeature[fFeatures.size()]);
330     }
331
332     public boolean containsPlugin(String JavaDoc id) {
333         return fPlugins.containsKey(id);
334     }
335
336     public boolean containsFeature(String JavaDoc id) {
337         return fFeatures.containsKey(id);
338     }
339
340     public boolean useAllPlugins() {
341         return fUseAllTargetPlatform;
342     }
343
344     public void setUseAllPlugins(boolean value) {
345         boolean oldValue = fUseAllTargetPlatform;
346         fUseAllTargetPlatform = value;
347         if (isEditable())
348             firePropertyChanged(P_ALL_PLUGINS, new Boolean JavaDoc(oldValue), new Boolean JavaDoc(fUseAllTargetPlatform));
349     }
350
351     public void setImplicitPluginsInfo(IImplicitDependenciesInfo info) {
352         fImplicitInfo = info;
353     }
354
355     public IImplicitDependenciesInfo getImplicitPluginsInfo() {
356         return fImplicitInfo;
357     }
358
359     public IAdditionalLocation[] getAdditionalDirectories() {
360         return (IAdditionalLocation[])
361             fAdditionalDirectories.toArray(new IAdditionalLocation[fAdditionalDirectories.size()]);
362     }
363
364     public void addAdditionalDirectories(IAdditionalLocation[] dirs) {
365         for (int i = 0; i < dirs.length; i++) {
366             fAdditionalDirectories.add(dirs[i]);
367         }
368         fireStructureChanged(dirs, IModelChangedEvent.INSERT);
369     }
370     
371     public void removeAdditionalDirectories(IAdditionalLocation[] dirs) {
372         for (int i = 0; i < dirs.length; i++) {
373             fAdditionalDirectories.remove(dirs[i]);
374         }
375         fireStructureChanged(dirs, IModelChangedEvent.REMOVE);
376     }
377
378     public void setDescription(String JavaDoc desc) {
379         fDescription = desc;
380     }
381
382     public String JavaDoc getDescription() {
383         return fDescription;
384     }
385     
386     // Would be better implemented if IFeaturePlugin extends IEnvironment. Look at doing this post 3.2
387
public boolean isValidFeatureObject(Object JavaDoc featureObj) {
388         IEnvironment env = null;
389         IFeaturePlugin plugin = null;
390         if (featureObj instanceof IEnvironment)
391             env = (IEnvironment) featureObj;
392         else
393             plugin = (IFeaturePlugin) featureObj;
394         boolean result = true;
395         
396         String JavaDoc value = (env != null) ? env.getArch() : plugin.getArch();
397         if (value != null && result) {
398             String JavaDoc arch = (fEnvInfo != null) ? fEnvInfo.getArch() : null;
399             if (arch == null || arch.length() == 0)
400                 arch = Platform.getOSArch();
401             result = containsProperty(arch, value);
402         }
403         
404         value = (env != null) ? env.getOS() : plugin.getOS();
405         if (value != null && result) {
406             String JavaDoc os = (fEnvInfo != null) ? fEnvInfo.getOS() : null;
407             if (os == null || os.length() == 0)
408                 os = Platform.getOS();
409             result = containsProperty(os, value);
410         }
411         
412         value = (env != null) ? env.getWS() : plugin.getWS();
413         if (value != null && result) {
414             String JavaDoc ws = (fEnvInfo != null) ? fEnvInfo.getWS() : null;
415             if (ws == null || ws.length() == 0)
416                 ws = Platform.getWS();
417             result = containsProperty(ws, value);
418         }
419         
420         value = (env != null) ? env.getNL() : plugin.getNL();
421         if (value != null && result) {
422             String JavaDoc nl = (fEnvInfo != null) ? fEnvInfo.getNL() : null;
423             if (nl == null || nl.length() == 0)
424                 nl = Platform.getNL();
425             result = containsProperty(nl, value);
426         }
427         return result;
428     }
429     
430     private boolean containsProperty(String JavaDoc property, String JavaDoc value) {
431         if (value == null || property == null)
432             return false;
433         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
434
while (tokenizer.hasMoreTokens()) {
435             if (property.equals(tokenizer.nextToken().trim()))
436                 return true;
437         }
438         return false;
439     }
440 }
441
Popular Tags