001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail.internet;
019    
020    // can be in the form major/minor; charset=jobby
021    
022    /**
023     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
024     */
025    public class ContentType {
026        private ParameterList _list;
027        private String _minor;
028        private String _major;
029    
030        public ContentType() {
031            // the Sun version makes everything null here.
032        }
033    
034        public ContentType(String major, String minor, ParameterList list) {
035            _major = major;
036            _minor = minor;
037            _list = list;
038        }
039    
040        public ContentType(String type) throws ParseException {
041            // get a token parser for the type information
042            HeaderTokenizer tokenizer = new HeaderTokenizer(type, HeaderTokenizer.MIME);
043    
044            // get the first token, which must be an ATOM
045            HeaderTokenizer.Token token = tokenizer.next();
046            if (token.getType() != HeaderTokenizer.Token.ATOM) {
047                throw new ParseException("Invalid content type");
048            }
049    
050            _major = token.getValue();
051    
052            // the MIME type must be major/minor
053            token = tokenizer.next();
054            if (token.getType() != '/') {
055                throw new ParseException("Invalid content type");
056            }
057    
058    
059            // this must also be an atom.  Content types are not permitted to be wild cards.
060            token = tokenizer.next();
061            if (token.getType() != HeaderTokenizer.Token.ATOM) {
062                throw new ParseException("Invalid content type");
063            }
064    
065            _minor = token.getValue();
066    
067            // the remainder is parameters, which ParameterList will take care of parsing.
068            String remainder = tokenizer.getRemainder();
069            if (remainder != null) {
070                _list = new ParameterList(remainder);
071            }
072        }
073    
074        public String getPrimaryType() {
075            return _major;
076        }
077    
078        public String getSubType() {
079            return _minor;
080        }
081    
082        public String getBaseType() {
083            return _major + "/" + _minor;
084        }
085    
086        public String getParameter(String name) {
087            return (_list == null ? null : _list.get(name));
088        }
089    
090        public ParameterList getParameterList() {
091            return _list;
092        }
093    
094        public void setPrimaryType(String major) {
095            _major = major;
096        }
097    
098        public void setSubType(String minor) {
099            _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    }