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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ParameterList.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,235 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @(#)ParameterList.java     1.10 03/02/12
    1.31 + */
    1.32 +
    1.33 +
    1.34 +
    1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
    1.36 +
    1.37 +import java.util.HashMap;
    1.38 +import java.util.Iterator;
    1.39 +import java.util.Map;
    1.40 +
    1.41 +/**
    1.42 + * This class holds MIME parameters (attribute-value pairs).
    1.43 + *
    1.44 + * @version 1.10, 03/02/12
    1.45 + * @author  John Mani
    1.46 + */
    1.47 +
    1.48 +public final class ParameterList {
    1.49 +
    1.50 +    private final HashMap list;
    1.51 +
    1.52 +    /**
    1.53 +     * No-arg Constructor.
    1.54 +     */
    1.55 +    public ParameterList() {
    1.56 +        this.list = new HashMap();
    1.57 +    }
    1.58 +
    1.59 +    private ParameterList(HashMap m) {
    1.60 +        this.list = m;
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * Constructor that takes a parameter-list string. The String
    1.65 +     * is parsed and the parameters are collected and stored internally.
    1.66 +     * A ParseException is thrown if the parse fails.
    1.67 +     * Note that an empty parameter-list string is valid and will be
    1.68 +     * parsed into an empty ParameterList.
    1.69 +     *
    1.70 +     * @param   s       the parameter-list string.
    1.71 +     * @exception       ParseException if the parse fails.
    1.72 +     */
    1.73 +    public ParameterList(String s) throws ParseException {
    1.74 +        HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
    1.75 +        HeaderTokenizer.Token tk;
    1.76 +        int type;
    1.77 +        String name;
    1.78 +
    1.79 +        list = new HashMap();
    1.80 +        while (true) {
    1.81 +            tk = h.next();
    1.82 +            type = tk.getType();
    1.83 +
    1.84 +            if (type == HeaderTokenizer.Token.EOF) // done
    1.85 +                return;
    1.86 +
    1.87 +            if ((char)type == ';') {
    1.88 +                // expect parameter name
    1.89 +                tk = h.next();
    1.90 +                // tolerate trailing semicolon, even though it violates the spec
    1.91 +                if (tk.getType() == HeaderTokenizer.Token.EOF)
    1.92 +                    return;
    1.93 +                // parameter name must be a MIME Atom
    1.94 +                if (tk.getType() != HeaderTokenizer.Token.ATOM)
    1.95 +                    throw new ParseException();
    1.96 +                name = tk.getValue().toLowerCase();
    1.97 +
    1.98 +                // expect '='
    1.99 +                tk = h.next();
   1.100 +                if ((char)tk.getType() != '=')
   1.101 +                    throw new ParseException();
   1.102 +
   1.103 +                // expect parameter value
   1.104 +                tk = h.next();
   1.105 +                type = tk.getType();
   1.106 +                // parameter value must be a MIME Atom or Quoted String
   1.107 +                if (type != HeaderTokenizer.Token.ATOM &&
   1.108 +                    type != HeaderTokenizer.Token.QUOTEDSTRING)
   1.109 +                    throw new ParseException();
   1.110 +
   1.111 +                list.put(name, tk.getValue());
   1.112 +            } else
   1.113 +                throw new ParseException();
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Return the number of parameters in this list.
   1.119 +     *
   1.120 +     * @return  number of parameters.
   1.121 +     */
   1.122 +    public int size() {
   1.123 +        return list.size();
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Returns the value of the specified parameter. Note that
   1.128 +     * parameter names are case-insensitive.
   1.129 +     *
   1.130 +     * @param name      parameter name.
   1.131 +     * @return          Value of the parameter. Returns
   1.132 +     *                  <code>null</code> if the parameter is not
   1.133 +     *                  present.
   1.134 +     */
   1.135 +    public String get(String name) {
   1.136 +        return (String)list.get(name.trim().toLowerCase());
   1.137 +    }
   1.138 +
   1.139 +    /**
   1.140 +     * Set a parameter. If this parameter already exists, it is
   1.141 +     * replaced by this new value.
   1.142 +     *
   1.143 +     * @param   name    name of the parameter.
   1.144 +     * @param   value   value of the parameter.
   1.145 +     */
   1.146 +    public void set(String name, String value) {
   1.147 +        list.put(name.trim().toLowerCase(), value);
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Removes the specified parameter from this ParameterList.
   1.152 +     * This method does nothing if the parameter is not present.
   1.153 +     *
   1.154 +     * @param   name    name of the parameter.
   1.155 +     */
   1.156 +    public void remove(String name) {
   1.157 +        list.remove(name.trim().toLowerCase());
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Return an enumeration of the names of all parameters in this
   1.162 +     * list.
   1.163 +     *
   1.164 +     * @return Enumeration of all parameter names in this list.
   1.165 +     */
   1.166 +    public Iterator getNames() {
   1.167 +        return list.keySet().iterator();
   1.168 +    }
   1.169 +
   1.170 +
   1.171 +    /**
   1.172 +     * Convert this ParameterList into a MIME String. If this is
   1.173 +     * an empty list, an empty string is returned.
   1.174 +     *
   1.175 +     * @return          String
   1.176 +     */
   1.177 +    public String toString() {
   1.178 +        return toString(0);
   1.179 +    }
   1.180 +
   1.181 +    /**
   1.182 +     * Convert this ParameterList into a MIME String. If this is
   1.183 +     * an empty list, an empty string is returned.
   1.184 +     *
   1.185 +     * The 'used' parameter specifies the number of character positions
   1.186 +     * already taken up in the field into which the resulting parameter
   1.187 +     * list is to be inserted. It's used to determine where to fold the
   1.188 +     * resulting parameter list.
   1.189 +     *
   1.190 +     * @param used      number of character positions already used, in
   1.191 +     *                  the field into which the parameter list is to
   1.192 +     *                  be inserted.
   1.193 +     * @return          String
   1.194 +     */
   1.195 +    public String toString(int used) {
   1.196 +        StringBuffer sb = new StringBuffer();
   1.197 +        Iterator itr = list.entrySet().iterator();
   1.198 +
   1.199 +        while (itr.hasNext()) {
   1.200 +            Map.Entry e = (Map.Entry)itr.next();
   1.201 +            String name = (String)e.getKey();
   1.202 +            String value = quote((String)e.getValue());
   1.203 +            sb.append("; ");
   1.204 +            used += 2;
   1.205 +            int len = name.length() + value.length() + 1;
   1.206 +            if (used + len > 76) { // overflows ...
   1.207 +                sb.append("\r\n\t"); // .. start new continuation line
   1.208 +                used = 8; // account for the starting <tab> char
   1.209 +            }
   1.210 +            sb.append(name).append('=');
   1.211 +            used += name.length() + 1;
   1.212 +            if (used + value.length() > 76) { // still overflows ...
   1.213 +                // have to fold value
   1.214 +                String s = MimeUtility.fold(used, value);
   1.215 +                sb.append(s);
   1.216 +                int lastlf = s.lastIndexOf('\n');
   1.217 +                if (lastlf >= 0)        // always true
   1.218 +                    used += s.length() - lastlf - 1;
   1.219 +                else
   1.220 +                    used += s.length();
   1.221 +            } else {
   1.222 +                sb.append(value);
   1.223 +                used += value.length();
   1.224 +            }
   1.225 +        }
   1.226 +
   1.227 +        return sb.toString();
   1.228 +    }
   1.229 +
   1.230 +    // Quote a parameter value token if required.
   1.231 +    private String quote(String value) {
   1.232 +        return MimeUtility.quote(value, HeaderTokenizer.MIME);
   1.233 +    }
   1.234 +
   1.235 +    public ParameterList copy() {
   1.236 +        return new ParameterList((HashMap)list.clone());
   1.237 +    }
   1.238 +}

mercurial