325N/A/*
325N/A * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.activation.registries;
325N/A
325N/Aimport java.io.*;
325N/Aimport java.util.*;
325N/A
325N/Apublic class MimeTypeFile {
325N/A private String fname = null;
325N/A private Hashtable type_hash = new Hashtable();
325N/A
325N/A /**
325N/A * The construtor that takes a filename as an argument.
325N/A *
325N/A * @param new_fname The file name of the mime types file.
325N/A */
325N/A public MimeTypeFile(String new_fname) throws IOException {
325N/A File mime_file = null;
325N/A FileReader fr = null;
325N/A
325N/A fname = new_fname; // remember the file name
325N/A
325N/A mime_file = new File(fname); // get a file object
325N/A
325N/A fr = new FileReader(mime_file);
325N/A
325N/A try {
325N/A parse(new BufferedReader(fr));
325N/A } finally {
325N/A try {
325N/A fr.close(); // close it
325N/A } catch (IOException e) {
325N/A // ignore it
325N/A }
325N/A }
325N/A }
325N/A
325N/A public MimeTypeFile(InputStream is) throws IOException {
325N/A parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
325N/A }
325N/A
325N/A /**
325N/A * Creates an empty DB.
325N/A */
325N/A public MimeTypeFile() {
325N/A }
325N/A
325N/A /**
325N/A * get the MimeTypeEntry based on the file extension
325N/A */
325N/A public MimeTypeEntry getMimeTypeEntry(String file_ext) {
325N/A return (MimeTypeEntry)type_hash.get((Object)file_ext);
325N/A }
325N/A
325N/A /**
325N/A * Get the MIME type string corresponding to the file extension.
325N/A */
325N/A public String getMIMETypeString(String file_ext) {
325N/A MimeTypeEntry entry = this.getMimeTypeEntry(file_ext);
325N/A
325N/A if (entry != null)
325N/A return entry.getMIMEType();
325N/A else
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * Appends string of entries to the types registry, must be valid
325N/A * .mime.types format.
325N/A * A mime.types entry is one of two forms:
325N/A *
325N/A * type/subtype ext1 ext2 ...
325N/A * or
325N/A * type=type/subtype desc="description of type" exts=ext1,ext2,...
325N/A *
325N/A * Example:
325N/A * # this is a test
325N/A * audio/basic au
325N/A * text/plain txt text
325N/A * type=application/postscript exts=ps,eps
325N/A */
325N/A public void appendToRegistry(String mime_types) {
325N/A try {
325N/A parse(new BufferedReader(new StringReader(mime_types)));
325N/A } catch (IOException ex) {
325N/A // can't happen
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Parse a stream of mime.types entries.
325N/A */
325N/A private void parse(BufferedReader buf_reader) throws IOException {
325N/A String line = null, prev = null;
325N/A
325N/A while ((line = buf_reader.readLine()) != null) {
325N/A if (prev == null)
325N/A prev = line;
325N/A else
325N/A prev += line;
325N/A int end = prev.length();
325N/A if (prev.length() > 0 && prev.charAt(end - 1) == '\\') {
325N/A prev = prev.substring(0, end - 1);
325N/A continue;
325N/A }
325N/A this.parseEntry(prev);
325N/A prev = null;
325N/A }
325N/A if (prev != null)
325N/A this.parseEntry(prev);
325N/A }
325N/A
325N/A /**
325N/A * Parse single mime.types entry.
325N/A */
325N/A private void parseEntry(String line) {
325N/A String mime_type = null;
325N/A String file_ext = null;
325N/A line = line.trim();
325N/A
325N/A if (line.length() == 0) // empty line...
325N/A return; // BAIL!
325N/A
325N/A // check to see if this is a comment line?
325N/A if (line.charAt(0) == '#')
325N/A return; // then we are done!
325N/A
325N/A // is it a new format line or old format?
325N/A if (line.indexOf('=') > 0) {
325N/A // new format
325N/A LineTokenizer lt = new LineTokenizer(line);
325N/A while (lt.hasMoreTokens()) {
325N/A String name = lt.nextToken();
325N/A String value = null;
325N/A if (lt.hasMoreTokens() && lt.nextToken().equals("=") &&
325N/A lt.hasMoreTokens())
325N/A value = lt.nextToken();
325N/A if (value == null) {
325N/A if (LogSupport.isLoggable())
325N/A LogSupport.log("Bad .mime.types entry: " + line);
325N/A return;
325N/A }
325N/A if (name.equals("type"))
325N/A mime_type = value;
325N/A else if (name.equals("exts")) {
325N/A StringTokenizer st = new StringTokenizer(value, ",");
325N/A while (st.hasMoreTokens()) {
325N/A file_ext = st.nextToken();
325N/A MimeTypeEntry entry =
325N/A new MimeTypeEntry(mime_type, file_ext);
325N/A type_hash.put(file_ext, entry);
325N/A if (LogSupport.isLoggable())
325N/A LogSupport.log("Added: " + entry.toString());
325N/A }
325N/A }
325N/A }
325N/A } else {
325N/A // old format
325N/A // count the tokens
325N/A StringTokenizer strtok = new StringTokenizer(line);
325N/A int num_tok = strtok.countTokens();
325N/A
325N/A if (num_tok == 0) // empty line
325N/A return;
325N/A
325N/A mime_type = strtok.nextToken(); // get the MIME type
325N/A
325N/A while (strtok.hasMoreTokens()) {
325N/A MimeTypeEntry entry = null;
325N/A
325N/A file_ext = strtok.nextToken();
325N/A entry = new MimeTypeEntry(mime_type, file_ext);
325N/A type_hash.put(file_ext, entry);
325N/A if (LogSupport.isLoggable())
325N/A LogSupport.log("Added: " + entry.toString());
325N/A }
325N/A }
325N/A }
325N/A
325N/A // for debugging
325N/A /*
325N/A public static void main(String[] argv) throws Exception {
325N/A MimeTypeFile mf = new MimeTypeFile(argv[0]);
325N/A System.out.println("ext " + argv[1] + " type " +
325N/A mf.getMIMETypeString(argv[1]));
325N/A System.exit(0);
325N/A }
325N/A */
325N/A}
325N/A
325N/Aclass LineTokenizer {
325N/A private int currentPosition;
325N/A private int maxPosition;
325N/A private String str;
325N/A private Vector stack = new Vector();
325N/A private static final String singles = "="; // single character tokens
325N/A
325N/A /**
325N/A * Constructs a tokenizer for the specified string.
325N/A * <p>
325N/A *
325N/A * @param str a string to be parsed.
325N/A */
325N/A public LineTokenizer(String str) {
325N/A currentPosition = 0;
325N/A this.str = str;
325N/A maxPosition = str.length();
325N/A }
325N/A
325N/A /**
325N/A * Skips white space.
325N/A */
325N/A private void skipWhiteSpace() {
325N/A while ((currentPosition < maxPosition) &&
325N/A Character.isWhitespace(str.charAt(currentPosition))) {
325N/A currentPosition++;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Tests if there are more tokens available from this tokenizer's string.
325N/A *
325N/A * @return <code>true</code> if there are more tokens available from this
325N/A * tokenizer's string; <code>false</code> otherwise.
325N/A */
325N/A public boolean hasMoreTokens() {
325N/A if (stack.size() > 0)
325N/A return true;
325N/A skipWhiteSpace();
325N/A return (currentPosition < maxPosition);
325N/A }
325N/A
325N/A /**
325N/A * Returns the next token from this tokenizer.
325N/A *
325N/A * @return the next token from this tokenizer.
325N/A * @exception NoSuchElementException if there are no more tokens in this
325N/A * tokenizer's string.
325N/A */
325N/A public String nextToken() {
325N/A int size = stack.size();
325N/A if (size > 0) {
325N/A String t = (String)stack.elementAt(size - 1);
325N/A stack.removeElementAt(size - 1);
325N/A return t;
325N/A }
325N/A skipWhiteSpace();
325N/A
325N/A if (currentPosition >= maxPosition) {
325N/A throw new NoSuchElementException();
325N/A }
325N/A
325N/A int start = currentPosition;
325N/A char c = str.charAt(start);
325N/A if (c == '"') {
325N/A currentPosition++;
325N/A boolean filter = false;
325N/A while (currentPosition < maxPosition) {
325N/A c = str.charAt(currentPosition++);
325N/A if (c == '\\') {
325N/A currentPosition++;
325N/A filter = true;
325N/A } else if (c == '"') {
325N/A String s;
325N/A
325N/A if (filter) {
325N/A StringBuffer sb = new StringBuffer();
325N/A for (int i = start + 1; i < currentPosition - 1; i++) {
325N/A c = str.charAt(i);
325N/A if (c != '\\')
325N/A sb.append(c);
325N/A }
325N/A s = sb.toString();
325N/A } else
325N/A s = str.substring(start + 1, currentPosition - 1);
325N/A return s;
325N/A }
325N/A }
325N/A } else if (singles.indexOf(c) >= 0) {
325N/A currentPosition++;
325N/A } else {
325N/A while ((currentPosition < maxPosition) &&
325N/A singles.indexOf(str.charAt(currentPosition)) < 0 &&
325N/A !Character.isWhitespace(str.charAt(currentPosition))) {
325N/A currentPosition++;
325N/A }
325N/A }
325N/A return str.substring(start, currentPosition);
325N/A }
325N/A
325N/A public void pushToken(String token) {
325N/A stack.addElement(token);
325N/A }
325N/A}