1 21 22 27 28 package javax.mail.internet; 29 30 import javax.mail.*; 31 import java.util.*; 32 import java.io.*; 33 34 42 43 public class ContentType { 44 45 private String primaryType; private String subType; private ParameterList list; 49 52 public ContentType() { } 53 54 61 public ContentType(String primaryType, String subType, 62 ParameterList list) { 63 this.primaryType = primaryType; 64 this.subType = subType; 65 this.list = list; 66 } 67 68 76 public ContentType(String s) throws ParseException { 77 HeaderTokenizer h = new HeaderTokenizer (s, HeaderTokenizer.MIME); 78 HeaderTokenizer.Token tk; 79 80 tk = h.next(); 82 if (tk.getType() != HeaderTokenizer.Token.ATOM) 83 throw new ParseException (); 84 primaryType = tk.getValue(); 85 86 tk = h.next(); 88 if ((char)tk.getType() != '/') 89 throw new ParseException (); 90 91 tk = h.next(); 93 if (tk.getType() != HeaderTokenizer.Token.ATOM) 94 throw new ParseException (); 95 subType = tk.getValue(); 96 97 String rem = h.getRemainder(); 99 if (rem != null) 100 list = new ParameterList (rem); 101 } 102 103 107 public String getPrimaryType() { 108 return primaryType; 109 } 110 111 115 public String getSubType() { 116 return subType; 117 } 118 119 126 public String getBaseType() { 127 return primaryType + '/' + subType; 128 } 129 130 135 public String getParameter(String name) { 136 if (list == null) 137 return null; 138 139 return list.get(name); 140 } 141 142 148 public ParameterList getParameterList() { 149 return list; 150 } 151 152 156 public void setPrimaryType(String primaryType) { 157 this.primaryType = primaryType; 158 } 159 160 164 public void setSubType(String subType) { 165 this.subType = subType; 166 } 167 168 175 public void setParameter(String name, String value) { 176 if (list == null) 177 list = new ParameterList (); 178 179 list.set(name, value); 180 } 181 182 186 public void setParameterList(ParameterList list) { 187 this.list = list; 188 } 189 190 197 public String toString() { 198 if (primaryType == null || subType == null) return null; 200 201 StringBuffer sb = new StringBuffer (); 202 sb.append(primaryType).append('/').append(subType); 203 if (list != null) 204 sb.append(list.toString(sb.length() + 14)); 208 209 return sb.toString(); 210 } 211 212 230 public boolean match(ContentType cType) { 231 if (!primaryType.equalsIgnoreCase(cType.getPrimaryType())) 233 return false; 234 235 String sType = cType.getSubType(); 236 237 if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*')) 239 return true; 240 241 if (!subType.equalsIgnoreCase(sType)) 243 return false; 244 245 return true; 246 } 247 248 264 public boolean match(String s) { 265 try { 266 return match(new ContentType (s)); 267 } catch (ParseException pex) { 268 return false; 269 } 270 } 271 } 272 | Popular Tags |