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