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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 494
2fcd3ddb57a6
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial