View Javadoc

1   /**
2    *
3    * Copyright 2003-2006 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: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
24   */
25  public class ContentType {
26      private ParameterList _list;
27      private String _minor;
28      private String _major;
29  
30      public ContentType() {
31          // the Sun version makes everything null here.
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          // get a token parser for the type information
42          HeaderTokenizer tokenizer = new HeaderTokenizer(type, HeaderTokenizer.MIME);
43  
44          // get the first token, which must be an ATOM
45          HeaderTokenizer.Token token = tokenizer.next();
46          if (token.getType() != HeaderTokenizer.Token.ATOM) {
47              throw new ParseException("Invalid content type");
48          }
49  
50          _major = token.getValue();
51  
52          // the MIME type must be major/minor
53          token = tokenizer.next();
54          if (token.getType() != '/') {
55              throw new ParseException("Invalid content type");
56          }
57  
58  
59          // this must also be an atom.  Content types are not permitted to be wild cards.
60          token = tokenizer.next();
61          if (token.getType() != HeaderTokenizer.Token.ATOM) {
62              throw new ParseException("Invalid content type");
63          }
64  
65          _minor = token.getValue();
66  
67          // the remainder is parameters, which ParameterList will take care of parsing.
68          String remainder = tokenizer.getRemainder();
69          if (remainder != null) {
70              _list = new ParameterList(remainder);
71          }
72      }
73  
74      public String getPrimaryType() {
75          return _major;
76      }
77  
78      public String getSubType() {
79          return _minor;
80      }
81  
82      public String getBaseType() {
83          return _major + "/" + _minor;
84      }
85  
86      public String getParameter(String name) {
87          return (_list == null ? null : _list.get(name));
88      }
89  
90      public ParameterList getParameterList() {
91          return _list;
92      }
93  
94      public void setPrimaryType(String major) {
95          _major = major;
96      }
97  
98      public void setSubType(String minor) {
99          _minor = minor;
100     }
101 
102     public void setParameter(String name, String value) {
103         if (_list == null) {
104             _list = new ParameterList();
105         }
106         _list.set(name, value);
107     }
108 
109     public void setParameterList(ParameterList list) {
110         _list = list;
111     }
112 
113     public String toString() {
114         if (_major == null || _minor == null) {
115             return null;
116         }
117 
118         return getBaseType() + (_list == null ? "" : _list.toString());
119     }
120 
121     public boolean match(ContentType other) {
122         return _major.equalsIgnoreCase(other._major)
123                 && (_minor.equalsIgnoreCase(other._minor)
124                 || _minor.equals("*")
125                 || other._minor.equals("*"));
126     }
127 
128     public boolean match(String contentType) {
129         try {
130             return match(new ContentType(contentType));
131         } catch (ParseException e) {
132             return false;
133         }
134     }
135 }