1 11 package org.eclipse.pde.internal.ui.model.bundle; 12 13 import java.util.*; 14 import java.util.jar.*; 15 import org.eclipse.jface.text.*; 16 import org.eclipse.pde.internal.core.*; 17 import org.eclipse.pde.internal.core.ibundle.*; 18 import org.eclipse.pde.internal.ui.model.*; 19 import org.osgi.framework.*; 20 21 public class Bundle implements IBundle { 22 23 private BundleModel fModel; 24 private Hashtable fDocumentHeaders = new Hashtable(); 25 26 public Bundle(BundleModel model) { 27 fModel = model; 28 } 29 30 public void clearHeaders() { 31 fDocumentHeaders.clear(); 32 } 33 34 public void load(Manifest manifest) { 35 Map attributes = manifest.getMainAttributes(); 36 Iterator iter = attributes.keySet().iterator(); 37 while (iter.hasNext()) { 38 Attributes.Name key = (Attributes.Name) iter.next(); 39 String value = (String )attributes.get(key); 40 ManifestHeader header = createHeader(key.toString(), value); 41 fDocumentHeaders.put(key.toString(), header); 42 } 43 adjustOffsets(fModel.getDocument()); 44 } 45 46 public void clearOffsets() { 47 Iterator iter = fDocumentHeaders.values().iterator(); 48 while (iter.hasNext()) { 49 ManifestHeader header = (ManifestHeader)iter.next(); 50 header.setOffset(-1); 51 header.setLength(-1); 52 } 53 } 54 55 public void trim() { 56 Iterator iter = fDocumentHeaders.keySet().iterator(); 57 while (iter.hasNext()) { 58 String key = iter.next().toString(); 59 ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key); 60 if (header.getOffset() == -1) 61 fDocumentHeaders.remove(key); 62 } 63 } 64 65 protected void adjustOffsets(IDocument document) { 66 int lines = document.getNumberOfLines(); 67 try { 68 IDocumentKey currentKey = null; 69 for (int i = 0; i < lines; i++) { 70 int offset = document.getLineOffset(i); 71 int length = document.getLineLength(i); 72 String line = document.get(offset, length); 73 74 if (currentKey != null) { 75 int lineNumber = line.startsWith(" ") ? i : i - 1; IRegion region = document.getLineInformation(lineNumber); 77 String delimiter = document.getLineDelimiter(lineNumber); 78 int keyLength = region.getOffset() + region.getLength() - currentKey.getOffset(); 79 currentKey.setLength(delimiter != null ? keyLength + delimiter.length() : keyLength); 80 if (!line.startsWith(" ")) { currentKey = null; 82 } 83 } 84 85 if (currentKey == null) { 86 int index = line.indexOf(':'); 87 String name = (index != -1) ? line.substring(0, index) : line; 88 currentKey = (IDocumentKey)fDocumentHeaders.get(name); 89 if (currentKey != null) { 90 IRegion region = document.getLineInformation(i); 91 currentKey.setOffset(region.getOffset()); 92 String delimiter = document.getLineDelimiter(i); 93 currentKey.setLength(delimiter != null ? region.getLength() + delimiter.length() : region.getLength()); 94 } 95 } 96 } 97 } catch (BadLocationException e) { 98 } 99 } 100 103 public void setHeader(String key, String value) { 104 ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key); 105 String old = header == null ? null : header.getValue(); 106 if (header == null) { 107 header = createHeader(key, value); 108 } else { 109 header.setValue(value); 110 } 111 fDocumentHeaders.put(key, header); 112 113 fModel.fireModelObjectChanged(header, key, old, value); 114 } 115 116 private ManifestHeader createHeader(String key, String value) { 117 ManifestHeader header = null; 118 if (key.equals(Constants.EXPORT_PACKAGE) || key.equals(ICoreConstants.PROVIDE_PACKAGE)) { 119 String newLine = TextUtilities.getDefaultLineDelimiter(fModel 120 .getDocument()); 121 header = new ExportPackageHeader(key, value, this, newLine); 122 } else if (key.equals(Constants.IMPORT_PACKAGE)){ 123 String newLine = TextUtilities.getDefaultLineDelimiter(fModel 124 .getDocument()); 125 header = new ImportPackageHeader(key, value, this, newLine); 126 } else { 127 header = new ManifestHeader(key, value, this); 128 } 129 return header; 130 } 131 134 public String getHeader(String key) { 135 ManifestHeader header = (ManifestHeader)fDocumentHeaders.get(key); 136 return (header != null) ? header.getValue() : null; 137 } 138 139 public ManifestHeader getManifestHeader(String key) { 140 return (ManifestHeader)fDocumentHeaders.get(key); 141 } 142 143 public Dictionary getHeaders() { 144 return fDocumentHeaders; 145 } 146 147 public IBundleModel getModel() { 148 return fModel; 149 } 150 } 151 | Popular Tags |