001: package dalma.spi;
002:
003: import java.io.UnsupportedEncodingException;
004: import java.net.URI;
005: import java.net.URLDecoder;
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: /**
010: * Parses the query part of the URL.
011: *
012: * @author Kohsuke Kawaguchi
013: */
014: public class UrlQueryParser {
015: private final Map<String, String> values = new HashMap<String, String>();
016:
017: /**
018: * Parses the query string.
019: *
020: * @param queryPart
021: * string like "a=b&c=d". May contain escape like '%8D'.
022: */
023: public UrlQueryParser(String queryPart) {
024: if (queryPart == null)
025: return; // nothing to parse, but that's not an error
026: try {
027: for (String token : queryPart.split("&")) {
028: int idx = token.indexOf('=');
029: if (idx < 0) {
030: values.put(token, "");
031: } else {
032: values.put(token.substring(0, idx), URLDecoder
033: .decode(token.substring(idx + 1), "UTF-8"));
034: }
035: }
036: } catch (UnsupportedEncodingException e) {
037: throw new AssertionError(); // impossible
038: }
039: }
040:
041: /**
042: * Parses the query string.
043: *
044: * @param uri
045: * URI whose query part will be parsed
046: */
047: public UrlQueryParser(URI uri) {
048: this (fixNull(uri.getQuery()));
049: }
050:
051: /**
052: * Gets the value for the specified key.
053: *
054: * <p>
055: * For example, if the query string was "a=b&c=d",
056: * you get "b" from {@code getValue("a")}.
057: *
058: * @return
059: * null if the value is not found for the key.
060: */
061: public String getValue(String key) {
062: return getValue(key, null);
063: }
064:
065: /**
066: * Gets the value for the specified key.
067: *
068: * <p>
069: * For example, if the query string was "a=b&c=d",
070: * you get "b" from {@code getValue("a")}.
071: *
072: * @param defaultValue
073: * if no value was found for the given key,
074: * this value will be returned.
075: */
076: public String getValue(String key, String defaultValue) {
077: String value = values.get(key);
078: if (value == null)
079: value = defaultValue;
080: return value;
081: }
082:
083: /**
084: * Gets the value for the specified key as an integer.
085: *
086: * <p>
087: * For example, if the query string was "a=b&c=d",
088: * you get "b" from {@code getValue("a")}.
089: *
090: * @param defaultValue
091: * if no value was found for the given key,
092: * this value will be returned.
093: * @throws NumberFormatException
094: * if the value was found but not a number.
095: */
096: public int getValue(String key, int defaultValue)
097: throws NumberFormatException {
098: String value = values.get(key);
099: if (value == null)
100: return defaultValue;
101: return Integer.parseInt(value);
102: }
103:
104: /**
105: * Returns true if the query has a parameter of the given name.
106: */
107: public boolean has(String key) {
108: return values.containsKey(key);
109: }
110:
111: /**
112: * Adds all the key/value pairs into the given map.
113: */
114: public void addTo(Map<? super String, ? super String> map) {
115: map.putAll(values);
116: }
117:
118: private static String fixNull(String s) {
119: if (s == null)
120: return "";
121: return s;
122: }
123: }
|