jdk/src/share/classes/sun/net/www/MimeTable.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1994-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.FileNameMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class MimeTable implements FileNameMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /** Keyed by content type, returns MimeEntries */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    40
    private Hashtable<String, MimeEntry> entries
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    41
        = new Hashtable<String, MimeEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** Keyed by file extension (with the .), returns MimeEntries */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    44
    private Hashtable<String, MimeEntry> extensionMap
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    45
        = new Hashtable<String, MimeEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Will be reset if in the platform-specific data file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String tempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    52
            new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    53
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                tempFileTemplate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    System.getProperty("content.types.temp.file.template",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                       "/tmp/%s");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                mailcapLocations = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    System.getProperty("user.mailcap"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    System.getProperty("user.home") + "/.mailcap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    "/etc/mailcap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    "/usr/etc/mailcap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    "/usr/local/etc/mailcap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    System.getProperty("hotjava.home",
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    65
                                           "/usr/local/hotjava")
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    66
                        + "/lib/mailcap",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String filePreamble = "sun.net.www MIME content-types table";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final String fileMagic = "#" + filePreamble;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static MimeTable defaultInstance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    MimeTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Get the single instance of this class.  First use will load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * table from a data file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static MimeTable getDefaultTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (defaultInstance == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    89
                new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    90
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    defaultInstance = new MimeTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    URLConnection.setFileNameMap(defaultInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return defaultInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static FileNameMap loadTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        MimeTable mt = getDefaultTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return (FileNameMap)mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public synchronized int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public synchronized String getContentTypeFor(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        MimeEntry entry = findByFileName(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return entry.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public synchronized void add(MimeEntry m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        entries.put(m.getType(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String exts[] = m.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (exts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i < exts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            extensionMap.put(exts[i], m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public synchronized MimeEntry remove(String type) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   136
        MimeEntry entry = entries.get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return remove(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public synchronized MimeEntry remove(MimeEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String[] extensionKeys = entry.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (extensionKeys != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (int i = 0; i < extensionKeys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                extensionMap.remove(extensionKeys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   148
        return entries.remove(entry.getType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public synchronized MimeEntry find(String type) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   152
        MimeEntry entry = entries.get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // try a wildcard lookup
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   155
            Enumeration<MimeEntry> e = entries.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            while (e.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   157
                MimeEntry wild = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (wild.matches(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    return wild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Locate a MimeEntry by the file extension that has been associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * with it. Parses general file names, and URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public MimeEntry findByFileName(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String ext = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int i = fname.lastIndexOf('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            fname = fname.substring(0, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        i = fname.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // REMIND: OS specific delimters appear here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        i = Math.max(i, fname.lastIndexOf('/'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        i = Math.max(i, fname.lastIndexOf('?'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (i != -1 && fname.charAt(i) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            ext = fname.substring(i).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return findByExt(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Locate a MimeEntry by the file extension that has been associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public synchronized MimeEntry findByExt(String fileExtension) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   197
        return extensionMap.get(fileExtension);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public synchronized MimeEntry findByDescription(String description) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   201
        Enumeration<MimeEntry> e = elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        while (e.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   203
            MimeEntry entry = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (description.equals(entry.getDescription())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // We failed, now try treating description as type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return find(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    String getTempFileTemplate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return tempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   217
    public synchronized Enumeration<MimeEntry> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return entries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // For backward compatibility -- mailcap format files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // This is not currently used, but may in the future when we add ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // to read BOTH the properties format and the mailcap format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    protected static String[] mailcapLocations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public synchronized void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Properties entries = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        File file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // First try to load the user-specific table, if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            String userTablePath =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                System.getProperty("content.types.user.table");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (userTablePath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                file = new File(userTablePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    // No user-table, try to load the default built-in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    file = new File(System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                    File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                    "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                    File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                    "content-types.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // No user table, try to load the default built-in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                file = new File(System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                "content-types.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            is = new BufferedInputStream(new FileInputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            entries.load(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            System.err.println("Warning: default mime table not found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                               file.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        parse(entries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    void parse(Properties entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // first, strip out the platform-specific temp file template
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        String tempFileTemplate = (String)entries.get("temp.file.template");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (tempFileTemplate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            entries.remove("temp.file.template");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            this.tempFileTemplate = tempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // now, parse the mime-type spec's
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   275
        Enumeration<?> types = entries.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        while (types.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            String type = (String)types.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            String attrs = entries.getProperty(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            parse(type, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // Table format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // <entry> ::= <table_tag> | <type_entry>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    // <table_tag> ::= <table_format_version> | <temp_file_template>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // <type_entry> ::= <type_subtype_pair> '=' <type_attrs_list>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // <type_subtype_pair> ::= <type> '/' <subtype>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // <type_attrs_list> ::= <attr_value_pair> [ ';' <attr_value_pair> ]*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    //                       | [ <attr_value_pair> ]+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // <attr_value_pair> ::= <attr_name> '=' <attr_value>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    // <attr_name> ::= 'description' | 'action' | 'application'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    //                 | 'file_extensions' | 'icon'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // <attr_value> ::= <legal_char>*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    // Embedded ';' in an <attr_value> are quoted with leading '\' .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // Interpretation of <attr_value> depends on the <attr_name> it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    // associated with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    void parse(String type, String attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        MimeEntry newEntry = new MimeEntry(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // REMIND handle embedded ';' and '|' and literal '"'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        StringTokenizer tokenizer = new StringTokenizer(attrs, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            String pair = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            parse(pair, newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        add(newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    void parse(String pair, MimeEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // REMIND add exception handling...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        boolean gotName = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        StringTokenizer tokenizer = new StringTokenizer(pair, "=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (gotName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                value = tokenizer.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                name = tokenizer.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                gotName = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        fill(entry, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    void fill(MimeEntry entry, String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if ("description".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            entry.setDescription(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        else if ("action".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            entry.setAction(getActionCode(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        else if ("application".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            entry.setCommand(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        else if ("icon".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            entry.setImageFileName(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        else if ("file_extensions".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            entry.setExtensions(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // else illegal name exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    String[] getExtensions(String list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        StringTokenizer tokenizer = new StringTokenizer(list, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int n = tokenizer.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        String[] extensions = new String[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            extensions[i] = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    int getActionCode(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        for (int i = 0; i < MimeEntry.actionKeywords.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (action.equalsIgnoreCase(MimeEntry.actionKeywords[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return MimeEntry.UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public synchronized boolean save(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (filename == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            filename = System.getProperty("user.home" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                          File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                          "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                          File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                          "content-types.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return saveAsProperties(new File(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public Properties getAsProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        Properties properties = new Properties();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   398
        Enumeration<MimeEntry> e = elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        while (e.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   400
            MimeEntry entry = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            properties.put(entry.getType(), entry.toProperty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected boolean saveAsProperties(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        FileOutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            os = new FileOutputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Properties properties = getAsProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            properties.put("temp.file.template", tempFileTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            String tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            String user = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (user != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                tag = "; customized for " + user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                properties.save(os, filePreamble + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                properties.save(os, filePreamble);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                try { os.close(); } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Debugging utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public void list(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        Enumeration keys = entries.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            String key = (String)keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            MimeEntry entry = (MimeEntry)entries.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            out.println(key + ": " + entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        MimeTable testTable = MimeTable.getDefaultTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        Enumeration e = testTable.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            MimeEntry entry = (MimeEntry)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            System.out.println(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        testTable.save(File.separator + "tmp" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                       File.separator + "mime_table.save");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
}