1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.internet;
  6. import java.util.*;
  7. import java.io.*;
  8. /**
  9. * This class holds MIME parameters (attribute-value pairs).
  10. *
  11. * @version 1.7, 00/09/20
  12. * @author John Mani
  13. */
  14. public class ParameterList {
  15. private Hashtable list = new Hashtable(); // internal hashtable
  16. /**
  17. * No-arg Constructor.
  18. */
  19. public ParameterList() {
  20. }
  21. /**
  22. * Constructor that takes a parameter-list string. The String
  23. * is parsed and the parameters are collected and stored internally.
  24. * A ParseException is thrown if the parse fails.
  25. * Note that an empty parameter-list string is valid and will be
  26. * parsed into an empty ParameterList.
  27. *
  28. * @param s the parameter-list string.
  29. * @exception ParseException if the parse fails.
  30. */
  31. public ParameterList(String s) throws ParseException {
  32. HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
  33. HeaderTokenizer.Token tk;
  34. int type;
  35. String name;
  36. while (true) {
  37. tk = h.next();
  38. type = tk.getType();
  39. if (type == HeaderTokenizer.Token.EOF) // done
  40. return;
  41. if ((char)type == ';') {
  42. // expect parameter name
  43. tk = h.next();
  44. // parameter name must be a MIME Atom
  45. if (tk.getType() != HeaderTokenizer.Token.ATOM)
  46. throw new ParseException();
  47. name = tk.getValue().toLowerCase();
  48. // expect '='
  49. tk = h.next();
  50. if ((char)tk.getType() != '=')
  51. throw new ParseException();
  52. // expect parameter value
  53. tk = h.next();
  54. type = tk.getType();
  55. // parameter value must be a MIME Atom or Quoted String
  56. if (type != HeaderTokenizer.Token.ATOM &&
  57. type != HeaderTokenizer.Token.QUOTEDSTRING)
  58. throw new ParseException();
  59. list.put(name, tk.getValue());
  60. } else
  61. throw new ParseException();
  62. }
  63. }
  64. /**
  65. * Return the number of parameters in this list.
  66. *
  67. * @return number of parameters.
  68. */
  69. public int size() {
  70. return list.size();
  71. }
  72. /**
  73. * Returns the value of the specified parameter. Note that
  74. * parameter names are case-insensitive.
  75. *
  76. * @param name parameter name.
  77. * @return Value of the parameter. Returns
  78. * <code>null</code> if the parameter is not
  79. * present.
  80. */
  81. public String get(String name) {
  82. return (String)list.get(name.trim().toLowerCase());
  83. }
  84. /**
  85. * Set a parameter. If this parameter already exists, it is
  86. * replaced by this new value.
  87. *
  88. * @param name name of the parameter.
  89. * @param value value of the parameter.
  90. */
  91. public void set(String name, String value) {
  92. list.put(name.trim().toLowerCase(), value);
  93. }
  94. /**
  95. * Removes the specified parameter from this ParameterList.
  96. * This method does nothing if the parameter is not present.
  97. *
  98. * @param name name of the parameter.
  99. */
  100. public void remove(String name) {
  101. list.remove(name.trim().toLowerCase());
  102. }
  103. /**
  104. * Return an enumeration of the names of all parameters in this
  105. * list.
  106. *
  107. * @return Enumeration of all parameter names in this list.
  108. */
  109. public Enumeration getNames() {
  110. return list.keys();
  111. }
  112. /**
  113. * Convert this ParameterList into a MIME String. If this is
  114. * an empty list, an empty string is returned.
  115. *
  116. * @return String
  117. */
  118. public String toString() {
  119. return toString(0);
  120. }
  121. /**
  122. * Convert this ParameterList into a MIME String. If this is
  123. * an empty list, an empty string is returned.
  124. *
  125. * The 'used' parameter specifies the number of character positions
  126. * already taken up in the field into which the resulting parameter
  127. * list is to be inserted. It's used to determine where to fold the
  128. * resulting parameter list.
  129. *
  130. * @param used number of character positions already used, in
  131. * the field into which the parameter list is to
  132. * be inserted.
  133. * @return String
  134. */
  135. public String toString(int used) {
  136. StringBuffer sb = new StringBuffer();
  137. Enumeration e = list.keys();
  138. while (e.hasMoreElements()) {
  139. String name = (String)e.nextElement();
  140. String value = quote((String)list.get(name));
  141. sb.append("; ");
  142. used += 2;
  143. int len = name.length() + value.length() + 1;
  144. if (used + len > 76) { // overflows ...
  145. sb.append("\r\n\t"); // .. start new continuation line
  146. used = 8; // account for the starting <tab> char
  147. }
  148. sb.append(name).append('=').append(value);
  149. }
  150. return sb.toString();
  151. }
  152. // Quote a parameter value token if required.
  153. private String quote(String value) {
  154. return MimeUtility.quote(value, HeaderTokenizer.MIME);
  155. }
  156. }