1 21 22 27 28 package com.sun.activation.registries; 29 30 import java.io.*; 31 import java.util.*; 32 33 public class MimeTypeFile { 34 private String fname = null; 35 private Hashtable type_hash = new Hashtable(); 36 37 42 public MimeTypeFile(String new_fname) throws IOException { 43 File mime_file = null; 44 FileReader fr = null; 45 46 fname = new_fname; 48 mime_file = new File(fname); 50 fr = new FileReader(mime_file); 51 52 try { 53 parse(new BufferedReader(fr)); 54 } finally { 55 try { 56 fr.close(); } catch (IOException e) { 58 } 60 } 61 } 62 63 public MimeTypeFile(InputStream is) throws IOException { 64 parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1"))); 65 } 66 67 70 public MimeTypeFile() { 71 } 72 73 76 public MimeTypeEntry getMimeTypeEntry(String file_ext) { 77 return (MimeTypeEntry)type_hash.get((Object )file_ext); 78 } 79 80 83 public String getMIMETypeString(String file_ext) { 84 MimeTypeEntry entry = this.getMimeTypeEntry(file_ext); 85 86 if (entry != null) 87 return entry.getMIMEType(); 88 else 89 return null; 90 } 91 92 107 public void appendToRegistry(String mime_types) { 108 try { 109 parse(new BufferedReader(new StringReader(mime_types))); 110 } catch (IOException ex) { 111 } 113 } 114 115 118 private void parse(BufferedReader buf_reader) throws IOException { 119 String line = null, prev = null; 120 121 while ((line = buf_reader.readLine()) != null) { 122 if (prev == null) 123 prev = line; 124 else 125 prev += line; 126 int end = prev.length(); 127 if (prev.length() > 0 && prev.charAt(end - 1) == '\\') { 128 prev = prev.substring(0, end - 1); 129 continue; 130 } 131 this.parseEntry(prev); 132 prev = null; 133 } 134 if (prev != null) 135 this.parseEntry(prev); 136 } 137 138 141 private void parseEntry(String line) { 142 String mime_type = null; 143 String file_ext = null; 144 line = line.trim(); 145 146 if (line.length() == 0) return; 149 if (line.charAt(0) == '#') 151 return; 153 if (line.indexOf('=') > 0) { 155 LineTokenizer lt = new LineTokenizer(line); 157 while (lt.hasMoreTokens()) { 158 String name = lt.nextToken(); 159 String value = null; 160 if (lt.hasMoreTokens() && lt.nextToken().equals("=") && 161 lt.hasMoreTokens()) 162 value = lt.nextToken(); 163 if (value == null) { 164 if (LogSupport.isLoggable()) 165 LogSupport.log("Bad .mime.types entry: " + line); 166 return; 167 } 168 if (name.equals("type")) 169 mime_type = value; 170 else if (name.equals("exts")) { 171 StringTokenizer st = new StringTokenizer(value, ","); 172 while (st.hasMoreTokens()) { 173 file_ext = st.nextToken(); 174 MimeTypeEntry entry = 175 new MimeTypeEntry(mime_type, file_ext); 176 type_hash.put(file_ext, entry); 177 if (LogSupport.isLoggable()) 178 LogSupport.log("Added: " + entry.toString()); 179 } 180 } 181 } 182 } else { 183 StringTokenizer strtok = new StringTokenizer(line); 186 int num_tok = strtok.countTokens(); 187 188 if (num_tok == 0) return; 190 191 mime_type = strtok.nextToken(); 193 while (strtok.hasMoreTokens()) { 194 MimeTypeEntry entry = null; 195 196 file_ext = strtok.nextToken(); 197 entry = new MimeTypeEntry(mime_type, file_ext); 198 type_hash.put(file_ext, entry); 199 if (LogSupport.isLoggable()) 200 LogSupport.log("Added: " + entry.toString()); 201 } 202 } 203 } 204 205 214 } 215 216 class LineTokenizer { 217 private int currentPosition; 218 private int maxPosition; 219 private String str; 220 private Vector stack = new Vector(); 221 private static final String singles = "="; 223 229 public LineTokenizer(String str) { 230 currentPosition = 0; 231 this.str = str; 232 maxPosition = str.length(); 233 } 234 235 238 private void skipWhiteSpace() { 239 while ((currentPosition < maxPosition) && 240 Character.isWhitespace(str.charAt(currentPosition))) { 241 currentPosition++; 242 } 243 } 244 245 251 public boolean hasMoreTokens() { 252 if (stack.size() > 0) 253 return true; 254 skipWhiteSpace(); 255 return (currentPosition < maxPosition); 256 } 257 258 265 public String nextToken() { 266 int size = stack.size(); 267 if (size > 0) { 268 String t = (String )stack.elementAt(size - 1); 269 stack.removeElementAt(size - 1); 270 return t; 271 } 272 skipWhiteSpace(); 273 274 if (currentPosition >= maxPosition) { 275 throw new NoSuchElementException(); 276 } 277 278 int start = currentPosition; 279 char c = str.charAt(start); 280 if (c == '"') { 281 currentPosition++; 282 boolean filter = false; 283 while (currentPosition < maxPosition) { 284 c = str.charAt(currentPosition++); 285 if (c == '\\') { 286 currentPosition++; 287 filter = true; 288 } else if (c == '"') { 289 String s; 290 291 if (filter) { 292 StringBuffer sb = new StringBuffer (); 293 for (int i = start + 1; i < currentPosition - 1; i++) { 294 c = str.charAt(i); 295 if (c != '\\') 296 sb.append(c); 297 } 298 s = sb.toString(); 299 } else 300 s = str.substring(start + 1, currentPosition - 1); 301 return s; 302 } 303 } 304 } else if (singles.indexOf(c) >= 0) { 305 currentPosition++; 306 } else { 307 while ((currentPosition < maxPosition) && 308 singles.indexOf(str.charAt(currentPosition)) < 0 && 309 !Character.isWhitespace(str.charAt(currentPosition))) { 310 currentPosition++; 311 } 312 } 313 return str.substring(start, currentPosition); 314 } 315 316 public void pushToken(String token) { 317 stack.addElement(token); 318 } 319 } 320 | Popular Tags |