Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.activation.registries; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | import java.util.*; |
aoqi@0 | 30 | |
aoqi@0 | 31 | public class MimeTypeFile { |
aoqi@0 | 32 | private String fname = null; |
aoqi@0 | 33 | private Hashtable type_hash = new Hashtable(); |
aoqi@0 | 34 | |
aoqi@0 | 35 | /** |
aoqi@0 | 36 | * The construtor that takes a filename as an argument. |
aoqi@0 | 37 | * |
aoqi@0 | 38 | * @param new_fname The file name of the mime types file. |
aoqi@0 | 39 | */ |
aoqi@0 | 40 | public MimeTypeFile(String new_fname) throws IOException { |
aoqi@0 | 41 | File mime_file = null; |
aoqi@0 | 42 | FileReader fr = null; |
aoqi@0 | 43 | |
aoqi@0 | 44 | fname = new_fname; // remember the file name |
aoqi@0 | 45 | |
aoqi@0 | 46 | mime_file = new File(fname); // get a file object |
aoqi@0 | 47 | |
aoqi@0 | 48 | fr = new FileReader(mime_file); |
aoqi@0 | 49 | |
aoqi@0 | 50 | try { |
aoqi@0 | 51 | parse(new BufferedReader(fr)); |
aoqi@0 | 52 | } finally { |
aoqi@0 | 53 | try { |
aoqi@0 | 54 | fr.close(); // close it |
aoqi@0 | 55 | } catch (IOException e) { |
aoqi@0 | 56 | // ignore it |
aoqi@0 | 57 | } |
aoqi@0 | 58 | } |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | public MimeTypeFile(InputStream is) throws IOException { |
aoqi@0 | 62 | parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1"))); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * Creates an empty DB. |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | public MimeTypeFile() { |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * get the MimeTypeEntry based on the file extension |
aoqi@0 | 73 | */ |
aoqi@0 | 74 | public MimeTypeEntry getMimeTypeEntry(String file_ext) { |
aoqi@0 | 75 | return (MimeTypeEntry)type_hash.get((Object)file_ext); |
aoqi@0 | 76 | } |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * Get the MIME type string corresponding to the file extension. |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | public String getMIMETypeString(String file_ext) { |
aoqi@0 | 82 | MimeTypeEntry entry = this.getMimeTypeEntry(file_ext); |
aoqi@0 | 83 | |
aoqi@0 | 84 | if (entry != null) |
aoqi@0 | 85 | return entry.getMIMEType(); |
aoqi@0 | 86 | else |
aoqi@0 | 87 | return null; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * Appends string of entries to the types registry, must be valid |
aoqi@0 | 92 | * .mime.types format. |
aoqi@0 | 93 | * A mime.types entry is one of two forms: |
aoqi@0 | 94 | * |
aoqi@0 | 95 | * type/subtype ext1 ext2 ... |
aoqi@0 | 96 | * or |
aoqi@0 | 97 | * type=type/subtype desc="description of type" exts=ext1,ext2,... |
aoqi@0 | 98 | * |
aoqi@0 | 99 | * Example: |
aoqi@0 | 100 | * # this is a test |
aoqi@0 | 101 | * audio/basic au |
aoqi@0 | 102 | * text/plain txt text |
aoqi@0 | 103 | * type=application/postscript exts=ps,eps |
aoqi@0 | 104 | */ |
aoqi@0 | 105 | public void appendToRegistry(String mime_types) { |
aoqi@0 | 106 | try { |
aoqi@0 | 107 | parse(new BufferedReader(new StringReader(mime_types))); |
aoqi@0 | 108 | } catch (IOException ex) { |
aoqi@0 | 109 | // can't happen |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Parse a stream of mime.types entries. |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | private void parse(BufferedReader buf_reader) throws IOException { |
aoqi@0 | 117 | String line = null, prev = null; |
aoqi@0 | 118 | |
aoqi@0 | 119 | while ((line = buf_reader.readLine()) != null) { |
aoqi@0 | 120 | if (prev == null) |
aoqi@0 | 121 | prev = line; |
aoqi@0 | 122 | else |
aoqi@0 | 123 | prev += line; |
aoqi@0 | 124 | int end = prev.length(); |
aoqi@0 | 125 | if (prev.length() > 0 && prev.charAt(end - 1) == '\\') { |
aoqi@0 | 126 | prev = prev.substring(0, end - 1); |
aoqi@0 | 127 | continue; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | this.parseEntry(prev); |
aoqi@0 | 130 | prev = null; |
aoqi@0 | 131 | } |
aoqi@0 | 132 | if (prev != null) |
aoqi@0 | 133 | this.parseEntry(prev); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | /** |
aoqi@0 | 137 | * Parse single mime.types entry. |
aoqi@0 | 138 | */ |
aoqi@0 | 139 | private void parseEntry(String line) { |
aoqi@0 | 140 | String mime_type = null; |
aoqi@0 | 141 | String file_ext = null; |
aoqi@0 | 142 | line = line.trim(); |
aoqi@0 | 143 | |
aoqi@0 | 144 | if (line.length() == 0) // empty line... |
aoqi@0 | 145 | return; // BAIL! |
aoqi@0 | 146 | |
aoqi@0 | 147 | // check to see if this is a comment line? |
aoqi@0 | 148 | if (line.charAt(0) == '#') |
aoqi@0 | 149 | return; // then we are done! |
aoqi@0 | 150 | |
aoqi@0 | 151 | // is it a new format line or old format? |
aoqi@0 | 152 | if (line.indexOf('=') > 0) { |
aoqi@0 | 153 | // new format |
aoqi@0 | 154 | LineTokenizer lt = new LineTokenizer(line); |
aoqi@0 | 155 | while (lt.hasMoreTokens()) { |
aoqi@0 | 156 | String name = lt.nextToken(); |
aoqi@0 | 157 | String value = null; |
aoqi@0 | 158 | if (lt.hasMoreTokens() && lt.nextToken().equals("=") && |
aoqi@0 | 159 | lt.hasMoreTokens()) |
aoqi@0 | 160 | value = lt.nextToken(); |
aoqi@0 | 161 | if (value == null) { |
aoqi@0 | 162 | if (LogSupport.isLoggable()) |
aoqi@0 | 163 | LogSupport.log("Bad .mime.types entry: " + line); |
aoqi@0 | 164 | return; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | if (name.equals("type")) |
aoqi@0 | 167 | mime_type = value; |
aoqi@0 | 168 | else if (name.equals("exts")) { |
aoqi@0 | 169 | StringTokenizer st = new StringTokenizer(value, ","); |
aoqi@0 | 170 | while (st.hasMoreTokens()) { |
aoqi@0 | 171 | file_ext = st.nextToken(); |
aoqi@0 | 172 | MimeTypeEntry entry = |
aoqi@0 | 173 | new MimeTypeEntry(mime_type, file_ext); |
aoqi@0 | 174 | type_hash.put(file_ext, entry); |
aoqi@0 | 175 | if (LogSupport.isLoggable()) |
aoqi@0 | 176 | LogSupport.log("Added: " + entry.toString()); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | } |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } else { |
aoqi@0 | 181 | // old format |
aoqi@0 | 182 | // count the tokens |
aoqi@0 | 183 | StringTokenizer strtok = new StringTokenizer(line); |
aoqi@0 | 184 | int num_tok = strtok.countTokens(); |
aoqi@0 | 185 | |
aoqi@0 | 186 | if (num_tok == 0) // empty line |
aoqi@0 | 187 | return; |
aoqi@0 | 188 | |
aoqi@0 | 189 | mime_type = strtok.nextToken(); // get the MIME type |
aoqi@0 | 190 | |
aoqi@0 | 191 | while (strtok.hasMoreTokens()) { |
aoqi@0 | 192 | MimeTypeEntry entry = null; |
aoqi@0 | 193 | |
aoqi@0 | 194 | file_ext = strtok.nextToken(); |
aoqi@0 | 195 | entry = new MimeTypeEntry(mime_type, file_ext); |
aoqi@0 | 196 | type_hash.put(file_ext, entry); |
aoqi@0 | 197 | if (LogSupport.isLoggable()) |
aoqi@0 | 198 | LogSupport.log("Added: " + entry.toString()); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | // for debugging |
aoqi@0 | 204 | /* |
aoqi@0 | 205 | public static void main(String[] argv) throws Exception { |
aoqi@0 | 206 | MimeTypeFile mf = new MimeTypeFile(argv[0]); |
aoqi@0 | 207 | System.out.println("ext " + argv[1] + " type " + |
aoqi@0 | 208 | mf.getMIMETypeString(argv[1])); |
aoqi@0 | 209 | System.exit(0); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | */ |
aoqi@0 | 212 | } |
aoqi@0 | 213 | |
aoqi@0 | 214 | class LineTokenizer { |
aoqi@0 | 215 | private int currentPosition; |
aoqi@0 | 216 | private int maxPosition; |
aoqi@0 | 217 | private String str; |
aoqi@0 | 218 | private Vector stack = new Vector(); |
aoqi@0 | 219 | private static final String singles = "="; // single character tokens |
aoqi@0 | 220 | |
aoqi@0 | 221 | /** |
aoqi@0 | 222 | * Constructs a tokenizer for the specified string. |
aoqi@0 | 223 | * <p> |
aoqi@0 | 224 | * |
aoqi@0 | 225 | * @param str a string to be parsed. |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | public LineTokenizer(String str) { |
aoqi@0 | 228 | currentPosition = 0; |
aoqi@0 | 229 | this.str = str; |
aoqi@0 | 230 | maxPosition = str.length(); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | /** |
aoqi@0 | 234 | * Skips white space. |
aoqi@0 | 235 | */ |
aoqi@0 | 236 | private void skipWhiteSpace() { |
aoqi@0 | 237 | while ((currentPosition < maxPosition) && |
aoqi@0 | 238 | Character.isWhitespace(str.charAt(currentPosition))) { |
aoqi@0 | 239 | currentPosition++; |
aoqi@0 | 240 | } |
aoqi@0 | 241 | } |
aoqi@0 | 242 | |
aoqi@0 | 243 | /** |
aoqi@0 | 244 | * Tests if there are more tokens available from this tokenizer's string. |
aoqi@0 | 245 | * |
aoqi@0 | 246 | * @return <code>true</code> if there are more tokens available from this |
aoqi@0 | 247 | * tokenizer's string; <code>false</code> otherwise. |
aoqi@0 | 248 | */ |
aoqi@0 | 249 | public boolean hasMoreTokens() { |
aoqi@0 | 250 | if (stack.size() > 0) |
aoqi@0 | 251 | return true; |
aoqi@0 | 252 | skipWhiteSpace(); |
aoqi@0 | 253 | return (currentPosition < maxPosition); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | /** |
aoqi@0 | 257 | * Returns the next token from this tokenizer. |
aoqi@0 | 258 | * |
aoqi@0 | 259 | * @return the next token from this tokenizer. |
aoqi@0 | 260 | * @exception NoSuchElementException if there are no more tokens in this |
aoqi@0 | 261 | * tokenizer's string. |
aoqi@0 | 262 | */ |
aoqi@0 | 263 | public String nextToken() { |
aoqi@0 | 264 | int size = stack.size(); |
aoqi@0 | 265 | if (size > 0) { |
aoqi@0 | 266 | String t = (String)stack.elementAt(size - 1); |
aoqi@0 | 267 | stack.removeElementAt(size - 1); |
aoqi@0 | 268 | return t; |
aoqi@0 | 269 | } |
aoqi@0 | 270 | skipWhiteSpace(); |
aoqi@0 | 271 | |
aoqi@0 | 272 | if (currentPosition >= maxPosition) { |
aoqi@0 | 273 | throw new NoSuchElementException(); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | int start = currentPosition; |
aoqi@0 | 277 | char c = str.charAt(start); |
aoqi@0 | 278 | if (c == '"') { |
aoqi@0 | 279 | currentPosition++; |
aoqi@0 | 280 | boolean filter = false; |
aoqi@0 | 281 | while (currentPosition < maxPosition) { |
aoqi@0 | 282 | c = str.charAt(currentPosition++); |
aoqi@0 | 283 | if (c == '\\') { |
aoqi@0 | 284 | currentPosition++; |
aoqi@0 | 285 | filter = true; |
aoqi@0 | 286 | } else if (c == '"') { |
aoqi@0 | 287 | String s; |
aoqi@0 | 288 | |
aoqi@0 | 289 | if (filter) { |
aoqi@0 | 290 | StringBuffer sb = new StringBuffer(); |
aoqi@0 | 291 | for (int i = start + 1; i < currentPosition - 1; i++) { |
aoqi@0 | 292 | c = str.charAt(i); |
aoqi@0 | 293 | if (c != '\\') |
aoqi@0 | 294 | sb.append(c); |
aoqi@0 | 295 | } |
aoqi@0 | 296 | s = sb.toString(); |
aoqi@0 | 297 | } else |
aoqi@0 | 298 | s = str.substring(start + 1, currentPosition - 1); |
aoqi@0 | 299 | return s; |
aoqi@0 | 300 | } |
aoqi@0 | 301 | } |
aoqi@0 | 302 | } else if (singles.indexOf(c) >= 0) { |
aoqi@0 | 303 | currentPosition++; |
aoqi@0 | 304 | } else { |
aoqi@0 | 305 | while ((currentPosition < maxPosition) && |
aoqi@0 | 306 | singles.indexOf(str.charAt(currentPosition)) < 0 && |
aoqi@0 | 307 | !Character.isWhitespace(str.charAt(currentPosition))) { |
aoqi@0 | 308 | currentPosition++; |
aoqi@0 | 309 | } |
aoqi@0 | 310 | } |
aoqi@0 | 311 | return str.substring(start, currentPosition); |
aoqi@0 | 312 | } |
aoqi@0 | 313 | |
aoqi@0 | 314 | public void pushToken(String token) { |
aoqi@0 | 315 | stack.addElement(token); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |