1 /*
   2  * Copyright (c) 1997, 2017, 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  */
  25 
  26 /*
  27  * @(#)ParameterList.java     1.10 03/02/12
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
  33 
  34 import java.util.HashMap;
  35 import java.util.Iterator;
  36 import java.util.Map;
  37 
  38 /**
  39  * This class holds MIME parameters (attribute-value pairs).
  40  *
  41  * @version 1.10, 03/02/12
  42  * @author  John Mani
  43  */
  44 
  45 public final class ParameterList {
  46 
  47     private final HashMap<String, String> list;
  48 
  49     /**
  50      * No-arg Constructor.
  51      */
  52     public ParameterList() {
  53         this.list = new HashMap<>();
  54     }
  55 
  56     private ParameterList(HashMap<String, String> m) {
  57         this.list = m;
  58     }
  59 
  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;
  75 
  76         list = new HashMap<>();
  77         while (true) {
  78             tk = h.next();
  79             type = tk.getType();
  80 
  81             if (type == HeaderTokenizer.Token.EOF) // done
  82                 return;
  83 
  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();
  94 
  95                 // expect '='
  96                 tk = h.next();
  97                 if ((char)tk.getType() != '=')
  98                     throw new ParseException();
  99 
 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();
 107 
 108                 list.put(name, tk.getValue());
 109             } else
 110                 throw new ParseException();
 111         }
 112     }
 113 
 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     }
 122 
 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 list.get(name.trim().toLowerCase());
 134     }
 135 
 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     }
 146 
 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     }
 156 
 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<String> getNames() {
 164         return list.keySet().iterator();
 165     }
 166 
 167 
 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     @Override
 175     public String toString() {
 176         return toString(0);
 177     }
 178 
 179     /**
 180      * Convert this ParameterList into a MIME String. If this is
 181      * an empty list, an empty string is returned.
 182      *
 183      * The 'used' parameter specifies the number of character positions
 184      * already taken up in the field into which the resulting parameter
 185      * list is to be inserted. It's used to determine where to fold the
 186      * resulting parameter list.
 187      *
 188      * @param used      number of character positions already used, in
 189      *                  the field into which the parameter list is to
 190      *                  be inserted.
 191      * @return          String
 192      */
 193     public String toString(int used) {
 194         StringBuilder sb = new StringBuilder();
 195         Iterator<Map.Entry<String, String>> itr = list.entrySet().iterator();
 196 
 197         while (itr.hasNext()) {
 198             Map.Entry<String, String> e = itr.next();
 199             String name = e.getKey();
 200             String value = quote(e.getValue());
 201             sb.append("; ");
 202             used += 2;
 203             int len = name.length() + value.length() + 1;
 204             if (used + len > 76) { // overflows ...
 205                 sb.append("\r\n\t"); // .. start new continuation line
 206                 used = 8; // account for the starting <tab> char
 207             }
 208             sb.append(name).append('=');
 209             used += name.length() + 1;
 210             if (used + value.length() > 76) { // still overflows ...
 211                 // have to fold value
 212                 String s = MimeUtility.fold(used, value);
 213                 sb.append(s);
 214                 int lastlf = s.lastIndexOf('\n');
 215                 if (lastlf >= 0)        // always true
 216                     used += s.length() - lastlf - 1;
 217                 else
 218                     used += s.length();
 219             } else {
 220                 sb.append(value);
 221                 used += value.length();
 222             }
 223         }
 224 
 225         return sb.toString();
 226     }
 227 
 228     // Quote a parameter value token if required.
 229     private String quote(String value) {
 230         if ("".equals(value))
 231                 return "\"\"";
 232         return MimeUtility.quote(value, HeaderTokenizer.MIME);
 233     }
 234 
 235     public ParameterList copy() {
 236         return new ParameterList((HashMap<String, String>)list.clone());
 237     }
 238 }
--- EOF ---