1 11 12 package org.eclipse.pde.internal.core.text.bundle; 13 14 import java.util.ArrayList ; 15 import java.util.Enumeration ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 import java.util.TreeMap ; 19 20 import org.eclipse.osgi.util.ManifestElement; 21 import org.eclipse.pde.internal.core.bundle.BundleObject; 22 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 23 import org.osgi.framework.BundleException; 24 25 public class PDEManifestElement extends BundleObject { 26 27 private static final long serialVersionUID = 1L; 28 29 protected String [] fValueComponents; 30 protected TreeMap fAttributes; 31 protected TreeMap fDirectives; 32 33 protected transient ManifestHeader fHeader; 34 35 public PDEManifestElement(ManifestHeader header, String value) { 36 setHeader(header); 37 setValue(value); 38 setModel(fHeader.getBundle().getModel()); 39 } 40 41 protected PDEManifestElement(ManifestHeader header, ManifestElement manifestElement) { 42 setHeader(header); 43 init(manifestElement); 44 setModel(fHeader.getBundle().getModel()); 45 } 46 47 public String [] getValueComponents() { 48 return fValueComponents; 49 } 50 51 protected void setValueComponents(String [] valueComponents) { 52 fValueComponents = valueComponents; 53 } 54 55 public String [] getAttributes(String key) { 56 return getTableValues(fAttributes, key); 57 } 58 59 public String getAttribute(String key) { 60 return getTableValue(fAttributes, key); 61 } 62 63 public Set getKeys() { 64 return getTableKeys(fAttributes); 65 } 66 67 public void addAttribute(String key, String value) { 68 fAttributes = addTableValue(fAttributes, key, value); 69 } 70 71 public void setAttribute(String key, String value) { 72 fAttributes = setTableValue(fAttributes, key, value); 73 } 74 75 public String getDirective(String key) { 76 return getTableValue(fDirectives, key); 77 } 78 79 public String [] getDirectives(String key) { 80 return getTableValues(fDirectives, key); 81 } 82 83 public Set getDirectiveKeys() { 84 return getTableKeys(fDirectives); 85 } 86 87 public void addDirective(String key, String value) { 88 fDirectives = addTableValue(fDirectives, key, value); 89 } 90 91 public void setDirective(String key, String value) { 92 fDirectives = setTableValue(fDirectives, key, value); 93 } 94 95 private String getTableValue(TreeMap table, String key) { 96 if (table == null) 97 return null; 98 Object result = table.get(key); 99 if (result == null) 100 return null; 101 if (result instanceof String ) 102 return (String ) result; 103 104 ArrayList valueList = (ArrayList ) result; 105 return (String ) valueList.get(valueList.size() - 1); 107 } 108 109 private String [] getTableValues(TreeMap table, String key) { 110 if (table == null) 111 return null; 112 Object result = table.get(key); 113 if (result == null) 114 return null; 115 if (result instanceof String ) 116 return new String [] {(String ) result}; 117 ArrayList valueList = (ArrayList ) result; 118 return (String []) valueList.toArray(new String [valueList.size()]); 119 } 120 121 private Set getTableKeys(TreeMap table) { 122 if (table == null) 123 return null; 124 return table.keySet(); 125 } 126 127 private TreeMap addTableValue(TreeMap table, String key, String value) { 128 if (table == null) { 129 table = new TreeMap (); 130 } 131 Object curValue = table.get(key); 132 if (curValue != null) { 133 ArrayList newList; 134 if (curValue instanceof ArrayList ) { 136 newList = (ArrayList ) curValue; 137 } else { 138 newList = new ArrayList (5); 139 newList.add(curValue); 140 } 141 newList.add(value); 142 table.put(key, newList); 143 } else { 144 table.put(key, value); 145 } 146 return table; 147 } 148 149 private TreeMap setTableValue(TreeMap table, String key, String value) { 150 if (table == null) { 151 table = new TreeMap (); 152 } 153 if (value == null || value.trim().length() == 0) 154 table.remove(key); 155 else { 156 table.put(key, value); 157 } 158 return table; 159 } 160 161 public void setValue(String value) { 162 if (value == null) { 163 setValueComponents(new String [0]); 164 return; 165 } 166 try { 167 ManifestElement[] elements = ManifestElement.parseHeader(fHeader.fName, value); 168 if (elements != null && elements.length > 0) 169 init(elements[0]); 170 } catch (BundleException e) { 171 } 172 } 173 174 private void init(ManifestElement manifestElement) { 175 setValueComponents(manifestElement.getValueComponents()); 176 Enumeration attKeys = manifestElement.getKeys(); 177 if (attKeys != null) { 178 while (attKeys.hasMoreElements()) { 179 String attKey = (String )attKeys.nextElement(); 180 String [] values = ManifestElement.getArrayFromList(manifestElement.getAttribute(attKey)); 181 for (int i = 0; i < values.length; i++) 182 addAttribute(attKey, values[i]); 183 } 184 } 185 Enumeration dirKeys = manifestElement.getDirectiveKeys(); 186 if (dirKeys != null) { 187 while (dirKeys.hasMoreElements()) { 188 String dirKey = (String )dirKeys.nextElement(); 189 String [] values = ManifestElement.getArrayFromList(manifestElement.getDirective(dirKey)); 190 for (int i = 0; i < values.length; i++) 191 addDirective(dirKey, values[i]); 192 } 193 } 194 } 195 196 public String write() { 197 StringBuffer sb = new StringBuffer (getValue()); 198 appendValuesToBuffer(sb, fAttributes); 199 appendValuesToBuffer(sb, fDirectives); 200 return sb.toString(); 201 } 202 203 public String getValue() { 204 StringBuffer sb = new StringBuffer (); 205 if (fValueComponents == null) 206 return ""; for (int i = 0; i < fValueComponents.length; i++) { 208 if (i != 0) sb.append("; "); sb.append(fValueComponents[i]); 210 } 211 return sb.toString(); 212 } 213 214 protected void appendValuesToBuffer(StringBuffer sb, TreeMap table) { 215 if (table == null) 216 return; 217 Iterator dkeys = table.keySet().iterator(); 218 while (dkeys.hasNext()) { 219 String dkey = (String )dkeys.next(); 220 Object value = table.get(dkey); 221 if (value == null) 222 continue; 223 sb.append(";"); sb.append(dkey); 225 sb.append(table.equals(fDirectives) ? ":=" : "="); 227 if (value instanceof String ) { 228 boolean wrap = shouldWrap(value.toString()); 229 if (wrap) sb.append("\""); sb.append(value); 231 if (wrap) sb.append("\""); } else if (value instanceof ArrayList ) { 233 ArrayList values = (ArrayList )value; 234 boolean wrap = (values.size() > 1 235 || (values.size() == 1 && shouldWrap(values.get(0).toString()))); 236 if (wrap) sb.append("\""); for (int i = 0; i < values.size(); i++) { 238 if (i != 0) sb.append(","); sb.append(values.get(i)); 240 } 241 if (wrap) sb.append("\""); } 243 } 244 } 245 246 private boolean shouldWrap(String value) { 247 return value.indexOf(' ') != -1 248 || value.indexOf(',') != -1 249 || value.indexOf('.') != -1 250 || value.indexOf('[') != -1 251 || value.indexOf('(') != -1; 252 } 253 254 public ManifestHeader getHeader() { 255 return fHeader; 256 } 257 258 public void setHeader(ManifestHeader header) { 259 fHeader = header; 260 } 261 262 266 public void reconnect(IBundleModel model, ManifestHeader header) { 267 super.reconnect(model); 268 fHeader = header; 270 } 271 272 } 273 | Popular Tags |