001: package com.sun.xml.ws.encoding;
002:
003: /*
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://jwsdp.dev.java.net/CDDLv1.0.html
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
017: * add the following below this CDDL HEADER, with the
018: * fields enclosed by brackets "[]" replaced with your
019: * own identifying information: Portions Copyright [yyyy]
020: * [name of copyright owner]
021: */
022: /*
023: * @(#)ParameterList.java 1.10 03/02/12
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: import javax.xml.ws.WebServiceException;
062: import java.util.HashMap;
063: import java.util.Iterator;
064: import java.util.Map;
065:
066: /**
067: * This class holds MIME parameters (attribute-value pairs).
068: *
069: * @version 1.10, 03/02/12
070: * @author John Mani
071: */
072:
073: final class ParameterList {
074:
075: private final Map<String, String> list;
076:
077: /**
078: * Constructor that takes a parameter-list string. The String
079: * is parsed and the parameters are collected and stored internally.
080: * A ParseException is thrown if the parse fails.
081: * Note that an empty parameter-list string is valid and will be
082: * parsed into an empty ParameterList.
083: *
084: * @param s the parameter-list string.
085: * @exception WebServiceException if the parse fails.
086: */
087: ParameterList(String s) {
088: HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
089: HeaderTokenizer.Token tk;
090: int type;
091: String name;
092:
093: list = new HashMap<String, String>();
094: while (true) {
095: tk = h.next();
096: type = tk.getType();
097:
098: if (type == HeaderTokenizer.Token.EOF) // done
099: return;
100:
101: if ((char) type == ';') {
102: // expect parameter name
103: tk = h.next();
104: // tolerate trailing semicolon, even though it violates the spec
105: if (tk.getType() == HeaderTokenizer.Token.EOF)
106: return;
107: // parameter name must be a MIME Atom
108: if (tk.getType() != HeaderTokenizer.Token.ATOM)
109: throw new WebServiceException();
110: name = tk.getValue().toLowerCase();
111:
112: // expect '='
113: tk = h.next();
114: if ((char) tk.getType() != '=')
115: throw new WebServiceException();
116:
117: // expect parameter value
118: tk = h.next();
119: type = tk.getType();
120: // parameter value must be a MIME Atom or Quoted String
121: if (type != HeaderTokenizer.Token.ATOM
122: && type != HeaderTokenizer.Token.QUOTEDSTRING)
123: throw new WebServiceException();
124:
125: list.put(name, tk.getValue());
126: } else
127: throw new WebServiceException();
128: }
129: }
130:
131: /**
132: * Return the number of parameters in this list.
133: *
134: * @return number of parameters.
135: */
136: int size() {
137: return list.size();
138: }
139:
140: /**
141: * Returns the value of the specified parameter. Note that
142: * parameter names are case-insensitive.
143: *
144: * @param name parameter name.
145: * @return Value of the parameter. Returns
146: * <code>null</code> if the parameter is not
147: * present.
148: */
149: String get(String name) {
150: return list.get(name.trim().toLowerCase());
151: }
152:
153: /**
154: * Return an enumeration of the names of all parameters in this
155: * list.
156: *
157: * @return Enumeration of all parameter names in this list.
158: */
159: Iterator<String> getNames() {
160: return list.keySet().iterator();
161: }
162:
163: }
|