13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.datatransfer;
27
28 import java.io.Externalizable;
29 import java.io.ObjectOutput;
30 import java.io.ObjectInput;
31 import java.io.IOException;
32 import java.util.Enumeration;
33
34
35 /**
36 * A Multipurpose Internet Mail Extension (MIME) type, as defined
37 * in RFC 2045 and 2046.
38 *
39 * THIS IS *NOT* - REPEAT *NOT* - A PUBLIC CLASS! DataFlavor IS
40 * THE PUBLIC INTERFACE, AND THIS IS PROVIDED AS A ***PRIVATE***
41 * (THAT IS AS IN *NOT* PUBLIC) HELPER CLASS!
42 */
43 class MimeType implements Externalizable, Cloneable {
44
45 /*
46 * serialization support
47 */
48
49 static final long serialVersionUID = -6568722458793895906L;
50
51 /**
52 * Constructor for externalization; this constructor should not be
76 * <code>sub</code> is null
77 */
78 public MimeType(String primary, String sub) throws MimeTypeParseException {
79 this(primary, sub, new MimeTypeParameterList());
80 }
81
82 /**
83 * Builds a <code>MimeType</code> with a pre-defined
84 * and valid (or empty) parameter list.
85 *
86 * @param primary the primary type of this <code>MimeType</code>
87 * @param sub the subtype of this <code>MimeType</code>
88 * @param mtpl the requested parameter list
89 * @throws NullPointerException if either <code>primary</code>,
90 * <code>sub</code> or <code>mtpl</code> is null
91 */
92 public MimeType(String primary, String sub, MimeTypeParameterList mtpl) throws
93 MimeTypeParseException {
94 // check to see if primary is valid
95 if(isValidToken(primary)) {
96 primaryType = primary.toLowerCase();
97 } else {
98 throw new MimeTypeParseException("Primary type is invalid.");
99 }
100
101 // check to see if sub is valid
102 if(isValidToken(sub)) {
103 subType = sub.toLowerCase();
104 } else {
105 throw new MimeTypeParseException("Sub type is invalid.");
106 }
107
108 parameters = (MimeTypeParameterList)mtpl.clone();
109 }
110
111 public int hashCode() {
112
113 // We sum up the hash codes for all of the strings. This
114 // way, the order of the strings is irrelevant
115 int code = 0;
116 code += primaryType.hashCode();
117 code += subType.hashCode();
118 code += parameters.hashCode();
119 return code;
120 } // hashCode()
121
122 /**
123 * <code>MimeType</code>s are equal if their primary types,
141 } // equals()
142
143 /**
144 * A routine for parsing the MIME type out of a String.
145 *
146 * @throws NullPointerException if <code>rawdata</code> is null
147 */
148 private void parse(String rawdata) throws MimeTypeParseException {
149 int slashIndex = rawdata.indexOf('/');
150 int semIndex = rawdata.indexOf(';');
151 if((slashIndex < 0) && (semIndex < 0)) {
152 // neither character is present, so treat it
153 // as an error
154 throw new MimeTypeParseException("Unable to find a sub type.");
155 } else if((slashIndex < 0) && (semIndex >= 0)) {
156 // we have a ';' (and therefore a parameter list),
157 // but no '/' indicating a sub type is present
158 throw new MimeTypeParseException("Unable to find a sub type.");
159 } else if((slashIndex >= 0) && (semIndex < 0)) {
160 // we have a primary and sub type but no parameter list
161 primaryType = rawdata.substring(0,
162 slashIndex).trim().toLowerCase();
163 subType = rawdata.substring(slashIndex +
164 1).trim().toLowerCase();
165 parameters = new MimeTypeParameterList();
166 } else if (slashIndex < semIndex) {
167 // we have all three items in the proper sequence
168 primaryType = rawdata.substring(0,
169 slashIndex).trim().toLowerCase();
170 subType = rawdata.substring(slashIndex + 1,
171 semIndex).trim().toLowerCase();
172 parameters = new
173 MimeTypeParameterList(rawdata.substring(semIndex));
174 } else {
175 // we have a ';' lexically before a '/' which means we have a primary type
176 // & a parameter list but no sub type
177 throw new MimeTypeParseException("Unable to find a sub type.");
178 }
179
180 // now validate the primary and sub types
181
182 // check to see if primary is valid
183 if(!isValidToken(primaryType)) {
184 throw new MimeTypeParseException("Primary type is invalid.");
185 }
186
187 // check to see if sub is valid
188 if(!isValidToken(subType)) {
189 throw new MimeTypeParseException("Sub type is invalid.");
190 }
191 }
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.datatransfer;
27
28 import java.io.Externalizable;
29 import java.io.ObjectOutput;
30 import java.io.ObjectInput;
31 import java.io.IOException;
32 import java.util.Enumeration;
33 import java.util.Locale;
34
35
36 /**
37 * A Multipurpose Internet Mail Extension (MIME) type, as defined
38 * in RFC 2045 and 2046.
39 *
40 * THIS IS *NOT* - REPEAT *NOT* - A PUBLIC CLASS! DataFlavor IS
41 * THE PUBLIC INTERFACE, AND THIS IS PROVIDED AS A ***PRIVATE***
42 * (THAT IS AS IN *NOT* PUBLIC) HELPER CLASS!
43 */
44 class MimeType implements Externalizable, Cloneable {
45
46 /*
47 * serialization support
48 */
49
50 static final long serialVersionUID = -6568722458793895906L;
51
52 /**
53 * Constructor for externalization; this constructor should not be
77 * <code>sub</code> is null
78 */
79 public MimeType(String primary, String sub) throws MimeTypeParseException {
80 this(primary, sub, new MimeTypeParameterList());
81 }
82
83 /**
84 * Builds a <code>MimeType</code> with a pre-defined
85 * and valid (or empty) parameter list.
86 *
87 * @param primary the primary type of this <code>MimeType</code>
88 * @param sub the subtype of this <code>MimeType</code>
89 * @param mtpl the requested parameter list
90 * @throws NullPointerException if either <code>primary</code>,
91 * <code>sub</code> or <code>mtpl</code> is null
92 */
93 public MimeType(String primary, String sub, MimeTypeParameterList mtpl) throws
94 MimeTypeParseException {
95 // check to see if primary is valid
96 if(isValidToken(primary)) {
97 primaryType = primary.toLowerCase(Locale.ENGLISH);
98 } else {
99 throw new MimeTypeParseException("Primary type is invalid.");
100 }
101
102 // check to see if sub is valid
103 if(isValidToken(sub)) {
104 subType = sub.toLowerCase(Locale.ENGLISH);
105 } else {
106 throw new MimeTypeParseException("Sub type is invalid.");
107 }
108
109 parameters = (MimeTypeParameterList)mtpl.clone();
110 }
111
112 public int hashCode() {
113
114 // We sum up the hash codes for all of the strings. This
115 // way, the order of the strings is irrelevant
116 int code = 0;
117 code += primaryType.hashCode();
118 code += subType.hashCode();
119 code += parameters.hashCode();
120 return code;
121 } // hashCode()
122
123 /**
124 * <code>MimeType</code>s are equal if their primary types,
142 } // equals()
143
144 /**
145 * A routine for parsing the MIME type out of a String.
146 *
147 * @throws NullPointerException if <code>rawdata</code> is null
148 */
149 private void parse(String rawdata) throws MimeTypeParseException {
150 int slashIndex = rawdata.indexOf('/');
151 int semIndex = rawdata.indexOf(';');
152 if((slashIndex < 0) && (semIndex < 0)) {
153 // neither character is present, so treat it
154 // as an error
155 throw new MimeTypeParseException("Unable to find a sub type.");
156 } else if((slashIndex < 0) && (semIndex >= 0)) {
157 // we have a ';' (and therefore a parameter list),
158 // but no '/' indicating a sub type is present
159 throw new MimeTypeParseException("Unable to find a sub type.");
160 } else if((slashIndex >= 0) && (semIndex < 0)) {
161 // we have a primary and sub type but no parameter list
162 primaryType = rawdata.substring(0,slashIndex).
163 trim().toLowerCase(Locale.ENGLISH);
164 subType = rawdata.substring(slashIndex + 1).
165 trim().toLowerCase(Locale.ENGLISH);
166 parameters = new MimeTypeParameterList();
167 } else if (slashIndex < semIndex) {
168 // we have all three items in the proper sequence
169 primaryType = rawdata.substring(0, slashIndex).
170 trim().toLowerCase(Locale.ENGLISH);
171 subType = rawdata.substring(slashIndex + 1,
172 semIndex).trim().toLowerCase(Locale.ENGLISH);
173 parameters = new
174 MimeTypeParameterList(rawdata.substring(semIndex));
175 } else {
176 // we have a ';' lexically before a '/' which means we have a primary type
177 // & a parameter list but no sub type
178 throw new MimeTypeParseException("Unable to find a sub type.");
179 }
180
181 // now validate the primary and sub types
182
183 // check to see if primary is valid
184 if(!isValidToken(primaryType)) {
185 throw new MimeTypeParseException("Primary type is invalid.");
186 }
187
188 // check to see if sub is valid
189 if(!isValidToken(subType)) {
190 throw new MimeTypeParseException("Sub type is invalid.");
191 }
192 }
|