feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
70
jdkSrc/jdk8/com/sun/activation/registries/LogSupport.java
Normal file
70
jdkSrc/jdk8/com/sun/activation/registries/LogSupport.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* Logging related methods.
|
||||
*/
|
||||
public class LogSupport {
|
||||
private static boolean debug = false;
|
||||
private static Logger logger;
|
||||
private static final Level level = Level.FINE;
|
||||
|
||||
static {
|
||||
try {
|
||||
debug = Boolean.getBoolean("javax.activation.debug");
|
||||
} catch (Throwable t) {
|
||||
// ignore any errors
|
||||
}
|
||||
logger = Logger.getLogger("javax.activation");
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
private LogSupport() {
|
||||
// private constructor, can't create instances
|
||||
}
|
||||
|
||||
public static void log(String msg) {
|
||||
if (debug)
|
||||
System.out.println(msg);
|
||||
logger.log(level, msg);
|
||||
}
|
||||
|
||||
public static void log(String msg, Throwable t) {
|
||||
if (debug)
|
||||
System.out.println(msg + "; Exception: " + t);
|
||||
logger.log(level, msg, t);
|
||||
}
|
||||
|
||||
public static boolean isLoggable() {
|
||||
return debug || logger.isLoggable(level);
|
||||
}
|
||||
}
|
563
jdkSrc/jdk8/com/sun/activation/registries/MailcapFile.java
Normal file
563
jdkSrc/jdk8/com/sun/activation/registries/MailcapFile.java
Normal file
@@ -0,0 +1,563 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class MailcapFile {
|
||||
|
||||
/**
|
||||
* A Map indexed by MIME type (string) that references
|
||||
* a Map of commands for each type. The comand Map
|
||||
* is indexed by the command name and references a List of
|
||||
* class names (strings) for each command.
|
||||
*/
|
||||
private Map type_hash = new HashMap();
|
||||
|
||||
/**
|
||||
* Another Map like above, but for fallback entries.
|
||||
*/
|
||||
private Map fallback_hash = new HashMap();
|
||||
|
||||
/**
|
||||
* A Map indexed by MIME type (string) that references
|
||||
* a List of native commands (string) corresponding to the type.
|
||||
*/
|
||||
private Map native_commands = new HashMap();
|
||||
|
||||
private static boolean addReverse = false;
|
||||
|
||||
static {
|
||||
try {
|
||||
addReverse = Boolean.getBoolean("javax.activation.addreverse");
|
||||
} catch (Throwable t) {
|
||||
// ignore any errors
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor that takes a filename as an argument.
|
||||
*
|
||||
* @param new_fname The file name of the mailcap file.
|
||||
*/
|
||||
public MailcapFile(String new_fname) throws IOException {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("new MailcapFile: file " + new_fname);
|
||||
FileReader reader = null;
|
||||
try {
|
||||
reader = new FileReader(new_fname);
|
||||
parse(new BufferedReader(reader));
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ex) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor that takes an input stream as an argument.
|
||||
*
|
||||
* @param is the input stream
|
||||
*/
|
||||
public MailcapFile(InputStream is) throws IOException {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("new MailcapFile: InputStream");
|
||||
parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mailcap file default constructor.
|
||||
*/
|
||||
public MailcapFile() {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("new MailcapFile: default");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Map of MailcapEntries based on the MIME type.
|
||||
*
|
||||
* <p>
|
||||
* <strong>Semantics:</strong> First check for the literal mime type,
|
||||
* if that fails looks for wildcard <type>/\* and return that. Return the
|
||||
* list of all that hit.
|
||||
*/
|
||||
public Map getMailcapList(String mime_type) {
|
||||
Map search_result = null;
|
||||
Map wildcard_result = null;
|
||||
|
||||
// first try the literal
|
||||
search_result = (Map)type_hash.get(mime_type);
|
||||
|
||||
// ok, now try the wildcard
|
||||
int separator = mime_type.indexOf('/');
|
||||
String subtype = mime_type.substring(separator + 1);
|
||||
if (!subtype.equals("*")) {
|
||||
String type = mime_type.substring(0, separator + 1) + "*";
|
||||
wildcard_result = (Map)type_hash.get(type);
|
||||
|
||||
if (wildcard_result != null) { // damn, we have to merge!!!
|
||||
if (search_result != null)
|
||||
search_result =
|
||||
mergeResults(search_result, wildcard_result);
|
||||
else
|
||||
search_result = wildcard_result;
|
||||
}
|
||||
}
|
||||
return search_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Map of fallback MailcapEntries based on the MIME type.
|
||||
*
|
||||
* <p>
|
||||
* <strong>Semantics:</strong> First check for the literal mime type,
|
||||
* if that fails looks for wildcard <type>/\* and return that. Return the
|
||||
* list of all that hit.
|
||||
*/
|
||||
public Map getMailcapFallbackList(String mime_type) {
|
||||
Map search_result = null;
|
||||
Map wildcard_result = null;
|
||||
|
||||
// first try the literal
|
||||
search_result = (Map)fallback_hash.get(mime_type);
|
||||
|
||||
// ok, now try the wildcard
|
||||
int separator = mime_type.indexOf('/');
|
||||
String subtype = mime_type.substring(separator + 1);
|
||||
if (!subtype.equals("*")) {
|
||||
String type = mime_type.substring(0, separator + 1) + "*";
|
||||
wildcard_result = (Map)fallback_hash.get(type);
|
||||
|
||||
if (wildcard_result != null) { // damn, we have to merge!!!
|
||||
if (search_result != null)
|
||||
search_result =
|
||||
mergeResults(search_result, wildcard_result);
|
||||
else
|
||||
search_result = wildcard_result;
|
||||
}
|
||||
}
|
||||
return search_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the MIME types known to this mailcap file.
|
||||
*/
|
||||
public String[] getMimeTypes() {
|
||||
Set types = new HashSet(type_hash.keySet());
|
||||
types.addAll(fallback_hash.keySet());
|
||||
types.addAll(native_commands.keySet());
|
||||
String[] mts = new String[types.size()];
|
||||
mts = (String[])types.toArray(mts);
|
||||
return mts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the native comands for the given MIME type.
|
||||
*/
|
||||
public String[] getNativeCommands(String mime_type) {
|
||||
String[] cmds = null;
|
||||
List v =
|
||||
(List)native_commands.get(mime_type.toLowerCase(Locale.ENGLISH));
|
||||
if (v != null) {
|
||||
cmds = new String[v.size()];
|
||||
cmds = (String[])v.toArray(cmds);
|
||||
}
|
||||
return cmds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge the first hash into the second.
|
||||
* This merge will only effect the hashtable that is
|
||||
* returned, we don't want to touch the one passed in since
|
||||
* its integrity must be maintained.
|
||||
*/
|
||||
private Map mergeResults(Map first, Map second) {
|
||||
Iterator verb_enum = second.keySet().iterator();
|
||||
Map clonedHash = new HashMap(first);
|
||||
|
||||
// iterate through the verbs in the second map
|
||||
while (verb_enum.hasNext()) {
|
||||
String verb = (String)verb_enum.next();
|
||||
List cmdVector = (List)clonedHash.get(verb);
|
||||
if (cmdVector == null) {
|
||||
clonedHash.put(verb, second.get(verb));
|
||||
} else {
|
||||
// merge the two
|
||||
List oldV = (List)second.get(verb);
|
||||
cmdVector = new ArrayList(cmdVector);
|
||||
cmdVector.addAll(oldV);
|
||||
clonedHash.put(verb, cmdVector);
|
||||
}
|
||||
}
|
||||
return clonedHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* appendToMailcap: Append to this Mailcap DB, use the mailcap
|
||||
* format:
|
||||
* Comment == "# <i>comment string</i>
|
||||
* Entry == "mimetype; javabeanclass<nl>
|
||||
*
|
||||
* Example:
|
||||
* # this is a comment
|
||||
* image/gif jaf.viewers.ImageViewer
|
||||
*/
|
||||
public void appendToMailcap(String mail_cap) {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("appendToMailcap: " + mail_cap);
|
||||
try {
|
||||
parse(new StringReader(mail_cap));
|
||||
} catch (IOException ex) {
|
||||
// can't happen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* parse file into a hash table of MC Type Entry Obj
|
||||
*/
|
||||
private void parse(Reader reader) throws IOException {
|
||||
BufferedReader buf_reader = new BufferedReader(reader);
|
||||
String line = null;
|
||||
String continued = null;
|
||||
|
||||
while ((line = buf_reader.readLine()) != null) {
|
||||
// LogSupport.log("parsing line: " + line);
|
||||
|
||||
line = line.trim();
|
||||
|
||||
try {
|
||||
if (line.charAt(0) == '#')
|
||||
continue;
|
||||
if (line.charAt(line.length() - 1) == '\\') {
|
||||
if (continued != null)
|
||||
continued += line.substring(0, line.length() - 1);
|
||||
else
|
||||
continued = line.substring(0, line.length() - 1);
|
||||
} else if (continued != null) {
|
||||
// handle the two strings
|
||||
continued = continued + line;
|
||||
// LogSupport.log("parse: " + continued);
|
||||
try {
|
||||
parseLine(continued);
|
||||
} catch (MailcapParseException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
continued = null;
|
||||
}
|
||||
else {
|
||||
// LogSupport.log("parse: " + line);
|
||||
try {
|
||||
parseLine(line);
|
||||
// LogSupport.log("hash.size = " + type_hash.size());
|
||||
} catch (MailcapParseException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (StringIndexOutOfBoundsException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A routine to parse individual entries in a Mailcap file.
|
||||
*
|
||||
* Note that this routine does not handle line continuations.
|
||||
* They should have been handled prior to calling this routine.
|
||||
*/
|
||||
protected void parseLine(String mailcapEntry)
|
||||
throws MailcapParseException, IOException {
|
||||
MailcapTokenizer tokenizer = new MailcapTokenizer(mailcapEntry);
|
||||
tokenizer.setIsAutoquoting(false);
|
||||
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("parse: " + mailcapEntry);
|
||||
// parse the primary type
|
||||
int currentToken = tokenizer.nextToken();
|
||||
if (currentToken != MailcapTokenizer.STRING_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.STRING_TOKEN, currentToken,
|
||||
tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
String primaryType =
|
||||
tokenizer.getCurrentTokenValue().toLowerCase(Locale.ENGLISH);
|
||||
String subType = "*";
|
||||
|
||||
// parse the '/' between primary and sub
|
||||
// if it's not present that's ok, we just don't have a subtype
|
||||
currentToken = tokenizer.nextToken();
|
||||
if ((currentToken != MailcapTokenizer.SLASH_TOKEN) &&
|
||||
(currentToken != MailcapTokenizer.SEMICOLON_TOKEN)) {
|
||||
reportParseError(MailcapTokenizer.SLASH_TOKEN,
|
||||
MailcapTokenizer.SEMICOLON_TOKEN, currentToken,
|
||||
tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
|
||||
// only need to look for a sub type if we got a '/'
|
||||
if (currentToken == MailcapTokenizer.SLASH_TOKEN) {
|
||||
// parse the sub type
|
||||
currentToken = tokenizer.nextToken();
|
||||
if (currentToken != MailcapTokenizer.STRING_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.STRING_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
subType =
|
||||
tokenizer.getCurrentTokenValue().toLowerCase(Locale.ENGLISH);
|
||||
|
||||
// get the next token to simplify the next step
|
||||
currentToken = tokenizer.nextToken();
|
||||
}
|
||||
|
||||
String mimeType = primaryType + "/" + subType;
|
||||
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log(" Type: " + mimeType);
|
||||
|
||||
// now setup the commands hashtable
|
||||
Map commands = new LinkedHashMap(); // keep commands in order found
|
||||
|
||||
// parse the ';' that separates the type from the parameters
|
||||
if (currentToken != MailcapTokenizer.SEMICOLON_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.SEMICOLON_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
// eat it
|
||||
|
||||
// parse the required view command
|
||||
tokenizer.setIsAutoquoting(true);
|
||||
currentToken = tokenizer.nextToken();
|
||||
tokenizer.setIsAutoquoting(false);
|
||||
if ((currentToken != MailcapTokenizer.STRING_TOKEN) &&
|
||||
(currentToken != MailcapTokenizer.SEMICOLON_TOKEN)) {
|
||||
reportParseError(MailcapTokenizer.STRING_TOKEN,
|
||||
MailcapTokenizer.SEMICOLON_TOKEN, currentToken,
|
||||
tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
|
||||
if (currentToken == MailcapTokenizer.STRING_TOKEN) {
|
||||
// have a native comand, save the entire mailcap entry
|
||||
//String nativeCommand = tokenizer.getCurrentTokenValue();
|
||||
List v = (List)native_commands.get(mimeType);
|
||||
if (v == null) {
|
||||
v = new ArrayList();
|
||||
v.add(mailcapEntry);
|
||||
native_commands.put(mimeType, v);
|
||||
} else {
|
||||
// XXX - check for duplicates?
|
||||
v.add(mailcapEntry);
|
||||
}
|
||||
}
|
||||
|
||||
// only have to get the next token if the current one isn't a ';'
|
||||
if (currentToken != MailcapTokenizer.SEMICOLON_TOKEN) {
|
||||
currentToken = tokenizer.nextToken();
|
||||
}
|
||||
|
||||
// look for a ';' which will indicate whether
|
||||
// a parameter list is present or not
|
||||
if (currentToken == MailcapTokenizer.SEMICOLON_TOKEN) {
|
||||
boolean isFallback = false;
|
||||
do {
|
||||
// eat the ';'
|
||||
|
||||
// parse the parameter name
|
||||
currentToken = tokenizer.nextToken();
|
||||
if (currentToken != MailcapTokenizer.STRING_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.STRING_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
String paramName = tokenizer.getCurrentTokenValue().
|
||||
toLowerCase(Locale.ENGLISH);
|
||||
|
||||
// parse the '=' which separates the name from the value
|
||||
currentToken = tokenizer.nextToken();
|
||||
if ((currentToken != MailcapTokenizer.EQUALS_TOKEN) &&
|
||||
(currentToken != MailcapTokenizer.SEMICOLON_TOKEN) &&
|
||||
(currentToken != MailcapTokenizer.EOI_TOKEN)) {
|
||||
reportParseError(MailcapTokenizer.EQUALS_TOKEN,
|
||||
MailcapTokenizer.SEMICOLON_TOKEN,
|
||||
MailcapTokenizer.EOI_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
|
||||
// we only have a useful command if it is named
|
||||
if (currentToken == MailcapTokenizer.EQUALS_TOKEN) {
|
||||
// eat it
|
||||
|
||||
// parse the parameter value (which is autoquoted)
|
||||
tokenizer.setIsAutoquoting(true);
|
||||
currentToken = tokenizer.nextToken();
|
||||
tokenizer.setIsAutoquoting(false);
|
||||
if (currentToken != MailcapTokenizer.STRING_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.STRING_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
String paramValue =
|
||||
tokenizer.getCurrentTokenValue();
|
||||
|
||||
// add the class to the list iff it is one we care about
|
||||
if (paramName.startsWith("x-java-")) {
|
||||
String commandName = paramName.substring(7);
|
||||
// 7 == "x-java-".length
|
||||
|
||||
if (commandName.equals("fallback-entry") &&
|
||||
paramValue.equalsIgnoreCase("true")) {
|
||||
isFallback = true;
|
||||
} else {
|
||||
|
||||
// setup the class entry list
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log(" Command: " + commandName +
|
||||
", Class: " + paramValue);
|
||||
List classes = (List)commands.get(commandName);
|
||||
if (classes == null) {
|
||||
classes = new ArrayList();
|
||||
commands.put(commandName, classes);
|
||||
}
|
||||
if (addReverse)
|
||||
classes.add(0, paramValue);
|
||||
else
|
||||
classes.add(paramValue);
|
||||
}
|
||||
}
|
||||
|
||||
// set up the next iteration
|
||||
currentToken = tokenizer.nextToken();
|
||||
}
|
||||
} while (currentToken == MailcapTokenizer.SEMICOLON_TOKEN);
|
||||
|
||||
Map masterHash = isFallback ? fallback_hash : type_hash;
|
||||
Map curcommands =
|
||||
(Map)masterHash.get(mimeType);
|
||||
if (curcommands == null) {
|
||||
masterHash.put(mimeType, commands);
|
||||
} else {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("Merging commands for type " + mimeType);
|
||||
// have to merge current and new commands
|
||||
// first, merge list of classes for commands already known
|
||||
Iterator cn = curcommands.keySet().iterator();
|
||||
while (cn.hasNext()) {
|
||||
String cmdName = (String)cn.next();
|
||||
List ccv = (List)curcommands.get(cmdName);
|
||||
List cv = (List)commands.get(cmdName);
|
||||
if (cv == null)
|
||||
continue;
|
||||
// add everything in cv to ccv, if it's not already there
|
||||
Iterator cvn = cv.iterator();
|
||||
while (cvn.hasNext()) {
|
||||
String clazz = (String)cvn.next();
|
||||
if (!ccv.contains(clazz))
|
||||
if (addReverse)
|
||||
ccv.add(0, clazz);
|
||||
else
|
||||
ccv.add(clazz);
|
||||
}
|
||||
}
|
||||
// now, add commands not previously known
|
||||
cn = commands.keySet().iterator();
|
||||
while (cn.hasNext()) {
|
||||
String cmdName = (String)cn.next();
|
||||
if (curcommands.containsKey(cmdName))
|
||||
continue;
|
||||
List cv = (List)commands.get(cmdName);
|
||||
curcommands.put(cmdName, cv);
|
||||
}
|
||||
}
|
||||
} else if (currentToken != MailcapTokenizer.EOI_TOKEN) {
|
||||
reportParseError(MailcapTokenizer.EOI_TOKEN,
|
||||
MailcapTokenizer.SEMICOLON_TOKEN,
|
||||
currentToken, tokenizer.getCurrentTokenValue());
|
||||
}
|
||||
}
|
||||
|
||||
protected static void reportParseError(int expectedToken, int actualToken,
|
||||
String actualTokenValue) throws MailcapParseException {
|
||||
throw new MailcapParseException("Encountered a " +
|
||||
MailcapTokenizer.nameForToken(actualToken) + " token (" +
|
||||
actualTokenValue + ") while expecting a " +
|
||||
MailcapTokenizer.nameForToken(expectedToken) + " token.");
|
||||
}
|
||||
|
||||
protected static void reportParseError(int expectedToken,
|
||||
int otherExpectedToken, int actualToken, String actualTokenValue)
|
||||
throws MailcapParseException {
|
||||
throw new MailcapParseException("Encountered a " +
|
||||
MailcapTokenizer.nameForToken(actualToken) + " token (" +
|
||||
actualTokenValue + ") while expecting a " +
|
||||
MailcapTokenizer.nameForToken(expectedToken) + " or a " +
|
||||
MailcapTokenizer.nameForToken(otherExpectedToken) + " token.");
|
||||
}
|
||||
|
||||
protected static void reportParseError(int expectedToken,
|
||||
int otherExpectedToken, int anotherExpectedToken, int actualToken,
|
||||
String actualTokenValue) throws MailcapParseException {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("PARSE ERROR: " + "Encountered a " +
|
||||
MailcapTokenizer.nameForToken(actualToken) + " token (" +
|
||||
actualTokenValue + ") while expecting a " +
|
||||
MailcapTokenizer.nameForToken(expectedToken) + ", a " +
|
||||
MailcapTokenizer.nameForToken(otherExpectedToken) + ", or a " +
|
||||
MailcapTokenizer.nameForToken(anotherExpectedToken) + " token.");
|
||||
throw new MailcapParseException("Encountered a " +
|
||||
MailcapTokenizer.nameForToken(actualToken) + " token (" +
|
||||
actualTokenValue + ") while expecting a " +
|
||||
MailcapTokenizer.nameForToken(expectedToken) + ", a " +
|
||||
MailcapTokenizer.nameForToken(otherExpectedToken) + ", or a " +
|
||||
MailcapTokenizer.nameForToken(anotherExpectedToken) + " token.");
|
||||
}
|
||||
|
||||
/** for debugging
|
||||
public static void main(String[] args) throws Exception {
|
||||
Map masterHash = new HashMap();
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
System.out.println("Entry " + i + ": " + args[i]);
|
||||
parseLine(args[i], masterHash);
|
||||
}
|
||||
|
||||
Enumeration types = masterHash.keys();
|
||||
while (types.hasMoreElements()) {
|
||||
String key = (String)types.nextElement();
|
||||
System.out.println("MIME Type: " + key);
|
||||
|
||||
Map commandHash = (Map)masterHash.get(key);
|
||||
Enumeration commands = commandHash.keys();
|
||||
while (commands.hasMoreElements()) {
|
||||
String command = (String)commands.nextElement();
|
||||
System.out.println(" Command: " + command);
|
||||
|
||||
Vector classes = (Vector)commandHash.get(command);
|
||||
for (int i = 0; i < classes.size(); ++i) {
|
||||
System.out.println(" Class: " +
|
||||
(String)classes.elementAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
/**
|
||||
* A class to encapsulate Mailcap parsing related exceptions
|
||||
*/
|
||||
public class MailcapParseException extends Exception {
|
||||
|
||||
public MailcapParseException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MailcapParseException(String inInfo) {
|
||||
super(inInfo);
|
||||
}
|
||||
}
|
321
jdkSrc/jdk8/com/sun/activation/registries/MailcapTokenizer.java
Normal file
321
jdkSrc/jdk8/com/sun/activation/registries/MailcapTokenizer.java
Normal file
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
/**
|
||||
* A tokenizer for strings in the form of "foo/bar; prop1=val1; ... ".
|
||||
* Useful for parsing MIME content types.
|
||||
*/
|
||||
public class MailcapTokenizer {
|
||||
|
||||
public static final int UNKNOWN_TOKEN = 0;
|
||||
public static final int START_TOKEN = 1;
|
||||
public static final int STRING_TOKEN = 2;
|
||||
public static final int EOI_TOKEN = 5;
|
||||
public static final int SLASH_TOKEN = '/';
|
||||
public static final int SEMICOLON_TOKEN = ';';
|
||||
public static final int EQUALS_TOKEN = '=';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @parameter inputString the string to tokenize
|
||||
*/
|
||||
public MailcapTokenizer(String inputString) {
|
||||
data = inputString;
|
||||
dataIndex = 0;
|
||||
dataLength = inputString.length();
|
||||
|
||||
currentToken = START_TOKEN;
|
||||
currentTokenValue = "";
|
||||
|
||||
isAutoquoting = false;
|
||||
autoquoteChar = ';';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether auto-quoting is on or off.
|
||||
*
|
||||
* Auto-quoting means that all characters after the first
|
||||
* non-whitespace, non-control character up to the auto-quote
|
||||
* terminator character or EOI (minus any whitespace immediatley
|
||||
* preceeding it) is considered a token.
|
||||
*
|
||||
* This is required for handling command strings in a mailcap entry.
|
||||
*/
|
||||
public void setIsAutoquoting(boolean value) {
|
||||
isAutoquoting = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve current token.
|
||||
*
|
||||
* @returns The current token value
|
||||
*/
|
||||
public int getCurrentToken() {
|
||||
return currentToken;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a String that describes the given token.
|
||||
*/
|
||||
public static String nameForToken(int token) {
|
||||
String name = "really unknown";
|
||||
|
||||
switch(token) {
|
||||
case UNKNOWN_TOKEN:
|
||||
name = "unknown";
|
||||
break;
|
||||
case START_TOKEN:
|
||||
name = "start";
|
||||
break;
|
||||
case STRING_TOKEN:
|
||||
name = "string";
|
||||
break;
|
||||
case EOI_TOKEN:
|
||||
name = "EOI";
|
||||
break;
|
||||
case SLASH_TOKEN:
|
||||
name = "'/'";
|
||||
break;
|
||||
case SEMICOLON_TOKEN:
|
||||
name = "';'";
|
||||
break;
|
||||
case EQUALS_TOKEN:
|
||||
name = "'='";
|
||||
break;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve current token value.
|
||||
*
|
||||
* @returns A String containing the current token value
|
||||
*/
|
||||
public String getCurrentTokenValue() {
|
||||
return currentTokenValue;
|
||||
}
|
||||
/*
|
||||
* Process the next token.
|
||||
*
|
||||
* @returns the next token
|
||||
*/
|
||||
public int nextToken() {
|
||||
if (dataIndex < dataLength) {
|
||||
// skip white space
|
||||
while ((dataIndex < dataLength) &&
|
||||
(isWhiteSpaceChar(data.charAt(dataIndex)))) {
|
||||
++dataIndex;
|
||||
}
|
||||
|
||||
if (dataIndex < dataLength) {
|
||||
// examine the current character and see what kind of token we have
|
||||
char c = data.charAt(dataIndex);
|
||||
if (isAutoquoting) {
|
||||
if (c == ';' || c == '=') {
|
||||
currentToken = c;
|
||||
currentTokenValue = new Character(c).toString();
|
||||
++dataIndex;
|
||||
} else {
|
||||
processAutoquoteToken();
|
||||
}
|
||||
} else {
|
||||
if (isStringTokenChar(c)) {
|
||||
processStringToken();
|
||||
} else if ((c == '/') || (c == ';') || (c == '=')) {
|
||||
currentToken = c;
|
||||
currentTokenValue = new Character(c).toString();
|
||||
++dataIndex;
|
||||
} else {
|
||||
currentToken = UNKNOWN_TOKEN;
|
||||
currentTokenValue = new Character(c).toString();
|
||||
++dataIndex;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
currentToken = EOI_TOKEN;
|
||||
currentTokenValue = null;
|
||||
}
|
||||
} else {
|
||||
currentToken = EOI_TOKEN;
|
||||
currentTokenValue = null;
|
||||
}
|
||||
|
||||
return currentToken;
|
||||
}
|
||||
|
||||
private void processStringToken() {
|
||||
// capture the initial index
|
||||
int initialIndex = dataIndex;
|
||||
|
||||
// skip to 1st non string token character
|
||||
while ((dataIndex < dataLength) &&
|
||||
isStringTokenChar(data.charAt(dataIndex))) {
|
||||
++dataIndex;
|
||||
}
|
||||
|
||||
currentToken = STRING_TOKEN;
|
||||
currentTokenValue = data.substring(initialIndex, dataIndex);
|
||||
}
|
||||
|
||||
private void processAutoquoteToken() {
|
||||
// capture the initial index
|
||||
int initialIndex = dataIndex;
|
||||
|
||||
// now skip to the 1st non-escaped autoquote termination character
|
||||
// XXX - doesn't actually consider escaping
|
||||
boolean foundTerminator = false;
|
||||
while ((dataIndex < dataLength) && !foundTerminator) {
|
||||
char c = data.charAt(dataIndex);
|
||||
if (c != autoquoteChar) {
|
||||
++dataIndex;
|
||||
} else {
|
||||
foundTerminator = true;
|
||||
}
|
||||
}
|
||||
|
||||
currentToken = STRING_TOKEN;
|
||||
currentTokenValue =
|
||||
fixEscapeSequences(data.substring(initialIndex, dataIndex));
|
||||
}
|
||||
|
||||
private static boolean isSpecialChar(char c) {
|
||||
boolean lAnswer = false;
|
||||
|
||||
switch(c) {
|
||||
case '(':
|
||||
case ')':
|
||||
case '<':
|
||||
case '>':
|
||||
case '@':
|
||||
case ',':
|
||||
case ';':
|
||||
case ':':
|
||||
case '\\':
|
||||
case '"':
|
||||
case '/':
|
||||
case '[':
|
||||
case ']':
|
||||
case '?':
|
||||
case '=':
|
||||
lAnswer = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return lAnswer;
|
||||
}
|
||||
|
||||
private static boolean isControlChar(char c) {
|
||||
return Character.isISOControl(c);
|
||||
}
|
||||
|
||||
private static boolean isWhiteSpaceChar(char c) {
|
||||
return Character.isWhitespace(c);
|
||||
}
|
||||
|
||||
private static boolean isStringTokenChar(char c) {
|
||||
return !isSpecialChar(c) && !isControlChar(c) && !isWhiteSpaceChar(c);
|
||||
}
|
||||
|
||||
private static String fixEscapeSequences(String inputString) {
|
||||
int inputLength = inputString.length();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.ensureCapacity(inputLength);
|
||||
|
||||
for (int i = 0; i < inputLength; ++i) {
|
||||
char currentChar = inputString.charAt(i);
|
||||
if (currentChar != '\\') {
|
||||
buffer.append(currentChar);
|
||||
} else {
|
||||
if (i < inputLength - 1) {
|
||||
char nextChar = inputString.charAt(i + 1);
|
||||
buffer.append(nextChar);
|
||||
|
||||
// force a skip over the next character too
|
||||
++i;
|
||||
} else {
|
||||
buffer.append(currentChar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private String data;
|
||||
private int dataIndex;
|
||||
private int dataLength;
|
||||
private int currentToken;
|
||||
private String currentTokenValue;
|
||||
private boolean isAutoquoting;
|
||||
private char autoquoteChar;
|
||||
|
||||
/*
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
MailcapTokenizer tokenizer = new MailcapTokenizer(args[i]);
|
||||
|
||||
System.out.println("Original: |" + args[i] + "|");
|
||||
|
||||
int currentToken = tokenizer.nextToken();
|
||||
while (currentToken != EOI_TOKEN) {
|
||||
switch(currentToken) {
|
||||
case UNKNOWN_TOKEN:
|
||||
System.out.println(" Unknown Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case START_TOKEN:
|
||||
System.out.println(" Start Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case STRING_TOKEN:
|
||||
System.out.println(" String Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case EOI_TOKEN:
|
||||
System.out.println(" EOI Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case SLASH_TOKEN:
|
||||
System.out.println(" Slash Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case SEMICOLON_TOKEN:
|
||||
System.out.println(" Semicolon Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
case EQUALS_TOKEN:
|
||||
System.out.println(" Equals Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
default:
|
||||
System.out.println(" Really Unknown Token: |" + tokenizer.getCurrentTokenValue() + "|");
|
||||
break;
|
||||
}
|
||||
|
||||
currentToken = tokenizer.nextToken();
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
50
jdkSrc/jdk8/com/sun/activation/registries/MimeTypeEntry.java
Normal file
50
jdkSrc/jdk8/com/sun/activation/registries/MimeTypeEntry.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
import java.lang.*;
|
||||
|
||||
public class MimeTypeEntry {
|
||||
private String type;
|
||||
private String extension;
|
||||
|
||||
public MimeTypeEntry(String mime_type, String file_ext) {
|
||||
type = mime_type;
|
||||
extension = file_ext;
|
||||
}
|
||||
|
||||
public String getMIMEType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getFileExtension() {
|
||||
return extension;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "MIMETypeEntry: " + type + ", " + extension;
|
||||
}
|
||||
}
|
317
jdkSrc/jdk8/com/sun/activation/registries/MimeTypeFile.java
Normal file
317
jdkSrc/jdk8/com/sun/activation/registries/MimeTypeFile.java
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.activation.registries;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class MimeTypeFile {
|
||||
private String fname = null;
|
||||
private Hashtable type_hash = new Hashtable();
|
||||
|
||||
/**
|
||||
* The construtor that takes a filename as an argument.
|
||||
*
|
||||
* @param new_fname The file name of the mime types file.
|
||||
*/
|
||||
public MimeTypeFile(String new_fname) throws IOException {
|
||||
File mime_file = null;
|
||||
FileReader fr = null;
|
||||
|
||||
fname = new_fname; // remember the file name
|
||||
|
||||
mime_file = new File(fname); // get a file object
|
||||
|
||||
fr = new FileReader(mime_file);
|
||||
|
||||
try {
|
||||
parse(new BufferedReader(fr));
|
||||
} finally {
|
||||
try {
|
||||
fr.close(); // close it
|
||||
} catch (IOException e) {
|
||||
// ignore it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MimeTypeFile(InputStream is) throws IOException {
|
||||
parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty DB.
|
||||
*/
|
||||
public MimeTypeFile() {
|
||||
}
|
||||
|
||||
/**
|
||||
* get the MimeTypeEntry based on the file extension
|
||||
*/
|
||||
public MimeTypeEntry getMimeTypeEntry(String file_ext) {
|
||||
return (MimeTypeEntry)type_hash.get((Object)file_ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the MIME type string corresponding to the file extension.
|
||||
*/
|
||||
public String getMIMETypeString(String file_ext) {
|
||||
MimeTypeEntry entry = this.getMimeTypeEntry(file_ext);
|
||||
|
||||
if (entry != null)
|
||||
return entry.getMIMEType();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends string of entries to the types registry, must be valid
|
||||
* .mime.types format.
|
||||
* A mime.types entry is one of two forms:
|
||||
*
|
||||
* type/subtype ext1 ext2 ...
|
||||
* or
|
||||
* type=type/subtype desc="description of type" exts=ext1,ext2,...
|
||||
*
|
||||
* Example:
|
||||
* # this is a test
|
||||
* audio/basic au
|
||||
* text/plain txt text
|
||||
* type=application/postscript exts=ps,eps
|
||||
*/
|
||||
public void appendToRegistry(String mime_types) {
|
||||
try {
|
||||
parse(new BufferedReader(new StringReader(mime_types)));
|
||||
} catch (IOException ex) {
|
||||
// can't happen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a stream of mime.types entries.
|
||||
*/
|
||||
private void parse(BufferedReader buf_reader) throws IOException {
|
||||
String line = null, prev = null;
|
||||
|
||||
while ((line = buf_reader.readLine()) != null) {
|
||||
if (prev == null)
|
||||
prev = line;
|
||||
else
|
||||
prev += line;
|
||||
int end = prev.length();
|
||||
if (prev.length() > 0 && prev.charAt(end - 1) == '\\') {
|
||||
prev = prev.substring(0, end - 1);
|
||||
continue;
|
||||
}
|
||||
this.parseEntry(prev);
|
||||
prev = null;
|
||||
}
|
||||
if (prev != null)
|
||||
this.parseEntry(prev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse single mime.types entry.
|
||||
*/
|
||||
private void parseEntry(String line) {
|
||||
String mime_type = null;
|
||||
String file_ext = null;
|
||||
line = line.trim();
|
||||
|
||||
if (line.length() == 0) // empty line...
|
||||
return; // BAIL!
|
||||
|
||||
// check to see if this is a comment line?
|
||||
if (line.charAt(0) == '#')
|
||||
return; // then we are done!
|
||||
|
||||
// is it a new format line or old format?
|
||||
if (line.indexOf('=') > 0) {
|
||||
// new format
|
||||
LineTokenizer lt = new LineTokenizer(line);
|
||||
while (lt.hasMoreTokens()) {
|
||||
String name = lt.nextToken();
|
||||
String value = null;
|
||||
if (lt.hasMoreTokens() && lt.nextToken().equals("=") &&
|
||||
lt.hasMoreTokens())
|
||||
value = lt.nextToken();
|
||||
if (value == null) {
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("Bad .mime.types entry: " + line);
|
||||
return;
|
||||
}
|
||||
if (name.equals("type"))
|
||||
mime_type = value;
|
||||
else if (name.equals("exts")) {
|
||||
StringTokenizer st = new StringTokenizer(value, ",");
|
||||
while (st.hasMoreTokens()) {
|
||||
file_ext = st.nextToken();
|
||||
MimeTypeEntry entry =
|
||||
new MimeTypeEntry(mime_type, file_ext);
|
||||
type_hash.put(file_ext, entry);
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("Added: " + entry.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// old format
|
||||
// count the tokens
|
||||
StringTokenizer strtok = new StringTokenizer(line);
|
||||
int num_tok = strtok.countTokens();
|
||||
|
||||
if (num_tok == 0) // empty line
|
||||
return;
|
||||
|
||||
mime_type = strtok.nextToken(); // get the MIME type
|
||||
|
||||
while (strtok.hasMoreTokens()) {
|
||||
MimeTypeEntry entry = null;
|
||||
|
||||
file_ext = strtok.nextToken();
|
||||
entry = new MimeTypeEntry(mime_type, file_ext);
|
||||
type_hash.put(file_ext, entry);
|
||||
if (LogSupport.isLoggable())
|
||||
LogSupport.log("Added: " + entry.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for debugging
|
||||
/*
|
||||
public static void main(String[] argv) throws Exception {
|
||||
MimeTypeFile mf = new MimeTypeFile(argv[0]);
|
||||
System.out.println("ext " + argv[1] + " type " +
|
||||
mf.getMIMETypeString(argv[1]));
|
||||
System.exit(0);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
class LineTokenizer {
|
||||
private int currentPosition;
|
||||
private int maxPosition;
|
||||
private String str;
|
||||
private Vector stack = new Vector();
|
||||
private static final String singles = "="; // single character tokens
|
||||
|
||||
/**
|
||||
* Constructs a tokenizer for the specified string.
|
||||
* <p>
|
||||
*
|
||||
* @param str a string to be parsed.
|
||||
*/
|
||||
public LineTokenizer(String str) {
|
||||
currentPosition = 0;
|
||||
this.str = str;
|
||||
maxPosition = str.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips white space.
|
||||
*/
|
||||
private void skipWhiteSpace() {
|
||||
while ((currentPosition < maxPosition) &&
|
||||
Character.isWhitespace(str.charAt(currentPosition))) {
|
||||
currentPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if there are more tokens available from this tokenizer's string.
|
||||
*
|
||||
* @return <code>true</code> if there are more tokens available from this
|
||||
* tokenizer's string; <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean hasMoreTokens() {
|
||||
if (stack.size() > 0)
|
||||
return true;
|
||||
skipWhiteSpace();
|
||||
return (currentPosition < maxPosition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next token from this tokenizer.
|
||||
*
|
||||
* @return the next token from this tokenizer.
|
||||
* @exception NoSuchElementException if there are no more tokens in this
|
||||
* tokenizer's string.
|
||||
*/
|
||||
public String nextToken() {
|
||||
int size = stack.size();
|
||||
if (size > 0) {
|
||||
String t = (String)stack.elementAt(size - 1);
|
||||
stack.removeElementAt(size - 1);
|
||||
return t;
|
||||
}
|
||||
skipWhiteSpace();
|
||||
|
||||
if (currentPosition >= maxPosition) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
int start = currentPosition;
|
||||
char c = str.charAt(start);
|
||||
if (c == '"') {
|
||||
currentPosition++;
|
||||
boolean filter = false;
|
||||
while (currentPosition < maxPosition) {
|
||||
c = str.charAt(currentPosition++);
|
||||
if (c == '\\') {
|
||||
currentPosition++;
|
||||
filter = true;
|
||||
} else if (c == '"') {
|
||||
String s;
|
||||
|
||||
if (filter) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = start + 1; i < currentPosition - 1; i++) {
|
||||
c = str.charAt(i);
|
||||
if (c != '\\')
|
||||
sb.append(c);
|
||||
}
|
||||
s = sb.toString();
|
||||
} else
|
||||
s = str.substring(start + 1, currentPosition - 1);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
} else if (singles.indexOf(c) >= 0) {
|
||||
currentPosition++;
|
||||
} else {
|
||||
while ((currentPosition < maxPosition) &&
|
||||
singles.indexOf(str.charAt(currentPosition)) < 0 &&
|
||||
!Character.isWhitespace(str.charAt(currentPosition))) {
|
||||
currentPosition++;
|
||||
}
|
||||
}
|
||||
return str.substring(start, currentPosition);
|
||||
}
|
||||
|
||||
public void pushToken(String token) {
|
||||
stack.addElement(token);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user