Removing dependency of java.nio on javax.activation by copying impl of MimeType class
1.1 --- a/src/share/classes/sun/nio/fs/MimeType.java Mon Jun 22 12:49:51 2009 +0200
1.2 +++ b/src/share/classes/sun/nio/fs/MimeType.java Mon Jun 22 13:08:07 2009 +0200
1.3 @@ -25,6 +25,8 @@
1.4
1.5 package sun.nio.fs;
1.6
1.7 +import java.util.Locale;
1.8 +
1.9 /**
1.10 * Represents a MIME type for the purposes of validation and matching. For
1.11 * now this class is implemented using the javax.activation.MimeType class but
1.12 @@ -32,9 +34,9 @@
1.13 */
1.14
1.15 public class MimeType {
1.16 - private final javax.activation.MimeType type;
1.17 + private final Impl type;
1.18
1.19 - private MimeType(javax.activation.MimeType type) {
1.20 + private MimeType(Impl type) {
1.21 this.type = type;
1.22 }
1.23
1.24 @@ -45,18 +47,14 @@
1.25 * If the string is not a valid MIME type
1.26 */
1.27 public static MimeType parse(String type) {
1.28 - try {
1.29 - return new MimeType(new javax.activation.MimeType(type));
1.30 - } catch (javax.activation.MimeTypeParseException x) {
1.31 - throw new IllegalArgumentException(x);
1.32 - }
1.33 + return new MimeType(new Impl(type));
1.34 }
1.35
1.36 /**
1.37 * Returns {@code true} if this MIME type has parameters.
1.38 */
1.39 public boolean hasParameters() {
1.40 - return !type.getParameters().isEmpty();
1.41 + return type.hasParameters;
1.42 }
1.43
1.44 /**
1.45 @@ -66,8 +64,220 @@
1.46 public boolean match(String other) {
1.47 try {
1.48 return type.match(other);
1.49 - } catch (javax.activation.MimeTypeParseException x) {
1.50 + } catch (IllegalArgumentException x) {
1.51 return false;
1.52 }
1.53 }
1.54 +
1.55 + /** copy of javax.activation.MimeType */
1.56 + private static class Impl {
1.57 +
1.58 + private String primaryType;
1.59 + private String subType;
1.60 + private boolean hasParameters;
1.61 +
1.62 + /**
1.63 + * A string that holds all the special chars.
1.64 + */
1.65 + private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
1.66 +
1.67 + /**
1.68 + * Default constructor.
1.69 + */
1.70 + public Impl() {
1.71 + primaryType = "application";
1.72 + subType = "*";
1.73 + hasParameters = false;
1.74 + }
1.75 +
1.76 + /**
1.77 + * Constructor that builds a MimeType from a String.
1.78 + *
1.79 + * @param rawdata the MIME type string
1.80 + */
1.81 + public Impl(String rawdata) throws IllegalArgumentException {
1.82 + parse(rawdata);
1.83 + }
1.84 +
1.85 + /**
1.86 + * A routine for parsing the MIME type out of a String.
1.87 + */
1.88 + private void parse(String rawdata) throws IllegalArgumentException {
1.89 + int slashIndex = rawdata.indexOf('/');
1.90 + int semIndex = rawdata.indexOf(';');
1.91 + if ((slashIndex < 0) && (semIndex < 0)) {
1.92 + // neither character is present, so treat it
1.93 + // as an error
1.94 + throw new IllegalArgumentException("Unable to find a sub type.");
1.95 + } else if ((slashIndex < 0) && (semIndex >= 0)) {
1.96 + // we have a ';' (and therefore a parameter list),
1.97 + // but no '/' indicating a sub type is present
1.98 + throw new IllegalArgumentException("Unable to find a sub type.");
1.99 + } else if ((slashIndex >= 0) && (semIndex < 0)) {
1.100 + // we have a primary and sub type but no parameter list
1.101 + primaryType = rawdata.substring(0, slashIndex).trim().
1.102 + toLowerCase(Locale.ENGLISH);
1.103 + subType = rawdata.substring(slashIndex + 1).trim().
1.104 + toLowerCase(Locale.ENGLISH);
1.105 + hasParameters = false;
1.106 + } else if (slashIndex < semIndex) {
1.107 + // we have all three items in the proper sequence
1.108 + primaryType = rawdata.substring(0, slashIndex).trim().
1.109 + toLowerCase(Locale.ENGLISH);
1.110 + subType = rawdata.substring(slashIndex + 1, semIndex).trim().
1.111 + toLowerCase(Locale.ENGLISH);
1.112 + hasParameters = true;
1.113 + } else {
1.114 + // we have a ';' lexically before a '/' which means we
1.115 + // have a primary type and a parameter list but no sub type
1.116 + throw new IllegalArgumentException("Unable to find a sub type.");
1.117 + }
1.118 +
1.119 + // now validate the primary and sub types
1.120 +
1.121 + // check to see if primary is valid
1.122 + if (!isValidToken(primaryType))
1.123 + throw new IllegalArgumentException("Primary type is invalid.");
1.124 +
1.125 + // check to see if sub is valid
1.126 + if (!isValidToken(subType))
1.127 + throw new IllegalArgumentException("Sub type is invalid.");
1.128 + }
1.129 +
1.130 + /**
1.131 + * Retrieve the primary type of this object.
1.132 + *
1.133 + * @return the primary MIME type
1.134 + */
1.135 + public String getPrimaryType() {
1.136 + return primaryType;
1.137 + }
1.138 +
1.139 + /**
1.140 + * Set the primary type for this object to the given String.
1.141 + *
1.142 + * @param primary the primary MIME type
1.143 + * @exception IllegalArgumentException if the primary type
1.144 + * is not a valid token
1.145 + */
1.146 + public void setPrimaryType(String primary) throws IllegalArgumentException {
1.147 + // check to see if primary is valid
1.148 + if (!isValidToken(primaryType))
1.149 + throw new IllegalArgumentException("Primary type is invalid.");
1.150 + primaryType = primary.toLowerCase(Locale.ENGLISH);
1.151 + }
1.152 +
1.153 + /**
1.154 + * Retrieve the subtype of this object.
1.155 + *
1.156 + * @return the MIME subtype
1.157 + */
1.158 + public String getSubType() {
1.159 + return subType;
1.160 + }
1.161 +
1.162 + /**
1.163 + * Set the subtype for this object to the given String.
1.164 + *
1.165 + * @param sub the MIME subtype
1.166 + * @exception IllegalArgumentException if the subtype
1.167 + * is not a valid token
1.168 + */
1.169 + public void setSubType(String sub) throws IllegalArgumentException {
1.170 + // check to see if sub is valid
1.171 + if (!isValidToken(subType))
1.172 + throw new IllegalArgumentException("Sub type is invalid.");
1.173 + subType = sub.toLowerCase(Locale.ENGLISH);
1.174 + }
1.175 +
1.176 + /**
1.177 + * Return the String representation of this object.
1.178 + */
1.179 + @Override
1.180 + public String toString() {
1.181 + return getBaseType();
1.182 + }
1.183 +
1.184 + /**
1.185 + * Return a String representation of this object
1.186 + * without the parameter list.
1.187 + *
1.188 + * @return the MIME type and sub-type
1.189 + */
1.190 + public String getBaseType() {
1.191 + return primaryType + "/" + subType;
1.192 + }
1.193 +
1.194 + /**
1.195 + * Determine if the primary and sub type of this object is
1.196 + * the same as what is in the given type.
1.197 + *
1.198 + * @param type the MimeType object to compare with
1.199 + * @return true if they match
1.200 + */
1.201 + public boolean match(Impl type) {
1.202 + return primaryType.equals(type.getPrimaryType())
1.203 + && (subType.equals("*")
1.204 + || type.getSubType().equals("*")
1.205 + || (subType.equals(type.getSubType())));
1.206 + }
1.207 +
1.208 + /**
1.209 + * Determine if the primary and sub type of this object is
1.210 + * the same as the content type described in rawdata.
1.211 + *
1.212 + * @param rawdata the MIME type string to compare with
1.213 + * @return true if they match
1.214 + */
1.215 + public boolean match(String rawdata) throws IllegalArgumentException {
1.216 + return match(new Impl(rawdata));
1.217 + }
1.218 +
1.219 + // below here be scary parsing related things
1.220 +
1.221 + /**
1.222 + * Determine whether or not a given character belongs to a legal token.
1.223 + */
1.224 + private static boolean isTokenChar(char c) {
1.225 + return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
1.226 + }
1.227 +
1.228 + /**
1.229 + * Determine whether or not a given string is a legal token.
1.230 + */
1.231 + private boolean isValidToken(String s) {
1.232 + int len = s.length();
1.233 + if (len > 0) {
1.234 + for (int i = 0; i < len; ++i) {
1.235 + char c = s.charAt(i);
1.236 + if (!isTokenChar(c)) {
1.237 + return false;
1.238 + }
1.239 + }
1.240 + return true;
1.241 + } else {
1.242 + return false;
1.243 + }
1.244 + }
1.245 +
1.246 + /**
1.247 + * A simple parser test,
1.248 + * for debugging...
1.249 + *
1.250 + public static void main(String[] args)
1.251 + throws IllegalArgumentException, IOException {
1.252 + for (int i = 0; i < args.length; ++i) {
1.253 + System.out.println("Original: " + args[i]);
1.254 +
1.255 + MimeType type = new MimeType(args[i]);
1.256 +
1.257 + System.out.println("Short: " + type.getBaseType());
1.258 + System.out.println("Parsed: " + type.toString());
1.259 + System.out.println();
1.260 + }
1.261 + }
1.262 + */
1.263 + }
1.264 +
1.265 +
1.266 }