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: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $ 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 return getBaseType() + (_list == null ? "" : _list.toString()); 121 } 122 123 public boolean match(ContentType other) { 124 return _major.equalsIgnoreCase(other._major) 125 && (_minor.equalsIgnoreCase(other._minor) 126 || _minor.equals("*") 127 || other._minor.equals("*")); 128 } 129 130 public boolean match(String contentType) { 131 try { 132 return match(new ContentType(contentType)); 133 } catch (ParseException e) { 134 return false; 135 } 136 } 137 }