001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util;
017:
018: import java.net.URI;
019: import java.net.URISyntaxException;
020:
021: import org.apache.commons.beanutils.ConvertUtils;
022: import org.apache.commons.beanutils.converters.BooleanConverter;
023: import org.apache.commons.beanutils.converters.ByteConverter;
024: import org.apache.commons.beanutils.converters.CharacterConverter;
025: import org.apache.commons.beanutils.converters.DoubleConverter;
026: import org.apache.commons.beanutils.converters.FloatConverter;
027: import org.apache.commons.beanutils.converters.IntegerConverter;
028: import org.apache.commons.beanutils.converters.ShortConverter;
029: import org.geotools.factory.Hints;
030:
031: /**
032: * ConverterFactory based on the apache commons {@link org.apache.commons.beanutils.Converter}
033: * interface.
034: *
035: * @author Justin Deoliveira, The Open Planning Project
036: * @since 2.4
037: */
038: public class CommonsConverterFactory implements ConverterFactory {
039:
040: //some additional converters
041:
042: /**
043: * converts a string to a uri.
044: */
045: static org.apache.commons.beanutils.Converter uri = new org.apache.commons.beanutils.Converter() {
046: public Object convert(Class target, Object value) {
047: String string = (String) value;
048: try {
049: return new URI(string);
050: } catch (URISyntaxException e) {
051: }
052:
053: return null;
054: }
055: };
056: /**
057: * converts a string to a number when the target class == Number, does so
058: * by delegating to the other numeric converters
059: */
060: static org.apache.commons.beanutils.Converter number = new org.apache.commons.beanutils.Converter() {
061: public Object convert(Class type, Object value) {
062: String string = (String) value;
063:
064: // wipe out white space, converters would fail trying to
065: // parse "153 " just because of the trailing space
066: if (string != null)
067: string = string.trim();
068: Number parsed = null;
069: try {
070: //first try integer
071: parsed = (Number) new IntegerConverter().convert(
072: Integer.class, string);
073: } catch (Exception e) {
074: }
075:
076: if (parsed == null) {
077: //try double
078: parsed = (Number) new DoubleConverter().convert(
079: Double.class, string);
080: }
081:
082: return parsed;
083: };
084: };
085:
086: static {
087: ConvertUtils.register(uri, URI.class);
088: ConvertUtils.register(number, Number.class);
089:
090: //make sure numeric converters do not use default value
091: ConvertUtils.register(new ByteConverter(null), Byte.class);
092: ConvertUtils.register(new ShortConverter(null), Short.class);
093: ConvertUtils
094: .register(new IntegerConverter(null), Integer.class);
095: ConvertUtils.register(new DoubleConverter(null), Double.class);
096: ConvertUtils.register(new FloatConverter(null), Float.class);
097:
098: ConvertUtils
099: .register(new BooleanConverter(null), Boolean.class);
100: ConvertUtils.register(new CharacterConverter(null),
101: Character.class);
102: }
103:
104: /**
105: * Delegates to {@link ConvertUtils#lookup(java.lang.Class)} to create a
106: * converter instance.
107: *
108: * @see ConverterFactory#createConverter(Class, Class, Hints).
109: */
110: public Converter createConverter(Class source, Class target,
111: Hints hints) {
112: //only do strings
113: if (source.equals(String.class)) {
114: org.apache.commons.beanutils.Converter converter = ConvertUtils
115: .lookup(target);
116: if (converter != null) {
117: return new CommonsConverterWrapper(converter);
118: }
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Decorates a beanutils converter in a geotools converter.
126: *
127: * @author Justin Deoliveira, The Open Planning Project
128: *
129: */
130: static class CommonsConverterWrapper implements Converter {
131:
132: org.apache.commons.beanutils.Converter delegate;
133:
134: public CommonsConverterWrapper(
135: org.apache.commons.beanutils.Converter delegate) {
136: this .delegate = delegate;
137: }
138:
139: public Object convert(Object source, Class target)
140: throws Exception {
141: return delegate.convert(target, source);
142: }
143:
144: }
145:
146: }
|