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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 494
2fcd3ddb57a6
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26
aoqi@0 27 package javax.activation;
aoqi@0 28
aoqi@0 29 import java.util.*;
aoqi@0 30 import java.io.*;
aoqi@0 31 import java.net.*;
aoqi@0 32 import com.sun.activation.registries.MailcapFile;
aoqi@0 33 import com.sun.activation.registries.LogSupport;
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * MailcapCommandMap extends the CommandMap
aoqi@0 37 * abstract class. It implements a CommandMap whose configuration
aoqi@0 38 * is based on mailcap files
aoqi@0 39 * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
aoqi@0 40 * The MailcapCommandMap can be configured both programmatically
aoqi@0 41 * and via configuration files.
aoqi@0 42 * <p>
aoqi@0 43 * <b>Mailcap file search order:</b><p>
aoqi@0 44 * The MailcapCommandMap looks in various places in the user's
aoqi@0 45 * system for mailcap file entries. When requests are made
aoqi@0 46 * to search for commands in the MailcapCommandMap, it searches
aoqi@0 47 * mailcap files in the following order:
aoqi@0 48 * <p>
aoqi@0 49 * <ol>
aoqi@0 50 * <li> Programatically added entries to the MailcapCommandMap instance.
aoqi@0 51 * <li> The file <code>.mailcap</code> in the user's home directory.
aoqi@0 52 * <li> The file &lt;<i>java.home</i>&gt;<code>/lib/mailcap</code>.
aoqi@0 53 * <li> The file or resources named <code>META-INF/mailcap</code>.
aoqi@0 54 * <li> The file or resource named <code>META-INF/mailcap.default</code>
aoqi@0 55 * (usually found only in the <code>activation.jar</code> file).
aoqi@0 56 * </ol>
aoqi@0 57 * <p>
aoqi@0 58 * <b>Mailcap file format:</b><p>
aoqi@0 59 *
aoqi@0 60 * Mailcap files must conform to the mailcap
aoqi@0 61 * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
aoqi@0 62 * For Multimedia Mail Format Information</i>).
aoqi@0 63 * The file format consists of entries corresponding to
aoqi@0 64 * particular MIME types. In general, the specification
aoqi@0 65 * specifies <i>applications</i> for clients to use when they
aoqi@0 66 * themselves cannot operate on the specified MIME type. The
aoqi@0 67 * MailcapCommandMap extends this specification by using a parameter mechanism
aoqi@0 68 * in mailcap files that allows JavaBeans(tm) components to be specified as
aoqi@0 69 * corresponding to particular commands for a MIME type.<p>
aoqi@0 70 *
aoqi@0 71 * When a mailcap file is
aoqi@0 72 * parsed, the MailcapCommandMap recognizes certain parameter signatures,
aoqi@0 73 * specifically those parameter names that begin with <code>x-java-</code>.
aoqi@0 74 * The MailcapCommandMap uses this signature to find
aoqi@0 75 * command entries for inclusion into its registries.
aoqi@0 76 * Parameter names with the form <code>x-java-&lt;name></code>
aoqi@0 77 * are read by the MailcapCommandMap as identifying a command
aoqi@0 78 * with the name <i>name</i>. When the <i>name</i> is <code>
aoqi@0 79 * content-handler</code> the MailcapCommandMap recognizes the class
aoqi@0 80 * signified by this parameter as a <i>DataContentHandler</i>.
aoqi@0 81 * All other commands are handled generically regardless of command
aoqi@0 82 * name. The command implementation is specified by a fully qualified
aoqi@0 83 * class name of a JavaBean(tm) component. For example; a command for viewing
aoqi@0 84 * some data can be specified as: <code>x-java-view=com.foo.ViewBean</code>.<p>
aoqi@0 85 *
aoqi@0 86 * When the command name is <code>fallback-entry</code>, the value of
aoqi@0 87 * the command may be <code>true</code> or <code>false</code>. An
aoqi@0 88 * entry for a MIME type that includes a parameter of
aoqi@0 89 * <code>x-java-fallback-entry=true</code> defines fallback commands
aoqi@0 90 * for that MIME type that will only be used if no non-fallback entry
aoqi@0 91 * can be found. For example, an entry of the form <code>text/*; ;
aoqi@0 92 * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer</code>
aoqi@0 93 * specifies a view command to be used for any text MIME type. This
aoqi@0 94 * view command would only be used if a non-fallback view command for
aoqi@0 95 * the MIME type could not be found.<p>
aoqi@0 96 *
aoqi@0 97 * MailcapCommandMap aware mailcap files have the
aoqi@0 98 * following general form:<p>
aoqi@0 99 * <code>
aoqi@0 100 * # Comments begin with a '#' and continue to the end of the line.<br>
aoqi@0 101 * &lt;mime type>; ; &lt;parameter list><br>
aoqi@0 102 * # Where a parameter list consists of one or more parameters,<br>
aoqi@0 103 * # where parameters look like: x-java-view=com.sun.TextViewer<br>
aoqi@0 104 * # and a parameter list looks like: <br>
aoqi@0 105 * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
aoqi@0 106 * <br>
aoqi@0 107 * # Note that mailcap entries that do not contain 'x-java' parameters<br>
aoqi@0 108 * # and comply to RFC 1524 are simply ignored:<br>
aoqi@0 109 * image/gif; /usr/dt/bin/sdtimage %s<br>
aoqi@0 110 *
aoqi@0 111 * </code>
aoqi@0 112 * <p>
aoqi@0 113 *
aoqi@0 114 * @author Bart Calder
aoqi@0 115 * @author Bill Shannon
aoqi@0 116 *
aoqi@0 117 * @since 1.6
aoqi@0 118 */
aoqi@0 119
aoqi@0 120 public class MailcapCommandMap extends CommandMap {
aoqi@0 121 /*
aoqi@0 122 * We manage a collection of databases, searched in order.
aoqi@0 123 */
aoqi@0 124 private MailcapFile[] DB;
aoqi@0 125 private static final int PROG = 0; // programmatically added entries
aoqi@0 126
aoqi@0 127 /**
aoqi@0 128 * The default Constructor.
aoqi@0 129 */
aoqi@0 130 public MailcapCommandMap() {
aoqi@0 131 super();
aoqi@0 132 List dbv = new ArrayList(5); // usually 5 or less databases
aoqi@0 133 MailcapFile mf = null;
aoqi@0 134 dbv.add(null); // place holder for PROG entry
aoqi@0 135
aoqi@0 136 LogSupport.log("MailcapCommandMap: load HOME");
aoqi@0 137 try {
aoqi@0 138 String user_home = System.getProperty("user.home");
aoqi@0 139
aoqi@0 140 if (user_home != null) {
aoqi@0 141 String path = user_home + File.separator + ".mailcap";
aoqi@0 142 mf = loadFile(path);
aoqi@0 143 if (mf != null)
aoqi@0 144 dbv.add(mf);
aoqi@0 145 }
aoqi@0 146 } catch (SecurityException ex) {}
aoqi@0 147
aoqi@0 148 LogSupport.log("MailcapCommandMap: load SYS");
aoqi@0 149 try {
aoqi@0 150 // check system's home
aoqi@0 151 String system_mailcap = System.getProperty("java.home") +
aoqi@0 152 File.separator + "lib" + File.separator + "mailcap";
aoqi@0 153 mf = loadFile(system_mailcap);
aoqi@0 154 if (mf != null)
aoqi@0 155 dbv.add(mf);
aoqi@0 156 } catch (SecurityException ex) {}
aoqi@0 157
aoqi@0 158 LogSupport.log("MailcapCommandMap: load JAR");
aoqi@0 159 // load from the app's jar file
aoqi@0 160 loadAllResources(dbv, "META-INF/mailcap");
aoqi@0 161
aoqi@0 162 LogSupport.log("MailcapCommandMap: load DEF");
aoqi@0 163 mf = loadResource("/META-INF/mailcap.default");
aoqi@0 164
aoqi@0 165 if (mf != null)
aoqi@0 166 dbv.add(mf);
aoqi@0 167
aoqi@0 168 DB = new MailcapFile[dbv.size()];
aoqi@0 169 DB = (MailcapFile[])dbv.toArray(DB);
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 /**
aoqi@0 173 * Load from the named resource.
aoqi@0 174 */
aoqi@0 175 private MailcapFile loadResource(String name) {
aoqi@0 176 InputStream clis = null;
aoqi@0 177 try {
aoqi@0 178 clis = SecuritySupport.getResourceAsStream(this.getClass(), name);
aoqi@0 179 if (clis != null) {
aoqi@0 180 MailcapFile mf = new MailcapFile(clis);
aoqi@0 181 if (LogSupport.isLoggable())
aoqi@0 182 LogSupport.log("MailcapCommandMap: successfully loaded " +
aoqi@0 183 "mailcap file: " + name);
aoqi@0 184 return mf;
aoqi@0 185 } else {
aoqi@0 186 if (LogSupport.isLoggable())
aoqi@0 187 LogSupport.log("MailcapCommandMap: not loading " +
aoqi@0 188 "mailcap file: " + name);
aoqi@0 189 }
aoqi@0 190 } catch (IOException e) {
aoqi@0 191 if (LogSupport.isLoggable())
aoqi@0 192 LogSupport.log("MailcapCommandMap: can't load " + name, e);
aoqi@0 193 } catch (SecurityException sex) {
aoqi@0 194 if (LogSupport.isLoggable())
aoqi@0 195 LogSupport.log("MailcapCommandMap: can't load " + name, sex);
aoqi@0 196 } finally {
aoqi@0 197 try {
aoqi@0 198 if (clis != null)
aoqi@0 199 clis.close();
aoqi@0 200 } catch (IOException ex) { } // ignore it
aoqi@0 201 }
aoqi@0 202 return null;
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 /**
aoqi@0 206 * Load all of the named resource.
aoqi@0 207 */
aoqi@0 208 private void loadAllResources(List v, String name) {
aoqi@0 209 boolean anyLoaded = false;
aoqi@0 210 try {
aoqi@0 211 URL[] urls;
aoqi@0 212 ClassLoader cld = null;
aoqi@0 213 // First try the "application's" class loader.
aoqi@0 214 cld = SecuritySupport.getContextClassLoader();
aoqi@0 215 if (cld == null)
aoqi@0 216 cld = this.getClass().getClassLoader();
aoqi@0 217 if (cld != null)
aoqi@0 218 urls = SecuritySupport.getResources(cld, name);
aoqi@0 219 else
aoqi@0 220 urls = SecuritySupport.getSystemResources(name);
aoqi@0 221 if (urls != null) {
aoqi@0 222 if (LogSupport.isLoggable())
aoqi@0 223 LogSupport.log("MailcapCommandMap: getResources");
aoqi@0 224 for (int i = 0; i < urls.length; i++) {
aoqi@0 225 URL url = urls[i];
aoqi@0 226 InputStream clis = null;
aoqi@0 227 if (LogSupport.isLoggable())
aoqi@0 228 LogSupport.log("MailcapCommandMap: URL " + url);
aoqi@0 229 try {
aoqi@0 230 clis = SecuritySupport.openStream(url);
aoqi@0 231 if (clis != null) {
aoqi@0 232 v.add(new MailcapFile(clis));
aoqi@0 233 anyLoaded = true;
aoqi@0 234 if (LogSupport.isLoggable())
aoqi@0 235 LogSupport.log("MailcapCommandMap: " +
aoqi@0 236 "successfully loaded " +
aoqi@0 237 "mailcap file from URL: " +
aoqi@0 238 url);
aoqi@0 239 } else {
aoqi@0 240 if (LogSupport.isLoggable())
aoqi@0 241 LogSupport.log("MailcapCommandMap: " +
aoqi@0 242 "not loading mailcap " +
aoqi@0 243 "file from URL: " + url);
aoqi@0 244 }
aoqi@0 245 } catch (IOException ioex) {
aoqi@0 246 if (LogSupport.isLoggable())
aoqi@0 247 LogSupport.log("MailcapCommandMap: can't load " +
aoqi@0 248 url, ioex);
aoqi@0 249 } catch (SecurityException sex) {
aoqi@0 250 if (LogSupport.isLoggable())
aoqi@0 251 LogSupport.log("MailcapCommandMap: can't load " +
aoqi@0 252 url, sex);
aoqi@0 253 } finally {
aoqi@0 254 try {
aoqi@0 255 if (clis != null)
aoqi@0 256 clis.close();
aoqi@0 257 } catch (IOException cex) { }
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260 }
aoqi@0 261 } catch (Exception ex) {
aoqi@0 262 if (LogSupport.isLoggable())
aoqi@0 263 LogSupport.log("MailcapCommandMap: can't load " + name, ex);
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 // if failed to load anything, fall back to old technique, just in case
aoqi@0 267 if (!anyLoaded) {
aoqi@0 268 if (LogSupport.isLoggable())
aoqi@0 269 LogSupport.log("MailcapCommandMap: !anyLoaded");
aoqi@0 270 MailcapFile mf = loadResource("/" + name);
aoqi@0 271 if (mf != null)
aoqi@0 272 v.add(mf);
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 /**
aoqi@0 277 * Load from the named file.
aoqi@0 278 */
aoqi@0 279 private MailcapFile loadFile(String name) {
aoqi@0 280 MailcapFile mtf = null;
aoqi@0 281
aoqi@0 282 try {
aoqi@0 283 mtf = new MailcapFile(name);
aoqi@0 284 } catch (IOException e) {
aoqi@0 285 // e.printStackTrace();
aoqi@0 286 }
aoqi@0 287 return mtf;
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 /**
aoqi@0 291 * Constructor that allows the caller to specify the path
aoqi@0 292 * of a <i>mailcap</i> file.
aoqi@0 293 *
aoqi@0 294 * @param fileName The name of the <i>mailcap</i> file to open
aoqi@0 295 * @exception IOException if the file can't be accessed
aoqi@0 296 */
aoqi@0 297 public MailcapCommandMap(String fileName) throws IOException {
aoqi@0 298 this();
aoqi@0 299
aoqi@0 300 if (LogSupport.isLoggable())
aoqi@0 301 LogSupport.log("MailcapCommandMap: load PROG from " + fileName);
aoqi@0 302 if (DB[PROG] == null) {
aoqi@0 303 DB[PROG] = new MailcapFile(fileName);
aoqi@0 304 }
aoqi@0 305 }
aoqi@0 306
aoqi@0 307
aoqi@0 308 /**
aoqi@0 309 * Constructor that allows the caller to specify an <i>InputStream</i>
aoqi@0 310 * containing a mailcap file.
aoqi@0 311 *
aoqi@0 312 * @param is InputStream of the <i>mailcap</i> file to open
aoqi@0 313 */
aoqi@0 314 public MailcapCommandMap(InputStream is) {
aoqi@0 315 this();
aoqi@0 316
aoqi@0 317 LogSupport.log("MailcapCommandMap: load PROG");
aoqi@0 318 if (DB[PROG] == null) {
aoqi@0 319 try {
aoqi@0 320 DB[PROG] = new MailcapFile(is);
aoqi@0 321 } catch (IOException ex) {
aoqi@0 322 // XXX - should throw it
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 /**
aoqi@0 328 * Get the preferred command list for a MIME Type. The MailcapCommandMap
aoqi@0 329 * searches the mailcap files as described above under
aoqi@0 330 * <i>Mailcap file search order</i>.<p>
aoqi@0 331 *
aoqi@0 332 * The result of the search is a proper subset of available
aoqi@0 333 * commands in all mailcap files known to this instance of
aoqi@0 334 * MailcapCommandMap. The first entry for a particular command
aoqi@0 335 * is considered the preferred command.
aoqi@0 336 *
aoqi@0 337 * @param mimeType the MIME type
aoqi@0 338 * @return the CommandInfo objects representing the preferred commands.
aoqi@0 339 */
aoqi@0 340 public synchronized CommandInfo[] getPreferredCommands(String mimeType) {
aoqi@0 341 List cmdList = new ArrayList();
aoqi@0 342 if (mimeType != null)
aoqi@0 343 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
aoqi@0 344
aoqi@0 345 for (int i = 0; i < DB.length; i++) {
aoqi@0 346 if (DB[i] == null)
aoqi@0 347 continue;
aoqi@0 348 Map cmdMap = DB[i].getMailcapList(mimeType);
aoqi@0 349 if (cmdMap != null)
aoqi@0 350 appendPrefCmdsToList(cmdMap, cmdList);
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 // now add the fallback commands
aoqi@0 354 for (int i = 0; i < DB.length; i++) {
aoqi@0 355 if (DB[i] == null)
aoqi@0 356 continue;
aoqi@0 357 Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
aoqi@0 358 if (cmdMap != null)
aoqi@0 359 appendPrefCmdsToList(cmdMap, cmdList);
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()];
aoqi@0 363 cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos);
aoqi@0 364
aoqi@0 365 return cmdInfos;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 /**
aoqi@0 369 * Put the commands that are in the hash table, into the list.
aoqi@0 370 */
aoqi@0 371 private void appendPrefCmdsToList(Map cmdHash, List cmdList) {
aoqi@0 372 Iterator verb_enum = cmdHash.keySet().iterator();
aoqi@0 373
aoqi@0 374 while (verb_enum.hasNext()) {
aoqi@0 375 String verb = (String)verb_enum.next();
aoqi@0 376 if (!checkForVerb(cmdList, verb)) {
aoqi@0 377 List cmdList2 = (List)cmdHash.get(verb); // get the list
aoqi@0 378 String className = (String)cmdList2.get(0);
aoqi@0 379 cmdList.add(new CommandInfo(verb, className));
aoqi@0 380 }
aoqi@0 381 }
aoqi@0 382 }
aoqi@0 383
aoqi@0 384 /**
aoqi@0 385 * Check the cmdList to see if this command exists, return
aoqi@0 386 * true if the verb is there.
aoqi@0 387 */
aoqi@0 388 private boolean checkForVerb(List cmdList, String verb) {
aoqi@0 389 Iterator ee = cmdList.iterator();
aoqi@0 390 while (ee.hasNext()) {
aoqi@0 391 String enum_verb =
aoqi@0 392 (String)((CommandInfo)ee.next()).getCommandName();
aoqi@0 393 if (enum_verb.equals(verb))
aoqi@0 394 return true;
aoqi@0 395 }
aoqi@0 396 return false;
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 /**
aoqi@0 400 * Get all the available commands in all mailcap files known to
aoqi@0 401 * this instance of MailcapCommandMap for this MIME type.
aoqi@0 402 *
aoqi@0 403 * @param mimeType the MIME type
aoqi@0 404 * @return the CommandInfo objects representing all the commands.
aoqi@0 405 */
aoqi@0 406 public synchronized CommandInfo[] getAllCommands(String mimeType) {
aoqi@0 407 List cmdList = new ArrayList();
aoqi@0 408 if (mimeType != null)
aoqi@0 409 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
aoqi@0 410
aoqi@0 411 for (int i = 0; i < DB.length; i++) {
aoqi@0 412 if (DB[i] == null)
aoqi@0 413 continue;
aoqi@0 414 Map cmdMap = DB[i].getMailcapList(mimeType);
aoqi@0 415 if (cmdMap != null)
aoqi@0 416 appendCmdsToList(cmdMap, cmdList);
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 // now add the fallback commands
aoqi@0 420 for (int i = 0; i < DB.length; i++) {
aoqi@0 421 if (DB[i] == null)
aoqi@0 422 continue;
aoqi@0 423 Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
aoqi@0 424 if (cmdMap != null)
aoqi@0 425 appendCmdsToList(cmdMap, cmdList);
aoqi@0 426 }
aoqi@0 427
aoqi@0 428 CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()];
aoqi@0 429 cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos);
aoqi@0 430
aoqi@0 431 return cmdInfos;
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 /**
aoqi@0 435 * Put the commands that are in the hash table, into the list.
aoqi@0 436 */
aoqi@0 437 private void appendCmdsToList(Map typeHash, List cmdList) {
aoqi@0 438 Iterator verb_enum = typeHash.keySet().iterator();
aoqi@0 439
aoqi@0 440 while (verb_enum.hasNext()) {
aoqi@0 441 String verb = (String)verb_enum.next();
aoqi@0 442 List cmdList2 = (List)typeHash.get(verb);
aoqi@0 443 Iterator cmd_enum = ((List)cmdList2).iterator();
aoqi@0 444
aoqi@0 445 while (cmd_enum.hasNext()) {
aoqi@0 446 String cmd = (String)cmd_enum.next();
aoqi@0 447 cmdList.add(new CommandInfo(verb, cmd));
aoqi@0 448 // cmdList.add(0, new CommandInfo(verb, cmd));
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 /**
aoqi@0 454 * Get the command corresponding to <code>cmdName</code> for the MIME type.
aoqi@0 455 *
aoqi@0 456 * @param mimeType the MIME type
aoqi@0 457 * @param cmdName the command name
aoqi@0 458 * @return the CommandInfo object corresponding to the command.
aoqi@0 459 */
aoqi@0 460 public synchronized CommandInfo getCommand(String mimeType,
aoqi@0 461 String cmdName) {
aoqi@0 462 if (mimeType != null)
aoqi@0 463 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
aoqi@0 464
aoqi@0 465 for (int i = 0; i < DB.length; i++) {
aoqi@0 466 if (DB[i] == null)
aoqi@0 467 continue;
aoqi@0 468 Map cmdMap = DB[i].getMailcapList(mimeType);
aoqi@0 469 if (cmdMap != null) {
aoqi@0 470 // get the cmd list for the cmd
aoqi@0 471 List v = (List)cmdMap.get(cmdName);
aoqi@0 472 if (v != null) {
aoqi@0 473 String cmdClassName = (String)v.get(0);
aoqi@0 474
aoqi@0 475 if (cmdClassName != null)
aoqi@0 476 return new CommandInfo(cmdName, cmdClassName);
aoqi@0 477 }
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 // now try the fallback list
aoqi@0 482 for (int i = 0; i < DB.length; i++) {
aoqi@0 483 if (DB[i] == null)
aoqi@0 484 continue;
aoqi@0 485 Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
aoqi@0 486 if (cmdMap != null) {
aoqi@0 487 // get the cmd list for the cmd
aoqi@0 488 List v = (List)cmdMap.get(cmdName);
aoqi@0 489 if (v != null) {
aoqi@0 490 String cmdClassName = (String)v.get(0);
aoqi@0 491
aoqi@0 492 if (cmdClassName != null)
aoqi@0 493 return new CommandInfo(cmdName, cmdClassName);
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 }
aoqi@0 497 return null;
aoqi@0 498 }
aoqi@0 499
aoqi@0 500 /**
aoqi@0 501 * Add entries to the registry. Programmatically
aoqi@0 502 * added entries are searched before other entries.<p>
aoqi@0 503 *
aoqi@0 504 * The string that is passed in should be in mailcap
aoqi@0 505 * format.
aoqi@0 506 *
aoqi@0 507 * @param mail_cap a correctly formatted mailcap string
aoqi@0 508 */
aoqi@0 509 public synchronized void addMailcap(String mail_cap) {
aoqi@0 510 // check to see if one exists
aoqi@0 511 LogSupport.log("MailcapCommandMap: add to PROG");
aoqi@0 512 if (DB[PROG] == null)
aoqi@0 513 DB[PROG] = new MailcapFile();
aoqi@0 514
aoqi@0 515 DB[PROG].appendToMailcap(mail_cap);
aoqi@0 516 }
aoqi@0 517
aoqi@0 518 /**
aoqi@0 519 * Return the DataContentHandler for the specified MIME type.
aoqi@0 520 *
aoqi@0 521 * @param mimeType the MIME type
aoqi@0 522 * @return the DataContentHandler
aoqi@0 523 */
aoqi@0 524 public synchronized DataContentHandler createDataContentHandler(
aoqi@0 525 String mimeType) {
aoqi@0 526 if (LogSupport.isLoggable())
aoqi@0 527 LogSupport.log(
aoqi@0 528 "MailcapCommandMap: createDataContentHandler for " + mimeType);
aoqi@0 529 if (mimeType != null)
aoqi@0 530 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
aoqi@0 531
aoqi@0 532 for (int i = 0; i < DB.length; i++) {
aoqi@0 533 if (DB[i] == null)
aoqi@0 534 continue;
aoqi@0 535 if (LogSupport.isLoggable())
aoqi@0 536 LogSupport.log(" search DB #" + i);
aoqi@0 537 Map cmdMap = DB[i].getMailcapList(mimeType);
aoqi@0 538 if (cmdMap != null) {
aoqi@0 539 List v = (List)cmdMap.get("content-handler");
aoqi@0 540 if (v != null) {
aoqi@0 541 String name = (String)v.get(0);
aoqi@0 542 DataContentHandler dch = getDataContentHandler(name);
aoqi@0 543 if (dch != null)
aoqi@0 544 return dch;
aoqi@0 545 }
aoqi@0 546 }
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 // now try the fallback entries
aoqi@0 550 for (int i = 0; i < DB.length; i++) {
aoqi@0 551 if (DB[i] == null)
aoqi@0 552 continue;
aoqi@0 553 if (LogSupport.isLoggable())
aoqi@0 554 LogSupport.log(" search fallback DB #" + i);
aoqi@0 555 Map cmdMap = DB[i].getMailcapFallbackList(mimeType);
aoqi@0 556 if (cmdMap != null) {
aoqi@0 557 List v = (List)cmdMap.get("content-handler");
aoqi@0 558 if (v != null) {
aoqi@0 559 String name = (String)v.get(0);
aoqi@0 560 DataContentHandler dch = getDataContentHandler(name);
aoqi@0 561 if (dch != null)
aoqi@0 562 return dch;
aoqi@0 563 }
aoqi@0 564 }
aoqi@0 565 }
aoqi@0 566 return null;
aoqi@0 567 }
aoqi@0 568
aoqi@0 569 private DataContentHandler getDataContentHandler(String name) {
aoqi@0 570 if (LogSupport.isLoggable())
aoqi@0 571 LogSupport.log(" got content-handler");
aoqi@0 572 if (LogSupport.isLoggable())
aoqi@0 573 LogSupport.log(" class " + name);
aoqi@0 574 try {
aoqi@0 575 ClassLoader cld = null;
aoqi@0 576 // First try the "application's" class loader.
aoqi@0 577 cld = SecuritySupport.getContextClassLoader();
aoqi@0 578 if (cld == null)
aoqi@0 579 cld = this.getClass().getClassLoader();
aoqi@0 580 Class cl = null;
aoqi@0 581 try {
aoqi@0 582 cl = cld.loadClass(name);
aoqi@0 583 } catch (Exception ex) {
aoqi@0 584 // if anything goes wrong, do it the old way
aoqi@0 585 cl = Class.forName(name);
aoqi@0 586 }
aoqi@0 587 if (cl != null) // XXX - always true?
aoqi@0 588 return (DataContentHandler)cl.newInstance();
aoqi@0 589 } catch (IllegalAccessException e) {
aoqi@0 590 if (LogSupport.isLoggable())
aoqi@0 591 LogSupport.log("Can't load DCH " + name, e);
aoqi@0 592 } catch (ClassNotFoundException e) {
aoqi@0 593 if (LogSupport.isLoggable())
aoqi@0 594 LogSupport.log("Can't load DCH " + name, e);
aoqi@0 595 } catch (InstantiationException e) {
aoqi@0 596 if (LogSupport.isLoggable())
aoqi@0 597 LogSupport.log("Can't load DCH " + name, e);
aoqi@0 598 }
aoqi@0 599 return null;
aoqi@0 600 }
aoqi@0 601
aoqi@0 602 /**
aoqi@0 603 * Get all the MIME types known to this command map.
aoqi@0 604 *
aoqi@0 605 * @return array of MIME types as strings
aoqi@0 606 * @since JAF 1.1
aoqi@0 607 */
aoqi@0 608 public synchronized String[] getMimeTypes() {
aoqi@0 609 List mtList = new ArrayList();
aoqi@0 610
aoqi@0 611 for (int i = 0; i < DB.length; i++) {
aoqi@0 612 if (DB[i] == null)
aoqi@0 613 continue;
aoqi@0 614 String[] ts = DB[i].getMimeTypes();
aoqi@0 615 if (ts != null) {
aoqi@0 616 for (int j = 0; j < ts.length; j++) {
aoqi@0 617 // eliminate duplicates
aoqi@0 618 if (!mtList.contains(ts[j]))
aoqi@0 619 mtList.add(ts[j]);
aoqi@0 620 }
aoqi@0 621 }
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 String[] mts = new String[mtList.size()];
aoqi@0 625 mts = (String[])mtList.toArray(mts);
aoqi@0 626
aoqi@0 627 return mts;
aoqi@0 628 }
aoqi@0 629
aoqi@0 630 /**
aoqi@0 631 * Get the native commands for the given MIME type.
aoqi@0 632 * Returns an array of strings where each string is
aoqi@0 633 * an entire mailcap file entry. The application
aoqi@0 634 * will need to parse the entry to extract the actual
aoqi@0 635 * command as well as any attributes it needs. See
aoqi@0 636 * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>
aoqi@0 637 * for details of the mailcap entry syntax. Only mailcap
aoqi@0 638 * entries that specify a view command for the specified
aoqi@0 639 * MIME type are returned.
aoqi@0 640 *
aoqi@0 641 * @return array of native command entries
aoqi@0 642 * @since JAF 1.1
aoqi@0 643 */
aoqi@0 644 public synchronized String[] getNativeCommands(String mimeType) {
aoqi@0 645 List cmdList = new ArrayList();
aoqi@0 646 if (mimeType != null)
aoqi@0 647 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
aoqi@0 648
aoqi@0 649 for (int i = 0; i < DB.length; i++) {
aoqi@0 650 if (DB[i] == null)
aoqi@0 651 continue;
aoqi@0 652 String[] cmds = DB[i].getNativeCommands(mimeType);
aoqi@0 653 if (cmds != null) {
aoqi@0 654 for (int j = 0; j < cmds.length; j++) {
aoqi@0 655 // eliminate duplicates
aoqi@0 656 if (!cmdList.contains(cmds[j]))
aoqi@0 657 cmdList.add(cmds[j]);
aoqi@0 658 }
aoqi@0 659 }
aoqi@0 660 }
aoqi@0 661
aoqi@0 662 String[] cmds = new String[cmdList.size()];
aoqi@0 663 cmds = (String[])cmdList.toArray(cmds);
aoqi@0 664
aoqi@0 665 return cmds;
aoqi@0 666 }
aoqi@0 667
aoqi@0 668 /**
aoqi@0 669 * for debugging...
aoqi@0 670 *
aoqi@0 671 public static void main(String[] argv) throws Exception {
aoqi@0 672 MailcapCommandMap map = new MailcapCommandMap();
aoqi@0 673 CommandInfo[] cmdInfo;
aoqi@0 674
aoqi@0 675 cmdInfo = map.getPreferredCommands(argv[0]);
aoqi@0 676 System.out.println("Preferred Commands:");
aoqi@0 677 for (int i = 0; i < cmdInfo.length; i++)
aoqi@0 678 System.out.println("Command " + cmdInfo[i].getCommandName() + " [" +
aoqi@0 679 cmdInfo[i].getCommandClass() + "]");
aoqi@0 680 cmdInfo = map.getAllCommands(argv[0]);
aoqi@0 681 System.out.println();
aoqi@0 682 System.out.println("All Commands:");
aoqi@0 683 for (int i = 0; i < cmdInfo.length; i++)
aoqi@0 684 System.out.println("Command " + cmdInfo[i].getCommandName() + " [" +
aoqi@0 685 cmdInfo[i].getCommandClass() + "]");
aoqi@0 686 DataContentHandler dch = map.createDataContentHandler(argv[0]);
aoqi@0 687 if (dch != null)
aoqi@0 688 System.out.println("DataContentHandler " +
aoqi@0 689 dch.getClass().toString());
aoqi@0 690 System.exit(0);
aoqi@0 691 }
aoqi@0 692 */
aoqi@0 693 }

mercurial