001 // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/util/KVP2Map.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2006 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstr. 19
030 53115 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042
043 ---------------------------------------------------------------------------*/
044 package org.deegree.framework.util;
045
046 import java.util.Enumeration;
047 import java.util.HashMap;
048 import java.util.Map;
049 import java.util.StringTokenizer;
050
051 import javax.servlet.http.HttpServletRequest;
052
053 /**
054 * offeres utility method for transformating a key-value-pair
055 * encoded request to a <tt>Map</tt>
056 *
057 * @version $Revision: 6259 $
058 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059 * @author last edited by: $Author: bezema $
060 *
061 * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
062 *
063 * @since 2.0
064 */
065 public class KVP2Map {
066
067 /**
068 * Transforms a String/KVPs like it is used for HTTP-GET request
069 * to a Map.
070 *
071 * TODO: Check if the trim () call may cause side effects. It is currently used to
072 * eliminate possible new line characters at the end of the string, that occured
073 * using the <code>GenericClient</code>.
074 *
075 * @param kvp key-value-pair encoded request
076 * @return created Map
077 */
078 public static Map<String,String> toMap(String kvp) {
079
080 StringTokenizer st = new StringTokenizer( kvp.trim (), "&?" );
081 HashMap<String,String> map = new HashMap<String,String>();
082
083 while ( st.hasMoreTokens() ) {
084 String s = st.nextToken();
085 if ( s != null ) {
086 int pos = s.indexOf( '=' );
087
088 if ( pos > -1 ) {
089 String s1 = s.substring( 0, pos );
090 String s2 = s.substring( pos + 1, s.length() );
091 map.put( s1.toUpperCase(), s2 );
092 }
093 }
094 }
095
096 return map;
097
098 }
099
100 /**
101 * @param iterator Enumeration containing KVP encoded parameters
102 * @return created Map
103 */
104 public static Map<String,String> toMap(Enumeration iterator) {
105 HashMap<String,String> map = new HashMap<String,String>();
106
107 while ( iterator.hasMoreElements() ) {
108 String s = (String)iterator.nextElement();
109 if ( s != null ) {
110 int pos = s.indexOf( '=' );
111
112 if ( pos > -1 ) {
113 String s1 = s.substring( 0, pos );
114 String s2 = s.substring( pos + 1, s.length() );
115 map.put( s1.toUpperCase(), s2 );
116 }
117 }
118 }
119
120 return map;
121 }
122
123 /**
124 * returns the parameters of a <tt>HttpServletRequest</tt> as <tt>Map</tt>
125 *
126 * @param request
127 * @return a Map which contains kvp's from the given request
128 */
129 public static Map<String, String> toMap(HttpServletRequest request) {
130 HashMap<String, String> map = new HashMap<String, String>();
131
132 Enumeration iterator = request.getParameterNames();
133 while ( iterator.hasMoreElements() ) {
134 String s = (String)iterator.nextElement();
135 String val = request.getParameter(s);
136 map.put(s.toUpperCase(), val);
137 }
138
139 return map;
140 }
141
142 }
143 /* ********************************************************************
144 Changes to this class. What the people have been up to:
145 $Log$
146 Revision 1.7 2006/11/28 16:39:07 bezema
147 Added String, String generic to map from toMap
148
149 Revision 1.6 2006/06/06 17:02:28 mschneider
150 Removed unnecessary synchronized modifiers. Added usage of Generics for type safety.
151
152 Revision 1.5 2006/04/06 20:25:28 poth
153 *** empty log message ***
154
155 Revision 1.4 2006/04/04 20:39:43 poth
156 *** empty log message ***
157
158 Revision 1.3 2006/03/30 21:20:27 poth
159 *** empty log message ***
160
161 Revision 1.2 2005/02/11 10:43:57 friebe
162 fit for java 1.5
163
164 Revision 1.1.1.1 2005/01/05 10:38:33 poth
165 no message
166
167 Revision 1.4 2004/08/04 18:49:31 mschneider
168 *** empty log message ***
169
170 Revision 1.3 2004/06/17 15:43:12 ap
171 no message
172
173 Revision 1.2 2004/05/25 07:19:13 ap
174 no message
175
176 Revision 1.1 2004/05/24 06:48:47 ap
177 no message
178
179
180 ********************************************************************** */