001: /*
002: * $Id: PageParameters.java 5377 2006-04-14 14:30:26 -0700 (Fri, 14 Apr 2006)
003: * jdonnerstag $ $Revision: 460456 $ $Date: 2006-04-14 14:30:26 -0700 (Fri, 14 Apr
004: * 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket;
020:
021: import java.util.Map;
022:
023: import wicket.util.string.IStringIterator;
024: import wicket.util.string.StringList;
025: import wicket.util.value.ValueMap;
026:
027: /**
028: * A typesafe abstraction and container for parameters to a requested page. Page
029: * parameters in HTTP are query string values in the request URL. In other
030: * protocols, the parameters to a page might come from some other source.
031: * <p>
032: * Pages which take a PageParameters object as an argument to their constructor
033: * can be accessed directly from a URL and are known as "bookmarkable" pages
034: * since the URL is stable across sessions and can be stored in a browser's
035: * bookmark database.
036: *
037: * @author Jonathan Locke
038: */
039: public final class PageParameters extends ValueMap {
040: private static final long serialVersionUID = 1L;
041:
042: /**
043: * Null value for page parameters
044: */
045: public static final PageParameters NULL = new PageParameters();
046:
047: /**
048: * Constructor
049: */
050: public PageParameters() {
051: }
052:
053: /**
054: * Copy constructor.
055: *
056: * @param map
057: * The map to copy
058: * @see ValueMap#ValueMap(java.util.Map)
059: */
060: public PageParameters(final Map parameterMap) {
061: super (parameterMap);
062: }
063:
064: /**
065: * Construct.
066: *
067: * @param keyValuePairs
068: * List of key value pairs separated by commas. For example,
069: * "param1=foo,param2=bar"
070: * @see ValueMap#ValueMap(String)
071: */
072: public PageParameters(final String keyValuePairs) {
073: this (keyValuePairs, ",");
074: }
075:
076: /**
077: * Construct.
078: *
079: * @param keyValuePairs
080: * List of key value pairs separated by commas. For example,
081: * "param1=foo,param2=bar"
082: * @param delimiter
083: * Delimiter string used to separate key/value pairs
084: * @see ValueMap#ValueMap(String)
085: */
086: public PageParameters(final String keyValuePairs,
087: final String delimiter) {
088: super ();
089:
090: // We can not use ValueMaps constructor as it uses
091: // VariableAssignmentParser which is more suitable for markup
092: // attributes, rather than URL parameters. URL param keys for
093: // examples are allowed to start with a digit (e.g. 0=xxx)
094: // and quotes are not "quotes".
095:
096: // Get list of strings separated by the delimiter
097: final StringList pairs = StringList.tokenize(keyValuePairs,
098: delimiter);
099:
100: // Go through each string in the list
101: for (IStringIterator iterator = pairs.iterator(); iterator
102: .hasNext();) {
103: // Get the next key value pair
104: final String pair = iterator.next();
105:
106: final int pos = pair.indexOf('=');
107: if (pos == 0) {
108: throw new IllegalArgumentException(
109: "URL parameter is missing the lvalue: " + pair);
110: } else if (pos != -1) {
111: final String key = pair.substring(0, pos).trim();
112: final String value = pair.substring(pos + 1).trim();
113:
114: put(key, value);
115: } else {
116: final String key = pair.trim();
117: final String value = null;
118:
119: put(key, value);
120: }
121: }
122: }
123: }
|