1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.FileLocator; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtension; 23 import org.eclipse.core.runtime.IExtensionDelta; 24 import org.eclipse.core.runtime.IExtensionRegistry; 25 import org.eclipse.core.runtime.IRegistryChangeEvent; 26 import org.eclipse.core.runtime.IRegistryChangeListener; 27 import org.eclipse.core.runtime.Platform; 28 import org.osgi.framework.Bundle; 29 30 public class TargetDefinitionManager implements IRegistryChangeListener{ 31 32 Map fTargets; 33 private static String [] attributes; 34 { 35 attributes = new String [] {"id", "name" }; } 37 38 public void registryChanged(IRegistryChangeEvent event) { 39 IExtensionDelta[] deltas = event.getExtensionDeltas(); 40 for (int i = 0; i < deltas.length; i++) { 41 IExtension extension = deltas[i].getExtension(); 42 String extensionId = extension.getExtensionPointUniqueIdentifier(); 43 if (extensionId.equals("org.eclipse.pde.core.targets")) { IConfigurationElement[] elems = extension.getConfigurationElements(); 45 if (deltas[i].getKind() == IExtensionDelta.ADDED) 46 add(elems); 47 else 48 remove(elems); 49 } 50 } 51 } 52 53 public IConfigurationElement[] getTargets() { 54 if (fTargets == null) 55 loadElements(); 56 return (IConfigurationElement[])fTargets.values().toArray(new IConfigurationElement[fTargets.size()]); 57 } 58 59 public IConfigurationElement[] getSortedTargets() { 60 if (fTargets == null) 61 loadElements(); 62 IConfigurationElement[] result = (IConfigurationElement[])fTargets.values().toArray(new IConfigurationElement[fTargets.size()]); 63 Arrays.sort(result, new Comparator () { 64 65 public int compare(Object o1, Object o2) { 66 String value1 = getString((IConfigurationElement)o1); 67 String value2 = getString((IConfigurationElement)o2); 68 return value1.compareTo(value2); 69 } 70 71 private String getString(IConfigurationElement elem){ 72 String name = elem.getAttribute("name"); String id = elem.getAttribute("id"); name = name + " [" + id + "]"; return name; 76 } 77 78 }); 79 return result; 80 } 81 82 public IConfigurationElement getTarget(String id) { 83 if (fTargets == null) 84 loadElements(); 85 return (IConfigurationElement)fTargets.get(id); 86 } 87 88 private void loadElements() { 89 fTargets = new HashMap (); 90 IExtensionRegistry registry = Platform.getExtensionRegistry(); 91 registry.addRegistryChangeListener(this); 92 IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.pde.core.targets"); add(elements); 94 } 95 96 private boolean isValid (IConfigurationElement elem) { 97 String value; 98 for (int i = 0; i < attributes.length; i++) { 99 value = elem.getAttribute(attributes[i]); 100 if (value == null || value.equals("")) return false; 102 } 103 value = elem.getAttribute("definition"); String symbolicName = elem.getDeclaringExtension().getNamespaceIdentifier(); 105 URL url = getResourceURL(symbolicName, value); 106 try { 107 if (url != null && url.openStream().available() > 0) 108 return true; 109 } catch (IOException e) { 110 } 112 return false; 113 } 114 115 public static URL getResourceURL(String bundleID, String resourcePath) { 116 try { 117 Bundle bundle = Platform.getBundle(bundleID); 118 if (bundle != null && resourcePath != null) { 119 URL entry = bundle.getEntry(resourcePath); 120 if (entry != null) 121 return FileLocator.toFileURL(entry); 122 } 123 } catch (IOException e) { 124 } 125 return null; 126 } 127 128 private void add(IConfigurationElement[] elems) { 129 for (int i = 0; i < elems.length; i++) { 130 IConfigurationElement elem = elems[i]; 131 if (isValid(elem)) { 132 String id = elem.getAttribute("id"); fTargets.put(id, elem); 134 } 135 } 136 } 137 138 private void remove(IConfigurationElement[] elems) { 139 for (int i = 0 ; i < elems.length; i++) { 140 fTargets.remove(elems[i].getAttribute("id")); } 142 } 143 144 public void shutdown() { 145 IExtensionRegistry registry = Platform.getExtensionRegistry(); 146 registry.removeRegistryChangeListener(this); 147 } 148 149 } 150 | Popular Tags |