178
179 /**
180 * Parameter map.
181 */
182 private class ParameterMap extends AbstractMap<String, String> {
183 public Set<Map.Entry<String, String>> entrySet() {
184 if (myEntrySet == null) {
185 myEntrySet = new ParameterMapEntrySet();
186 }
187 return myEntrySet;
188 }
189 }
190
191 /**
192 * Construct a new MIME type object from the given string. The given
193 * string is converted into canonical form and stored internally.
194 *
195 * @param s MIME media type string.
196 *
197 * @exception NullPointerException
198 * (unchecked exception) Thrown if <CODE>s</CODE> is null.
199 * @exception IllegalArgumentException
200 * (unchecked exception) Thrown if <CODE>s</CODE> does not obey the
201 * syntax for a MIME media type string.
202 */
203 public MimeType(String s) {
204 parse (s);
205 }
206
207 /**
208 * Returns this MIME type object's MIME type string based on the canonical
209 * form. Each parameter value is enclosed in quotes.
210 */
211 public String getMimeType() {
212 return getStringValue();
213 }
214
215 /**
216 * Returns this MIME type object's media type.
217 */
218 public String getMediaType() {
219 return myPieces[0];
220 }
254 /**
255 * Returns a hash code for this MIME type object.
256 */
257 public int hashCode() {
258 return getStringValue().hashCode();
259 }
260
261 /**
262 * Determine if this MIME type object is equal to the given object. The two
263 * are equal if the given object is not null, is an instance of class
264 * net.jini.print.data.MimeType, and has the same canonical form as this
265 * MIME type object (that is, has the same type, subtype, and parameters).
266 * Thus, if two MIME type objects are the same except for comments, they are
267 * considered equal. However, "text/plain" and "text/plain;
268 * charset=us-ascii" are not considered equal, even though they represent
269 * the same media type (because the default character set for plain text is
270 * US-ASCII).
271 *
272 * @param obj Object to test.
273 *
274 * @return True if this MIME type object equals <CODE>obj</CODE>, false
275 * otherwise.
276 */
277 public boolean equals (Object obj) {
278 return(obj != null &&
279 obj instanceof MimeType &&
280 getStringValue().equals(((MimeType) obj).getStringValue()));
281 }
282
283 /**
284 * Returns this MIME type's string value in canonical form.
285 */
286 private String getStringValue() {
287 if (myStringValue == null) {
288 StringBuilder result = new StringBuilder();
289 result.append (myPieces[0]);
290 result.append ('/');
291 result.append (myPieces[1]);
292 int n = myPieces.length;
293 for (int i = 2; i < n; i += 2) {
294 result.append(';');
508 */
509 private static String addQuotes(String s) {
510 int n = s.length();
511 int i;
512 char c;
513 StringBuilder result = new StringBuilder (n+2);
514 result.append ('\"');
515 for (i = 0; i < n; ++ i) {
516 c = s.charAt (i);
517 if (c == '\"') {
518 result.append ('\\');
519 }
520 result.append (c);
521 }
522 result.append ('\"');
523 return result.toString();
524 }
525
526 /**
527 * Parses the given string into canonical pieces and stores the pieces in
528 * {@link #myPieces <CODE>myPieces</CODE>}.
529 * <P>
530 * Special rules applied:
531 * <UL>
532 * <LI> If the media type is text, the value of a charset parameter is
533 * converted to lowercase.
534 * </UL>
535 *
536 * @param s MIME media type string.
537 *
538 * @exception NullPointerException
539 * (unchecked exception) Thrown if <CODE>s</CODE> is null.
540 * @exception IllegalArgumentException
541 * (unchecked exception) Thrown if <CODE>s</CODE> does not obey the
542 * syntax for a MIME media type string.
543 */
544 private void parse(String s) {
545 // Initialize.
546 if (s == null) {
547 throw new NullPointerException();
548 }
549 LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
550 int theLexemeType;
551 Vector<String> thePieces = new Vector<>();
552 boolean mediaTypeIsText = false;
553 boolean parameterNameIsCharset = false;
554
555 // Parse media type.
556 if (theLexer.getLexemeType() == TOKEN_LEXEME) {
557 String mt = toUnicodeLowerCase (theLexer.getLexeme());
558 thePieces.add (mt);
559 theLexer.nextLexeme();
560 mediaTypeIsText = mt.equals ("text");
561 } else {
|
178
179 /**
180 * Parameter map.
181 */
182 private class ParameterMap extends AbstractMap<String, String> {
183 public Set<Map.Entry<String, String>> entrySet() {
184 if (myEntrySet == null) {
185 myEntrySet = new ParameterMapEntrySet();
186 }
187 return myEntrySet;
188 }
189 }
190
191 /**
192 * Construct a new MIME type object from the given string. The given
193 * string is converted into canonical form and stored internally.
194 *
195 * @param s MIME media type string.
196 *
197 * @exception NullPointerException
198 * (unchecked exception) Thrown if {@code s} is null.
199 * @exception IllegalArgumentException
200 * (unchecked exception) Thrown if {@code s} does not obey the
201 * syntax for a MIME media type string.
202 */
203 public MimeType(String s) {
204 parse (s);
205 }
206
207 /**
208 * Returns this MIME type object's MIME type string based on the canonical
209 * form. Each parameter value is enclosed in quotes.
210 */
211 public String getMimeType() {
212 return getStringValue();
213 }
214
215 /**
216 * Returns this MIME type object's media type.
217 */
218 public String getMediaType() {
219 return myPieces[0];
220 }
254 /**
255 * Returns a hash code for this MIME type object.
256 */
257 public int hashCode() {
258 return getStringValue().hashCode();
259 }
260
261 /**
262 * Determine if this MIME type object is equal to the given object. The two
263 * are equal if the given object is not null, is an instance of class
264 * net.jini.print.data.MimeType, and has the same canonical form as this
265 * MIME type object (that is, has the same type, subtype, and parameters).
266 * Thus, if two MIME type objects are the same except for comments, they are
267 * considered equal. However, "text/plain" and "text/plain;
268 * charset=us-ascii" are not considered equal, even though they represent
269 * the same media type (because the default character set for plain text is
270 * US-ASCII).
271 *
272 * @param obj Object to test.
273 *
274 * @return True if this MIME type object equals {@code obj}, false
275 * otherwise.
276 */
277 public boolean equals (Object obj) {
278 return(obj != null &&
279 obj instanceof MimeType &&
280 getStringValue().equals(((MimeType) obj).getStringValue()));
281 }
282
283 /**
284 * Returns this MIME type's string value in canonical form.
285 */
286 private String getStringValue() {
287 if (myStringValue == null) {
288 StringBuilder result = new StringBuilder();
289 result.append (myPieces[0]);
290 result.append ('/');
291 result.append (myPieces[1]);
292 int n = myPieces.length;
293 for (int i = 2; i < n; i += 2) {
294 result.append(';');
508 */
509 private static String addQuotes(String s) {
510 int n = s.length();
511 int i;
512 char c;
513 StringBuilder result = new StringBuilder (n+2);
514 result.append ('\"');
515 for (i = 0; i < n; ++ i) {
516 c = s.charAt (i);
517 if (c == '\"') {
518 result.append ('\\');
519 }
520 result.append (c);
521 }
522 result.append ('\"');
523 return result.toString();
524 }
525
526 /**
527 * Parses the given string into canonical pieces and stores the pieces in
528 * {@link #myPieces myPieces}.
529 * <P>
530 * Special rules applied:
531 * <UL>
532 * <LI> If the media type is text, the value of a charset parameter is
533 * converted to lowercase.
534 * </UL>
535 *
536 * @param s MIME media type string.
537 *
538 * @exception NullPointerException
539 * (unchecked exception) Thrown if {@code s} is null.
540 * @exception IllegalArgumentException
541 * (unchecked exception) Thrown if {@code s} does not obey the
542 * syntax for a MIME media type string.
543 */
544 private void parse(String s) {
545 // Initialize.
546 if (s == null) {
547 throw new NullPointerException();
548 }
549 LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
550 int theLexemeType;
551 Vector<String> thePieces = new Vector<>();
552 boolean mediaTypeIsText = false;
553 boolean parameterNameIsCharset = false;
554
555 // Parse media type.
556 if (theLexer.getLexemeType() == TOKEN_LEXEME) {
557 String mt = toUnicodeLowerCase (theLexer.getLexeme());
558 thePieces.add (mt);
559 theLexer.nextLexeme();
560 mediaTypeIsText = mt.equals ("text");
561 } else {
|