< prev index next >

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

Print this page




  28 
  29 import java.util.*;
  30 import java.io.*;
  31 import java.net.*;
  32 import com.sun.activation.registries.MailcapFile;
  33 import com.sun.activation.registries.LogSupport;
  34 
  35 /**
  36  * MailcapCommandMap extends the CommandMap
  37  * abstract class. It implements a CommandMap whose configuration
  38  * is based on mailcap files
  39  * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
  40  * The MailcapCommandMap can be configured both programmatically
  41  * and via configuration files.
  42  * <p>
  43  * <b>Mailcap file search order:</b><p>
  44  * The MailcapCommandMap looks in various places in the user's
  45  * system for mailcap file entries. When requests are made
  46  * to search for commands in the MailcapCommandMap, it searches
  47  * mailcap files in the following order:
  48  * <p>
  49  * <ol>
  50  * <li> Programatically added entries to the MailcapCommandMap instance.
  51  * <li> The file <code>.mailcap</code> in the user's home directory.
  52  * <li> The file &lt;<i>java.home</i>&gt;<code>/lib/mailcap</code>.
  53  * <li> The file or resources named <code>META-INF/mailcap</code>.
  54  * <li> The file or resource named <code>META-INF/mailcap.default</code>
  55  * (usually found only in the <code>activation.jar</code> file).
  56  * </ol>
  57  * <p>
  58  * <b>Mailcap file format:</b><p>
  59  *
  60  * Mailcap files must conform to the mailcap
  61  * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
  62  * For Multimedia Mail Format Information</i>).
  63  * The file format consists of entries corresponding to
  64  * particular MIME types. In general, the specification
  65  * specifies <i>applications</i> for clients to use when they
  66  * themselves cannot operate on the specified MIME type. The
  67  * MailcapCommandMap extends this specification by using a parameter mechanism
  68  * in mailcap files that allows JavaBeans(tm) components to be specified as
  69  * corresponding to particular commands for a MIME type.<p>
  70  *
  71  * When a mailcap file is
  72  * parsed, the MailcapCommandMap recognizes certain parameter signatures,
  73  * specifically those parameter names that begin with <code>x-java-</code>.
  74  * The MailcapCommandMap uses this signature to find
  75  * command entries for inclusion into its registries.
  76  * Parameter names with the form <code>x-java-&lt;name></code>
  77  * are read by the MailcapCommandMap as identifying a command
  78  * with the name <i>name</i>. When the <i>name</i> is <code>
  79  * content-handler</code> the MailcapCommandMap recognizes the class
  80  * signified by this parameter as a <i>DataContentHandler</i>.
  81  * All other commands are handled generically regardless of command
  82  * name. The command implementation is specified by a fully qualified
  83  * class name of a JavaBean(tm) component. For example; a command for viewing
  84  * some data can be specified as: <code>x-java-view=com.foo.ViewBean</code>.<p>
  85  *
  86  * When the command name is <code>fallback-entry</code>, the value of
  87  * the command may be <code>true</code> or <code>false</code>.  An
  88  * entry for a MIME type that includes a parameter of
  89  * <code>x-java-fallback-entry=true</code> defines fallback commands
  90  * for that MIME type that will only be used if no non-fallback entry
  91  * can be found.  For example, an entry of the form <code>text/*; ;
  92  * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer</code>
  93  * specifies a view command to be used for any text MIME type.  This
  94  * view command would only be used if a non-fallback view command for
  95  * the MIME type could not be found.<p>
  96  *
  97  * MailcapCommandMap aware mailcap files have the
  98  * following general form:<p>
  99  * <code>
 100  * # Comments begin with a '#' and continue to the end of the line.<br>
 101  * &lt;mime type>; ; &lt;parameter list><br>
 102  * # Where a parameter list consists of one or more parameters,<br>
 103  * # where parameters look like: x-java-view=com.sun.TextViewer<br>
 104  * # and a parameter list looks like: <br>
 105  * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
 106  * <br>
 107  * # Note that mailcap entries that do not contain 'x-java' parameters<br>
 108  * # and comply to RFC 1524 are simply ignored:<br>
 109  * image/gif; /usr/dt/bin/sdtimage %s<br>
 110  *
 111  * </code>
 112  * <p>
 113  *
 114  * @author Bart Calder
 115  * @author Bill Shannon
 116  *
 117  * @since 1.6
 118  */
 119 
 120 public class MailcapCommandMap extends CommandMap {
 121     /*
 122      * We manage a collection of databases, searched in order.
 123      */
 124     private MailcapFile[] DB;
 125     private static final int PROG = 0;  // programmatically added entries
 126 
 127     /**
 128      * The default Constructor.
 129      */
 130     public MailcapCommandMap() {
 131         super();
 132         List dbv = new ArrayList(5);    // usually 5 or less databases


 434     /**
 435      * Put the commands that are in the hash table, into the list.
 436      */
 437     private void appendCmdsToList(Map typeHash, List cmdList) {
 438         Iterator verb_enum = typeHash.keySet().iterator();
 439 
 440         while (verb_enum.hasNext()) {
 441             String verb = (String)verb_enum.next();
 442             List cmdList2 = (List)typeHash.get(verb);
 443             Iterator cmd_enum = ((List)cmdList2).iterator();
 444 
 445             while (cmd_enum.hasNext()) {
 446                 String cmd = (String)cmd_enum.next();
 447                 cmdList.add(new CommandInfo(verb, cmd));
 448                 // cmdList.add(0, new CommandInfo(verb, cmd));
 449             }
 450         }
 451     }
 452 
 453     /**
 454      * Get the command corresponding to <code>cmdName</code> for the MIME type.
 455      *
 456      * @param mimeType  the MIME type
 457      * @param cmdName   the command name
 458      * @return the CommandInfo object corresponding to the command.
 459      */
 460     public synchronized CommandInfo getCommand(String mimeType,
 461                                                         String cmdName) {
 462         if (mimeType != null)
 463             mimeType = mimeType.toLowerCase(Locale.ENGLISH);
 464 
 465         for (int i = 0; i < DB.length; i++) {
 466             if (DB[i] == null)
 467                 continue;
 468             Map cmdMap = DB[i].getMailcapList(mimeType);
 469             if (cmdMap != null) {
 470                 // get the cmd list for the cmd
 471                 List v = (List)cmdMap.get(cmdName);
 472                 if (v != null) {
 473                     String cmdClassName = (String)v.get(0);
 474 




  28 
  29 import java.util.*;
  30 import java.io.*;
  31 import java.net.*;
  32 import com.sun.activation.registries.MailcapFile;
  33 import com.sun.activation.registries.LogSupport;
  34 
  35 /**
  36  * MailcapCommandMap extends the CommandMap
  37  * abstract class. It implements a CommandMap whose configuration
  38  * is based on mailcap files
  39  * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
  40  * The MailcapCommandMap can be configured both programmatically
  41  * and via configuration files.
  42  * <p>
  43  * <b>Mailcap file search order:</b><p>
  44  * The MailcapCommandMap looks in various places in the user's
  45  * system for mailcap file entries. When requests are made
  46  * to search for commands in the MailcapCommandMap, it searches
  47  * mailcap files in the following order:

  48  * <ol>
  49  * <li> Programatically added entries to the MailcapCommandMap instance.
  50  * <li> The file {@code .mailcap} in the user's home directory.
  51  * <li> The file {@literal <}<i>java.home</i>{@literal >}{@code /lib/mailcap}.
  52  * <li> The file or resources named {@code META-INF/mailcap}.
  53  * <li> The file or resource named {@code META-INF/mailcap.default}
  54  * (usually found only in the {@code activation.jar} file).
  55  * </ol>
  56  * <p>
  57  * <b>Mailcap file format:</b><p>
  58  *
  59  * Mailcap files must conform to the mailcap
  60  * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
  61  * For Multimedia Mail Format Information</i>).
  62  * The file format consists of entries corresponding to
  63  * particular MIME types. In general, the specification
  64  * specifies <i>applications</i> for clients to use when they
  65  * themselves cannot operate on the specified MIME type. The
  66  * MailcapCommandMap extends this specification by using a parameter mechanism
  67  * in mailcap files that allows JavaBeans(tm) components to be specified as
  68  * corresponding to particular commands for a MIME type.<p>
  69  *
  70  * When a mailcap file is
  71  * parsed, the MailcapCommandMap recognizes certain parameter signatures,
  72  * specifically those parameter names that begin with {@code x-java-}.
  73  * The MailcapCommandMap uses this signature to find
  74  * command entries for inclusion into its registries.
  75  * Parameter names with the form {@code x-java-<name>}
  76  * are read by the MailcapCommandMap as identifying a command
  77  * with the name <i>name</i>. When the <i>name</i> is {@code 
  78  * content-handler} the MailcapCommandMap recognizes the class
  79  * signified by this parameter as a <i>DataContentHandler</i>.
  80  * All other commands are handled generically regardless of command
  81  * name. The command implementation is specified by a fully qualified
  82  * class name of a JavaBean(tm) component. For example; a command for viewing
  83  * some data can be specified as: {@code x-java-view=com.foo.ViewBean}.<p>
  84  *
  85  * When the command name is {@code fallback-entry}, the value of
  86  * the command may be {@code true} or {@code false}.  An
  87  * entry for a MIME type that includes a parameter of
  88  * {@code x-java-fallback-entry=true} defines fallback commands
  89  * for that MIME type that will only be used if no non-fallback entry
  90  * can be found.  For example, an entry of the form {@code text/*; ;
  91  * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer}
  92  * specifies a view command to be used for any text MIME type.  This
  93  * view command would only be used if a non-fallback view command for
  94  * the MIME type could not be found.<p>
  95  *
  96  * MailcapCommandMap aware mailcap files have the
  97  * following general form:
  98  * <pre>{@code
  99  * # Comments begin with a '#' and continue to the end of the line.
 100  * <mime type>; ; <parameter list>
 101  * # Where a parameter list consists of one or more parameters,
 102  * # where parameters look like: x-java-view=com.sun.TextViewer
 103  * # and a parameter list looks like:
 104  * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
 105  * # Note that mailcap entries that do not contain 'x-java' parameters
 106  * # and comply to RFC 1524 are simply ignored:
 107  * image/gif; /usr/dt/bin/sdtimage %s
 108  * }</pre>



 109  *
 110  * @author Bart Calder
 111  * @author Bill Shannon
 112  *
 113  * @since 1.6
 114  */
 115 
 116 public class MailcapCommandMap extends CommandMap {
 117     /*
 118      * We manage a collection of databases, searched in order.
 119      */
 120     private MailcapFile[] DB;
 121     private static final int PROG = 0;  // programmatically added entries
 122 
 123     /**
 124      * The default Constructor.
 125      */
 126     public MailcapCommandMap() {
 127         super();
 128         List dbv = new ArrayList(5);    // usually 5 or less databases


 430     /**
 431      * Put the commands that are in the hash table, into the list.
 432      */
 433     private void appendCmdsToList(Map typeHash, List cmdList) {
 434         Iterator verb_enum = typeHash.keySet().iterator();
 435 
 436         while (verb_enum.hasNext()) {
 437             String verb = (String)verb_enum.next();
 438             List cmdList2 = (List)typeHash.get(verb);
 439             Iterator cmd_enum = ((List)cmdList2).iterator();
 440 
 441             while (cmd_enum.hasNext()) {
 442                 String cmd = (String)cmd_enum.next();
 443                 cmdList.add(new CommandInfo(verb, cmd));
 444                 // cmdList.add(0, new CommandInfo(verb, cmd));
 445             }
 446         }
 447     }
 448 
 449     /**
 450      * Get the command corresponding to {@code cmdName} for the MIME type.
 451      *
 452      * @param mimeType  the MIME type
 453      * @param cmdName   the command name
 454      * @return the CommandInfo object corresponding to the command.
 455      */
 456     public synchronized CommandInfo getCommand(String mimeType,
 457                                                         String cmdName) {
 458         if (mimeType != null)
 459             mimeType = mimeType.toLowerCase(Locale.ENGLISH);
 460 
 461         for (int i = 0; i < DB.length; i++) {
 462             if (DB[i] == null)
 463                 continue;
 464             Map cmdMap = DB[i].getMailcapList(mimeType);
 465             if (cmdMap != null) {
 466                 // get the cmd list for the cmd
 467                 List v = (List)cmdMap.get(cmdName);
 468                 if (v != null) {
 469                     String cmdClassName = (String)v.get(0);
 470 


< prev index next >