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 javax.mail.*;
  7. import java.util.*;
  8. import java.io.*;
  9. /**
  10. * This class represents a MIME ContentDisposition value. It provides
  11. * methods to parse a ContentDisposition string into individual components
  12. * and to generate a MIME style ContentDisposition string.
  13. *
  14. * @version 1.5, 00/09/20
  15. * @author John Mani
  16. */
  17. public class ContentDisposition {
  18. private String disposition; // disposition
  19. private ParameterList list; // parameter list
  20. /**
  21. * No-arg Constructor.
  22. */
  23. public ContentDisposition() { }
  24. /**
  25. * Constructor.
  26. *
  27. * @param disposition disposition
  28. * @param list ParameterList
  29. * @since JavaMail 1.2
  30. */
  31. public ContentDisposition(String disposition, ParameterList list) {
  32. this.disposition = disposition;
  33. this.list = list;
  34. }
  35. /**
  36. * Constructor that takes a ContentDisposition string. The String
  37. * is parsed into its constituents: dispostion and parameters.
  38. * A ParseException is thrown if the parse fails.
  39. *
  40. * @param s the ContentDisposition string.
  41. * @exception ParseException if the parse fails.
  42. * @since JavaMail 1.2
  43. */
  44. public ContentDisposition(String s) throws ParseException {
  45. HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
  46. HeaderTokenizer.Token tk;
  47. // First "disposition" ..
  48. tk = h.next();
  49. if (tk.getType() != HeaderTokenizer.Token.ATOM)
  50. throw new ParseException();
  51. disposition = tk.getValue();
  52. // Then parameters ..
  53. String rem = h.getRemainder();
  54. if (rem != null)
  55. list = new ParameterList(rem);
  56. }
  57. /**
  58. * Return the disposition value.
  59. * @return the disposition
  60. * @since JavaMail 1.2
  61. */
  62. public String getDisposition() {
  63. return disposition;
  64. }
  65. /**
  66. * Return the specified parameter value. Returns <code>null</code>
  67. * if this parameter is absent.
  68. * @return parameter value
  69. * @since JavaMail 1.2
  70. */
  71. public String getParameter(String name) {
  72. if (list == null)
  73. return null;
  74. return list.get(name);
  75. }
  76. /**
  77. * Return a ParameterList object that holds all the available
  78. * parameters. Returns null if no parameters are available.
  79. *
  80. * @return ParameterList
  81. * @since JavaMail 1.2
  82. */
  83. public ParameterList getParameterList() {
  84. return list;
  85. }
  86. /**
  87. * Set the primary type. Overrides existing primary type.
  88. * @param primaryType primary type
  89. * @since JavaMail 1.2
  90. */
  91. public void setDisposition(String disposition) {
  92. this.disposition = disposition;
  93. }
  94. /**
  95. * Set the specified parameter. If this parameter already exists,
  96. * it is replaced by this new value.
  97. *
  98. * @param name parameter name
  99. * @param value parameter value
  100. * @since JavaMail 1.2
  101. */
  102. public void setParameter(String name, String value) {
  103. if (list == null)
  104. list = new ParameterList();
  105. list.set(name, value);
  106. }
  107. /**
  108. * Set a new ParameterList.
  109. * @param list ParameterList
  110. * @since JavaMail 1.2
  111. */
  112. public void setParameterList(ParameterList list) {
  113. this.list = list;
  114. }
  115. /**
  116. * Retrieve a RFC2045 style string representation of
  117. * this ContentDisposition. Returns <code>null</code> if
  118. * the conversion failed.
  119. *
  120. * @return RFC2045 style string
  121. * @since JavaMail 1.2
  122. */
  123. public String toString() {
  124. if (disposition == null)
  125. return null;
  126. if (list == null)
  127. return disposition;
  128. StringBuffer sb = new StringBuffer(disposition);
  129. // append the parameter list
  130. // use the length of the string buffer + the length of
  131. // the header name formatted as follows "Content-Disposition: "
  132. sb.append(list.toString(sb.length() + 21));
  133. return sb.toString();
  134. }
  135. }