001: /*
002: * Created on 19.02.2005
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package de.schlund.pfixxml.util;
008:
009: import java.net.URLDecoder;
010: import java.util.Enumeration;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.StringTokenizer;
015: import java.util.Vector;
016:
017: import javax.servlet.http.HttpServletRequest;
018:
019: import org.apache.log4j.Logger;
020:
021: /**
022: * @author ml
023: *
024: * To change the template for this generated type comment go to
025: * Window - Preferences - Java - Code Generation - Code and Comments
026: */
027: public class URIParameters {
028:
029: private final static Logger LOG = Logger
030: .getLogger(URIParameters.class);
031:
032: HttpServletRequest request;
033: String encoding;
034: HashMap<String, String[]> params;
035:
036: public URIParameters(HttpServletRequest request, String encoding)
037: throws Exception {
038: this .request = request;
039: this .encoding = encoding;
040: params = parse(request.getQueryString(), encoding);
041: if (LOG.isDebugEnabled())
042: LOG.debug(toString());
043: }
044:
045: public HttpServletRequest getRequest() {
046: return request;
047: }
048:
049: public String getEncoding() {
050: return encoding;
051: }
052:
053: private HashMap<String, String[]> parse(String queryStr,
054: String encoding) throws Exception {
055: HashMap<String, String[]> map = new HashMap<String, String[]>();
056: if (queryStr != null) {
057: StringTokenizer st = new StringTokenizer(queryStr, "&");
058: while (st.hasMoreTokens()) {
059: String pair = st.nextToken();
060: int ind = pair.indexOf('=');
061: if (ind > 0) {
062: String key = pair.substring(0, ind);
063: String val = pair.substring(ind + 1);
064: String decKey = URLDecoder.decode(key, encoding);
065: String decVal = URLDecoder.decode(val, encoding);
066: String[] vals = (String[]) map.get(decKey);
067: if (vals == null)
068: vals = new String[] { decVal };
069: else {
070: String[] tmp = new String[vals.length + 1];
071: for (int i = 0; i < vals.length; i++)
072: tmp[i] = vals[i];
073: tmp[vals.length] = decVal;
074: vals = tmp;
075: }
076: map.put(decKey, vals);
077: }
078: }
079: }
080: return map;
081: }
082:
083: public String getParameter(String name) {
084: String[] vals = (String[]) params.get(name);
085: if (vals != null && vals.length > 0)
086: return vals[0];
087: return null;
088: }
089:
090: public Enumeration<String> getParameterNames() {
091: Vector<String> v = new Vector<String>();
092: if (!params.isEmpty()) {
093: Iterator<String> it = params.keySet().iterator();
094: while (it.hasNext())
095: v.add(it.next());
096: }
097: return v.elements();
098: }
099:
100: public String[] getParameterValues(String name) {
101: return params.get(name);
102: }
103:
104: public Map<String, String[]> getParameterMap() {
105: return params;
106: }
107:
108: public String toString() {
109: StringBuffer sb = new StringBuffer();
110: sb.append("URIParameters");
111: sb.append(" (");
112: sb.append(encoding);
113: sb.append("):\n");
114: Iterator<String> it = params.keySet().iterator();
115: while (it.hasNext()) {
116: String name = it.next();
117: sb.append(" ");
118: sb.append(name);
119: sb.append(" = ");
120: String[] values = (String[]) params.get(name);
121: for (int i = 0; i < values.length; i++) {
122: sb.append(values[i]);
123: if (i < values.length - 1)
124: sb.append(" | ");
125: }
126: sb.append("\n");
127: }
128: return sb.toString();
129: }
130:
131: }
|