/* * Copyright (c) 2000, 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 javax.print; import java.io.Serializable; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.Vector; /** * Class MimeType encapsulates a Multipurpose Internet Mail Extensions (MIME) * media type as defined in RFC * 2045 and RFC 2046. A * MIME type object is part of a {@link DocFlavor DocFlavor} object and * specifies the format of the print data. *
* Class MimeType is similar to the like-named * class in package {@link java.awt.datatransfer java.awt.datatransfer}. Class * java.awt.datatransfer.MimeType is not used in the Jini Print Service API * for two reasons: *
*
* Class MimeType's serialized representation is based on the following * canonical form of a MIME type string. Thus, two MIME types that are not * identical but that are equivalent (that have the same canonical form) will * be considered equal by the JLUS's matching algorithm. *
*
* @author Alan Kaminsky
*/
class MimeType implements Serializable, Cloneable {
private static final long serialVersionUID = -2785720609362367683L;
/**
* Array of strings that hold pieces of this MIME type's canonical form.
* If the MIME type has n parameters, n >= 0, then the
* strings in the array are:
*
* Special rules applied:
*
Index 0 -- Media type.
*
Index 1 -- Media subtype.
*
Index 2i+2 -- Name of parameter i,
* i=0,1,...,n-1.
*
Index 2i+3 -- Value of parameter i,
* i=0,1,...,n-1.
*
Parameters are arranged in ascending order of parameter name.
* @serial
*/
private String[] myPieces;
/**
* String value for this MIME type. Computed when needed and cached.
*/
private transient String myStringValue = null;
/**
* Parameter map entry set. Computed when needed and cached.
*/
private transient ParameterMapEntrySet myEntrySet = null;
/**
* Parameter map. Computed when needed and cached.
*/
private transient ParameterMap myParameterMap = null;
/**
* Parameter map entry.
*/
private class ParameterMapEntry implements Map.Entrys
is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if s
does not obey the
* syntax for a MIME media type string.
*/
public MimeType(String s) {
parse (s);
}
/**
* Returns this MIME type object's MIME type string based on the canonical
* form. Each parameter value is enclosed in quotes.
*/
public String getMimeType() {
return getStringValue();
}
/**
* Returns this MIME type object's media type.
*/
public String getMediaType() {
return myPieces[0];
}
/**
* Returns this MIME type object's media subtype.
*/
public String getMediaSubtype() {
return myPieces[1];
}
/**
* Returns an unmodifiable map view of the parameters in this MIME type
* object. Each entry in the parameter map view consists of a parameter
* name String (key) mapping to a parameter value String. If this MIME
* type object has no parameters, an empty map is returned.
*
* @return Parameter map for this MIME type object.
*/
public Mapobj
, false
* otherwise.
*/
public boolean equals (Object obj) {
return(obj != null &&
obj instanceof MimeType &&
getStringValue().equals(((MimeType) obj).getStringValue()));
}
/**
* Returns this MIME type's string value in canonical form.
*/
private String getStringValue() {
if (myStringValue == null) {
StringBuffer result = new StringBuffer();
result.append (myPieces[0]);
result.append ('/');
result.append (myPieces[1]);
int n = myPieces.length;
for (int i = 2; i < n; i += 2) {
result.append(';');
result.append(' ');
result.append(myPieces[i]);
result.append('=');
result.append(addQuotes (myPieces[i+1]));
}
myStringValue = result.toString();
}
return myStringValue;
}
// Hidden classes, constants, and operations for parsing a MIME media type
// string.
// Lexeme types.
private static final int TOKEN_LEXEME = 0;
private static final int QUOTED_STRING_LEXEME = 1;
private static final int TSPECIAL_LEXEME = 2;
private static final int EOF_LEXEME = 3;
private static final int ILLEGAL_LEXEME = 4;
// Class for a lexical analyzer.
private static class LexicalAnalyzer {
protected String mySource;
protected int mySourceLength;
protected int myCurrentIndex;
protected int myLexemeType;
protected int myLexemeBeginIndex;
protected int myLexemeEndIndex;
public LexicalAnalyzer(String theSource) {
mySource = theSource;
mySourceLength = theSource.length();
myCurrentIndex = 0;
nextLexeme();
}
public int getLexemeType() {
return myLexemeType;
}
public String getLexeme() {
return(myLexemeBeginIndex >= mySourceLength ?
null :
mySource.substring(myLexemeBeginIndex, myLexemeEndIndex));
}
public char getLexemeFirstCharacter() {
return(myLexemeBeginIndex >= mySourceLength ?
'\u0000' :
mySource.charAt(myLexemeBeginIndex));
}
public void nextLexeme() {
int state = 0;
int commentLevel = 0;
char c;
while (state >= 0) {
switch (state) {
// Looking for a token, quoted string, or tspecial
case 0:
if (myCurrentIndex >= mySourceLength) {
myLexemeType = EOF_LEXEME;
myLexemeBeginIndex = mySourceLength;
myLexemeEndIndex = mySourceLength;
state = -1;
} else if (Character.isWhitespace
(c = mySource.charAt (myCurrentIndex ++))) {
state = 0;
} else if (c == '\"') {
myLexemeType = QUOTED_STRING_LEXEME;
myLexemeBeginIndex = myCurrentIndex;
state = 1;
} else if (c == '(') {
++ commentLevel;
state = 3;
} else if (c == '/' || c == ';' || c == '=' ||
c == ')' || c == '<' || c == '>' ||
c == '@' || c == ',' || c == ':' ||
c == '\\' || c == '[' || c == ']' ||
c == '?') {
myLexemeType = TSPECIAL_LEXEME;
myLexemeBeginIndex = myCurrentIndex - 1;
myLexemeEndIndex = myCurrentIndex;
state = -1;
} else {
myLexemeType = TOKEN_LEXEME;
myLexemeBeginIndex = myCurrentIndex - 1;
state = 5;
}
break;
// In a quoted string
case 1:
if (myCurrentIndex >= mySourceLength) {
myLexemeType = ILLEGAL_LEXEME;
myLexemeBeginIndex = mySourceLength;
myLexemeEndIndex = mySourceLength;
state = -1;
} else if ((c = mySource.charAt (myCurrentIndex ++)) == '\"') {
myLexemeEndIndex = myCurrentIndex - 1;
state = -1;
} else if (c == '\\') {
state = 2;
} else {
state = 1;
}
break;
// In a quoted string, backslash seen
case 2:
if (myCurrentIndex >= mySourceLength) {
myLexemeType = ILLEGAL_LEXEME;
myLexemeBeginIndex = mySourceLength;
myLexemeEndIndex = mySourceLength;
state = -1;
} else {
++ myCurrentIndex;
state = 1;
} break;
// In a comment
case 3: if (myCurrentIndex >= mySourceLength) {
myLexemeType = ILLEGAL_LEXEME;
myLexemeBeginIndex = mySourceLength;
myLexemeEndIndex = mySourceLength;
state = -1;
} else if ((c = mySource.charAt (myCurrentIndex ++)) == '(') {
++ commentLevel;
state = 3;
} else if (c == ')') {
-- commentLevel;
state = commentLevel == 0 ? 0 : 3;
} else if (c == '\\') {
state = 4;
} else { state = 3;
}
break;
// In a comment, backslash seen
case 4:
if (myCurrentIndex >= mySourceLength) {
myLexemeType = ILLEGAL_LEXEME;
myLexemeBeginIndex = mySourceLength;
myLexemeEndIndex = mySourceLength;
state = -1;
} else {
++ myCurrentIndex;
state = 3;
}
break;
// In a token
case 5:
if (myCurrentIndex >= mySourceLength) {
myLexemeEndIndex = myCurrentIndex;
state = -1;
} else if (Character.isWhitespace
(c = mySource.charAt (myCurrentIndex ++))) {
myLexemeEndIndex = myCurrentIndex - 1;
state = -1;
} else if (c == '\"' || c == '(' || c == '/' ||
c == ';' || c == '=' || c == ')' ||
c == '<' || c == '>' || c == '@' ||
c == ',' || c == ':' || c == '\\' ||
c == '[' || c == ']' || c == '?') {
-- myCurrentIndex;
myLexemeEndIndex = myCurrentIndex;
state = -1;
} else {
state = 5;
}
break;
}
}
}
}
/**
* Returns a lowercase version of the given string. The lowercase version
* is constructed by applying Character.toLowerCase() to each character of
* the given string, which maps characters to lowercase using the rules of
* Unicode. This mapping is the same regardless of locale, whereas the
* mapping of String.toLowerCase() may be different depending on the
* default locale.
*/
private static String toUnicodeLowerCase(String s) {
int n = s.length();
char[] result = new char [n];
for (int i = 0; i < n; ++ i) {
result[i] = Character.toLowerCase (s.charAt (i));
}
return new String (result);
}
/**
* Returns a version of the given string with backslashes removed.
*/
private static String removeBackslashes(String s) {
int n = s.length();
char[] result = new char [n];
int i;
int j = 0;
char c;
for (i = 0; i < n; ++ i) {
c = s.charAt (i);
if (c == '\\') {
c = s.charAt (++ i);
}
result[j++] = c;
}
return new String (result, 0, j);
}
/**
* Returns a version of the string surrounded by quotes and with interior
* quotes preceded by a backslash.
*/
private static String addQuotes(String s) {
int n = s.length();
int i;
char c;
StringBuffer result = new StringBuffer (n+2);
result.append ('\"');
for (i = 0; i < n; ++ i) {
c = s.charAt (i);
if (c == '\"') {
result.append ('\\');
}
result.append (c);
}
result.append ('\"');
return result.toString();
}
/**
* Parses the given string into canonical pieces and stores the pieces in
* {@link #myPieces myPieces
}.
*
*
*
* @param s MIME media type string.
*
* @exception NullPointerException
* (unchecked exception) Thrown if s
is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if s
does not obey the
* syntax for a MIME media type string.
*/
private void parse(String s) {
// Initialize.
if (s == null) {
throw new NullPointerException();
}
LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
int theLexemeType;
Vector