Attributes.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* Attributes.java -- Represents attribute name/value pairs from a Manifest
  2. Copyright (C) 2000, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.jar;
  32. import gnu.java.util.jar.JarUtils;
  33. import java.util.Collection;
  34. import java.util.Hashtable;
  35. import java.util.Map;
  36. import java.util.Set;
  37. /**
  38. * Represents attribute name/value pairs from a Manifest as a Map.
  39. * The names of an attribute are represented by the
  40. * <code>Attributes.Name</code> class and should confirm to the restrictions
  41. * described in that class. Note that the Map interface that Attributes
  42. * implements allows you to put names and values into the attribute that don't
  43. * follow these restriction (and are not really Atrribute.Names, but if you do
  44. * that it might cause undefined behaviour later).
  45. * <p>
  46. * If you use the constants defined in the inner class Name then you can be
  47. * sure that you always access the right attribute names. This makes
  48. * manipulating the Attributes more or less type safe.
  49. * <p>
  50. * Most of the methods are wrappers to implement the Map interface. The really
  51. * useful and often used methods are <code>getValue(Name)</code> and
  52. * <code>getValue(String)</code>. If you actually want to set attributes you
  53. * may want to use the <code>putValue(String, String)</code> method
  54. * (sorry there is no public type safe <code>putValue(Name, String)</code>
  55. * method).
  56. *
  57. * @see java.util.jar.Attributes.Name
  58. * @author Mark Wielaard (mark@klomp.org)
  59. */
  60. public class Attributes
  61. implements Cloneable, Map<Object, Object>
  62. {
  63. // Fields
  64. /**
  65. * The map that holds all the attribute name/value pairs. In this
  66. * implementation it is actually a Hashtable, but that can be different in
  67. * other implementations.
  68. */
  69. protected Map<Object, Object> map;
  70. // Inner class
  71. /**
  72. * Represents a name of a Manifest Attribute. Defines a couple of well
  73. * know names for the general main attributes, stand alone application
  74. * attributes, applet attributes, extension identification attributes,
  75. * package versioning and sealing attributes, file contents attributes,
  76. * bean objects attribute and signing attributes. See the
  77. *
  78. * <p>The characters of a Name must obey the following restrictions:</p>
  79. *
  80. * <ul>
  81. * <li>Must contain at least one character</li>
  82. * <li>The first character must be alphanumeric (a-z, A-Z, 0-9)</li>
  83. * <li>All other characters must be alphanumeric, a '-' or a '_'</li>
  84. * </ul>
  85. *
  86. * <p>When comparing Names (with <code>equals</code>) all characters are
  87. * converted to lowercase. But you can get the original case sensitive
  88. * string with the <code>toString()</code> method.</p>
  89. *
  90. * <p>Most important attributes have a constant defined in this
  91. * class. Some other attributes used in Manifest files are:
  92. * <ul>
  93. * <li> "Created-By" - General main attribute, tool and version
  94. * that created this Manifest file.</li>
  95. * <li> "Java-Bean" - Bean objects attribute, whether the entry is a Bean.
  96. * Value is either "true" or "false".</li>
  97. * <li> "Magic" - Signing attribute, application specific signing attribute.
  98. * Must be understood by the manifest parser when present to validate the
  99. * jar (entry).</li>
  100. * </ul>
  101. *
  102. * @since 1.2
  103. * @author Mark Wielaard (mark@klomp.org)
  104. */
  105. public static class Name
  106. {
  107. // General Main Attributes
  108. /**
  109. * General main attribute -
  110. * the version of this Manifest file.
  111. */
  112. public static final Name MANIFEST_VERSION = new Name(JarUtils.MANIFEST_VERSION);
  113. /**
  114. * General main attribute -
  115. * the version of the jar file signature.
  116. */
  117. public static final Name SIGNATURE_VERSION = new Name(JarUtils.SIGNATURE_VERSION);
  118. /**
  119. * General main attribute -
  120. * (relative) file paths of the libraries/classpaths that the Classes in
  121. * this jar file depend on. Paths are separated by spaces.
  122. */
  123. public static final Name CLASS_PATH = new Name("Class-Path");
  124. /**
  125. * Stand alone application attribute -
  126. * the entry (without the .class ending) that is the main
  127. * class of this jar file.
  128. */
  129. public static final Name MAIN_CLASS = new Name("Main-Class");
  130. /**
  131. * Applet attribute -
  132. * a list of extension libraries that the applet in this
  133. * jar file depends on.
  134. * For every named extension there should be some Attributes in the
  135. * Manifest manifest file with the following Names:
  136. * <ul>
  137. * <li> &lt;extension&gt;-Extension-Name:
  138. * unique name of the extension</li>
  139. * <li> &lt;extension&gt;-Specification-Version:
  140. * minimum specification version</li>
  141. * <li> &lt;extension&gt;-Implementation-Version:
  142. * minimum implementation version</li>
  143. * <li> &lt;extension&gt;-Implementation-Vendor-Id:
  144. * unique id of implementation vendor</li>
  145. * <li> &lt;extension&gt;-Implementation-URL:
  146. * where the latest version of the extension library can be found</li>
  147. * </ul>
  148. */
  149. public static final Name EXTENSION_LIST = new Name("Extension-List");
  150. /**
  151. * Extension identification attribute -
  152. * the name if the extension library contained in the jar.
  153. */
  154. public static final Name EXTENSION_NAME = new Name("Extension-Name");
  155. /**
  156. * Extension identification attribute -
  157. * synonym for <code>EXTENSTION_NAME</code>.
  158. */
  159. public static final Name EXTENSION_INSTALLATION = EXTENSION_NAME;
  160. // Package versioning and sealing attributes
  161. /**
  162. * Package versioning -
  163. * name of extension library contained in this jar.
  164. */
  165. public static final Name IMPLEMENTATION_TITLE
  166. = new Name("Implementation-Title");
  167. /**
  168. * Package versioning -
  169. * version of the extension library contained in this jar.
  170. */
  171. public static final Name IMPLEMENTATION_VERSION
  172. = new Name("Implementation-Version");
  173. /**
  174. * Package versioning -
  175. * name of extension library creator contained in this jar.
  176. */
  177. public static final Name IMPLEMENTATION_VENDOR
  178. = new Name("Implementation-Vendor");
  179. /**
  180. * Package versioning -
  181. * unique id of extension library creator.
  182. */
  183. public static final Name IMPLEMENTATION_VENDOR_ID
  184. = new Name("Implementation-Vendor-Id");
  185. /**
  186. * Package versioning -
  187. * location where this implementation can be downloaded.
  188. */
  189. public static final Name IMPLEMENTATION_URL
  190. = new Name("Implementation-URL");
  191. /**
  192. * Package versioning -
  193. * title of the specification contained in this jar.
  194. */
  195. public static final Name SPECIFICATION_TITLE
  196. = new Name("Specification-Title");
  197. /**
  198. * Package versioning -
  199. * version of the specification contained in this jar.
  200. */
  201. public static final Name SPECIFICATION_VERSION
  202. = new Name("Specification-Version");
  203. /**
  204. * Package versioning -
  205. * organisation that maintains the specification contains in this
  206. * jar.
  207. */
  208. public static final Name SPECIFICATION_VENDOR
  209. = new Name("Specification-Vendor");
  210. /**
  211. * Package sealing -
  212. * whether (all) package(s) is(/are) sealed. Value is either "true"
  213. * or "false".
  214. */
  215. public static final Name SEALED = new Name("Sealed");
  216. /**
  217. * File contents attribute -
  218. * Mime type and subtype for the jar entry.
  219. */
  220. public static final Name CONTENT_TYPE = new Name("Content-Type");
  221. /** The (lowercase) String representation of this Name */
  222. private final String name;
  223. /** The original String given to the constructor */
  224. private final String origName;
  225. // Constructor
  226. /**
  227. * Creates a new Name from the given String.
  228. * Throws an IllegalArgumentException if the given String is empty or
  229. * contains any illegal Name characters.
  230. *
  231. * @param name the name of the new Name
  232. * @exception IllegalArgumentException if name isn't a valid String
  233. * representation of a Name
  234. * @exception NullPointerException if name is null
  235. */
  236. public Name(String name) throws IllegalArgumentException,
  237. NullPointerException
  238. {
  239. // name must not be null
  240. // this will throw a NullPointerException if it is
  241. char chars[] = name.toCharArray();
  242. // there must be at least one character
  243. if (chars.length == 0)
  244. throw new
  245. IllegalArgumentException
  246. ("There must be at least one character in a name");
  247. // first character must be alphanum
  248. char c = chars[0];
  249. if (!((c >= 'a' && c <= 'z') ||
  250. (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')))
  251. throw new
  252. IllegalArgumentException("First character must be alphanum");
  253. // all other characters must be alphanums, '-' or '_'
  254. for (int i = 1; i < chars.length; i++)
  255. {
  256. c = chars[i];
  257. if (!((c >= 'a' && c <= 'z') ||
  258. (c >= 'A' && c <= 'Z') ||
  259. (c >= '0' && c <= '9') || (c == '-') || (c == '_')))
  260. throw new
  261. IllegalArgumentException
  262. ("Characters must be alphanums, '-' or '_'");
  263. }
  264. // Still here? Then convert to lower case and be done.
  265. // Store the original name for toString();
  266. this.origName = name;
  267. this.name = name.toLowerCase();
  268. }
  269. /**
  270. * Returns the hash code of the (lowercase) String representation of
  271. * this Name.
  272. */
  273. public int hashCode()
  274. {
  275. return name.hashCode();
  276. }
  277. /**
  278. * Checks if another object is equal to this Name object.
  279. * Another object is equal to this Name object if it is an instance of
  280. * Name and the (lowercase) string representation of the name is equal.
  281. */
  282. public boolean equals(Object o)
  283. {
  284. // Quick and dirty check
  285. if (name == o)
  286. return true;
  287. try
  288. {
  289. // Note that the constructor already converts the strings to
  290. // lowercase.
  291. String otherName = ((Name) o).name;
  292. return name.equals(otherName);
  293. }
  294. catch (ClassCastException cce)
  295. {
  296. return false;
  297. }
  298. catch (NullPointerException npe)
  299. {
  300. return false;
  301. }
  302. }
  303. /**
  304. * Returns the string representation of this Name as given to the
  305. * constructor (not neccesarily the lower case representation).
  306. */
  307. public String toString()
  308. {
  309. return origName;
  310. }
  311. }
  312. // Constructors
  313. /**
  314. * Creates an empty Attributes map.
  315. */
  316. public Attributes()
  317. {
  318. map = new Hashtable();
  319. }
  320. /**
  321. * Creates an empty Attributes map with the given initial size.
  322. * @param size the initial size of the underlying map
  323. */
  324. public Attributes(int size)
  325. {
  326. map = new Hashtable(size);
  327. }
  328. /**
  329. * Creates an Attributes map with the initial values taken from another
  330. * Attributes map.
  331. * @param attr Attributes map to take the initial values from
  332. */
  333. public Attributes(Attributes attr)
  334. {
  335. map = new Hashtable(attr.map);
  336. }
  337. // Methods
  338. /**
  339. * Gets the value of an attribute name given as a String.
  340. *
  341. * @param name a String describing the Name to look for
  342. * @return the value gotten from the map of null when not found
  343. */
  344. public String getValue(String name)
  345. {
  346. return (String) get(new Name(name));
  347. }
  348. /**
  349. * Gets the value of the given attribute name.
  350. *
  351. * @param name the Name to look for
  352. * @return the value gotten from the map of null when not found
  353. */
  354. public String getValue(Name name)
  355. {
  356. return (String) get(name);
  357. }
  358. /**
  359. * Stores an attribute name (represented by a String) and value in this
  360. * Attributes map.
  361. * When the (case insensitive string) name already exists the value is
  362. * replaced and the old value is returned.
  363. *
  364. * @param name a (case insensitive) String representation of the attribite
  365. * name to add/replace
  366. * @param value the (new) value of the attribute name
  367. * @returns the old value of the attribute name or null if it didn't exist
  368. * yet
  369. */
  370. public String putValue(String name, String value)
  371. {
  372. return putValue(new Name(name), value);
  373. }
  374. /**
  375. * Stores an attribute name (represented by a String) and value in this
  376. * Attributes map.
  377. * When the name already exists the value is replaced and the old value
  378. * is returned.
  379. *
  380. * @param name the attribite name to add/replace
  381. * @param value the (new) value of the attribute name
  382. * @returns the old value of the attribute name or null if it didn't exist
  383. * yet
  384. */
  385. private String putValue(Name name, String value)
  386. {
  387. return (String) put(name, value);
  388. }
  389. // Methods from Cloneable interface
  390. /**
  391. * Return a clone of this attribute map.
  392. */
  393. public Object clone()
  394. {
  395. return new Attributes(this);
  396. }
  397. // Methods from Map interface
  398. /**
  399. * Removes all attributes.
  400. */
  401. public void clear()
  402. {
  403. map.clear();
  404. }
  405. /**
  406. * Checks to see if there is an attribute with the specified name.
  407. * XXX - what if the object is a String?
  408. *
  409. * @param attrName the name of the attribute to check
  410. * @return true if there is an attribute with the specified name, false
  411. * otherwise
  412. */
  413. public boolean containsKey(Object attrName)
  414. {
  415. return map.containsKey(attrName);
  416. }
  417. /**
  418. * Checks to see if there is an attribute name with the specified value.
  419. *
  420. * @param attrValue the value of a attribute to check
  421. * @return true if there is an attribute name with the specified value,
  422. * false otherwise
  423. */
  424. public boolean containsValue(Object attrValue)
  425. {
  426. return map.containsValue(attrValue);
  427. }
  428. /**
  429. * Gives a Set of attribute name and values pairs as MapEntries.
  430. * @see java.util.Map.Entry
  431. * @see java.util.Map#entrySet()
  432. *
  433. * @return a set of attribute name value pairs
  434. */
  435. public Set<Map.Entry<Object, Object>> entrySet()
  436. {
  437. return map.entrySet();
  438. }
  439. /**
  440. * Checks to see if two Attributes are equal. The supplied object must be
  441. * a real instance of Attributes and contain the same attribute name/value
  442. * pairs.
  443. *
  444. * @param o another Attribute object which should be checked for equality
  445. * @return true if the object is an instance of Attributes and contains the
  446. * same name/value pairs, false otherwise
  447. */
  448. public boolean equals(Object o)
  449. {
  450. // quick and dirty check
  451. if (this == o)
  452. return true;
  453. try
  454. {
  455. return map.equals(((Attributes) o).map);
  456. }
  457. catch (ClassCastException cce)
  458. {
  459. return false;
  460. }
  461. catch (NullPointerException npe)
  462. {
  463. return false;
  464. }
  465. }
  466. /**
  467. * Gets the value of a specified attribute name.
  468. * XXX - what if the object is a String?
  469. *
  470. * @param attrName the name of the attribute we want the value of
  471. * @return the value of the specified attribute name or null when there is
  472. * no such attribute name
  473. */
  474. public Object get(Object attrName)
  475. {
  476. return map.get(attrName);
  477. }
  478. /**
  479. * Returns the hashcode of the attribute name/value map.
  480. */
  481. public int hashCode()
  482. {
  483. return map.hashCode();
  484. }
  485. /**
  486. * Returns true if there are no attributes set, false otherwise.
  487. */
  488. public boolean isEmpty()
  489. {
  490. return map.isEmpty();
  491. }
  492. /**
  493. * Gives a Set of all the values of defined attribute names.
  494. */
  495. public Set<Object> keySet()
  496. {
  497. return map.keySet();
  498. }
  499. /**
  500. * Adds or replaces a attribute name/value pair.
  501. * XXX - What if the name is a string? What if the name is neither a Name
  502. * nor a String? What if the value is not a string?
  503. *
  504. * @param name the name of the attribute
  505. * @param value the (new) value of the attribute
  506. * @return the old value of the attribute or null when there was no old
  507. * attribute with this name
  508. */
  509. public Object put(Object name, Object value)
  510. {
  511. return map.put(name, value);
  512. }
  513. /**
  514. * Adds or replaces all attribute name/value pairs from another
  515. * Attributes object to this one. The supplied Map must be an instance of
  516. * Attributes.
  517. *
  518. * @param attr the Attributes object to merge with this one
  519. * @exception ClassCastException if the supplied map is not an instance of
  520. * Attributes
  521. */
  522. public void putAll(Map<?, ?> attr)
  523. {
  524. if (!(attr instanceof Attributes))
  525. {
  526. throw new
  527. ClassCastException("Supplied Map is not an instance of Attributes");
  528. }
  529. map.putAll(attr);
  530. }
  531. /**
  532. * Remove a attribute name/value pair.
  533. * XXX - What if the name is a String?
  534. *
  535. * @param name the name of the attribute name/value pair to remove
  536. * @return the old value of the attribute or null if the attribute didn't
  537. * exist
  538. */
  539. public Object remove(Object name)
  540. {
  541. return map.remove(name);
  542. }
  543. /**
  544. * Returns the number of defined attribute name/value pairs.
  545. */
  546. public int size()
  547. {
  548. return map.size();
  549. }
  550. /**
  551. * Returns all the values of the defined attribute name/value pairs as a
  552. * Collection.
  553. */
  554. public Collection<Object> values()
  555. {
  556. return map.values();
  557. }
  558. }