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