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.vfny.geoserver.global;
006:
007: import java.io.File;
008: import java.net.URL;
009: import java.nio.charset.Charset;
010: import java.util.List;
011: import java.util.Map;
012: import java.util.logging.Level;
013: import java.util.logging.Logger;
014:
015: /**
016: * This class implements some library function for all the sub classes in this
017: * package. This class follows the Layer Supertype pattern as defined in
018: * Fowler's Patterns of Enterprise Application Architecture. Here the pattern
019: * is described in great detail between pages 475 and 479. The main idea is to
020: * have a super class contain all the common functionality of the sub-classes
021: * which does not pertain to particular manipulations within the classes.
022: *
023: * @author Gabriel Rold�n
024: * @author dzwiers
025: * @version $Id: GlobalLayerSupertype.java 7746 2007-11-13 15:38:35Z aaime $
026: */
027: abstract class GlobalLayerSupertype {
028: /** for debugging */
029: protected static final Logger LOGGER = org.geotools.util.logging.Logging
030: .getLogger("org.vfny.geoserver.global");
031:
032: /**
033: * getDTO purpose.
034: *
035: * <p>
036: * A hook to get the DTO object of all subclasses. This method is
037: * dangerous as it return the original. It is not intended to be public,
038: * and should only be accessed without a clone being created inside the
039: * package.
040: * </p>
041: *
042: * @return A DTO object to be casted as appropriate.
043: */
044: abstract Object toDTO();
045:
046: /**
047: * checks <code>s</code> for nullity and if so, returns an empty String,
048: * else just returns <code>s</code>
049: *
050: * @param s String
051: *
052: * @return String non-null String (null -> "")
053: */
054: protected String notNull(String s) {
055: return (s == null) ? "" : s;
056: }
057:
058: /**
059: * get purpose.
060: *
061: * <p>
062: * Gets a String from a map of Strings
063: * </p>
064: *
065: * @param map Map the map to extract the string from
066: * @param key String the key for the map.
067: *
068: * @return String the value in the map.
069: *
070: * @see Map
071: */
072: public static String get(Map map, String key) {
073: if (map.containsKey(key)) {
074: return (String) map.get(key);
075: }
076:
077: return null;
078: }
079:
080: /**
081: * get purpose.
082: *
083: * <p>
084: * Gets a File from a map given the specified key. If the file is not found
085: * the default file is returned.
086: * </p>
087: *
088: * @param map Map the map to extract the file from
089: * @param key String the key to extract the value for
090: * @param defaultFile The default value should the key not exist.
091: *
092: * @return File a File as described above.
093: */
094: public static File get(Map map, String key, File defaultFile) {
095: if (map.containsKey(key)) {
096: return (File) map.get(key);
097: }
098:
099: return defaultFile;
100: }
101:
102: /**
103: * get purpose.
104: *
105: * <p>
106: * Gets a String from a map of Strings, and returns the default if the
107: * string does not exist.
108: * </p>
109: *
110: * @param map Map the map to extract the string from
111: * @param key String the key for the map.
112: * @param defaultValue The default value should the key not exist.
113: *
114: * @return String the value in the map.
115: *
116: * @see Map
117: */
118: public static String get(Map map, String key, String defaultValue) {
119: if (map.containsKey(key)) {
120: return (String) map.get(key);
121: }
122:
123: return defaultValue;
124: }
125:
126: /**
127: * get purpose.
128: *
129: * <p>
130: * Gets a List from a map given the specified key. If the list is not found
131: * the default list is returned.
132: * </p>
133: *
134: * @param map Map the map to extract the file from
135: * @param key String the key to extract the value for
136: * @param defaultList The default value should the key not exist.
137: *
138: * @return List a List as described above.
139: */
140: public static List get(Map map, String key, List defaultList) {
141: if (map.containsKey(key)) {
142: return (List) map.get(key);
143: }
144:
145: return defaultList;
146: }
147:
148: /**
149: * get purpose.
150: *
151: * <p>
152: * Gets a Map from a map given the specified key. If the map is not found
153: * the default map is returned.
154: * </p>
155: *
156: * @param map Map the map to extract the file from
157: * @param key String the key to extract the value for
158: * @param defaultMap The default value should the key not exist.
159: *
160: * @return Map a Map as described above.
161: */
162: public static Map get(Map map, String key, Map defaultMap) {
163: if (map.containsKey(key)) {
164: return (Map) map.get(key);
165: }
166:
167: return defaultMap;
168: }
169:
170: /**
171: * get purpose.
172: *
173: * <p>
174: * Gets a int from a map given the specified key. If the int is not found
175: * the default int is returned.
176: * </p>
177: *
178: * @param map Map the map to extract the file from
179: * @param key String the key to extract the value for
180: * @param defaultValue The default value should the key not exist.
181: *
182: * @return int an int as described above.
183: */
184: public static int get(Map map, String key, int defaultValue) {
185: if (map.containsKey(key)) {
186: return Integer.parseInt((String) map.get(key));
187: } else {
188: return defaultValue;
189: }
190: }
191:
192: /**
193: * get purpose.
194: *
195: * <p>
196: * Gets a boolean from a map given the specified key. If the boolean is not
197: * found the default boolean is returned.
198: * </p>
199: *
200: * @param map Map the map to extract the file from
201: * @param key String the key to extract the value for
202: * @param defaultValue The default value should the key not exist.
203: *
204: * @return boolean an boolean as described above.
205: */
206: public static boolean get(Map map, String key, boolean defaultValue) {
207: if (map.containsKey(key)) {
208: return Boolean.getBoolean((String) map.get(key));
209: } else {
210: return defaultValue;
211: }
212: }
213:
214: /**
215: * get purpose.
216: *
217: * <p>
218: * Gets a Charset from a map given the specified key. If the Charset is not
219: * found the default Charset is returned.
220: * </p>
221: *
222: * @param map Map the map to extract the file from
223: * @param key String the key to extract the value for
224: * @param defaultCharSet The default value should the key not exist.
225: *
226: * @return Charset an boolean as described above.
227: */
228: public static Charset get(Map map, String key,
229: Charset defaultCharSet) {
230: if (map.containsKey(key)) {
231: return (Charset) map.get(key);
232: }
233:
234: return defaultCharSet;
235: }
236:
237: /**
238: * get purpose.
239: *
240: * <p>
241: * Gets a Level from a map given the specified key. If the Level is not
242: * found the default Level is returned.
243: * </p>
244: *
245: * @param map Map the map to extract the file from
246: * @param key String the key to extract the value for
247: * @param defaultLevel The default value should the key not exist.
248: *
249: * @return Level an boolean as described above.
250: */
251: public static Level get(Map map, String key, Level defaultLevel) {
252: if (map.containsKey(key)) {
253: return (Level) map.get(key);
254: }
255:
256: return defaultLevel;
257: }
258:
259: /**
260: * get purpose.
261: *
262: * <p>
263: * Gets a URL from a map given the specified key. If the URL is not found
264: * the default URL is returned.
265: * </p>
266: *
267: * @param map Map the map to extract the file from
268: * @param key String the key to extract the value for
269: * @param defaultUrl The default value should the key not exist.
270: *
271: * @return URL an boolean as described above.
272: */
273: public static URL get(Map map, String key, URL defaultUrl) {
274: if (map.containsKey(key)) {
275: return (URL) map.get(key);
276: }
277:
278: return defaultUrl;
279: }
280:
281: /**
282: * get purpose.
283: *
284: * <p>
285: * Gets a Class from a map given the specified key. If the Class is not
286: * found the default Class is returned.
287: * </p>
288: *
289: * @param map Map the map to extract the file from
290: * @param key String the key to extract the value for
291: * @param defaultType The default value should the key not exist.
292: *
293: * @return Class an boolean as described above.
294: */
295: public static Class get(Map map, String key, Class defaultType) {
296: if (!map.containsKey(key)) {
297: return defaultType;
298: }
299:
300: Object value = map.get(key);
301:
302: if (value instanceof Class) {
303: return (Class) value;
304: }
305:
306: if (value instanceof String) {
307: try {
308: return Class.forName((String) value);
309: } catch (ClassNotFoundException e) {
310: LOGGER.log(Level.FINEST, e.getMessage(), e);
311: }
312: }
313:
314: return defaultType;
315: }
316: }
|