src/share/jaf_classes/javax/activation/MailcapCommandMap.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaf_classes/javax/activation/MailcapCommandMap.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,693 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +package javax.activation;
    1.31 +
    1.32 +import java.util.*;
    1.33 +import java.io.*;
    1.34 +import java.net.*;
    1.35 +import com.sun.activation.registries.MailcapFile;
    1.36 +import com.sun.activation.registries.LogSupport;
    1.37 +
    1.38 +/**
    1.39 + * MailcapCommandMap extends the CommandMap
    1.40 + * abstract class. It implements a CommandMap whose configuration
    1.41 + * is based on mailcap files
    1.42 + * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
    1.43 + * The MailcapCommandMap can be configured both programmatically
    1.44 + * and via configuration files.
    1.45 + * <p>
    1.46 + * <b>Mailcap file search order:</b><p>
    1.47 + * The MailcapCommandMap looks in various places in the user's
    1.48 + * system for mailcap file entries. When requests are made
    1.49 + * to search for commands in the MailcapCommandMap, it searches
    1.50 + * mailcap files in the following order:
    1.51 + * <p>
    1.52 + * <ol>
    1.53 + * <li> Programatically added entries to the MailcapCommandMap instance.
    1.54 + * <li> The file <code>.mailcap</code> in the user's home directory.
    1.55 + * <li> The file &lt;<i>java.home</i>&gt;<code>/lib/mailcap</code>.
    1.56 + * <li> The file or resources named <code>META-INF/mailcap</code>.
    1.57 + * <li> The file or resource named <code>META-INF/mailcap.default</code>
    1.58 + * (usually found only in the <code>activation.jar</code> file).
    1.59 + * </ol>
    1.60 + * <p>
    1.61 + * <b>Mailcap file format:</b><p>
    1.62 + *
    1.63 + * Mailcap files must conform to the mailcap
    1.64 + * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
    1.65 + * For Multimedia Mail Format Information</i>).
    1.66 + * The file format consists of entries corresponding to
    1.67 + * particular MIME types. In general, the specification
    1.68 + * specifies <i>applications</i> for clients to use when they
    1.69 + * themselves cannot operate on the specified MIME type. The
    1.70 + * MailcapCommandMap extends this specification by using a parameter mechanism
    1.71 + * in mailcap files that allows JavaBeans(tm) components to be specified as
    1.72 + * corresponding to particular commands for a MIME type.<p>
    1.73 + *
    1.74 + * When a mailcap file is
    1.75 + * parsed, the MailcapCommandMap recognizes certain parameter signatures,
    1.76 + * specifically those parameter names that begin with <code>x-java-</code>.
    1.77 + * The MailcapCommandMap uses this signature to find
    1.78 + * command entries for inclusion into its registries.
    1.79 + * Parameter names with the form <code>x-java-&lt;name></code>
    1.80 + * are read by the MailcapCommandMap as identifying a command
    1.81 + * with the name <i>name</i>. When the <i>name</i> is <code>
    1.82 + * content-handler</code> the MailcapCommandMap recognizes the class
    1.83 + * signified by this parameter as a <i>DataContentHandler</i>.
    1.84 + * All other commands are handled generically regardless of command
    1.85 + * name. The command implementation is specified by a fully qualified
    1.86 + * class name of a JavaBean(tm) component. For example; a command for viewing
    1.87 + * some data can be specified as: <code>x-java-view=com.foo.ViewBean</code>.<p>
    1.88 + *
    1.89 + * When the command name is <code>fallback-entry</code>, the value of
    1.90 + * the command may be <code>true</code> or <code>false</code>.  An
    1.91 + * entry for a MIME type that includes a parameter of
    1.92 + * <code>x-java-fallback-entry=true</code> defines fallback commands
    1.93 + * for that MIME type that will only be used if no non-fallback entry
    1.94 + * can be found.  For example, an entry of the form <code>text/*; ;
    1.95 + * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer</code>
    1.96 + * specifies a view command to be used for any text MIME type.  This
    1.97 + * view command would only be used if a non-fallback view command for
    1.98 + * the MIME type could not be found.<p>
    1.99 + *
   1.100 + * MailcapCommandMap aware mailcap files have the
   1.101 + * following general form:<p>
   1.102 + * <code>
   1.103 + * # Comments begin with a '#' and continue to the end of the line.<br>
   1.104 + * &lt;mime type>; ; &lt;parameter list><br>
   1.105 + * # Where a parameter list consists of one or more parameters,<br>
   1.106 + * # where parameters look like: x-java-view=com.sun.TextViewer<br>
   1.107 + * # and a parameter list looks like: <br>
   1.108 + * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
   1.109 + * <br>
   1.110 + * # Note that mailcap entries that do not contain 'x-java' parameters<br>
   1.111 + * # and comply to RFC 1524 are simply ignored:<br>
   1.112 + * image/gif; /usr/dt/bin/sdtimage %s<br>
   1.113 + *
   1.114 + * </code>
   1.115 + * <p>
   1.116 + *
   1.117 + * @author Bart Calder
   1.118 + * @author Bill Shannon
   1.119 + *
   1.120 + * @since 1.6
   1.121 + */
   1.122 +
   1.123 +public class MailcapCommandMap extends CommandMap {
   1.124 +    /*
   1.125 +     * We manage a collection of databases, searched in order.
   1.126 +     */
   1.127 +    private MailcapFile[] DB;
   1.128 +    private static final int PROG = 0;  // programmatically added entries
   1.129 +
   1.130 +    /**
   1.131 +     * The default Constructor.
   1.132 +     */
   1.133 +    public MailcapCommandMap() {
   1.134 +        super();
   1.135 +        List dbv = new ArrayList(5);    // usually 5 or less databases
   1.136 +        MailcapFile mf = null;
   1.137 +        dbv.add(null);          // place holder for PROG entry
   1.138 +
   1.139 +        LogSupport.log("MailcapCommandMap: load HOME");
   1.140 +        try {
   1.141 +            String user_home = System.getProperty("user.home");
   1.142 +
   1.143 +            if (user_home != null) {
   1.144 +                String path = user_home + File.separator + ".mailcap";
   1.145 +                mf = loadFile(path);
   1.146 +                if (mf != null)
   1.147 +                    dbv.add(mf);
   1.148 +            }
   1.149 +        } catch (SecurityException ex) {}
   1.150 +
   1.151 +        LogSupport.log("MailcapCommandMap: load SYS");
   1.152 +        try {
   1.153 +            // check system's home
   1.154 +            String system_mailcap = System.getProperty("java.home") +
   1.155 +                File.separator + "lib" + File.separator + "mailcap";
   1.156 +            mf = loadFile(system_mailcap);
   1.157 +            if (mf != null)
   1.158 +                dbv.add(mf);
   1.159 +        } catch (SecurityException ex) {}
   1.160 +
   1.161 +        LogSupport.log("MailcapCommandMap: load JAR");
   1.162 +        // load from the app's jar file
   1.163 +        loadAllResources(dbv, "META-INF/mailcap");
   1.164 +
   1.165 +        LogSupport.log("MailcapCommandMap: load DEF");
   1.166 +        mf = loadResource("/META-INF/mailcap.default");
   1.167 +
   1.168 +        if (mf != null)
   1.169 +            dbv.add(mf);
   1.170 +
   1.171 +        DB = new MailcapFile[dbv.size()];
   1.172 +        DB = (MailcapFile[])dbv.toArray(DB);
   1.173 +    }
   1.174 +
   1.175 +    /**
   1.176 +     * Load from the named resource.
   1.177 +     */
   1.178 +    private MailcapFile loadResource(String name) {
   1.179 +        InputStream clis = null;
   1.180 +        try {
   1.181 +            clis = SecuritySupport.getResourceAsStream(this.getClass(), name);
   1.182 +            if (clis != null) {
   1.183 +                MailcapFile mf = new MailcapFile(clis);
   1.184 +                if (LogSupport.isLoggable())
   1.185 +                    LogSupport.log("MailcapCommandMap: successfully loaded " +
   1.186 +                        "mailcap file: " + name);
   1.187 +                return mf;
   1.188 +            } else {
   1.189 +                if (LogSupport.isLoggable())
   1.190 +                    LogSupport.log("MailcapCommandMap: not loading " +
   1.191 +                        "mailcap file: " + name);
   1.192 +            }
   1.193 +        } catch (IOException e) {
   1.194 +            if (LogSupport.isLoggable())
   1.195 +                LogSupport.log("MailcapCommandMap: can't load " + name, e);
   1.196 +        } catch (SecurityException sex) {
   1.197 +            if (LogSupport.isLoggable())
   1.198 +                LogSupport.log("MailcapCommandMap: can't load " + name, sex);
   1.199 +        } finally {
   1.200 +            try {
   1.201 +                if (clis != null)
   1.202 +                    clis.close();
   1.203 +            } catch (IOException ex) { }        // ignore it
   1.204 +        }
   1.205 +        return null;
   1.206 +    }
   1.207 +
   1.208 +    /**
   1.209 +     * Load all of the named resource.
   1.210 +     */
   1.211 +    private void loadAllResources(List v, String name) {
   1.212 +        boolean anyLoaded = false;
   1.213 +        try {
   1.214 +            URL[] urls;
   1.215 +            ClassLoader cld = null;
   1.216 +            // First try the "application's" class loader.
   1.217 +            cld = SecuritySupport.getContextClassLoader();
   1.218 +            if (cld == null)
   1.219 +                cld = this.getClass().getClassLoader();
   1.220 +            if (cld != null)
   1.221 +                urls = SecuritySupport.getResources(cld, name);
   1.222 +            else
   1.223 +                urls = SecuritySupport.getSystemResources(name);
   1.224 +            if (urls != null) {
   1.225 +                if (LogSupport.isLoggable())
   1.226 +                    LogSupport.log("MailcapCommandMap: getResources");
   1.227 +                for (int i = 0; i < urls.length; i++) {
   1.228 +                    URL url = urls[i];
   1.229 +                    InputStream clis = null;
   1.230 +                    if (LogSupport.isLoggable())
   1.231 +                        LogSupport.log("MailcapCommandMap: URL " + url);
   1.232 +                    try {
   1.233 +                        clis = SecuritySupport.openStream(url);
   1.234 +                        if (clis != null) {
   1.235 +                            v.add(new MailcapFile(clis));
   1.236 +                            anyLoaded = true;
   1.237 +                            if (LogSupport.isLoggable())
   1.238 +                                LogSupport.log("MailcapCommandMap: " +
   1.239 +                                    "successfully loaded " +
   1.240 +                                    "mailcap file from URL: " +
   1.241 +                                    url);
   1.242 +                        } else {
   1.243 +                            if (LogSupport.isLoggable())
   1.244 +                                LogSupport.log("MailcapCommandMap: " +
   1.245 +                                    "not loading mailcap " +
   1.246 +                                    "file from URL: " + url);
   1.247 +                        }
   1.248 +                    } catch (IOException ioex) {
   1.249 +                        if (LogSupport.isLoggable())
   1.250 +                            LogSupport.log("MailcapCommandMap: can't load " +
   1.251 +                                                url, ioex);
   1.252 +                    } catch (SecurityException sex) {
   1.253 +                        if (LogSupport.isLoggable())
   1.254 +                            LogSupport.log("MailcapCommandMap: can't load " +
   1.255 +                                                url, sex);
   1.256 +                    } finally {
   1.257 +                        try {
   1.258 +                            if (clis != null)
   1.259 +                                clis.close();
   1.260 +                        } catch (IOException cex) { }
   1.261 +                    }
   1.262 +                }
   1.263 +            }
   1.264 +        } catch (Exception ex) {
   1.265 +            if (LogSupport.isLoggable())
   1.266 +                LogSupport.log("MailcapCommandMap: can't load " + name, ex);
   1.267 +        }
   1.268 +
   1.269 +        // if failed to load anything, fall back to old technique, just in case
   1.270 +        if (!anyLoaded) {
   1.271 +            if (LogSupport.isLoggable())
   1.272 +                LogSupport.log("MailcapCommandMap: !anyLoaded");
   1.273 +            MailcapFile mf = loadResource("/" + name);
   1.274 +            if (mf != null)
   1.275 +                v.add(mf);
   1.276 +        }
   1.277 +    }
   1.278 +
   1.279 +    /**
   1.280 +     * Load from the named file.
   1.281 +     */
   1.282 +    private MailcapFile loadFile(String name) {
   1.283 +        MailcapFile mtf = null;
   1.284 +
   1.285 +        try {
   1.286 +            mtf = new MailcapFile(name);
   1.287 +        } catch (IOException e) {
   1.288 +            //  e.printStackTrace();
   1.289 +        }
   1.290 +        return mtf;
   1.291 +    }
   1.292 +
   1.293 +    /**
   1.294 +     * Constructor that allows the caller to specify the path
   1.295 +     * of a <i>mailcap</i> file.
   1.296 +     *
   1.297 +     * @param fileName The name of the <i>mailcap</i> file to open
   1.298 +     * @exception       IOException     if the file can't be accessed
   1.299 +     */
   1.300 +    public MailcapCommandMap(String fileName) throws IOException {
   1.301 +        this();
   1.302 +
   1.303 +        if (LogSupport.isLoggable())
   1.304 +            LogSupport.log("MailcapCommandMap: load PROG from " + fileName);
   1.305 +        if (DB[PROG] == null) {
   1.306 +            DB[PROG] = new MailcapFile(fileName);
   1.307 +        }
   1.308 +    }
   1.309 +
   1.310 +
   1.311 +    /**
   1.312 +     * Constructor that allows the caller to specify an <i>InputStream</i>
   1.313 +     * containing a mailcap file.
   1.314 +     *
   1.315 +     * @param is        InputStream of the <i>mailcap</i> file to open
   1.316 +     */
   1.317 +    public MailcapCommandMap(InputStream is) {
   1.318 +        this();
   1.319 +
   1.320 +        LogSupport.log("MailcapCommandMap: load PROG");
   1.321 +        if (DB[PROG] == null) {
   1.322 +            try {
   1.323 +                DB[PROG] = new MailcapFile(is);
   1.324 +            } catch (IOException ex) {
   1.325 +                // XXX - should throw it
   1.326 +            }
   1.327 +        }
   1.328 +    }
   1.329 +
   1.330 +    /**
   1.331 +     * Get the preferred command list for a MIME Type. The MailcapCommandMap
   1.332 +     * searches the mailcap files as described above under
   1.333 +     * <i>Mailcap file search order</i>.<p>
   1.334 +     *
   1.335 +     * The result of the search is a proper subset of available
   1.336 +     * commands in all mailcap files known to this instance of
   1.337 +     * MailcapCommandMap.  The first entry for a particular command
   1.338 +     * is considered the preferred command.
   1.339 +     *
   1.340 +     * @param mimeType  the MIME type
   1.341 +     * @return the CommandInfo objects representing the preferred commands.
   1.342 +     */
   1.343 +    public synchronized CommandInfo[] getPreferredCommands(String mimeType) {
   1.344 +        List cmdList = new ArrayList();
   1.345 +        if (mimeType != null)
   1.346 +            mimeType = mimeType.toLowerCase(Locale.ENGLISH);
   1.347 +
   1.348 +        for (int i = 0; i < DB.length; i++) {
   1.349 +            if (DB[i] == null)
   1.350 +                continue;
   1.351 +            Map cmdMap = DB[i].getMailcapList(mimeType);
   1.352 +            if (cmdMap != null)
   1.353 +                appendPrefCmdsToList(cmdMap, cmdList);
   1.354 +        }
   1.355 +
   1.356 +        // now add the fallback commands
   1.357 +        for (int i = 0; i < DB.length; i++) {
   1.358 +            if (DB[i] == null)
   1.359 +                continue;
   1.360 +            Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
   1.361 +            if (cmdMap != null)
   1.362 +                appendPrefCmdsToList(cmdMap, cmdList);
   1.363 +        }
   1.364 +
   1.365 +        CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()];
   1.366 +        cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos);
   1.367 +
   1.368 +        return cmdInfos;
   1.369 +    }
   1.370 +
   1.371 +    /**
   1.372 +     * Put the commands that are in the hash table, into the list.
   1.373 +     */
   1.374 +    private void appendPrefCmdsToList(Map cmdHash, List cmdList) {
   1.375 +        Iterator verb_enum = cmdHash.keySet().iterator();
   1.376 +
   1.377 +        while (verb_enum.hasNext()) {
   1.378 +            String verb = (String)verb_enum.next();
   1.379 +            if (!checkForVerb(cmdList, verb)) {
   1.380 +                List cmdList2 = (List)cmdHash.get(verb); // get the list
   1.381 +                String className = (String)cmdList2.get(0);
   1.382 +                cmdList.add(new CommandInfo(verb, className));
   1.383 +            }
   1.384 +        }
   1.385 +    }
   1.386 +
   1.387 +    /**
   1.388 +     * Check the cmdList to see if this command exists, return
   1.389 +     * true if the verb is there.
   1.390 +     */
   1.391 +    private boolean checkForVerb(List cmdList, String verb) {
   1.392 +        Iterator ee = cmdList.iterator();
   1.393 +        while (ee.hasNext()) {
   1.394 +            String enum_verb =
   1.395 +                (String)((CommandInfo)ee.next()).getCommandName();
   1.396 +            if (enum_verb.equals(verb))
   1.397 +                return true;
   1.398 +        }
   1.399 +        return false;
   1.400 +    }
   1.401 +
   1.402 +    /**
   1.403 +     * Get all the available commands in all mailcap files known to
   1.404 +     * this instance of MailcapCommandMap for this MIME type.
   1.405 +     *
   1.406 +     * @param mimeType  the MIME type
   1.407 +     * @return the CommandInfo objects representing all the commands.
   1.408 +     */
   1.409 +    public synchronized CommandInfo[] getAllCommands(String mimeType) {
   1.410 +        List cmdList = new ArrayList();
   1.411 +        if (mimeType != null)
   1.412 +            mimeType = mimeType.toLowerCase(Locale.ENGLISH);
   1.413 +
   1.414 +        for (int i = 0; i < DB.length; i++) {
   1.415 +            if (DB[i] == null)
   1.416 +                continue;
   1.417 +            Map cmdMap = DB[i].getMailcapList(mimeType);
   1.418 +            if (cmdMap != null)
   1.419 +                appendCmdsToList(cmdMap, cmdList);
   1.420 +        }
   1.421 +
   1.422 +        // now add the fallback commands
   1.423 +        for (int i = 0; i < DB.length; i++) {
   1.424 +            if (DB[i] == null)
   1.425 +                continue;
   1.426 +            Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
   1.427 +            if (cmdMap != null)
   1.428 +                appendCmdsToList(cmdMap, cmdList);
   1.429 +        }
   1.430 +
   1.431 +        CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()];
   1.432 +        cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos);
   1.433 +
   1.434 +        return cmdInfos;
   1.435 +    }
   1.436 +
   1.437 +    /**
   1.438 +     * Put the commands that are in the hash table, into the list.
   1.439 +     */
   1.440 +    private void appendCmdsToList(Map typeHash, List cmdList) {
   1.441 +        Iterator verb_enum = typeHash.keySet().iterator();
   1.442 +
   1.443 +        while (verb_enum.hasNext()) {
   1.444 +            String verb = (String)verb_enum.next();
   1.445 +            List cmdList2 = (List)typeHash.get(verb);
   1.446 +            Iterator cmd_enum = ((List)cmdList2).iterator();
   1.447 +
   1.448 +            while (cmd_enum.hasNext()) {
   1.449 +                String cmd = (String)cmd_enum.next();
   1.450 +                cmdList.add(new CommandInfo(verb, cmd));
   1.451 +                // cmdList.add(0, new CommandInfo(verb, cmd));
   1.452 +            }
   1.453 +        }
   1.454 +    }
   1.455 +
   1.456 +    /**
   1.457 +     * Get the command corresponding to <code>cmdName</code> for the MIME type.
   1.458 +     *
   1.459 +     * @param mimeType  the MIME type
   1.460 +     * @param cmdName   the command name
   1.461 +     * @return the CommandInfo object corresponding to the command.
   1.462 +     */
   1.463 +    public synchronized CommandInfo getCommand(String mimeType,
   1.464 +                                                        String cmdName) {
   1.465 +        if (mimeType != null)
   1.466 +            mimeType = mimeType.toLowerCase(Locale.ENGLISH);
   1.467 +
   1.468 +        for (int i = 0; i < DB.length; i++) {
   1.469 +            if (DB[i] == null)
   1.470 +                continue;
   1.471 +            Map cmdMap = DB[i].getMailcapList(mimeType);
   1.472 +            if (cmdMap != null) {
   1.473 +                // get the cmd list for the cmd
   1.474 +                List v = (List)cmdMap.get(cmdName);
   1.475 +                if (v != null) {
   1.476 +                    String cmdClassName = (String)v.get(0);
   1.477 +
   1.478 +                    if (cmdClassName != null)
   1.479 +                        return new CommandInfo(cmdName, cmdClassName);
   1.480 +                }
   1.481 +            }
   1.482 +        }
   1.483 +
   1.484 +        // now try the fallback list
   1.485 +        for (int i = 0; i < DB.length; i++) {
   1.486 +            if (DB[i] == null)
   1.487 +                continue;
   1.488 +            Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
   1.489 +            if (cmdMap != null) {
   1.490 +                // get the cmd list for the cmd
   1.491 +                List v = (List)cmdMap.get(cmdName);
   1.492 +                if (v != null) {
   1.493 +                    String cmdClassName = (String)v.get(0);
   1.494 +
   1.495 +                    if (cmdClassName != null)
   1.496 +                        return new CommandInfo(cmdName, cmdClassName);
   1.497 +                }
   1.498 +            }
   1.499 +        }
   1.500 +        return null;
   1.501 +    }
   1.502 +
   1.503 +    /**
   1.504 +     * Add entries to the registry.  Programmatically
   1.505 +     * added entries are searched before other entries.<p>
   1.506 +     *
   1.507 +     * The string that is passed in should be in mailcap
   1.508 +     * format.
   1.509 +     *
   1.510 +     * @param mail_cap a correctly formatted mailcap string
   1.511 +     */
   1.512 +    public synchronized void addMailcap(String mail_cap) {
   1.513 +        // check to see if one exists
   1.514 +        LogSupport.log("MailcapCommandMap: add to PROG");
   1.515 +        if (DB[PROG] == null)
   1.516 +            DB[PROG] = new MailcapFile();
   1.517 +
   1.518 +        DB[PROG].appendToMailcap(mail_cap);
   1.519 +    }
   1.520 +
   1.521 +    /**
   1.522 +     * Return the DataContentHandler for the specified MIME type.
   1.523 +     *
   1.524 +     * @param mimeType  the MIME type
   1.525 +     * @return          the DataContentHandler
   1.526 +     */
   1.527 +    public synchronized DataContentHandler createDataContentHandler(
   1.528 +                                                        String mimeType) {
   1.529 +        if (LogSupport.isLoggable())
   1.530 +            LogSupport.log(
   1.531 +                "MailcapCommandMap: createDataContentHandler for " + mimeType);
   1.532 +        if (mimeType != null)
   1.533 +            mimeType = mimeType.toLowerCase(Locale.ENGLISH);
   1.534 +
   1.535 +        for (int i = 0; i < DB.length; i++) {
   1.536 +            if (DB[i] == null)
   1.537 +                continue;
   1.538 +            if (LogSupport.isLoggable())
   1.539 +                LogSupport.log("  search DB #" + i);
   1.540 +            Map cmdMap = DB[i].getMailcapList(mimeType);
   1.541 +            if (cmdMap != null) {
   1.542 +                List v = (List)cmdMap.get("content-handler");
   1.543 +                if (v != null) {
   1.544 +                    String name = (String)v.get(0);
   1.545 +                    DataContentHandler dch = getDataContentHandler(name);
   1.546 +                    if (dch != null)
   1.547 +                        return dch;
   1.548 +                }
   1.549 +            }
   1.550 +        }
   1.551 +
   1.552 +        // now try the fallback entries
   1.553 +        for (int i = 0; i < DB.length; i++) {
   1.554 +            if (DB[i] == null)
   1.555 +                continue;
   1.556 +            if (LogSupport.isLoggable())
   1.557 +                LogSupport.log("  search fallback DB #" + i);
   1.558 +            Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
   1.559 +            if (cmdMap != null) {
   1.560 +                List v = (List)cmdMap.get("content-handler");
   1.561 +                if (v != null) {
   1.562 +                    String name = (String)v.get(0);
   1.563 +                    DataContentHandler dch = getDataContentHandler(name);
   1.564 +                    if (dch != null)
   1.565 +                        return dch;
   1.566 +                }
   1.567 +            }
   1.568 +        }
   1.569 +        return null;
   1.570 +    }
   1.571 +
   1.572 +    private DataContentHandler getDataContentHandler(String name) {
   1.573 +        if (LogSupport.isLoggable())
   1.574 +            LogSupport.log("    got content-handler");
   1.575 +        if (LogSupport.isLoggable())
   1.576 +            LogSupport.log("      class " + name);
   1.577 +        try {
   1.578 +            ClassLoader cld = null;
   1.579 +            // First try the "application's" class loader.
   1.580 +            cld = SecuritySupport.getContextClassLoader();
   1.581 +            if (cld == null)
   1.582 +                cld = this.getClass().getClassLoader();
   1.583 +            Class cl = null;
   1.584 +            try {
   1.585 +                cl = cld.loadClass(name);
   1.586 +            } catch (Exception ex) {
   1.587 +                // if anything goes wrong, do it the old way
   1.588 +                cl = Class.forName(name);
   1.589 +            }
   1.590 +            if (cl != null)             // XXX - always true?
   1.591 +                return (DataContentHandler)cl.newInstance();
   1.592 +        } catch (IllegalAccessException e) {
   1.593 +            if (LogSupport.isLoggable())
   1.594 +                LogSupport.log("Can't load DCH " + name, e);
   1.595 +        } catch (ClassNotFoundException e) {
   1.596 +            if (LogSupport.isLoggable())
   1.597 +                LogSupport.log("Can't load DCH " + name, e);
   1.598 +        } catch (InstantiationException e) {
   1.599 +            if (LogSupport.isLoggable())
   1.600 +                LogSupport.log("Can't load DCH " + name, e);
   1.601 +        }
   1.602 +        return null;
   1.603 +    }
   1.604 +
   1.605 +    /**
   1.606 +     * Get all the MIME types known to this command map.
   1.607 +     *
   1.608 +     * @return          array of MIME types as strings
   1.609 +     * @since   JAF 1.1
   1.610 +     */
   1.611 +    public synchronized String[] getMimeTypes() {
   1.612 +        List mtList = new ArrayList();
   1.613 +
   1.614 +        for (int i = 0; i < DB.length; i++) {
   1.615 +            if (DB[i] == null)
   1.616 +                continue;
   1.617 +            String[] ts = DB[i].getMimeTypes();
   1.618 +            if (ts != null) {
   1.619 +                for (int j = 0; j < ts.length; j++) {
   1.620 +                    // eliminate duplicates
   1.621 +                    if (!mtList.contains(ts[j]))
   1.622 +                        mtList.add(ts[j]);
   1.623 +                }
   1.624 +            }
   1.625 +        }
   1.626 +
   1.627 +        String[] mts = new String[mtList.size()];
   1.628 +        mts = (String[])mtList.toArray(mts);
   1.629 +
   1.630 +        return mts;
   1.631 +    }
   1.632 +
   1.633 +    /**
   1.634 +     * Get the native commands for the given MIME type.
   1.635 +     * Returns an array of strings where each string is
   1.636 +     * an entire mailcap file entry.  The application
   1.637 +     * will need to parse the entry to extract the actual
   1.638 +     * command as well as any attributes it needs. See
   1.639 +     * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>
   1.640 +     * for details of the mailcap entry syntax.  Only mailcap
   1.641 +     * entries that specify a view command for the specified
   1.642 +     * MIME type are returned.
   1.643 +     *
   1.644 +     * @return          array of native command entries
   1.645 +     * @since   JAF 1.1
   1.646 +     */
   1.647 +    public synchronized String[] getNativeCommands(String mimeType) {
   1.648 +        List cmdList = new ArrayList();
   1.649 +        if (mimeType != null)
   1.650 +            mimeType = mimeType.toLowerCase(Locale.ENGLISH);
   1.651 +
   1.652 +        for (int i = 0; i < DB.length; i++) {
   1.653 +            if (DB[i] == null)
   1.654 +                continue;
   1.655 +            String[] cmds = DB[i].getNativeCommands(mimeType);
   1.656 +            if (cmds != null) {
   1.657 +                for (int j = 0; j < cmds.length; j++) {
   1.658 +                    // eliminate duplicates
   1.659 +                    if (!cmdList.contains(cmds[j]))
   1.660 +                        cmdList.add(cmds[j]);
   1.661 +                }
   1.662 +            }
   1.663 +        }
   1.664 +
   1.665 +        String[] cmds = new String[cmdList.size()];
   1.666 +        cmds = (String[])cmdList.toArray(cmds);
   1.667 +
   1.668 +        return cmds;
   1.669 +    }
   1.670 +
   1.671 +    /**
   1.672 +     * for debugging...
   1.673 +     *
   1.674 +    public static void main(String[] argv) throws Exception {
   1.675 +        MailcapCommandMap map = new MailcapCommandMap();
   1.676 +        CommandInfo[] cmdInfo;
   1.677 +
   1.678 +        cmdInfo = map.getPreferredCommands(argv[0]);
   1.679 +        System.out.println("Preferred Commands:");
   1.680 +        for (int i = 0; i < cmdInfo.length; i++)
   1.681 +            System.out.println("Command " + cmdInfo[i].getCommandName() + " [" +
   1.682 +                                            cmdInfo[i].getCommandClass() + "]");
   1.683 +        cmdInfo = map.getAllCommands(argv[0]);
   1.684 +        System.out.println();
   1.685 +        System.out.println("All Commands:");
   1.686 +        for (int i = 0; i < cmdInfo.length; i++)
   1.687 +            System.out.println("Command " + cmdInfo[i].getCommandName() + " [" +
   1.688 +                                            cmdInfo[i].getCommandClass() + "]");
   1.689 +        DataContentHandler dch = map.createDataContentHandler(argv[0]);
   1.690 +        if (dch != null)
   1.691 +            System.out.println("DataContentHandler " +
   1.692 +                                                dch.getClass().toString());
   1.693 +        System.exit(0);
   1.694 +    }
   1.695 +    */
   1.696 +}

mercurial