1 11 package org.eclipse.pde.internal.core.target; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 import java.util.StringTokenizer ; 19 import java.util.TreeMap ; 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 ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.NodeList ; 40 41 public class Target extends TargetObject implements ITarget { 42 43 private static final long serialVersionUID = 1L; 44 private String fName; 45 private TreeMap fPlugins = new TreeMap (); 46 private TreeMap fFeatures = new TreeMap (); 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 fAdditionalDirectories = new HashSet (); 54 private String 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 node) { 73 if (node.getNodeType() == Node.ELEMENT_NODE 74 && node.getNodeName().equals("target")) { Element element = (Element )node; 76 fName = element.getAttribute(P_NAME); 77 NodeList children = node.getChildNodes(); 78 ITargetModelFactory factory = getModel().getFactory(); 79 for (int i = 0; i < children.getLength(); i++) { 80 Node child = children.item(i); 81 if (child.getNodeType() == Node.ELEMENT_NODE) { 82 String name = child.getNodeName(); 83 if (name.equals("launcherArgs")) { fArgsInfo = factory.createArguments(); 85 fArgsInfo.parse(child); 86 } else if (name.equals("content")) { parseContent((Element )child); 88 } else if (name.equals("environment")) { fEnvInfo = factory.createEnvironment(); 90 fEnvInfo.parse(child); 91 } else if (name.equals("targetJRE")) { fRuntimeInfo = factory.createJREInfo(); 93 fRuntimeInfo.parse(child); 94 } else if (name.equals("location")) { fLocationInfo = factory.createLocation(); 96 fLocationInfo.parse(child); 97 } else if (name.equals("implicitDependencies")) { fImplicitInfo = factory.createImplicitPluginInfo(); 99 fImplicitInfo.parse(child); 100 } 101 } 102 } 103 } 104 } 105 106 private void parseContent(Element content) { 107 fUseAllTargetPlatform = 108 "true".equals(content.getAttribute("useAllPlugins")); NodeList children = content.getChildNodes(); 110 for (int i = 0; i < children.getLength(); i++) { 111 Node node = children.item(i); 112 if ("plugins".equals(node.getNodeName())) { parsePlugins(node.getChildNodes()); 114 } else if ("features".equals(node.getNodeName())) { parseFeatures(node.getChildNodes()); 116 } else if ("extraLocations".equals(node.getNodeName())) { parseLocations(node.getChildNodes()); 118 } 119 } 120 } 121 122 private void parsePlugins(NodeList children) { 123 for (int i = 0; i < children.getLength(); i++) { 124 Node child = children.item(i); 125 if (child.getNodeType() == Node.ELEMENT_NODE) { 126 if (child.getNodeName().equals("plugin")) { 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 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")) { 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 children) { 149 for (int i = 0; i < children.getLength(); i++) { 150 Node child = children.item(i); 151 if (child.getNodeType() == Node.ELEMENT_NODE) { 152 if (child.getNodeName().equals("location")) { IAdditionalLocation loc = getModel().getFactory().createAdditionalLocation(); 154 loc.parse(child); 155 fAdditionalDirectories.add(loc); 156 } 157 } 158 } 159 } 160 161 public void write(String indent, PrintWriter writer) { 162 writer.print(indent + "<target"); if (fName != null && fName.length() > 0) 164 writer.print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); writer.println(">"); if (fArgsInfo != null) { 167 fArgsInfo.write(indent + " ", writer); } 169 if (fEnvInfo != null) { 170 fEnvInfo.write(indent + " ", writer); } 172 if (fRuntimeInfo != null) { 173 fRuntimeInfo.write(indent + " ", writer); } 175 if (fLocationInfo != null) { 176 fLocationInfo.write(indent + " ", writer); } 178 179 writer.println(); 180 if (fUseAllTargetPlatform) { 181 writer.println(indent + " <content useAllPlugins=\"true\">"); } else { 183 writer.println(indent + " <content>"); } 185 186 writer.println(indent + " <plugins>"); Iterator iter = fPlugins.values().iterator(); 188 while (iter.hasNext()) { 189 ITargetPlugin plugin = (ITargetPlugin) iter.next(); 190 plugin.write(indent + " ", writer); } 192 writer.println(indent + " </plugins>"); writer.println(indent + " <features>"); iter = fFeatures.values().iterator(); 195 while (iter.hasNext()) { 196 ITargetFeature feature = (ITargetFeature) iter.next(); 197 feature.write(indent + " ", writer); } 199 writer.println(indent + " </features>"); if (!fAdditionalDirectories.isEmpty()) { 201 writer.println(indent + " <extraLocations>"); iter = fAdditionalDirectories.iterator(); 203 while (iter.hasNext()) { 204 IAdditionalLocation location = (IAdditionalLocation) iter.next(); 205 location.write(indent + " ", writer); } 207 writer.println(indent + " </extraLocations>"); } 209 writer.println(indent + " </content>"); if (fImplicitInfo != null) { 211 fImplicitInfo.write(indent + " ", writer); } 213 writer.println(); 214 writer.println(indent + "</target>"); } 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 getName() { 243 return fName; 244 } 245 246 public void setName(String name) { 247 String 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 list = new ArrayList (); 266 for (int i = 0; i < plugins.length; i ++ ) { 267 String 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 list = new ArrayList (); 286 for (int i = 0; i < features.length; i++) { 287 String 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 id) { 333 return fPlugins.containsKey(id); 334 } 335 336 public boolean containsFeature(String 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 (oldValue), new Boolean (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 desc) { 379 fDescription = desc; 380 } 381 382 public String getDescription() { 383 return fDescription; 384 } 385 386 public boolean isValidFeatureObject(Object 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 value = (env != null) ? env.getArch() : plugin.getArch(); 397 if (value != null && result) { 398 String 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 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 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 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 property, String value) { 431 if (value == null || property == null) 432 return false; 433 StringTokenizer tokenizer = new StringTokenizer (value, ","); while (tokenizer.hasMoreTokens()) { 435 if (property.equals(tokenizer.nextToken().trim())) 436 return true; 437 } 438 return false; 439 } 440 } 441 | Popular Tags |