1 11 package org.eclipse.pde.internal.ui.model.bundle; 12 13 import java.util.Enumeration ; 14 15 import org.eclipse.osgi.util.ManifestElement; 16 import org.eclipse.pde.internal.core.bundle.BundleObject; 17 18 public class PackageObject extends BundleObject { 19 20 private static final long serialVersionUID = 1L; 21 22 private String fVersionAttribute; 23 private String fName; 24 private String fVersion; 25 private transient ManifestElement fElement; 26 27 private ManifestHeader fHeader; 28 29 public PackageObject(ManifestHeader header, ManifestElement element, String versionAttribute) { 30 fHeader = header; 31 fVersionAttribute = versionAttribute; 32 fName = element.getValue(); 33 fVersion = element.getAttribute(fVersionAttribute); 34 fElement = element; 35 setModel(fHeader.getBundle().getModel()); 36 } 37 38 public PackageObject(ManifestHeader header, String name, String version, String versionAttribute) { 39 fHeader = header; 40 fVersion = version; 41 fVersionAttribute = versionAttribute; 42 fName = name; 43 setModel(fHeader.getBundle().getModel()); 44 } 45 46 public String toString() { 47 StringBuffer buffer = new StringBuffer (fName); 48 if (fVersion != null && fVersion.length() > 0) { 49 buffer.append(" "); boolean wrap = Character.isDigit(fVersion.charAt(0)); 51 if (wrap) 52 buffer.append("("); buffer.append(fVersion); 54 if (wrap) 55 buffer.append(")"); } 57 return buffer.toString(); 58 } 59 60 public String write() { 61 StringBuffer buffer = new StringBuffer (); 62 buffer.append(fName); 63 64 appendSupportedAttributes(buffer); 65 66 if (fElement == null) 67 return buffer.toString(); 68 69 Enumeration attrs = fElement.getKeys(); 70 if (attrs != null) { 71 while (attrs.hasMoreElements()) { 72 String attr = attrs.nextElement().toString(); 73 if (attr.equals(fVersionAttribute)) 74 continue; 75 buffer.append(";"); buffer.append(attr); 77 buffer.append("=\""); buffer.append(fElement.getAttribute(attr)); 79 buffer.append("\""); } 81 } 82 83 Enumeration directives = fElement.getDirectiveKeys(); 84 if (directives != null) { 85 while (directives.hasMoreElements()) { 86 String directive = directives.nextElement().toString(); 87 if (skipDirective(directive)) 88 continue; 89 buffer.append(";"); buffer.append(directive); 91 buffer.append(":="); buffer.append("\""); buffer.append(fElement.getDirective(directive)); 94 buffer.append("\""); } 96 } 97 return buffer.toString(); 98 } 99 100 protected void appendSupportedAttributes(StringBuffer buffer) { 101 if (fVersion != null && fVersion.length() > 0) { 102 buffer.append(";"); buffer.append(fVersionAttribute); 104 buffer.append("=\""); buffer.append(fVersion.trim()); 106 buffer.append("\""); } 108 } 109 110 protected boolean skipDirective(String directive) { 111 return false; 112 } 113 114 public String getVersion() { 115 return fVersion; 116 } 117 118 public String getName() { 119 return fName; 120 } 121 122 public void setVersion(String version) { 123 String old = fVersion; 124 fVersion = version; 125 firePropertyChanged(this, fVersionAttribute, old, version); 126 } 127 128 public ManifestHeader getHeader() { 129 return fHeader; 130 } 131 132 } 133 | Popular Tags |