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