1 15 package org.apache.tapestry.util; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 31 public class ContentType 32 { 33 private String _baseType; 34 private String _subType; 35 private Map _parameters; 36 37 40 public ContentType() 41 { 42 initialize(); 43 } 44 45 51 public ContentType(String contentType) 52 { 53 this(); 54 parse(contentType); 55 } 56 57 private void initialize() 58 { 59 _baseType = ""; 60 _subType = ""; 61 _parameters = new HashMap (); 62 } 63 64 67 public String getBaseType() 68 { 69 return _baseType; 70 } 71 72 75 public void setBaseType(String baseType) 76 { 77 _baseType = baseType; 78 } 79 80 83 public String getSubType() 84 { 85 return _subType; 86 } 87 88 91 public void setSubType(String subType) 92 { 93 _subType = subType; 94 } 95 96 99 public String getMimeType() 100 { 101 return _baseType + "/" + _subType; 102 } 103 104 107 public String [] getParameterNames() 108 { 109 Set parameterNames = _parameters.keySet(); 110 return (String []) parameterNames.toArray(new String [parameterNames.size()]); 111 } 112 113 117 public String getParameter(String key) 118 { 119 return (String ) _parameters.get(key); 120 } 121 122 126 public void setParameter(String key, String value) 127 { 128 _parameters.put(key.toLowerCase(), value); 129 } 130 131 137 public void parse(String contentType) 138 { 139 initialize(); 140 141 StringTokenizer tokens = new StringTokenizer (contentType, ";"); 142 if (!tokens.hasMoreTokens()) 143 return; 144 145 String mimeType = tokens.nextToken(); 146 StringTokenizer mimeTokens = new StringTokenizer (mimeType, "/"); 147 setBaseType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); 148 setSubType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); 149 150 while (tokens.hasMoreTokens()) { 151 String parameter = tokens.nextToken(); 152 153 StringTokenizer parameterTokens = new StringTokenizer (parameter, "="); 154 String key = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; 155 String value = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; 156 setParameter(key, value); 157 } 158 } 159 160 161 162 165 public String unparse() 166 { 167 StringBuffer buf = new StringBuffer (getMimeType()); 168 169 String [] parameterNames = getParameterNames(); 170 for (int i = 0; i < parameterNames.length; i++) 171 { 172 String key = parameterNames[i]; 173 String value = getParameter(key); 174 buf.append(";" + key + "=" + value); 175 } 176 177 return buf.toString(); 178 } 179 180 183 public String toString() 184 { 185 return unparse(); 186 } 187 188 } 189 | Popular Tags |