1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail.internet;
19
20 // can be in the form major/minor; charset=jobby
21
22 /**
23 * @version $Rev: 54266 $ $Date: 2004-10-10 14:02:50 -0700 (Sun, 10 Oct 2004) $
24 */
25 public class ContentType {
26 private ParameterList _list;
27 private String _minor;
28 private String _major;
29
30 public ContentType() {
31 this("text", "plain", new ParameterList());
32 }
33
34 public ContentType(String major, String minor, ParameterList list) {
35 _major = major;
36 _minor = minor;
37 _list = list;
38 }
39
40 public ContentType(String type) throws ParseException {
41 final int slash = type.indexOf("/");
42 final int semi = type.indexOf(";");
43 try {
44 _major = type.substring(0, slash);
45 if (semi == -1) {
46 _minor = type.substring(slash + 1);
47 } else {
48 _minor = type.substring(slash + 1, semi);
49 _list = new ParameterList(type.substring(semi + 1));
50 }
51 } catch (StringIndexOutOfBoundsException e) {
52 throw new ParseException("Type invalid: " + type);
53 }
54 }
55
56 public String getPrimaryType() {
57 return _major;
58 }
59
60 public String getSubType() {
61 return _minor;
62 }
63
64 public String getBaseType() {
65 return _major + "/" + _minor;
66 }
67
68 public String getParameter(String name) {
69 return (_list == null ? null : _list.get(name));
70 }
71
72 public ParameterList getParameterList() {
73 return _list;
74 }
75
76 public void setPrimaryType(String major) {
77 _major = major;
78 }
79
80 public void setSubType(String minor) {
81 _minor = minor;
82 }
83
84 public void setParameter(String name, String value) {
85 if (_list == null) {
86 _list = new ParameterList();
87 }
88 _list.set(name, value);
89 }
90
91 public void setParameterList(ParameterList list) {
92 _list = list;
93 }
94
95 public String toString() {
96 return getBaseType() + (_list == null ? "" : ";" + _list.toString());
97 }
98
99 public boolean match(ContentType other) {
100 return _major.equals(other._major)
101 && (_minor.equals(other._minor)
102 || _minor.equals("*")
103 || other._minor.equals("*"));
104 }
105
106 public boolean match(String contentType) {
107 try {
108 return match(new ContentType(contentType));
109 } catch (ParseException e) {
110 return false;
111 }
112 }
113 }