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  */
25 
26 package com.sun.xml.internal.ws.encoding;
27 
28 import javax.xml.ws.WebServiceException;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 
33 /**
34  * This class holds MIME parameters (attribute-value pairs).
35  *
36  * @version 1.10, 03/02/12
37  * @author  John Mani
38  */
39 
40 final class ParameterList {
41 
42     private final Map<String, String> list;
43 
44     /**
45      * Constructor that takes a parameter-list string. The String
46      * is parsed and the parameters are collected and stored internally.
47      * A ParseException is thrown if the parse fails.
48      * Note that an empty parameter-list string is valid and will be
49      * parsed into an empty ParameterList.
50      *
51      * @param   s       the parameter-list string.
52      * @exception WebServiceException if the parse fails.
53      */
ParameterList(String s)54     ParameterList(String s) {
55         HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
56         HeaderTokenizer.Token tk;
57         int type;
58         String name;
59 
60         list = new HashMap<String, String>();
61         while (true) {
62             tk = h.next();
63             type = tk.getType();
64 
65             if (type == HeaderTokenizer.Token.EOF) // done
66             return;
67 
68             if ((char)type == ';') {
69                 // expect parameter name
70                 tk = h.next();
71                 // tolerate trailing semicolon, even though it violates the spec
72                 if (tk.getType() == HeaderTokenizer.Token.EOF)
73                     return;
74                 // parameter name must be a MIME Atom
75                 if (tk.getType() != HeaderTokenizer.Token.ATOM)
76                     throw new WebServiceException();
77                 name = tk.getValue().toLowerCase();
78 
79                 // expect '='
80                 tk = h.next();
81                 if ((char)tk.getType() != '=')
82                     throw new WebServiceException();
83 
84                 // expect parameter value
85                 tk = h.next();
86                 type = tk.getType();
87                 // parameter value must be a MIME Atom or Quoted String
88                 if (type != HeaderTokenizer.Token.ATOM &&
89                     type != HeaderTokenizer.Token.QUOTEDSTRING)
90                     throw new WebServiceException();
91 
92                 list.put(name, tk.getValue());
93             } else
94                 throw new WebServiceException();
95         }
96     }
97 
98     /**
99      * Return the number of parameters in this list.
100      *
101      * @return  number of parameters.
102      */
size()103     int size() {
104             return list.size();
105     }
106 
107     /**
108      * Returns the value of the specified parameter. Note that
109      * parameter names are case-insensitive.
110      *
111      * @param name      parameter name.
112      * @return          Value of the parameter. Returns
113      *                  <code>null</code> if the parameter is not
114      *                  present.
115      */
get(String name)116     String get(String name) {
117             return list.get(name.trim().toLowerCase());
118     }
119 
120 
121     /**
122      * Return an enumeration of the names of all parameters in this
123      * list.
124      *
125      * @return Enumeration of all parameter names in this list.
126      */
getNames()127     Iterator<String> getNames() {
128             return list.keySet().iterator();
129     }
130 
131 }
132