1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.util.ArrayList ; 14 import java.util.Locale ; 15 16 import javax.xml.parsers.FactoryConfigurationError ; 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.osgi.service.resolver.BundleDescription; 23 import org.eclipse.osgi.service.resolver.BundleSpecification; 24 import org.eclipse.osgi.service.resolver.VersionRange; 25 import org.eclipse.pde.core.IModelChangedEvent; 26 import org.eclipse.pde.core.plugin.IMatchRules; 27 import org.eclipse.pde.core.plugin.IPluginBase; 28 import org.eclipse.pde.core.plugin.IPluginImport; 29 import org.eclipse.pde.core.plugin.IPluginLibrary; 30 import org.eclipse.pde.internal.core.PDECoreMessages; 31 import org.eclipse.pde.internal.core.PDEState; 32 import org.eclipse.pde.internal.core.PDEStateHelper; 33 import org.osgi.framework.Version; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.xml.sax.SAXException ; 37 38 public abstract class PluginBase extends AbstractExtensions implements IPluginBase { 39 private static final Version maxVersion = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); 40 41 private ArrayList fLibraries = new ArrayList (); 42 private ArrayList fImports = new ArrayList (); 43 private String fProviderName; 44 private String fId; 45 private String fVersion; 46 private boolean fHasBundleStructure; 47 48 public void add(IPluginLibrary library) throws CoreException { 49 ensureModelEditable(); 50 fLibraries.add(library); 51 ((PluginLibrary) library).setInTheModel(true); 52 ((PluginLibrary) library).setParent(this); 53 fireStructureChanged(library, IModelChangedEvent.INSERT); 54 } 55 public void add(IPluginImport iimport) throws CoreException { 56 ensureModelEditable(); 57 ((PluginImport) iimport).setInTheModel(true); 58 ((PluginImport) iimport).setParent(this); 59 fImports.add(iimport); 60 fireStructureChanged(iimport, IModelChangedEvent.INSERT); 61 } 62 public void add(IPluginImport[] iimports) throws CoreException { 63 ensureModelEditable(); 64 for (int i = 0; i < iimports.length; i++) { 65 ((PluginImport) iimports[i]).setInTheModel(true); 66 ((PluginImport) iimports[i]).setParent(this); 67 fImports.add(iimports[i]); 68 } 69 fireStructureChanged(iimports, IModelChangedEvent.INSERT); 70 } 71 public IPluginLibrary[] getLibraries() { 72 return (IPluginLibrary[])fLibraries.toArray(new IPluginLibrary[fLibraries.size()]); 73 } 74 public IPluginImport[] getImports() { 75 return (IPluginImport[])fImports.toArray(new IPluginImport[fImports.size()]); 76 } 77 public IPluginBase getPluginBase() { 78 return this; 79 } 80 public String getProviderName() { 81 return fProviderName; 82 } 83 public String getVersion() { 84 return fVersion; 85 } 86 public String getId() { 87 return fId; 88 } 89 90 void load(BundleDescription bundleDesc, PDEState state) { 91 fId = bundleDesc.getSymbolicName(); 92 fVersion = bundleDesc.getVersion().toString(); 93 fName = state.getPluginName(bundleDesc.getBundleId()); 94 fProviderName = state.getProviderName(bundleDesc.getBundleId()); 95 fSchemaVersion = state.getSchemaVersion(bundleDesc.getBundleId()); 96 fHasBundleStructure = state.hasBundleStructure(bundleDesc.getBundleId()); 97 loadRuntime(bundleDesc, state); 98 loadImports(bundleDesc); 99 loadExtensionPoints(state.getExtensionPoints(bundleDesc.getBundleId())); 100 loadExtensions(state.getExtensions(bundleDesc.getBundleId())); 101 } 102 103 void loadExtensions(Node [] list) { 104 fExtensions = new ArrayList (); 105 for (int i = 0; i < list.length; i++) { 106 PluginExtension extension = new PluginExtension(); 107 extension.setInTheModel(true); 108 extension.setModel(getModel()); 109 extension.setParent(this); 110 extension.load(list[i]); 111 fExtensions.add(extension); 112 } 113 } 114 115 void loadExtensionPoints(Node [] list) { 116 fExtensionPoints = new ArrayList (list.length); 117 for (int i = 0; i < list.length; i++) { 118 PluginExtensionPoint extPoint = new PluginExtensionPoint(); 119 extPoint.setInTheModel(true); 120 extPoint.setModel(getModel()); 121 extPoint.setParent(this); 122 extPoint.load(list[i]); 123 fExtensionPoints.add(extPoint); 124 } 125 } 126 127 public void restoreProperty(String name, Object oldValue, Object newValue) 128 throws CoreException { 129 if (name.equals(P_ID)) { 130 setId(newValue != null ? newValue.toString() : null); 131 return; 132 } 133 if (name.equals(P_VERSION)) { 134 setVersion(newValue != null ? newValue.toString() : null); 135 return; 136 } 137 if (name.equals(P_PROVIDER)) { 138 setProviderName(newValue != null ? newValue.toString() : null); 139 return; 140 } 141 if (name.equals(P_LIBRARY_ORDER)) { 142 swap((IPluginLibrary) oldValue, (IPluginLibrary) newValue); 143 return; 144 } 145 super.restoreProperty(name, oldValue, newValue); 146 } 147 148 void load(Node node, String schemaVersion) { 149 if (node == null) 150 return; 151 fSchemaVersion = schemaVersion; 152 fId = getNodeAttribute(node, "id"); fName = getNodeAttribute(node, "name"); fProviderName = getNodeAttribute(node, "provider-name"); fVersion = getNodeAttribute(node, "version"); 157 NodeList children = node.getChildNodes(); 158 for (int i = 0; i < children.getLength(); i++) { 159 Node child = children.item(i); 160 if (child.getNodeType() == Node.ELEMENT_NODE) { 161 processChild(child); 162 } 163 } 164 } 165 166 void loadRuntime(BundleDescription description, PDEState state) { 167 String [] libraryNames = state.getLibraryNames(description.getBundleId()); 168 for (int i = 0; i < libraryNames.length; i++) { 169 PluginLibrary library = new PluginLibrary(); 170 library.setModel(getModel()); 171 library.setInTheModel(true); 172 library.setParent(this); 173 library.load(libraryNames[i]); 174 fLibraries.add(library); 175 } 176 } 177 178 void loadRuntime(Node node) { 179 NodeList children = node.getChildNodes(); 180 for (int i = 0; i < children.getLength(); i++) { 181 Node child = children.item(i); 182 if (child.getNodeType() == Node.ELEMENT_NODE 183 && child.getNodeName().toLowerCase(Locale.ENGLISH).equals("library")) { PluginLibrary library = new PluginLibrary(); 185 library.setModel(getModel()); 186 library.setInTheModel(true); 187 library.setParent(this); 188 fLibraries.add(library); 189 library.load(child); 190 } 191 } 192 } 193 194 void loadImports(BundleDescription description) { 195 BundleSpecification[] required = description.getRequiredBundles(); 196 for (int i = 0; i < required.length; i++) { 197 PluginImport importElement = new PluginImport(); 198 importElement.setModel(getModel()); 199 importElement.setInTheModel(true); 200 importElement.setParent(this); 201 fImports.add(importElement); 202 importElement.load(required[i]); 203 } 204 BundleDescription[] imported = PDEStateHelper.getImportedBundles(description); 205 for (int i = 0; i < imported.length; i++) { 206 PluginImport importElement = new PluginImport(); 207 importElement.setModel(getModel()); 208 importElement.setInTheModel(true); 209 importElement.setParent(this); 210 fImports.add(importElement); 211 importElement.load(imported[i]); 212 } 213 } 214 215 void loadImports(Node node) { 216 NodeList children = node.getChildNodes(); 217 for (int i = 0; i < children.getLength(); i++) { 218 Node child = children.item(i); 219 if (child.getNodeType() == Node.ELEMENT_NODE 220 && child.getNodeName().toLowerCase(Locale.ENGLISH).equals("import")) { PluginImport importElement = new PluginImport(); 222 importElement.setModel(getModel()); 223 importElement.setInTheModel(true); 224 importElement.setParent(this); 225 fImports.add(importElement); 226 importElement.load(child); 227 } 228 } 229 } 230 protected void processChild(Node child) { 231 String name = child.getNodeName().toLowerCase(Locale.ENGLISH); 232 if (name.equals("runtime")) { loadRuntime(child); 234 } else if (name.equals("requires")) { loadImports(child); 236 } else { 237 super.processChild(child); 238 } 239 } 240 241 public void remove(IPluginLibrary library) throws CoreException { 242 ensureModelEditable(); 243 fLibraries.remove(library); 244 ((PluginLibrary) library).setInTheModel(false); 245 fireStructureChanged(library, IModelChangedEvent.REMOVE); 246 } 247 public void remove(IPluginImport iimport) throws CoreException { 248 ensureModelEditable(); 249 fImports.remove(iimport); 250 ((PluginImport) iimport).setInTheModel(false); 251 fireStructureChanged(iimport, IModelChangedEvent.REMOVE); 252 } 253 public void remove(IPluginImport[] iimports) throws CoreException { 254 ensureModelEditable(); 255 for (int i = 0; i < iimports.length; i++){ 256 fImports.remove(iimports[i]); 257 ((PluginImport) iimports[i]).setInTheModel(false); 258 } 259 fireStructureChanged(iimports, IModelChangedEvent.REMOVE); 260 } 261 public void reset() { 262 fLibraries = new ArrayList (); 263 fImports = new ArrayList (); 264 fProviderName = null; 265 fSchemaVersion = null; 266 fVersion = ""; fName = ""; fId = ""; if (getModel() != null && getModel().getUnderlyingResource() != null) { 270 fId = getModel().getUnderlyingResource().getProject().getName(); 271 fName = fId; 272 fVersion = "0.0.0"; } 274 super.reset(); 275 } 276 277 public void setProviderName(String providerName) throws CoreException { 278 ensureModelEditable(); 279 String oldValue = fProviderName; 280 fProviderName = providerName; 281 firePropertyChanged(P_PROVIDER, oldValue, fProviderName); 282 } 283 284 public void setVersion(String newVersion) throws CoreException { 285 ensureModelEditable(); 286 String oldValue = fVersion; 287 fVersion = newVersion; 288 firePropertyChanged(P_VERSION, oldValue, fVersion); 289 } 290 291 public void setId(String newId) throws CoreException { 292 ensureModelEditable(); 293 String oldValue = fId; 294 fId = newId; 295 firePropertyChanged(P_ID, oldValue, fId); 296 } 297 298 public void internalSetVersion(String newVersion) { 299 fVersion = newVersion; 300 } 301 302 public void swap(IPluginLibrary l1, IPluginLibrary l2) 303 throws CoreException { 304 ensureModelEditable(); 305 int index1 = fLibraries.indexOf(l1); 306 int index2 = fLibraries.indexOf(l2); 307 if (index1 == -1 || index2 == -1) 308 throwCoreException(PDECoreMessages.PluginBase_librariesNotFoundException); 309 fLibraries.set(index2, l1); 310 fLibraries.set(index1, l2); 311 firePropertyChanged(this, P_LIBRARY_ORDER, l1, l2); 312 } 313 314 317 public void swap(IPluginImport import1, IPluginImport import2) 318 throws CoreException { 319 ensureModelEditable(); 320 int index1 = fImports.indexOf(import1); 321 int index2 = fImports.indexOf(import2); 322 if (index1 == -1 || index2 == -1) 323 throwCoreException(PDECoreMessages.PluginBase_importsNotFoundException); 324 fImports.set(index2, import1); 325 fImports.set(index1, import2); 326 firePropertyChanged(this, P_IMPORT_ORDER, import1, import2); 327 } 328 329 public boolean isValid() { 330 return hasRequiredAttributes(); 331 } 332 protected boolean hasRequiredAttributes(){ 333 if (fName==null) return false; 334 if (fId==null) return false; 335 if (fVersion==null) return false; 336 337 for (int i = 0; i < fLibraries.size(); i++) { 339 IPluginLibrary library = (IPluginLibrary)fLibraries.get(i); 340 if (!library.isValid()) return false; 341 } 342 for (int i = 0; i < fImports.size(); i++) { 344 IPluginImport iimport = (IPluginImport)fImports.get(i); 345 if (!iimport.isValid()) return false; 346 } 347 return super.hasRequiredAttributes(); 348 } 349 350 protected SAXParser getSaxParser() throws ParserConfigurationException , SAXException , FactoryConfigurationError { 351 return SAXParserFactory.newInstance().newSAXParser(); 352 } 353 354 public static int getMatchRule(VersionRange versionRange) { 355 if (versionRange == null || versionRange.getMinimum() == null) 356 return IMatchRules.NONE; 357 358 Version minimum = versionRange.getMinimum(); 359 Version maximum = versionRange.getMaximum() == null ? maxVersion : versionRange.getMaximum(); 360 361 if (maximum.compareTo(maxVersion) >= 0) 362 return IMatchRules.GREATER_OR_EQUAL; 363 else if (minimum.equals(maximum)) 364 return IMatchRules.PERFECT; 365 else if (!versionRange.isIncluded(minimum) || versionRange.isIncluded(maximum)) 366 return IMatchRules.NONE; else if (minimum.getMajor() == maximum.getMajor() - 1) 368 return IMatchRules.COMPATIBLE; 369 else if (minimum.getMajor() != maximum.getMajor()) 370 return IMatchRules.NONE; else if (minimum.getMinor() == maximum.getMinor() - 1) 372 return IMatchRules.EQUIVALENT; 373 else if (minimum.getMinor() != maximum.getMinor()) 374 return IMatchRules.NONE; else if (minimum.getMicro() == maximum.getMicro() - 1) 376 return IMatchRules.PERFECT; 378 return IMatchRules.NONE; } 380 381 public boolean hasBundleStructure() { 382 return fHasBundleStructure; 383 } 384 385 } 386 | Popular Tags |