ohair@286: /* mkos@494: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: ohair@286: package javax.activation; ohair@286: ohair@286: import java.util.*; ohair@286: import java.io.*; ohair@286: import java.net.*; ohair@286: import com.sun.activation.registries.MailcapFile; ohair@286: import com.sun.activation.registries.LogSupport; ohair@286: ohair@286: /** ohair@286: * MailcapCommandMap extends the CommandMap ohair@286: * abstract class. It implements a CommandMap whose configuration ohair@286: * is based on mailcap files ohair@286: * (RFC 1524). ohair@286: * The MailcapCommandMap can be configured both programmatically ohair@286: * and via configuration files. ohair@286: *

ohair@286: * Mailcap file search order:

ohair@286: * The MailcapCommandMap looks in various places in the user's ohair@286: * system for mailcap file entries. When requests are made ohair@286: * to search for commands in the MailcapCommandMap, it searches ohair@286: * mailcap files in the following order: ohair@286: *

ohair@286: *

    ohair@286: *
  1. Programatically added entries to the MailcapCommandMap instance. ohair@286: *
  2. The file .mailcap in the user's home directory. ohair@286: *
  3. The file <java.home>/lib/mailcap. ohair@286: *
  4. The file or resources named META-INF/mailcap. ohair@286: *
  5. The file or resource named META-INF/mailcap.default ohair@286: * (usually found only in the activation.jar file). ohair@286: *
ohair@286: *

ohair@286: * Mailcap file format:

ohair@286: * ohair@286: * Mailcap files must conform to the mailcap ohair@286: * file specification (RFC 1524, A User Agent Configuration Mechanism ohair@286: * For Multimedia Mail Format Information). ohair@286: * The file format consists of entries corresponding to ohair@286: * particular MIME types. In general, the specification ohair@286: * specifies applications for clients to use when they ohair@286: * themselves cannot operate on the specified MIME type. The ohair@286: * MailcapCommandMap extends this specification by using a parameter mechanism ohair@286: * in mailcap files that allows JavaBeans(tm) components to be specified as ohair@286: * corresponding to particular commands for a MIME type.

ohair@286: * ohair@286: * When a mailcap file is ohair@286: * parsed, the MailcapCommandMap recognizes certain parameter signatures, ohair@286: * specifically those parameter names that begin with x-java-. ohair@286: * The MailcapCommandMap uses this signature to find ohair@286: * command entries for inclusion into its registries. ohair@286: * Parameter names with the form x-java-<name> ohair@286: * are read by the MailcapCommandMap as identifying a command ohair@286: * with the name name. When the name is ohair@286: * content-handler the MailcapCommandMap recognizes the class ohair@286: * signified by this parameter as a DataContentHandler. ohair@286: * All other commands are handled generically regardless of command ohair@286: * name. The command implementation is specified by a fully qualified ohair@286: * class name of a JavaBean(tm) component. For example; a command for viewing ohair@286: * some data can be specified as: x-java-view=com.foo.ViewBean.

ohair@286: * ohair@286: * When the command name is fallback-entry, the value of ohair@286: * the command may be true or false. An ohair@286: * entry for a MIME type that includes a parameter of ohair@286: * x-java-fallback-entry=true defines fallback commands ohair@286: * for that MIME type that will only be used if no non-fallback entry ohair@286: * can be found. For example, an entry of the form text/*; ; ohair@286: * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer ohair@286: * specifies a view command to be used for any text MIME type. This ohair@286: * view command would only be used if a non-fallback view command for ohair@286: * the MIME type could not be found.

ohair@286: * ohair@286: * MailcapCommandMap aware mailcap files have the ohair@286: * following general form:

ohair@286: * ohair@286: * # Comments begin with a '#' and continue to the end of the line.
ohair@286: * <mime type>; ; <parameter list>
ohair@286: * # Where a parameter list consists of one or more parameters,
ohair@286: * # where parameters look like: x-java-view=com.sun.TextViewer
ohair@286: * # and a parameter list looks like:
ohair@286: * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit ohair@286: *
ohair@286: * # Note that mailcap entries that do not contain 'x-java' parameters
ohair@286: * # and comply to RFC 1524 are simply ignored:
ohair@286: * image/gif; /usr/dt/bin/sdtimage %s
ohair@286: * ohair@286: *
ohair@286: *

ohair@286: * ohair@286: * @author Bart Calder ohair@286: * @author Bill Shannon ohair@286: * ohair@286: * @since 1.6 ohair@286: */ ohair@286: ohair@286: public class MailcapCommandMap extends CommandMap { ohair@286: /* ohair@286: * We manage a collection of databases, searched in order. ohair@286: */ ohair@286: private MailcapFile[] DB; ohair@286: private static final int PROG = 0; // programmatically added entries ohair@286: ohair@286: /** ohair@286: * The default Constructor. ohair@286: */ ohair@286: public MailcapCommandMap() { ohair@286: super(); ohair@286: List dbv = new ArrayList(5); // usually 5 or less databases ohair@286: MailcapFile mf = null; ohair@286: dbv.add(null); // place holder for PROG entry ohair@286: ohair@286: LogSupport.log("MailcapCommandMap: load HOME"); ohair@286: try { ohair@286: String user_home = System.getProperty("user.home"); ohair@286: ohair@286: if (user_home != null) { ohair@286: String path = user_home + File.separator + ".mailcap"; ohair@286: mf = loadFile(path); ohair@286: if (mf != null) ohair@286: dbv.add(mf); ohair@286: } ohair@286: } catch (SecurityException ex) {} ohair@286: ohair@286: LogSupport.log("MailcapCommandMap: load SYS"); ohair@286: try { ohair@286: // check system's home ohair@286: String system_mailcap = System.getProperty("java.home") + ohair@286: File.separator + "lib" + File.separator + "mailcap"; ohair@286: mf = loadFile(system_mailcap); ohair@286: if (mf != null) ohair@286: dbv.add(mf); ohair@286: } catch (SecurityException ex) {} ohair@286: ohair@286: LogSupport.log("MailcapCommandMap: load JAR"); ohair@286: // load from the app's jar file ohair@286: loadAllResources(dbv, "META-INF/mailcap"); ohair@286: ohair@286: LogSupport.log("MailcapCommandMap: load DEF"); mkos@494: mf = loadResource("/META-INF/mailcap.default"); ohair@286: mkos@494: if (mf != null) mkos@494: dbv.add(mf); ohair@286: ohair@286: DB = new MailcapFile[dbv.size()]; ohair@286: DB = (MailcapFile[])dbv.toArray(DB); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Load from the named resource. ohair@286: */ ohair@286: private MailcapFile loadResource(String name) { ohair@286: InputStream clis = null; ohair@286: try { ohair@286: clis = SecuritySupport.getResourceAsStream(this.getClass(), name); ohair@286: if (clis != null) { ohair@286: MailcapFile mf = new MailcapFile(clis); ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: successfully loaded " + ohair@286: "mailcap file: " + name); ohair@286: return mf; ohair@286: } else { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: not loading " + ohair@286: "mailcap file: " + name); ohair@286: } ohair@286: } catch (IOException e) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: can't load " + name, e); ohair@286: } catch (SecurityException sex) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: can't load " + name, sex); ohair@286: } finally { ohair@286: try { ohair@286: if (clis != null) ohair@286: clis.close(); ohair@286: } catch (IOException ex) { } // ignore it ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Load all of the named resource. ohair@286: */ ohair@286: private void loadAllResources(List v, String name) { ohair@286: boolean anyLoaded = false; ohair@286: try { ohair@286: URL[] urls; ohair@286: ClassLoader cld = null; ohair@286: // First try the "application's" class loader. ohair@286: cld = SecuritySupport.getContextClassLoader(); ohair@286: if (cld == null) ohair@286: cld = this.getClass().getClassLoader(); ohair@286: if (cld != null) ohair@286: urls = SecuritySupport.getResources(cld, name); ohair@286: else ohair@286: urls = SecuritySupport.getSystemResources(name); ohair@286: if (urls != null) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: getResources"); ohair@286: for (int i = 0; i < urls.length; i++) { ohair@286: URL url = urls[i]; ohair@286: InputStream clis = null; ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: URL " + url); ohair@286: try { ohair@286: clis = SecuritySupport.openStream(url); ohair@286: if (clis != null) { ohair@286: v.add(new MailcapFile(clis)); ohair@286: anyLoaded = true; ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: " + ohair@286: "successfully loaded " + ohair@286: "mailcap file from URL: " + ohair@286: url); ohair@286: } else { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: " + ohair@286: "not loading mailcap " + ohair@286: "file from URL: " + url); ohair@286: } ohair@286: } catch (IOException ioex) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: can't load " + ohair@286: url, ioex); ohair@286: } catch (SecurityException sex) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: can't load " + ohair@286: url, sex); ohair@286: } finally { ohair@286: try { ohair@286: if (clis != null) ohair@286: clis.close(); ohair@286: } catch (IOException cex) { } ohair@286: } ohair@286: } ohair@286: } ohair@286: } catch (Exception ex) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: can't load " + name, ex); ohair@286: } ohair@286: ohair@286: // if failed to load anything, fall back to old technique, just in case ohair@286: if (!anyLoaded) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: !anyLoaded"); ohair@286: MailcapFile mf = loadResource("/" + name); ohair@286: if (mf != null) ohair@286: v.add(mf); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Load from the named file. ohair@286: */ ohair@286: private MailcapFile loadFile(String name) { ohair@286: MailcapFile mtf = null; ohair@286: ohair@286: try { ohair@286: mtf = new MailcapFile(name); ohair@286: } catch (IOException e) { ohair@286: // e.printStackTrace(); ohair@286: } ohair@286: return mtf; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Constructor that allows the caller to specify the path ohair@286: * of a mailcap file. ohair@286: * ohair@286: * @param fileName The name of the mailcap file to open ohair@286: * @exception IOException if the file can't be accessed ohair@286: */ ohair@286: public MailcapCommandMap(String fileName) throws IOException { ohair@286: this(); ohair@286: ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("MailcapCommandMap: load PROG from " + fileName); ohair@286: if (DB[PROG] == null) { ohair@286: DB[PROG] = new MailcapFile(fileName); ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Constructor that allows the caller to specify an InputStream ohair@286: * containing a mailcap file. ohair@286: * ohair@286: * @param is InputStream of the mailcap file to open ohair@286: */ ohair@286: public MailcapCommandMap(InputStream is) { ohair@286: this(); ohair@286: ohair@286: LogSupport.log("MailcapCommandMap: load PROG"); ohair@286: if (DB[PROG] == null) { ohair@286: try { ohair@286: DB[PROG] = new MailcapFile(is); ohair@286: } catch (IOException ex) { ohair@286: // XXX - should throw it ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Get the preferred command list for a MIME Type. The MailcapCommandMap ohair@286: * searches the mailcap files as described above under ohair@286: * Mailcap file search order.

ohair@286: * ohair@286: * The result of the search is a proper subset of available ohair@286: * commands in all mailcap files known to this instance of ohair@286: * MailcapCommandMap. The first entry for a particular command ohair@286: * is considered the preferred command. ohair@286: * ohair@286: * @param mimeType the MIME type ohair@286: * @return the CommandInfo objects representing the preferred commands. ohair@286: */ ohair@286: public synchronized CommandInfo[] getPreferredCommands(String mimeType) { ohair@286: List cmdList = new ArrayList(); ohair@286: if (mimeType != null) ohair@286: mimeType = mimeType.toLowerCase(Locale.ENGLISH); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapList(mimeType); ohair@286: if (cmdMap != null) ohair@286: appendPrefCmdsToList(cmdMap, cmdList); ohair@286: } ohair@286: ohair@286: // now add the fallback commands ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapFallbackList(mimeType); ohair@286: if (cmdMap != null) ohair@286: appendPrefCmdsToList(cmdMap, cmdList); ohair@286: } ohair@286: ohair@286: CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()]; ohair@286: cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos); ohair@286: ohair@286: return cmdInfos; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Put the commands that are in the hash table, into the list. ohair@286: */ ohair@286: private void appendPrefCmdsToList(Map cmdHash, List cmdList) { ohair@286: Iterator verb_enum = cmdHash.keySet().iterator(); ohair@286: ohair@286: while (verb_enum.hasNext()) { ohair@286: String verb = (String)verb_enum.next(); ohair@286: if (!checkForVerb(cmdList, verb)) { ohair@286: List cmdList2 = (List)cmdHash.get(verb); // get the list ohair@286: String className = (String)cmdList2.get(0); ohair@286: cmdList.add(new CommandInfo(verb, className)); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Check the cmdList to see if this command exists, return ohair@286: * true if the verb is there. ohair@286: */ ohair@286: private boolean checkForVerb(List cmdList, String verb) { ohair@286: Iterator ee = cmdList.iterator(); ohair@286: while (ee.hasNext()) { ohair@286: String enum_verb = ohair@286: (String)((CommandInfo)ee.next()).getCommandName(); ohair@286: if (enum_verb.equals(verb)) ohair@286: return true; ohair@286: } ohair@286: return false; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Get all the available commands in all mailcap files known to ohair@286: * this instance of MailcapCommandMap for this MIME type. ohair@286: * ohair@286: * @param mimeType the MIME type ohair@286: * @return the CommandInfo objects representing all the commands. ohair@286: */ ohair@286: public synchronized CommandInfo[] getAllCommands(String mimeType) { ohair@286: List cmdList = new ArrayList(); ohair@286: if (mimeType != null) ohair@286: mimeType = mimeType.toLowerCase(Locale.ENGLISH); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapList(mimeType); ohair@286: if (cmdMap != null) ohair@286: appendCmdsToList(cmdMap, cmdList); ohair@286: } ohair@286: ohair@286: // now add the fallback commands ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapFallbackList(mimeType); ohair@286: if (cmdMap != null) ohair@286: appendCmdsToList(cmdMap, cmdList); ohair@286: } ohair@286: ohair@286: CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()]; ohair@286: cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos); ohair@286: ohair@286: return cmdInfos; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Put the commands that are in the hash table, into the list. ohair@286: */ ohair@286: private void appendCmdsToList(Map typeHash, List cmdList) { ohair@286: Iterator verb_enum = typeHash.keySet().iterator(); ohair@286: ohair@286: while (verb_enum.hasNext()) { ohair@286: String verb = (String)verb_enum.next(); ohair@286: List cmdList2 = (List)typeHash.get(verb); ohair@286: Iterator cmd_enum = ((List)cmdList2).iterator(); ohair@286: ohair@286: while (cmd_enum.hasNext()) { ohair@286: String cmd = (String)cmd_enum.next(); ohair@286: cmdList.add(new CommandInfo(verb, cmd)); ohair@286: // cmdList.add(0, new CommandInfo(verb, cmd)); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Get the command corresponding to cmdName for the MIME type. ohair@286: * ohair@286: * @param mimeType the MIME type ohair@286: * @param cmdName the command name ohair@286: * @return the CommandInfo object corresponding to the command. ohair@286: */ ohair@286: public synchronized CommandInfo getCommand(String mimeType, ohair@286: String cmdName) { ohair@286: if (mimeType != null) ohair@286: mimeType = mimeType.toLowerCase(Locale.ENGLISH); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapList(mimeType); ohair@286: if (cmdMap != null) { ohair@286: // get the cmd list for the cmd ohair@286: List v = (List)cmdMap.get(cmdName); ohair@286: if (v != null) { ohair@286: String cmdClassName = (String)v.get(0); ohair@286: ohair@286: if (cmdClassName != null) ohair@286: return new CommandInfo(cmdName, cmdClassName); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: // now try the fallback list ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: Map cmdMap = DB[i].getMailcapFallbackList(mimeType); ohair@286: if (cmdMap != null) { ohair@286: // get the cmd list for the cmd ohair@286: List v = (List)cmdMap.get(cmdName); ohair@286: if (v != null) { ohair@286: String cmdClassName = (String)v.get(0); ohair@286: ohair@286: if (cmdClassName != null) ohair@286: return new CommandInfo(cmdName, cmdClassName); ohair@286: } ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Add entries to the registry. Programmatically ohair@286: * added entries are searched before other entries.

ohair@286: * ohair@286: * The string that is passed in should be in mailcap ohair@286: * format. ohair@286: * ohair@286: * @param mail_cap a correctly formatted mailcap string ohair@286: */ ohair@286: public synchronized void addMailcap(String mail_cap) { ohair@286: // check to see if one exists ohair@286: LogSupport.log("MailcapCommandMap: add to PROG"); ohair@286: if (DB[PROG] == null) ohair@286: DB[PROG] = new MailcapFile(); ohair@286: ohair@286: DB[PROG].appendToMailcap(mail_cap); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return the DataContentHandler for the specified MIME type. ohair@286: * ohair@286: * @param mimeType the MIME type ohair@286: * @return the DataContentHandler ohair@286: */ ohair@286: public synchronized DataContentHandler createDataContentHandler( ohair@286: String mimeType) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log( ohair@286: "MailcapCommandMap: createDataContentHandler for " + mimeType); ohair@286: if (mimeType != null) ohair@286: mimeType = mimeType.toLowerCase(Locale.ENGLISH); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log(" search DB #" + i); ohair@286: Map cmdMap = DB[i].getMailcapList(mimeType); ohair@286: if (cmdMap != null) { ohair@286: List v = (List)cmdMap.get("content-handler"); ohair@286: if (v != null) { ohair@286: String name = (String)v.get(0); ohair@286: DataContentHandler dch = getDataContentHandler(name); ohair@286: if (dch != null) ohair@286: return dch; ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: // now try the fallback entries ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log(" search fallback DB #" + i); ohair@286: Map cmdMap = DB[i].getMailcapFallbackList(mimeType); ohair@286: if (cmdMap != null) { ohair@286: List v = (List)cmdMap.get("content-handler"); ohair@286: if (v != null) { ohair@286: String name = (String)v.get(0); ohair@286: DataContentHandler dch = getDataContentHandler(name); ohair@286: if (dch != null) ohair@286: return dch; ohair@286: } ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: private DataContentHandler getDataContentHandler(String name) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log(" got content-handler"); ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log(" class " + name); ohair@286: try { ohair@286: ClassLoader cld = null; ohair@286: // First try the "application's" class loader. ohair@286: cld = SecuritySupport.getContextClassLoader(); ohair@286: if (cld == null) ohair@286: cld = this.getClass().getClassLoader(); ohair@286: Class cl = null; ohair@286: try { ohair@286: cl = cld.loadClass(name); ohair@286: } catch (Exception ex) { ohair@286: // if anything goes wrong, do it the old way ohair@286: cl = Class.forName(name); ohair@286: } ohair@286: if (cl != null) // XXX - always true? ohair@286: return (DataContentHandler)cl.newInstance(); ohair@286: } catch (IllegalAccessException e) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("Can't load DCH " + name, e); ohair@286: } catch (ClassNotFoundException e) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("Can't load DCH " + name, e); ohair@286: } catch (InstantiationException e) { ohair@286: if (LogSupport.isLoggable()) ohair@286: LogSupport.log("Can't load DCH " + name, e); ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Get all the MIME types known to this command map. ohair@286: * ohair@286: * @return array of MIME types as strings ohair@286: * @since JAF 1.1 ohair@286: */ ohair@286: public synchronized String[] getMimeTypes() { ohair@286: List mtList = new ArrayList(); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: String[] ts = DB[i].getMimeTypes(); ohair@286: if (ts != null) { ohair@286: for (int j = 0; j < ts.length; j++) { ohair@286: // eliminate duplicates ohair@286: if (!mtList.contains(ts[j])) ohair@286: mtList.add(ts[j]); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: String[] mts = new String[mtList.size()]; ohair@286: mts = (String[])mtList.toArray(mts); ohair@286: ohair@286: return mts; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Get the native commands for the given MIME type. ohair@286: * Returns an array of strings where each string is ohair@286: * an entire mailcap file entry. The application ohair@286: * will need to parse the entry to extract the actual ohair@286: * command as well as any attributes it needs. See ohair@286: * RFC 1524 ohair@286: * for details of the mailcap entry syntax. Only mailcap ohair@286: * entries that specify a view command for the specified ohair@286: * MIME type are returned. ohair@286: * ohair@286: * @return array of native command entries ohair@286: * @since JAF 1.1 ohair@286: */ ohair@286: public synchronized String[] getNativeCommands(String mimeType) { ohair@286: List cmdList = new ArrayList(); ohair@286: if (mimeType != null) ohair@286: mimeType = mimeType.toLowerCase(Locale.ENGLISH); ohair@286: ohair@286: for (int i = 0; i < DB.length; i++) { ohair@286: if (DB[i] == null) ohair@286: continue; ohair@286: String[] cmds = DB[i].getNativeCommands(mimeType); ohair@286: if (cmds != null) { ohair@286: for (int j = 0; j < cmds.length; j++) { ohair@286: // eliminate duplicates ohair@286: if (!cmdList.contains(cmds[j])) ohair@286: cmdList.add(cmds[j]); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: String[] cmds = new String[cmdList.size()]; ohair@286: cmds = (String[])cmdList.toArray(cmds); ohair@286: ohair@286: return cmds; ohair@286: } ohair@286: ohair@286: /** ohair@286: * for debugging... ohair@286: * ohair@286: public static void main(String[] argv) throws Exception { ohair@286: MailcapCommandMap map = new MailcapCommandMap(); ohair@286: CommandInfo[] cmdInfo; ohair@286: ohair@286: cmdInfo = map.getPreferredCommands(argv[0]); ohair@286: System.out.println("Preferred Commands:"); ohair@286: for (int i = 0; i < cmdInfo.length; i++) ohair@286: System.out.println("Command " + cmdInfo[i].getCommandName() + " [" + ohair@286: cmdInfo[i].getCommandClass() + "]"); ohair@286: cmdInfo = map.getAllCommands(argv[0]); ohair@286: System.out.println(); ohair@286: System.out.println("All Commands:"); ohair@286: for (int i = 0; i < cmdInfo.length; i++) ohair@286: System.out.println("Command " + cmdInfo[i].getCommandName() + " [" + ohair@286: cmdInfo[i].getCommandClass() + "]"); ohair@286: DataContentHandler dch = map.createDataContentHandler(argv[0]); ohair@286: if (dch != null) ohair@286: System.out.println("DataContentHandler " + ohair@286: dch.getClass().toString()); ohair@286: System.exit(0); ohair@286: } ohair@286: */ ohair@286: }