001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.mail.internet;
021    
022    // can be in the form major/minor; charset=jobby
023    
024    /**
025     * @version $Rev: 669445 $ $Date: 2008-06-19 06:48:18 -0400 (Thu, 19 Jun 2008) $
026     */
027    public class ContentType {
028        private ParameterList _list;
029        private String _minor;
030        private String _major;
031    
032        public ContentType() {
033            // the Sun version makes everything null here.
034        }
035    
036        public ContentType(String major, String minor, ParameterList list) {
037            _major = major;
038            _minor = minor;
039            _list = list;
040        }
041    
042        public ContentType(String type) throws ParseException {
043            // get a token parser for the type information
044            HeaderTokenizer tokenizer = new HeaderTokenizer(type, HeaderTokenizer.MIME);
045    
046            // get the first token, which must be an ATOM
047            HeaderTokenizer.Token token = tokenizer.next();
048            if (token.getType() != HeaderTokenizer.Token.ATOM) {
049                throw new ParseException("Invalid content type");
050            }
051    
052            _major = token.getValue();
053    
054            // the MIME type must be major/minor
055            token = tokenizer.next();
056            if (token.getType() != '/') {
057                throw new ParseException("Invalid content type");
058            }
059    
060    
061            // this must also be an atom.  Content types are not permitted to be wild cards.
062            token = tokenizer.next();
063            if (token.getType() != HeaderTokenizer.Token.ATOM) {
064                throw new ParseException("Invalid content type");
065            }
066    
067            _minor = token.getValue();
068    
069            // the remainder is parameters, which ParameterList will take care of parsing.
070            String remainder = tokenizer.getRemainder();
071            if (remainder != null) {
072                _list = new ParameterList(remainder);
073            }
074        }
075    
076        public String getPrimaryType() {
077            return _major;
078        }
079    
080        public String getSubType() {
081            return _minor;
082        }
083    
084        public String getBaseType() {
085            return _major + "/" + _minor;
086        }
087    
088        public String getParameter(String name) {
089            return (_list == null ? null : _list.get(name));
090        }
091    
092        public ParameterList getParameterList() {
093            return _list;
094        }
095    
096        public void setPrimaryType(String major) {
097            _major = major;
098        }
099    
100        public void setSubType(String minor) {
101            _minor = minor;
102        }
103    
104        public void setParameter(String name, String value) {
105            if (_list == null) {
106                _list = new ParameterList();
107            }
108            _list.set(name, value);
109        }
110    
111        public void setParameterList(ParameterList list) {
112            _list = list;
113        }
114    
115        public String toString() {
116            if (_major == null || _minor == null) {
117                return null;
118            }
119            
120            // We need to format this as if we're doing it to set into the Content-Type
121            // header.  So the parameter list gets added on as if the header name was 
122            // also included. 
123            String baseType = getBaseType(); 
124            if (_list != null) {
125                baseType += _list.toString(baseType.length() + "Content-Type: ".length()); 
126            }
127            
128            return baseType;
129        }
130    
131        public boolean match(ContentType other) {
132            return _major.equalsIgnoreCase(other._major)
133                    && (_minor.equalsIgnoreCase(other._minor)
134                    || _minor.equals("*")
135                    || other._minor.equals("*"));
136        }
137    
138        public boolean match(String contentType) {
139            try {
140                return match(new ContentType(contentType));
141            } catch (ParseException e) {
142                return false;
143            }
144        }
145    }