jdk/src/share/classes/javax/print/MimeType.java
author prr
Wed, 18 Jun 2014 13:14:15 -0700
changeset 25144 e2bf17cee34b
parent 25093 8ec5ec01740e
parent 24969 afa6934dd8e8
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.AbstractMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.AbstractSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Class MimeType encapsulates a Multipurpose Internet Mail Extensions (MIME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * media type as defined in <A HREF="http://www.ietf.org/rfc/rfc2045.txt">RFC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 2045</A> and <A HREF="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</A>. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * MIME type object is part of a {@link DocFlavor DocFlavor} object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specifies the format of the print data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Class MimeType is similar to the like-named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * class in package {@link java.awt.datatransfer java.awt.datatransfer}. Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * java.awt.datatransfer.MimeType is not used in the Jini Print Service API
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * for two reasons:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <OL TYPE=1>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Since not all Java profiles include the AWT, the Jini Print Service should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * not depend on an AWT class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The implementation of class java.awt.datatransfer.MimeType does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * that equivalent MIME types will have the same serialized representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Thus, since the Jini Lookup Service (JLUS) matches service attributes based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * on equality of serialized representations, JLUS searches involving MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * types encapsulated in class java.awt.datatransfer.MimeType may incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * fail to match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Class MimeType's serialized representation is based on the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * canonical form of a MIME type string. Thus, two MIME types that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * identical but that are equivalent (that have the same canonical form) will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * be considered equal by the JLUS's matching algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <LI> The media type, media subtype, and parameters are retained, but all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      comments and whitespace characters are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <LI> The media type, media subtype, and parameter names are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <LI> The parameter values retain their original case, except a charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      parameter value for a text media type is converted to lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <LI> Quote characters surrounding parameter values are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <LI> Quoting backslash characters inside parameter values are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <LI> The parameters are arranged in ascending order of parameter name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author  Alan Kaminsky
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
class MimeType implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final long serialVersionUID = -2785720609362367683L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Array of strings that hold pieces of this MIME type's canonical form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * If the MIME type has <I>n</I> parameters, <I>n</I> &gt;= 0, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * strings in the array are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <BR>Index 0 -- Media type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <BR>Index 1 -- Media subtype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <BR>Index 2<I>i</I>+2 -- Name of parameter <I>i</I>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <I>i</I>=0,1,...,<I>n</I>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <BR>Index 2<I>i</I>+3 -- Value of parameter <I>i</I>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <I>i</I>=0,1,...,<I>n</I>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <BR>Parameters are arranged in ascending order of parameter name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private String[] myPieces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * String value for this MIME type. Computed when needed and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private transient String myStringValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Parameter map entry set. Computed when needed and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private transient ParameterMapEntrySet myEntrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Parameter map. Computed when needed and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private transient ParameterMap myParameterMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Parameter map entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   120
    private class ParameterMapEntry implements Map.Entry<String, String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        private int myIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public ParameterMapEntry(int theIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            myIndex = theIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   125
        public String getKey(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return myPieces[myIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   128
        public String getValue(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return myPieces[myIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   131
        public String setValue (String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return (o != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    o instanceof Map.Entry &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    getKey().equals (((Map.Entry) o).getKey()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    getValue().equals(((Map.Entry) o).getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return getKey().hashCode() ^ getValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Parameter map entry set iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   148
    private class ParameterMapEntrySetIterator implements Iterator<Map.Entry<String, String>> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private int myIndex = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return myIndex < myPieces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   153
        public Map.Entry<String, String> next() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                ParameterMapEntry result = new ParameterMapEntry (myIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                myIndex += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Parameter map entry set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   170
    private class ParameterMapEntrySet extends AbstractSet<Map.Entry<String, String>> {
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   171
        public Iterator<Map.Entry<String, String>> iterator() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return new ParameterMapEntrySetIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return (myPieces.length - 2) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Parameter map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   182
    private class ParameterMap extends AbstractMap<String, String> {
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   183
        public Set<Map.Entry<String, String>> entrySet() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (myEntrySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                myEntrySet = new ParameterMapEntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return myEntrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Construct a new MIME type object from the given string. The given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * string is converted into canonical form and stored internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param  s  MIME media type string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *     (unchecked exception) Thrown if <CODE>s</CODE> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *     (unchecked exception) Thrown if <CODE>s</CODE> does not obey the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *     syntax for a MIME media type string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public MimeType(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        parse (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns this MIME type object's MIME type string based on the canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * form. Each parameter value is enclosed in quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public String getMimeType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns this MIME type object's media type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public String getMediaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return myPieces[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns this MIME type object's media subtype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public String getMediaSubtype() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return myPieces[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Returns an unmodifiable map view of the parameters in this MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * object. Each entry in the parameter map view consists of a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * name String (key) mapping to a parameter value String. If this MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * type object has no parameters, an empty map is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return  Parameter map for this MIME type object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   237
    public Map<String, String> getParameterMap() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (myParameterMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            myParameterMap = new ParameterMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return myParameterMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Converts this MIME type object to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return  MIME type string based on the canonical form. Each parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *          value is enclosed in quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns a hash code for this MIME type object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return getStringValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Determine if this MIME type object is equal to the given object. The two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * are equal if the given object is not null, is an instance of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * net.jini.print.data.MimeType, and has the same canonical form as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * MIME type object (that is, has the same type, subtype, and parameters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Thus, if two MIME type objects are the same except for comments, they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * considered equal. However, "text/plain" and "text/plain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * charset=us-ascii" are not considered equal, even though they represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * the same media type (because the default character set for plain text is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * US-ASCII).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param  obj  Object to test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return  True if this MIME type object equals <CODE>obj</CODE>, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public boolean equals (Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return(obj != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
               obj instanceof MimeType &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
               getStringValue().equals(((MimeType) obj).getStringValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns this MIME type's string value in canonical form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private String getStringValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (myStringValue == null) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   288
            StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            result.append (myPieces[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            result.append ('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            result.append (myPieces[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            int n = myPieces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            for (int i = 2; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                result.append(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                result.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                result.append(myPieces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                result.append(addQuotes (myPieces[i+1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            myStringValue = result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return myStringValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
// Hidden classes, constants, and operations for parsing a MIME media type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
// string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Lexeme types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private static final int TOKEN_LEXEME         = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static final int QUOTED_STRING_LEXEME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private static final int TSPECIAL_LEXEME      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static final int EOF_LEXEME           = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static final int ILLEGAL_LEXEME       = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    // Class for a lexical analyzer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static class LexicalAnalyzer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        protected String mySource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        protected int mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        protected int myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        protected int myLexemeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        protected int myLexemeBeginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        protected int myLexemeEndIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public LexicalAnalyzer(String theSource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            mySource = theSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            mySourceLength = theSource.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            myCurrentIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public int getLexemeType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return myLexemeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public String getLexeme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return(myLexemeBeginIndex >= mySourceLength ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                   null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                   mySource.substring(myLexemeBeginIndex, myLexemeEndIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        public char getLexemeFirstCharacter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return(myLexemeBeginIndex >= mySourceLength ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                   '\u0000' :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                   mySource.charAt(myLexemeBeginIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public void nextLexeme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            int state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int commentLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            while (state >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // Looking for a token, quoted string, or tspecial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        myLexemeType = EOF_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        myLexemeBeginIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        myLexemeEndIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    } else if (Character.isWhitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                               (c = mySource.charAt (myCurrentIndex ++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    } else if (c == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        myLexemeType = QUOTED_STRING_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        myLexemeBeginIndex = myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    } else if (c == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        ++ commentLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    } else if (c == '/'  || c == ';' || c == '=' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                               c == ')'  || c == '<' || c == '>' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                               c == '@'  || c == ',' || c == ':' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                               c == '\\' || c == '[' || c == ']' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                               c == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        myLexemeType = TSPECIAL_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        myLexemeBeginIndex = myCurrentIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        myLexemeEndIndex = myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        myLexemeType = TOKEN_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        myLexemeBeginIndex = myCurrentIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        state = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    // In a quoted string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        myLexemeType = ILLEGAL_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        myLexemeBeginIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        myLexemeEndIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    } else if ((c = mySource.charAt (myCurrentIndex ++)) == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        myLexemeEndIndex = myCurrentIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    } else if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        state = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    // In a quoted string, backslash seen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        myLexemeType = ILLEGAL_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        myLexemeBeginIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        myLexemeEndIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        ++ myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    } break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    // In a comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                case 3: if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    myLexemeType = ILLEGAL_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    myLexemeBeginIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    myLexemeEndIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                } else if ((c = mySource.charAt (myCurrentIndex ++)) == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    ++ commentLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } else if (c == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    -- commentLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    state = commentLevel == 0 ? 0 : 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                } else if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    state = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                } else { state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                // In a comment, backslash seen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        myLexemeType = ILLEGAL_LEXEME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        myLexemeBeginIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        myLexemeEndIndex = mySourceLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        ++ myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    // In a token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    if (myCurrentIndex >= mySourceLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        myLexemeEndIndex = myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    } else if (Character.isWhitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                               (c = mySource.charAt (myCurrentIndex ++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        myLexemeEndIndex = myCurrentIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    } else if (c == '\"' || c == '(' || c == '/' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                               c == ';'  || c == '=' || c == ')' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                               c == '<' || c == '>'  || c == '@' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                               c == ',' || c == ':' || c == '\\' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                               c == '[' || c == ']' || c == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        -- myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        myLexemeEndIndex = myCurrentIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        state = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Returns a lowercase version of the given string. The lowercase version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * is constructed by applying Character.toLowerCase() to each character of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * the given string, which maps characters to lowercase using the rules of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Unicode. This mapping is the same regardless of locale, whereas the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * mapping of String.toLowerCase() may be different depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    private static String toUnicodeLowerCase(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        char[] result = new char [n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            result[i] = Character.toLowerCase (s.charAt (i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return new String (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Returns a version of the given string with backslashes removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private static String removeBackslashes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        char[] result = new char [n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        for (i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            c = s.charAt (i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                c = s.charAt (++ i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            result[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return new String (result, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns a version of the string surrounded by quotes and with interior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * quotes preceded by a backslash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private static String addQuotes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        char c;
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   513
        StringBuilder result = new StringBuilder (n+2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        result.append ('\"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        for (i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            c = s.charAt (i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (c == '\"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                result.append ('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            result.append (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        result.append ('\"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Parses the given string into canonical pieces and stores the pieces in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * {@link #myPieces <CODE>myPieces</CODE>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Special rules applied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <LI> If the media type is text, the value of a charset parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *      converted to lowercase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param  s  MIME media type string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @exception  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *     (unchecked exception) Thrown if <CODE>s</CODE> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *     (unchecked exception) Thrown if <CODE>s</CODE> does not obey the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *     syntax for a MIME media type string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private void parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // Initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        int theLexemeType;
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   551
        Vector<String> thePieces = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        boolean mediaTypeIsText = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        boolean parameterNameIsCharset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // Parse media type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (theLexer.getLexemeType() == TOKEN_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            String mt = toUnicodeLowerCase (theLexer.getLexeme());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            thePieces.add (mt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            mediaTypeIsText = mt.equals ("text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // Parse slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
              theLexer.getLexemeFirstCharacter() == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (theLexer.getLexemeType() == TOKEN_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            thePieces.add (toUnicodeLowerCase (theLexer.getLexeme()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // Parse zero or more parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        while (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
               theLexer.getLexemeFirstCharacter() == ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            // Parse semicolon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            // Parse parameter name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            if (theLexer.getLexemeType() == TOKEN_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                String pn = toUnicodeLowerCase (theLexer.getLexeme());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                thePieces.add (pn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                parameterNameIsCharset = pn.equals ("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            // Parse equals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                theLexer.getLexemeFirstCharacter() == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            // Parse parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (theLexer.getLexemeType() == TOKEN_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                String pv = theLexer.getLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                              toUnicodeLowerCase (pv) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                              pv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else if (theLexer.getLexemeType() == QUOTED_STRING_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                String pv = removeBackslashes (theLexer.getLexeme());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                              toUnicodeLowerCase (pv) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                              pv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                theLexer.nextLexeme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // Make sure we've consumed everything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (theLexer.getLexemeType() != EOF_LEXEME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // Save the pieces. Parameters are not in ascending order yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        int n = thePieces.size();
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   626
        myPieces = thePieces.toArray (new String [n]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // Sort the parameters into ascending order using an insertion sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        String temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        for (i = 4; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            j = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            while (j < i && myPieces[j].compareTo (myPieces[i]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                j += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            while (j < i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                temp = myPieces[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                myPieces[j] = myPieces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                myPieces[i] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                temp = myPieces[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                myPieces[j+1] = myPieces[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                myPieces[i+1] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                j += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
}