Path: blob/master/src/java.base/share/classes/sun/net/www/MimeEntry.java
41159 views
/*1* Copyright (c) 1994, 2021, 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.StringJoiner;29import java.util.StringTokenizer;3031public class MimeEntry implements Cloneable {32private String typeName; // of the form: "type/subtype"33private String tempFileNameTemplate;3435private int action;36private String command;37private String description;38private String imageFileName;39private String fileExtensions[];4041boolean starred;4243// Actions44public static final int UNKNOWN = 0;45public static final int LOAD_INTO_BROWSER = 1;46public static final int SAVE_TO_FILE = 2;47public static final int LAUNCH_APPLICATION = 3;4849static final String[] actionKeywords = {50"unknown",51"browser",52"save",53"application",54};5556/**57* Construct an empty entry of the given type and subtype.58*/59public MimeEntry(String type) {60// Default action is UNKNOWN so clients can decide what the default61// should be, typically save to file or ask user.62this(type, UNKNOWN, null, null, null);63}6465//66// The next two constructors are used only by the deprecated67// PlatformMimeTable classes or, in last case, is called by the public68// constructor. They are kept here anticipating putting support for69// mailcap formatted config files back in (so BOTH the properties format70// and the mailcap formats are supported).71//72MimeEntry(String type, String imageFileName, String extensionString) {73typeName = type.toLowerCase();74action = UNKNOWN;75command = null;76this.imageFileName = imageFileName;77setExtensions(extensionString);78starred = isStarred(typeName);79}8081// For use with MimeTable::parseMailCap82MimeEntry(String typeName, int action, String command,83String tempFileNameTemplate) {84this.typeName = typeName.toLowerCase();85this.action = action;86this.command = command;87this.imageFileName = null;88this.fileExtensions = null;8990this.tempFileNameTemplate = tempFileNameTemplate;91}9293// This is the one called by the public constructor.94MimeEntry(String typeName, int action, String command,95String imageFileName, String fileExtensions[]) {9697this.typeName = typeName.toLowerCase();98this.action = action;99this.command = command;100this.imageFileName = imageFileName;101this.fileExtensions = fileExtensions;102103starred = isStarred(typeName);104105}106107public synchronized String getType() {108return typeName;109}110111public synchronized void setType(String type) {112typeName = type.toLowerCase();113}114115public synchronized int getAction() {116return action;117}118119public synchronized void setAction(int action, String command) {120this.action = action;121this.command = command;122}123124public synchronized void setAction(int action) {125this.action = action;126}127128public synchronized String getLaunchString() {129return command;130}131132public synchronized void setCommand(String command) {133this.command = command;134}135136public synchronized String getDescription() {137return (description != null ? description : typeName);138}139140public synchronized void setDescription(String description) {141this.description = description;142}143144// ??? what to return for the image -- the file name or should this return145// something more advanced like an image source or something?146// returning the name has the least policy associated with it.147// pro tempore, we'll use the name148public String getImageFileName() {149return imageFileName;150}151152public synchronized void setImageFileName(String filename) {153File file = new File(filename);154if (file.getParent() == null) {155imageFileName = System.getProperty(156"java.net.ftp.imagepath."+filename);157}158else {159imageFileName = filename;160}161162if (filename.lastIndexOf('.') < 0) {163imageFileName = imageFileName + ".gif";164}165}166167public String getTempFileTemplate() {168return tempFileNameTemplate;169}170171public synchronized String[] getExtensions() {172return fileExtensions;173}174175public synchronized String getExtensionsAsList() {176String extensionsAsString = "";177if (fileExtensions != null) {178for (int i = 0; i < fileExtensions.length; i++) {179extensionsAsString += fileExtensions[i];180if (i < (fileExtensions.length - 1)) {181extensionsAsString += ",";182}183}184}185186return extensionsAsString;187}188189public synchronized void setExtensions(String extensionString) {190StringTokenizer extTokens = new StringTokenizer(extensionString, ",");191int numExts = extTokens.countTokens();192String extensionStrings[] = new String[numExts];193194for (int i = 0; i < numExts; i++) {195String ext = (String)extTokens.nextElement();196extensionStrings[i] = ext.trim();197}198199fileExtensions = extensionStrings;200}201202private boolean isStarred(String typeName) {203return typeName != null && typeName.endsWith("/*");204}205206public boolean matches(String type) {207if (starred) {208// REMIND: is this the right thing or not?209return type.startsWith(typeName);210} else {211return type.equals(typeName);212}213}214215public Object clone() {216// return a shallow copy of this.217MimeEntry theClone = new MimeEntry(typeName);218theClone.action = action;219theClone.command = command;220theClone.description = description;221theClone.imageFileName = imageFileName;222theClone.tempFileNameTemplate = tempFileNameTemplate;223theClone.fileExtensions = fileExtensions;224225return theClone;226}227228public synchronized String toProperty() {229StringJoiner sj = new StringJoiner("; ");230231int action = getAction();232if (action != MimeEntry.UNKNOWN) {233sj.add("action=" + actionKeywords[action]);234}235236String command = getLaunchString();237if (command != null && command.length() > 0) {238sj.add("application=" + command);239}240241String image = getImageFileName();242if (image != null) {243sj.add("icon=" + image);244}245246String extensions = getExtensionsAsList();247if (!extensions.isEmpty()) {248sj.add("file_extensions=" + extensions);249}250251String description = getDescription();252if (description != null && !description.equals(getType())) {253sj.add("description=" + description);254}255256return sj.toString();257}258259public String toString() {260return "MimeEntry[contentType=" + typeName261+ ", image=" + imageFileName262+ ", action=" + action263+ ", command=" + command264+ ", extensions=" + getExtensionsAsList()265+ "]";266}267}268269270