KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > ContentDisposition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)ContentDisposition.java 1.8 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import javax.mail.*;
31 import java.util.*;
32 import java.io.*;
33
34 /**
35  * This class represents a MIME ContentDisposition value. It provides
36  * methods to parse a ContentDisposition string into individual components
37  * and to generate a MIME style ContentDisposition string.
38  *
39  * @version 1.8, 05/08/29
40  * @author John Mani
41  */

42
43 public class ContentDisposition {
44
45     private String JavaDoc disposition; // disposition
46
private ParameterList JavaDoc list; // parameter list
47

48     /**
49      * No-arg Constructor.
50      */

51     public ContentDisposition() { }
52
53     /**
54      * Constructor.
55      *
56      * @param disposition disposition
57      * @param list ParameterList
58      * @since JavaMail 1.2
59      */

60     public ContentDisposition(String JavaDoc disposition, ParameterList JavaDoc list) {
61     this.disposition = disposition;
62     this.list = list;
63     }
64
65     /**
66      * Constructor that takes a ContentDisposition string. The String
67      * is parsed into its constituents: dispostion and parameters.
68      * A ParseException is thrown if the parse fails.
69      *
70      * @param s the ContentDisposition string.
71      * @exception ParseException if the parse fails.
72      * @since JavaMail 1.2
73      */

74     public ContentDisposition(String JavaDoc s) throws ParseException JavaDoc {
75     HeaderTokenizer JavaDoc h = new HeaderTokenizer JavaDoc(s, HeaderTokenizer.MIME);
76     HeaderTokenizer.Token JavaDoc tk;
77
78     // First "disposition" ..
79
tk = h.next();
80     if (tk.getType() != HeaderTokenizer.Token.ATOM)
81         throw new ParseException JavaDoc();
82     disposition = tk.getValue();
83
84     // Then parameters ..
85
String JavaDoc rem = h.getRemainder();
86     if (rem != null)
87         list = new ParameterList JavaDoc(rem);
88     }
89
90     /**
91      * Return the disposition value.
92      * @return the disposition
93      * @since JavaMail 1.2
94      */

95     public String JavaDoc getDisposition() {
96     return disposition;
97     }
98
99     /**
100      * Return the specified parameter value. Returns <code>null</code>
101      * if this parameter is absent.
102      * @return parameter value
103      * @since JavaMail 1.2
104      */

105     public String JavaDoc getParameter(String JavaDoc name) {
106     if (list == null)
107         return null;
108
109     return list.get(name);
110     }
111
112     /**
113      * Return a ParameterList object that holds all the available
114      * parameters. Returns null if no parameters are available.
115      *
116      * @return ParameterList
117      * @since JavaMail 1.2
118      */

119     public ParameterList JavaDoc getParameterList() {
120     return list;
121     }
122
123     /**
124      * Set the disposition. Replaces the existing disposition.
125      * @param disposition the disposition
126      * @since JavaMail 1.2
127      */

128     public void setDisposition(String JavaDoc disposition) {
129     this.disposition = disposition;
130     }
131
132     /**
133      * Set the specified parameter. If this parameter already exists,
134      * it is replaced by this new value.
135      *
136      * @param name parameter name
137      * @param value parameter value
138      * @since JavaMail 1.2
139      */

140     public void setParameter(String JavaDoc name, String JavaDoc value) {
141     if (list == null)
142         list = new ParameterList JavaDoc();
143
144     list.set(name, value);
145     }
146
147     /**
148      * Set a new ParameterList.
149      * @param list ParameterList
150      * @since JavaMail 1.2
151      */

152     public void setParameterList(ParameterList JavaDoc list) {
153     this.list = list;
154     }
155
156     /**
157      * Retrieve a RFC2045 style string representation of
158      * this ContentDisposition. Returns <code>null</code> if
159      * the conversion failed.
160      *
161      * @return RFC2045 style string
162      * @since JavaMail 1.2
163      */

164     public String JavaDoc toString() {
165     if (disposition == null)
166         return null;
167
168     if (list == null)
169         return disposition;
170
171     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(disposition);
172
173         // append the parameter list
174
// use the length of the string buffer + the length of
175
// the header name formatted as follows "Content-Disposition: "
176
sb.append(list.toString(sb.length() + 21));
177     return sb.toString();
178     }
179 }
180
Popular Tags