/* * @(#)MimeEntry.java 1.18 96/12/21 * * Copyright (c) 1994,1996 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for NON-COMMERCIAL purposes and without fee is hereby * granted provided that this copyright notice appears in all copies. Please * refer to the file "copyright.html" for further important copyright and * licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR * ITS DERIVATIVES. */ package sun.net.www; import java.net.URL; import java.io.*; import java.util.StringTokenizer; public class MimeEntry implements Cloneable { public static final String defaultImagePath = "doc:/lib/images/ftp"; private String typeName; // of the form: "type/subtype" private String tempFileNameTemplate; private int action; private String command; private String description; private String imageFileName; private String fileExtensions[]; boolean starred; // Actions public static final int UNKNOWN = 0; public static final int LOAD_INTO_BROWSER = 1; public static final int SAVE_TO_FILE = 2; public static final int LAUNCH_APPLICATION = 3; static final String[] actionKeywords = { "unknown", "browser", "save", "application", }; /** * Construct an empty entry of the given type and subtype. */ public MimeEntry(String type) { // Default action is UNKNOWN so clients can decide what the default // should be, typically save to file or ask user. this(type, UNKNOWN, null, null, null); } // // The next two constructors are used only by the deprecated // PlatformMimeTable classes or, in last case, is called by the public // constructor. They are kept here anticipating putting support for // mailcap formatted config files back in (so BOTH the properties format // and the mailcap formats are supported). // MimeEntry(String type, String imageFileName, String extensionString) { typeName = type.toLowerCase(); action = UNKNOWN; command = null; this.imageFileName = imageFileName; setExtensions(extensionString); starred = isStarred(typeName); } // For use with MimeTable::parseMailCap MimeEntry(String typeName, int action, String command, String tempFileNameTemplate) { this.typeName = typeName.toLowerCase(); this.action = action; this.command = command; this.imageFileName = null; this.fileExtensions = null; this.tempFileNameTemplate = tempFileNameTemplate; } // This is the one called by the public constructor. MimeEntry(String typeName, int action, String command, String imageFileName, String fileExtensions[]) { this.typeName = typeName.toLowerCase(); this.action = action; this.command = command; this.imageFileName = imageFileName; this.fileExtensions = fileExtensions; starred = isStarred(typeName); } public synchronized String getType() { return typeName; } public synchronized void setType(String type) { typeName = type.toLowerCase(); } public synchronized int getAction() { return action; } public synchronized void setAction(int action, String command) { this.action = action; this.command = command; } public synchronized void setAction(int action) { this.action = action; } public synchronized String getLaunchString() { return command; } public synchronized void setCommand(String command) { this.command = command; } public synchronized String getDescription() { return (description != null ? description : typeName); } public synchronized void setDescription(String description) { this.description = description; } // ??? what to return for the image -- the file name or should this return // something more advanced like an image source or something? // returning the name has the least policy associated with it. // pro tempore, we'll use the name public String getImageFileName() { return imageFileName; } public synchronized void setImageFileName(String filename) { File file = new File(filename); if (file.getParent() == null) { imageFileName = MimeEntry.defaultImagePath + File.separator + filename; } else { imageFileName = filename; } if (filename.lastIndexOf('.') < 0) { imageFileName = imageFileName + ".gif"; } } public String getTempFileTemplate() { return tempFileNameTemplate; } public synchronized String[] getExtensions() { return fileExtensions; } public synchronized String getExtensionsAsList() { String extensionsAsString = ""; if (fileExtensions != null) { for (int i = 0; i < fileExtensions.length; i++) { extensionsAsString += fileExtensions[i]; if (i < (fileExtensions.length - 1)) { extensionsAsString += ","; } } } return extensionsAsString; } public synchronized void setExtensions(String extensionString) { StringTokenizer extTokens = new StringTokenizer(extensionString, ","); int numExts = extTokens.countTokens(); String extensionStrings[] = new String[numExts]; for (int i = 0; i < numExts; i++) { String ext = (String)extTokens.nextElement(); extensionStrings[i] = ext.trim(); } fileExtensions = extensionStrings; } private boolean isStarred(String typeName) { return (typeName != null) && (typeName.length() > 0) && (typeName.endsWith("/*")); } /** * Invoke the MIME type specific behavior for this MIME type. * Returned value can be one of several types: *