Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/MimeEntry.java
38830 views
/*1* Copyright (c) 1994, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.www;26import java.net.URL;27import java.io.*;28import java.util.StringTokenizer;2930public class MimeEntry implements Cloneable {31private String typeName; // of the form: "type/subtype"32private String tempFileNameTemplate;3334private int action;35private String command;36private String description;37private String imageFileName;38private String fileExtensions[];3940boolean starred;4142// Actions43public static final int UNKNOWN = 0;44public static final int LOAD_INTO_BROWSER = 1;45public static final int SAVE_TO_FILE = 2;46public static final int LAUNCH_APPLICATION = 3;4748static final String[] actionKeywords = {49"unknown",50"browser",51"save",52"application",53};5455/**56* Construct an empty entry of the given type and subtype.57*/58public MimeEntry(String type) {59// Default action is UNKNOWN so clients can decide what the default60// should be, typically save to file or ask user.61this(type, UNKNOWN, null, null, null);62}6364//65// The next two constructors are used only by the deprecated66// PlatformMimeTable classes or, in last case, is called by the public67// constructor. They are kept here anticipating putting support for68// mailcap formatted config files back in (so BOTH the properties format69// and the mailcap formats are supported).70//71MimeEntry(String type, String imageFileName, String extensionString) {72typeName = type.toLowerCase();73action = UNKNOWN;74command = null;75this.imageFileName = imageFileName;76setExtensions(extensionString);77starred = isStarred(typeName);78}7980// For use with MimeTable::parseMailCap81MimeEntry(String typeName, int action, String command,82String tempFileNameTemplate) {83this.typeName = typeName.toLowerCase();84this.action = action;85this.command = command;86this.imageFileName = null;87this.fileExtensions = null;8889this.tempFileNameTemplate = tempFileNameTemplate;90}9192// This is the one called by the public constructor.93MimeEntry(String typeName, int action, String command,94String imageFileName, String fileExtensions[]) {9596this.typeName = typeName.toLowerCase();97this.action = action;98this.command = command;99this.imageFileName = imageFileName;100this.fileExtensions = fileExtensions;101102starred = isStarred(typeName);103104}105106public synchronized String getType() {107return typeName;108}109110public synchronized void setType(String type) {111typeName = type.toLowerCase();112}113114public synchronized int getAction() {115return action;116}117118public synchronized void setAction(int action, String command) {119this.action = action;120this.command = command;121}122123public synchronized void setAction(int action) {124this.action = action;125}126127public synchronized String getLaunchString() {128return command;129}130131public synchronized void setCommand(String command) {132this.command = command;133}134135public synchronized String getDescription() {136return (description != null ? description : typeName);137}138139public synchronized void setDescription(String description) {140this.description = description;141}142143// ??? what to return for the image -- the file name or should this return144// something more advanced like an image source or something?145// returning the name has the least policy associated with it.146// pro tempore, we'll use the name147public String getImageFileName() {148return imageFileName;149}150151public synchronized void setImageFileName(String filename) {152File file = new File(filename);153if (file.getParent() == null) {154imageFileName = System.getProperty(155"java.net.ftp.imagepath."+filename);156}157else {158imageFileName = filename;159}160161if (filename.lastIndexOf('.') < 0) {162imageFileName = imageFileName + ".gif";163}164}165166public String getTempFileTemplate() {167return tempFileNameTemplate;168}169170public synchronized String[] getExtensions() {171return fileExtensions;172}173174public synchronized String getExtensionsAsList() {175String extensionsAsString = "";176if (fileExtensions != null) {177for (int i = 0; i < fileExtensions.length; i++) {178extensionsAsString += fileExtensions[i];179if (i < (fileExtensions.length - 1)) {180extensionsAsString += ",";181}182}183}184185return extensionsAsString;186}187188public synchronized void setExtensions(String extensionString) {189StringTokenizer extTokens = new StringTokenizer(extensionString, ",");190int numExts = extTokens.countTokens();191String extensionStrings[] = new String[numExts];192193for (int i = 0; i < numExts; i++) {194String ext = (String)extTokens.nextElement();195extensionStrings[i] = ext.trim();196}197198fileExtensions = extensionStrings;199}200201private boolean isStarred(String typeName) {202return (typeName != null)203&& (typeName.length() > 0)204&& (typeName.endsWith("/*"));205}206207/**208* Invoke the MIME type specific behavior for this MIME type.209* Returned value can be one of several types:210* <ol>211* <li>A thread -- the caller can choose when to launch this thread.212* <li>A string -- the string is loaded into the browser directly.213* <li>An input stream -- the caller can read from this byte stream and214* will typically store the results in a file.215* <li>A document (?) --216* </ol>217*/218public Object launch(java.net.URLConnection urlc, InputStream is, MimeTable mt) throws ApplicationLaunchException {219switch (action) {220case SAVE_TO_FILE:221// REMIND: is this really the right thing to do?222try {223return is;224} catch(Exception e) {225// I18N226return "Load to file failed:\n" + e;227}228229case LOAD_INTO_BROWSER:230// REMIND: invoke the content handler?231// may be the right thing to do, may not be -- short term232// where docs are not loaded asynch, loading and returning233// the content is the right thing to do.234try {235return urlc.getContent();236} catch (Exception e) {237return null;238}239240case LAUNCH_APPLICATION:241{242String threadName = command;243int fst = threadName.indexOf(' ');244if (fst > 0) {245threadName = threadName.substring(0, fst);246}247248return new MimeLauncher(this, urlc, is,249mt.getTempFileTemplate(), threadName);250}251252case UNKNOWN:253// REMIND: What do do here?254return null;255}256257return null;258}259260public boolean matches(String type) {261if (starred) {262// REMIND: is this the right thing or not?263return type.startsWith(typeName);264} else {265return type.equals(typeName);266}267}268269public Object clone() {270// return a shallow copy of this.271MimeEntry theClone = new MimeEntry(typeName);272theClone.action = action;273theClone.command = command;274theClone.description = description;275theClone.imageFileName = imageFileName;276theClone.tempFileNameTemplate = tempFileNameTemplate;277theClone.fileExtensions = fileExtensions;278279return theClone;280}281282public synchronized String toProperty() {283StringBuffer buf = new StringBuffer();284285String separator = "; ";286boolean needSeparator = false;287288int action = getAction();289if (action != MimeEntry.UNKNOWN) {290buf.append("action=" + actionKeywords[action]);291needSeparator = true;292}293294String command = getLaunchString();295if (command != null && command.length() > 0) {296if (needSeparator) {297buf.append(separator);298}299buf.append("application=" + command);300needSeparator = true;301}302303if (getImageFileName() != null) {304if (needSeparator) {305buf.append(separator);306}307buf.append("icon=" + getImageFileName());308needSeparator = true;309}310311String extensions = getExtensionsAsList();312if (extensions.length() > 0) {313if (needSeparator) {314buf.append(separator);315}316buf.append("file_extensions=" + extensions);317needSeparator = true;318}319320String description = getDescription();321if (description != null && !description.equals(getType())) {322if (needSeparator) {323buf.append(separator);324}325buf.append("description=" + description);326}327328return buf.toString();329}330331public String toString() {332return "MimeEntry[contentType=" + typeName333+ ", image=" + imageFileName334+ ", action=" + action335+ ", command=" + command336+ ", extensions=" + getExtensionsAsList()337+ "]";338}339}340341342