01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.ows.kvp;
06:
07: import org.geoserver.ows.KvpParser;
08: import org.geoserver.ows.util.CaseInsensitiveMap;
09: import org.geoserver.platform.GeoServerExtensions;
10: import org.springframework.beans.BeansException;
11: import org.springframework.context.ApplicationContext;
12: import org.springframework.context.ApplicationContextAware;
13: import java.util.HashMap;
14: import java.util.Iterator;
15: import java.util.List;
16: import java.util.Map;
17: import java.util.StringTokenizer;
18: import java.util.logging.Level;
19:
20: /**
21: * Parses the format options parameter which is of the form:
22: * <pre>FORMAT_OPTIONS=opt1:val1,val2;opt2:val1;opt3:...</pre>
23: *
24: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
25: *
26: */
27: public class FormatOptionsKvpParser extends KvpParser implements
28: ApplicationContextAware {
29: /**
30: * application context used to lookup KvpParsers
31: */
32: ApplicationContext applicationContext;
33:
34: public FormatOptionsKvpParser() {
35: super ("format_options", Map.class);
36: }
37:
38: public void setApplicationContext(
39: ApplicationContext applicationContext)
40: throws BeansException {
41: this .applicationContext = applicationContext;
42: }
43:
44: public Object parse(String value) throws Exception {
45: List parsers = GeoServerExtensions.extensions(KvpParser.class,
46: applicationContext);
47: Map formatOptions = new CaseInsensitiveMap(new HashMap());
48:
49: //TODO: refactor some of this routine out into utility class since
50: // much of the logic is duplicated from the dispatcher
51: StringTokenizer st = new StringTokenizer(value, ";");
52:
53: while (st.hasMoreTokens()) {
54: String kvp = (String) st.nextToken();
55: String[] kv = kvp.split(":");
56:
57: String key = null;
58: String raw = null;
59:
60: if (kv.length == 1) {
61: //assume its a on/off (boolean) kvp
62: key = kv[0];
63: raw = "true";
64: } else {
65: key = kv[0];
66: raw = kv[1];
67: }
68:
69: Object parsed = null;
70:
71: for (Iterator p = parsers.iterator(); p.hasNext();) {
72: KvpParser parser = (KvpParser) p.next();
73: if (key.equalsIgnoreCase(parser.getKey())) {
74: parsed = parser.parse(raw);
75: if (parsed != null) {
76:
77: break;
78: }
79: }
80: }
81:
82: if (parsed == null) {
83: if (LOGGER.isLoggable(Level.FINER))
84: LOGGER.finer("Could not find kvp parser for: '"
85: + key + "'. Storing as raw string.");
86: parsed = raw;
87: }
88:
89: formatOptions.put(key, parsed);
90: }
91:
92: return formatOptions;
93: }
94: }
|