1 11 12 package org.eclipse.osgi.util; 13 14 import java.io.*; 15 import java.util.*; 16 import org.eclipse.osgi.framework.debug.Debug; 17 import org.eclipse.osgi.framework.internal.core.Msg; 18 import org.eclipse.osgi.framework.internal.core.Tokenizer; 19 import org.osgi.framework.BundleException; 20 21 68 public class ManifestElement { 69 70 73 protected String value; 74 75 78 protected String [] valueComponents; 79 80 83 protected Hashtable attributes; 84 85 88 protected Hashtable directives; 89 90 93 protected ManifestElement() { 94 super(); 95 } 96 97 112 public String getValue() { 113 return value; 114 } 115 116 134 public String [] getValueComponents() { 135 return valueComponents; 136 } 137 138 156 public String getAttribute(String key) { 157 return getTableValue(attributes, key); 158 } 159 160 168 public String [] getAttributes(String key) { 169 return getTableValues(attributes, key); 170 } 171 172 178 public Enumeration getKeys() { 179 return getTableKeys(attributes); 180 } 181 182 188 protected void addAttribute(String key, String value) { 189 attributes = addTableValue(attributes, key, value); 190 } 191 192 209 public String getDirective(String key) { 210 return getTableValue(directives, key); 211 } 212 213 221 public String [] getDirectives(String key) { 222 return getTableValues(directives, key); 223 } 224 225 231 public Enumeration getDirectiveKeys() { 232 return getTableKeys(directives); 233 } 234 235 241 protected void addDirective(String key, String value) { 242 directives = addTableValue(directives, key, value); 243 } 244 245 248 private String getTableValue(Hashtable table, String key) { 249 if (table == null) 250 return null; 251 Object result = table.get(key); 252 if (result == null) 253 return null; 254 if (result instanceof String ) 255 return (String ) result; 256 257 ArrayList valueList = (ArrayList) result; 258 return (String ) valueList.get(valueList.size() - 1); 260 } 261 262 265 private String [] getTableValues(Hashtable table, String key) { 266 if (table == null) 267 return null; 268 Object result = table.get(key); 269 if (result == null) 270 return null; 271 if (result instanceof String ) 272 return new String [] {(String ) result}; 273 ArrayList valueList = (ArrayList) result; 274 return (String []) valueList.toArray(new String [valueList.size()]); 275 } 276 277 280 private Enumeration getTableKeys(Hashtable table) { 281 if (table == null) 282 return null; 283 return table.keys(); 284 } 285 286 291 private Hashtable addTableValue(Hashtable table, String key, String value) { 292 if (table == null) { 293 table = new Hashtable(7); 294 } 295 Object curValue = table.get(key); 296 if (curValue != null) { 297 ArrayList newList; 298 if (curValue instanceof ArrayList) { 300 newList = (ArrayList) curValue; 301 } else { 302 newList = new ArrayList(5); 303 newList.add(curValue); 304 } 305 newList.add(value); 306 table.put(key, newList); 307 } else { 308 table.put(key, value); 309 } 310 return table; 311 } 312 313 325 public static ManifestElement[] parseHeader(String header, String value) throws BundleException { 326 if (value == null) 327 return (null); 328 ArrayList headerElements = new ArrayList(10); 329 Tokenizer tokenizer = new Tokenizer(value); 330 parseloop: while (true) { 331 String next = tokenizer.getString(";,"); if (next == null) 333 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 334 ArrayList headerValues = new ArrayList(); 335 StringBuffer headerValue = new StringBuffer (next); 336 headerValues.add(next); 337 338 if (Debug.DEBUG && Debug.DEBUG_MANIFEST) 339 Debug.print("paserHeader: " + next); boolean directive = false; 341 char c = tokenizer.getChar(); 342 while (c == ';') { 344 next = tokenizer.getString(";,=:"); if (next == null) 346 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 347 c = tokenizer.getChar(); 348 while (c == ':') { c = tokenizer.getChar(); 350 if (c != '=') { 351 String restOfNext = tokenizer.getToken(";,=:"); if (restOfNext == null) 353 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 354 next += ":" + c + restOfNext; c = tokenizer.getChar(); 356 } else 357 directive = true; 358 } 359 if (c == ';' || c == '\0') { 360 headerValues.add(next); 361 headerValue.append(";").append(next); if (Debug.DEBUG && Debug.DEBUG_MANIFEST) 363 Debug.print(";" + next); } 365 } 366 ManifestElement manifestElement = new ManifestElement(); 368 manifestElement.value = headerValue.toString(); 369 manifestElement.valueComponents = (String []) headerValues.toArray(new String [headerValues.size()]); 370 371 while (c == '=' || c == ':') { 373 while (c == ':') { c = tokenizer.getChar(); 375 if (c != '=') { 376 String restOfNext = tokenizer.getToken("=:"); if (restOfNext == null) 378 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 379 next += ":" + c + restOfNext; c = tokenizer.getChar(); 381 } else 382 directive = true; 383 } 384 String val = tokenizer.getString(";,"); if (val == null) 386 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 387 388 if (Debug.DEBUG && Debug.DEBUG_MANIFEST) 389 Debug.print(";" + next + "=" + val); try { 391 if (directive) 392 manifestElement.addDirective(next, val); 393 else 394 manifestElement.addAttribute(next, val); 395 directive = false; 396 } catch (Exception e) { 397 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 398 } 399 c = tokenizer.getChar(); 400 if (c == ';') { 401 next = tokenizer.getToken("=:"); if (next == null) 403 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 404 c = tokenizer.getChar(); 405 } 406 } 407 headerElements.add(manifestElement); 408 if (Debug.DEBUG && Debug.DEBUG_MANIFEST) 409 Debug.println(""); if (c == ',') 411 continue parseloop; 412 if (c == '\0') 413 break parseloop; 414 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, header, value)); 415 } 416 int size = headerElements.size(); 417 if (size == 0) 418 return (null); 419 420 ManifestElement[] result = (ManifestElement[]) headerElements.toArray(new ManifestElement[size]); 421 return (result); 422 } 423 424 430 public static String [] getArrayFromList(String stringList) { 431 String [] result = getArrayFromList(stringList, ","); return result.length == 0 ? null : result; 433 } 434 435 445 public static String [] getArrayFromList(String stringList, String separator) { 446 if (stringList == null || stringList.trim().length() == 0) 447 return new String [0]; 448 ArrayList list = new ArrayList(); 449 StringTokenizer tokens = new StringTokenizer(stringList, separator); 450 while (tokens.hasMoreTokens()) { 451 String token = tokens.nextToken().trim(); 452 if (token.length() != 0) 453 list.add(token); 454 } 455 return (String []) list.toArray(new String [list.size()]); 456 } 457 458 473 public static Map parseBundleManifest(InputStream manifest, Map headers) throws IOException, BundleException { 474 if (headers == null) 475 headers = new HashMap(); 476 BufferedReader br; 477 try { 478 br = new BufferedReader(new InputStreamReader(manifest, "UTF8")); } catch (UnsupportedEncodingException e) { 480 br = new BufferedReader(new InputStreamReader(manifest)); 481 } 482 try { 483 String header = null; 484 StringBuffer value = new StringBuffer (256); 485 boolean firstLine = true; 486 487 while (true) { 488 String line = br.readLine(); 489 494 495 if ((line == null) || (line.length() == 0)) 496 { 497 if (!firstLine) 498 { 499 headers.put(header, value.toString().trim()); 500 } 501 break; 502 } 503 504 if (line.charAt(0) == ' ') 505 { 506 if (firstLine) 507 { 508 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_SPACE, line)); 509 } 510 value.append(line.substring(1)); 511 continue; 512 } 513 514 if (!firstLine) { 515 headers.put(header, value.toString().trim()); 516 value.setLength(0); 517 } 518 519 int colon = line.indexOf(':'); 520 if (colon == -1) 521 { 522 throw new BundleException(NLS.bind(Msg.MANIFEST_INVALID_LINE_NOCOLON, line)); 523 } 524 header = line.substring(0, colon).trim(); 525 value.append(line.substring(colon + 1)); 526 firstLine = false; 527 } 528 } finally { 529 try { 530 br.close(); 531 } catch (IOException ee) { 532 } 534 } 535 return headers; 536 } 537 } 538 | Popular Tags |