1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/com/sun/activation/registries/MimeTypeFile.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,317 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.activation.registries; 1.30 + 1.31 +import java.io.*; 1.32 +import java.util.*; 1.33 + 1.34 +public class MimeTypeFile { 1.35 + private String fname = null; 1.36 + private Hashtable type_hash = new Hashtable(); 1.37 + 1.38 + /** 1.39 + * The construtor that takes a filename as an argument. 1.40 + * 1.41 + * @param new_fname The file name of the mime types file. 1.42 + */ 1.43 + public MimeTypeFile(String new_fname) throws IOException { 1.44 + File mime_file = null; 1.45 + FileReader fr = null; 1.46 + 1.47 + fname = new_fname; // remember the file name 1.48 + 1.49 + mime_file = new File(fname); // get a file object 1.50 + 1.51 + fr = new FileReader(mime_file); 1.52 + 1.53 + try { 1.54 + parse(new BufferedReader(fr)); 1.55 + } finally { 1.56 + try { 1.57 + fr.close(); // close it 1.58 + } catch (IOException e) { 1.59 + // ignore it 1.60 + } 1.61 + } 1.62 + } 1.63 + 1.64 + public MimeTypeFile(InputStream is) throws IOException { 1.65 + parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1"))); 1.66 + } 1.67 + 1.68 + /** 1.69 + * Creates an empty DB. 1.70 + */ 1.71 + public MimeTypeFile() { 1.72 + } 1.73 + 1.74 + /** 1.75 + * get the MimeTypeEntry based on the file extension 1.76 + */ 1.77 + public MimeTypeEntry getMimeTypeEntry(String file_ext) { 1.78 + return (MimeTypeEntry)type_hash.get((Object)file_ext); 1.79 + } 1.80 + 1.81 + /** 1.82 + * Get the MIME type string corresponding to the file extension. 1.83 + */ 1.84 + public String getMIMETypeString(String file_ext) { 1.85 + MimeTypeEntry entry = this.getMimeTypeEntry(file_ext); 1.86 + 1.87 + if (entry != null) 1.88 + return entry.getMIMEType(); 1.89 + else 1.90 + return null; 1.91 + } 1.92 + 1.93 + /** 1.94 + * Appends string of entries to the types registry, must be valid 1.95 + * .mime.types format. 1.96 + * A mime.types entry is one of two forms: 1.97 + * 1.98 + * type/subtype ext1 ext2 ... 1.99 + * or 1.100 + * type=type/subtype desc="description of type" exts=ext1,ext2,... 1.101 + * 1.102 + * Example: 1.103 + * # this is a test 1.104 + * audio/basic au 1.105 + * text/plain txt text 1.106 + * type=application/postscript exts=ps,eps 1.107 + */ 1.108 + public void appendToRegistry(String mime_types) { 1.109 + try { 1.110 + parse(new BufferedReader(new StringReader(mime_types))); 1.111 + } catch (IOException ex) { 1.112 + // can't happen 1.113 + } 1.114 + } 1.115 + 1.116 + /** 1.117 + * Parse a stream of mime.types entries. 1.118 + */ 1.119 + private void parse(BufferedReader buf_reader) throws IOException { 1.120 + String line = null, prev = null; 1.121 + 1.122 + while ((line = buf_reader.readLine()) != null) { 1.123 + if (prev == null) 1.124 + prev = line; 1.125 + else 1.126 + prev += line; 1.127 + int end = prev.length(); 1.128 + if (prev.length() > 0 && prev.charAt(end - 1) == '\\') { 1.129 + prev = prev.substring(0, end - 1); 1.130 + continue; 1.131 + } 1.132 + this.parseEntry(prev); 1.133 + prev = null; 1.134 + } 1.135 + if (prev != null) 1.136 + this.parseEntry(prev); 1.137 + } 1.138 + 1.139 + /** 1.140 + * Parse single mime.types entry. 1.141 + */ 1.142 + private void parseEntry(String line) { 1.143 + String mime_type = null; 1.144 + String file_ext = null; 1.145 + line = line.trim(); 1.146 + 1.147 + if (line.length() == 0) // empty line... 1.148 + return; // BAIL! 1.149 + 1.150 + // check to see if this is a comment line? 1.151 + if (line.charAt(0) == '#') 1.152 + return; // then we are done! 1.153 + 1.154 + // is it a new format line or old format? 1.155 + if (line.indexOf('=') > 0) { 1.156 + // new format 1.157 + LineTokenizer lt = new LineTokenizer(line); 1.158 + while (lt.hasMoreTokens()) { 1.159 + String name = lt.nextToken(); 1.160 + String value = null; 1.161 + if (lt.hasMoreTokens() && lt.nextToken().equals("=") && 1.162 + lt.hasMoreTokens()) 1.163 + value = lt.nextToken(); 1.164 + if (value == null) { 1.165 + if (LogSupport.isLoggable()) 1.166 + LogSupport.log("Bad .mime.types entry: " + line); 1.167 + return; 1.168 + } 1.169 + if (name.equals("type")) 1.170 + mime_type = value; 1.171 + else if (name.equals("exts")) { 1.172 + StringTokenizer st = new StringTokenizer(value, ","); 1.173 + while (st.hasMoreTokens()) { 1.174 + file_ext = st.nextToken(); 1.175 + MimeTypeEntry entry = 1.176 + new MimeTypeEntry(mime_type, file_ext); 1.177 + type_hash.put(file_ext, entry); 1.178 + if (LogSupport.isLoggable()) 1.179 + LogSupport.log("Added: " + entry.toString()); 1.180 + } 1.181 + } 1.182 + } 1.183 + } else { 1.184 + // old format 1.185 + // count the tokens 1.186 + StringTokenizer strtok = new StringTokenizer(line); 1.187 + int num_tok = strtok.countTokens(); 1.188 + 1.189 + if (num_tok == 0) // empty line 1.190 + return; 1.191 + 1.192 + mime_type = strtok.nextToken(); // get the MIME type 1.193 + 1.194 + while (strtok.hasMoreTokens()) { 1.195 + MimeTypeEntry entry = null; 1.196 + 1.197 + file_ext = strtok.nextToken(); 1.198 + entry = new MimeTypeEntry(mime_type, file_ext); 1.199 + type_hash.put(file_ext, entry); 1.200 + if (LogSupport.isLoggable()) 1.201 + LogSupport.log("Added: " + entry.toString()); 1.202 + } 1.203 + } 1.204 + } 1.205 + 1.206 + // for debugging 1.207 + /* 1.208 + public static void main(String[] argv) throws Exception { 1.209 + MimeTypeFile mf = new MimeTypeFile(argv[0]); 1.210 + System.out.println("ext " + argv[1] + " type " + 1.211 + mf.getMIMETypeString(argv[1])); 1.212 + System.exit(0); 1.213 + } 1.214 + */ 1.215 +} 1.216 + 1.217 +class LineTokenizer { 1.218 + private int currentPosition; 1.219 + private int maxPosition; 1.220 + private String str; 1.221 + private Vector stack = new Vector(); 1.222 + private static final String singles = "="; // single character tokens 1.223 + 1.224 + /** 1.225 + * Constructs a tokenizer for the specified string. 1.226 + * <p> 1.227 + * 1.228 + * @param str a string to be parsed. 1.229 + */ 1.230 + public LineTokenizer(String str) { 1.231 + currentPosition = 0; 1.232 + this.str = str; 1.233 + maxPosition = str.length(); 1.234 + } 1.235 + 1.236 + /** 1.237 + * Skips white space. 1.238 + */ 1.239 + private void skipWhiteSpace() { 1.240 + while ((currentPosition < maxPosition) && 1.241 + Character.isWhitespace(str.charAt(currentPosition))) { 1.242 + currentPosition++; 1.243 + } 1.244 + } 1.245 + 1.246 + /** 1.247 + * Tests if there are more tokens available from this tokenizer's string. 1.248 + * 1.249 + * @return <code>true</code> if there are more tokens available from this 1.250 + * tokenizer's string; <code>false</code> otherwise. 1.251 + */ 1.252 + public boolean hasMoreTokens() { 1.253 + if (stack.size() > 0) 1.254 + return true; 1.255 + skipWhiteSpace(); 1.256 + return (currentPosition < maxPosition); 1.257 + } 1.258 + 1.259 + /** 1.260 + * Returns the next token from this tokenizer. 1.261 + * 1.262 + * @return the next token from this tokenizer. 1.263 + * @exception NoSuchElementException if there are no more tokens in this 1.264 + * tokenizer's string. 1.265 + */ 1.266 + public String nextToken() { 1.267 + int size = stack.size(); 1.268 + if (size > 0) { 1.269 + String t = (String)stack.elementAt(size - 1); 1.270 + stack.removeElementAt(size - 1); 1.271 + return t; 1.272 + } 1.273 + skipWhiteSpace(); 1.274 + 1.275 + if (currentPosition >= maxPosition) { 1.276 + throw new NoSuchElementException(); 1.277 + } 1.278 + 1.279 + int start = currentPosition; 1.280 + char c = str.charAt(start); 1.281 + if (c == '"') { 1.282 + currentPosition++; 1.283 + boolean filter = false; 1.284 + while (currentPosition < maxPosition) { 1.285 + c = str.charAt(currentPosition++); 1.286 + if (c == '\\') { 1.287 + currentPosition++; 1.288 + filter = true; 1.289 + } else if (c == '"') { 1.290 + String s; 1.291 + 1.292 + if (filter) { 1.293 + StringBuffer sb = new StringBuffer(); 1.294 + for (int i = start + 1; i < currentPosition - 1; i++) { 1.295 + c = str.charAt(i); 1.296 + if (c != '\\') 1.297 + sb.append(c); 1.298 + } 1.299 + s = sb.toString(); 1.300 + } else 1.301 + s = str.substring(start + 1, currentPosition - 1); 1.302 + return s; 1.303 + } 1.304 + } 1.305 + } else if (singles.indexOf(c) >= 0) { 1.306 + currentPosition++; 1.307 + } else { 1.308 + while ((currentPosition < maxPosition) && 1.309 + singles.indexOf(str.charAt(currentPosition)) < 0 && 1.310 + !Character.isWhitespace(str.charAt(currentPosition))) { 1.311 + currentPosition++; 1.312 + } 1.313 + } 1.314 + return str.substring(start, currentPosition); 1.315 + } 1.316 + 1.317 + public void pushToken(String token) { 1.318 + stack.addElement(token); 1.319 + } 1.320 +}