001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.ows;
006:
007: import org.geoserver.ows.util.KvpUtils;
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: /**
012: * A kvp parser which parses a value consisting of tokens in a flat list.
013: * <p>
014: * A value in flat form is a list of tokens seperated by a single delimiter.
015: * The default delimiter is a comma ( , ). Example:
016: * <pre>
017: * <code>
018: * key=token1,token2,...,tokenN
019: * </code>
020: * </pre>
021: * </p>
022: * <p>
023: * Upon processing of each token, the token is parsed into an instance of
024: * {@link #getBinding()}. Subclasses should override the method
025: * {@link #parseToken(String)}.
026: * </p>
027: * <p>
028: * By default, the {@link #parse(String)} method returns an list which contains
029: * instances of {@link #getBinding()}. The {@link #parse(List)} method may
030: * be overidden to return a differnt type of object.
031: * </p>
032: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
033: *
034: */
035: public class FlatKvpParser extends KvpParser {
036: /**
037: * the delimiter to use to seperate tokens
038: */
039: final String delimiter;
040:
041: /**
042: * Constructs the flat kvp parser specifying the key and class binding.
043: *
044: * @param key The key to bind to.
045: * @param binding The class of each token in the value.
046: */
047: public FlatKvpParser(String key, Class binding) {
048: this (key, binding, ",");
049: }
050:
051: /**
052: * Constructs the flat kvp parser specifying the key, class binding, and
053: * token delimiter.
054: *
055: * @param key The key to bind to.
056: * @param binding The class of each token in the value.
057: * @param delimiter The delimiter used to seperate tokens
058: */
059: public FlatKvpParser(String key, Class binding, String delimiter) {
060: super (key, binding);
061:
062: this .delimiter = delimiter;
063: }
064:
065: /**
066: * Tokenizes the value and delegates to {@link #parseToken(String)} to
067: * parse each token.
068: */
069: public final Object parse(String value) throws Exception {
070: List tokens = KvpUtils.readFlat(value, delimiter);
071: List parsed = new ArrayList(tokens.size());
072:
073: for (int i = 0; i < tokens.size(); i++) {
074: String token = (String) tokens.get(i);
075: parsed.add(parseToken(token));
076: }
077:
078: return parse(parsed);
079: }
080:
081: /**
082: * Parses the token into an instance of {@link #getBinding()}.
083: * <p>
084: * Subclasses should override this method, the default implementation
085: * just returns token passed in.
086: * </p>
087: * @param token Part of the value being parsed.
088: *
089: * @return The token parsed into an object.
090: *
091: */
092: protected Object parseToken(String token) throws Exception {
093: return token;
094: }
095:
096: /**
097: * Parses the parsed tokens into a final representation.
098: * <p>
099: * Subclasses may choose to override this method. The default implementation
100: * just return the array passed in.
101: * </p>
102: * @param values The parsed tokens, each value is an instance of {@link #getBinding()}.
103: *
104: * @return The final object.
105: */
106: protected Object parse(List values) throws Exception {
107: return values;
108: }
109: }
|