1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.io.PrintWriter ; 14 import java.io.Serializable ; 15 import java.util.Locale ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.osgi.service.resolver.BundleDescription; 19 import org.eclipse.osgi.service.resolver.BundleSpecification; 20 import org.eclipse.osgi.service.resolver.VersionRange; 21 import org.eclipse.osgi.util.ManifestElement; 22 import org.eclipse.pde.core.plugin.IMatchRules; 23 import org.eclipse.pde.core.plugin.IPluginImport; 24 import org.eclipse.pde.core.plugin.IPluginModelBase; 25 import org.eclipse.pde.core.plugin.IPluginObject; 26 import org.eclipse.pde.core.plugin.ISharedPluginModel; 27 import org.eclipse.pde.internal.core.ICoreConstants; 28 import org.eclipse.pde.internal.core.bundle.BundlePluginBase; 29 import org.eclipse.pde.internal.core.ibundle.IBundle; 30 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 31 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 32 import org.eclipse.pde.internal.core.ibundle.IManifestHeader; 33 import org.eclipse.pde.internal.core.text.bundle.ManifestHeader; 34 import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject; 35 import org.osgi.framework.Constants; 36 import org.w3c.dom.Node ; 37 38 public class PluginImport extends IdentifiablePluginObject implements 39 IPluginImport, Serializable { 40 41 private static final long serialVersionUID = 1L; 42 private int match = NONE; 43 private boolean reexported = false; 44 private boolean optional = false; 45 private String version; 46 47 public PluginImport() { 48 } 49 50 public PluginImport(ISharedPluginModel model, String id) { 51 try { 52 setModel(model); 53 ensureModelEditable(); 54 this.fID = id; 55 } catch (CoreException e) { 56 } 57 } 58 59 public boolean isValid() { 60 return getId()!=null; 61 } 62 63 public int getMatch() { 64 return match; 65 } 66 67 public String getVersion() { 68 return version; 69 } 70 71 public boolean isReexported() { 72 return reexported; 73 } 74 75 public boolean isOptional() { 76 return optional; 77 } 78 79 public void load(BundleDescription description) { 80 this.fID = description.getSymbolicName(); 81 } 82 83 public void load(ManifestElement element, int bundleManifestVersion) { 84 this.fID = element.getValue(); 85 if (bundleManifestVersion >= 2) { 86 this.optional = Constants.RESOLUTION_OPTIONAL.equals(element.getDirective(Constants.RESOLUTION_DIRECTIVE)); 87 this.reexported = Constants.VISIBILITY_REEXPORT.equals(element.getDirective(Constants.VISIBILITY_DIRECTIVE)); 88 } 89 else { 90 this.optional = "true".equals(element.getAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE)); this.reexported ="true".equals(element.getAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE)); } 93 String bundleVersion = element.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE); 94 if (bundleVersion != null) { 95 try { 96 VersionRange versionRange = new VersionRange(bundleVersion); 97 this.version = bundleVersion; 98 this.match = PluginBase.getMatchRule(versionRange); 99 } catch (IllegalArgumentException e) { 100 } 101 } 102 } 103 104 public void load(BundleSpecification importModel) { 105 this.fID = importModel.getName(); 106 this.reexported = importModel.isExported(); 107 this.optional = importModel.isOptional(); 108 VersionRange versionRange = importModel.getVersionRange(); 109 if (versionRange == null || VersionRange.emptyRange.equals(versionRange)) { 110 this.version = null; 111 match = IMatchRules.NONE; 112 } else { 113 this.version = versionRange.getMinimum() != null ? versionRange.getMinimum().toString() : null; 114 match = PluginBase.getMatchRule(versionRange); 115 } 116 } 117 118 public boolean equals(Object obj) { 119 if (obj == this) 120 return true; 121 if (obj == null) 122 return false; 123 if (obj instanceof IPluginImport) { 124 IPluginImport target = (IPluginImport) obj; 125 if (target.getModel().equals(getModel())) 128 return false; 129 130 if (target.getId().equals(getId()) 131 && target.isReexported() == isReexported() 132 && stringEqualWithNull(target.getVersion(),getVersion()) 133 && target.getMatch() == getMatch() 134 && target.isOptional() == isOptional()) 135 return true; 136 } 137 return false; 138 } 139 140 void load(Node node) { 141 String id = getNodeAttribute(node, "plugin"); String export = getNodeAttribute(node, "export"); String option = getNodeAttribute(node, "optional"); String version = getNodeAttribute(node, "version"); String match = getNodeAttribute(node, "match"); boolean reexport = 147 export != null && export.toLowerCase(Locale.ENGLISH).equals("true"); boolean optional = 149 option != null && option.toLowerCase(Locale.ENGLISH).equals("true"); this.match = NONE; 151 if (match != null) { 152 String lmatch = match.toLowerCase(Locale.ENGLISH); 153 if (lmatch.equals("exact")) lmatch = RULE_EQUIVALENT; 155 for (int i = 0; i < RULE_NAME_TABLE.length; i++) { 156 if (lmatch.equals(RULE_NAME_TABLE[i])) { 157 this.match = i; 158 break; 159 } 160 } 161 } 162 this.version = version; 163 this.fID = id; 164 this.reexported = reexport; 165 this.optional = optional; 166 } 167 public void setMatch(int match) throws CoreException { 168 ensureModelEditable(); 169 Integer oldValue = new Integer (this.match); 170 this.match = match; 171 firePropertyChanged(P_MATCH, oldValue, new Integer (match)); 172 } 173 public void setReexported(boolean value) throws CoreException { 174 ensureModelEditable(); 175 Boolean oldValue = new Boolean (reexported); 176 this.reexported = value; 177 firePropertyChanged(P_REEXPORTED, oldValue, new Boolean (value)); 178 } 179 public void setOptional(boolean value) throws CoreException { 180 ensureModelEditable(); 181 Boolean oldValue = new Boolean (this.optional); 182 this.optional = value; 183 firePropertyChanged(P_OPTIONAL, oldValue, new Boolean (value)); 184 } 185 public void setVersion(String version) throws CoreException { 186 ensureModelEditable(); 187 String oldValue = this.version; 188 this.version = version; 189 firePropertyChanged(P_VERSION, oldValue, version); 190 } 191 192 public void restoreProperty(String name, Object oldValue, Object newValue) 193 throws CoreException { 194 if (name.equals(P_MATCH)) { 195 setMatch(((Integer ) newValue).intValue()); 196 return; 197 } 198 if (name.equals(P_REEXPORTED)) { 199 setReexported(((Boolean ) newValue).booleanValue()); 200 return; 201 } 202 if (name.equals(P_OPTIONAL)) { 203 setOptional(((Boolean ) newValue).booleanValue()); 204 return; 205 } 206 if (name.equals(P_VERSION)) { 207 setVersion(newValue != null ? newValue.toString() : null); 208 return; 209 } 210 super.restoreProperty(name, oldValue, newValue); 211 } 212 213 216 public void write(String indent, PrintWriter writer) { 217 224 IPluginModelBase modelBase = getPluginModel(); 226 if ((modelBase instanceof IBundlePluginModelBase) == false) { 228 writer.print(indent); 229 writer.print("<import plugin=\"" + getId() + "\""); if (isReexported()) 231 writer.print(" export=\"true\""); if (isOptional()) 233 writer.print(" optional=\"true\""); if (version != null && version.length() > 0) 235 writer.print(" version=\"" + version + "\""); if (match != NONE && match != COMPATIBLE) { 237 String matchValue = RULE_NAME_TABLE[match]; 238 writer.print(" match=\"" + matchValue + "\""); } 240 writer.println("/>"); return; 242 } 243 IBundleModel bundleModel = ((IBundlePluginModelBase)modelBase).getBundleModel(); 244 if (bundleModel == null) { 246 return; 247 } 248 IBundle bundle = bundleModel.getBundle(); 250 IManifestHeader manifestHeader = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE); 252 if ((manifestHeader instanceof ManifestHeader) == false) { 255 return; 256 } 257 ManifestHeader header = (ManifestHeader)manifestHeader; 258 RequireBundleObject element = new RequireBundleObject(header, fID); 261 int bundleManifestVersion = BundlePluginBase.getBundleManifestVersion(bundle); 263 if (optional) { 266 if (bundleManifestVersion > 1) { 267 element.setDirective(Constants.RESOLUTION_DIRECTIVE, Constants.RESOLUTION_OPTIONAL); 268 } else { 269 element.setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); } 271 } 272 if (reexported) { 274 if (bundleManifestVersion > 1) { 275 element.setDirective(Constants.VISIBILITY_DIRECTIVE, Constants.VISIBILITY_REEXPORT); 276 } else { 277 element.setAttribute(ICoreConstants.REPROVIDE_ATTRIBUTE, "true"); } 279 } 280 if ((version != null) && 282 (version.trim().length() > 0)) { 283 element.setAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE, version.trim()); 284 } 285 writer.print(element.write()); 287 } 288 289 292 public void reconnect(ISharedPluginModel model, IPluginObject parent) { 293 super.reconnect(model, parent); 294 } 296 297 300 public void writeDelimeter(PrintWriter writer) { 301 writer.println(','); 302 writer.print(' '); 303 } 304 305 } 306 | Popular Tags |