src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ParameterList.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 /*
    27  * @(#)ParameterList.java     1.10 03/02/12
    28  */
    32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
    34 import java.util.HashMap;
    35 import java.util.Iterator;
    36 import java.util.Map;
    38 /**
    39  * This class holds MIME parameters (attribute-value pairs).
    40  *
    41  * @version 1.10, 03/02/12
    42  * @author  John Mani
    43  */
    45 public final class ParameterList {
    47     private final HashMap list;
    49     /**
    50      * No-arg Constructor.
    51      */
    52     public ParameterList() {
    53         this.list = new HashMap();
    54     }
    56     private ParameterList(HashMap m) {
    57         this.list = m;
    58     }
    60     /**
    61      * Constructor that takes a parameter-list string. The String
    62      * is parsed and the parameters are collected and stored internally.
    63      * A ParseException is thrown if the parse fails.
    64      * Note that an empty parameter-list string is valid and will be
    65      * parsed into an empty ParameterList.
    66      *
    67      * @param   s       the parameter-list string.
    68      * @exception       ParseException if the parse fails.
    69      */
    70     public ParameterList(String s) throws ParseException {
    71         HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
    72         HeaderTokenizer.Token tk;
    73         int type;
    74         String name;
    76         list = new HashMap();
    77         while (true) {
    78             tk = h.next();
    79             type = tk.getType();
    81             if (type == HeaderTokenizer.Token.EOF) // done
    82                 return;
    84             if ((char)type == ';') {
    85                 // expect parameter name
    86                 tk = h.next();
    87                 // tolerate trailing semicolon, even though it violates the spec
    88                 if (tk.getType() == HeaderTokenizer.Token.EOF)
    89                     return;
    90                 // parameter name must be a MIME Atom
    91                 if (tk.getType() != HeaderTokenizer.Token.ATOM)
    92                     throw new ParseException();
    93                 name = tk.getValue().toLowerCase();
    95                 // expect '='
    96                 tk = h.next();
    97                 if ((char)tk.getType() != '=')
    98                     throw new ParseException();
   100                 // expect parameter value
   101                 tk = h.next();
   102                 type = tk.getType();
   103                 // parameter value must be a MIME Atom or Quoted String
   104                 if (type != HeaderTokenizer.Token.ATOM &&
   105                     type != HeaderTokenizer.Token.QUOTEDSTRING)
   106                     throw new ParseException();
   108                 list.put(name, tk.getValue());
   109             } else
   110                 throw new ParseException();
   111         }
   112     }
   114     /**
   115      * Return the number of parameters in this list.
   116      *
   117      * @return  number of parameters.
   118      */
   119     public int size() {
   120         return list.size();
   121     }
   123     /**
   124      * Returns the value of the specified parameter. Note that
   125      * parameter names are case-insensitive.
   126      *
   127      * @param name      parameter name.
   128      * @return          Value of the parameter. Returns
   129      *                  <code>null</code> if the parameter is not
   130      *                  present.
   131      */
   132     public String get(String name) {
   133         return (String)list.get(name.trim().toLowerCase());
   134     }
   136     /**
   137      * Set a parameter. If this parameter already exists, it is
   138      * replaced by this new value.
   139      *
   140      * @param   name    name of the parameter.
   141      * @param   value   value of the parameter.
   142      */
   143     public void set(String name, String value) {
   144         list.put(name.trim().toLowerCase(), value);
   145     }
   147     /**
   148      * Removes the specified parameter from this ParameterList.
   149      * This method does nothing if the parameter is not present.
   150      *
   151      * @param   name    name of the parameter.
   152      */
   153     public void remove(String name) {
   154         list.remove(name.trim().toLowerCase());
   155     }
   157     /**
   158      * Return an enumeration of the names of all parameters in this
   159      * list.
   160      *
   161      * @return Enumeration of all parameter names in this list.
   162      */
   163     public Iterator getNames() {
   164         return list.keySet().iterator();
   165     }
   168     /**
   169      * Convert this ParameterList into a MIME String. If this is
   170      * an empty list, an empty string is returned.
   171      *
   172      * @return          String
   173      */
   174     public String toString() {
   175         return toString(0);
   176     }
   178     /**
   179      * Convert this ParameterList into a MIME String. If this is
   180      * an empty list, an empty string is returned.
   181      *
   182      * The 'used' parameter specifies the number of character positions
   183      * already taken up in the field into which the resulting parameter
   184      * list is to be inserted. It's used to determine where to fold the
   185      * resulting parameter list.
   186      *
   187      * @param used      number of character positions already used, in
   188      *                  the field into which the parameter list is to
   189      *                  be inserted.
   190      * @return          String
   191      */
   192     public String toString(int used) {
   193         StringBuffer sb = new StringBuffer();
   194         Iterator itr = list.entrySet().iterator();
   196         while (itr.hasNext()) {
   197             Map.Entry e = (Map.Entry)itr.next();
   198             String name = (String)e.getKey();
   199             String value = quote((String)e.getValue());
   200             sb.append("; ");
   201             used += 2;
   202             int len = name.length() + value.length() + 1;
   203             if (used + len > 76) { // overflows ...
   204                 sb.append("\r\n\t"); // .. start new continuation line
   205                 used = 8; // account for the starting <tab> char
   206             }
   207             sb.append(name).append('=');
   208             used += name.length() + 1;
   209             if (used + value.length() > 76) { // still overflows ...
   210                 // have to fold value
   211                 String s = MimeUtility.fold(used, value);
   212                 sb.append(s);
   213                 int lastlf = s.lastIndexOf('\n');
   214                 if (lastlf >= 0)        // always true
   215                     used += s.length() - lastlf - 1;
   216                 else
   217                     used += s.length();
   218             } else {
   219                 sb.append(value);
   220                 used += value.length();
   221             }
   222         }
   224         return sb.toString();
   225     }
   227     // Quote a parameter value token if required.
   228     private String quote(String value) {
   229         if ("".equals(value))
   230                 return "\"\"";
   231         return MimeUtility.quote(value, HeaderTokenizer.MIME);
   232     }
   234     public ParameterList copy() {
   235         return new ParameterList((HashMap)list.clone());
   236     }
   237 }

mercurial