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.feature;
017:
018: import org.geotools.factory.CommonFactoryFinder;
019: import org.geotools.feature.type.FeatureAttributeType;
020: import org.geotools.feature.type.GeometricAttributeType;
021: import org.geotools.feature.type.NumericAttributeType;
022: import org.geotools.feature.type.TemporalAttributeType;
023: import org.geotools.feature.type.TextualAttributeType;
024: import org.geotools.filter.IllegalFilterException;
025: import org.geotools.filter.LengthFunction;
026: import org.opengis.filter.Filter;
027: import org.opengis.filter.FilterFactory;
028: import org.opengis.filter.expression.Expression;
029: import org.opengis.referencing.crs.CoordinateReferenceSystem;
030:
031: import com.vividsolutions.jts.geom.Geometry;
032:
033: /**
034: * Factory for creating DefaultAttributeTypes.
035: *
036: * @author Ian Schneider
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/DefaultAttributeTypeFactory.java $
038: * @version $Id: DefaultAttributeTypeFactory.java 26175 2007-07-08 23:03:07Z groldan $
039: */
040: public class DefaultAttributeTypeFactory extends AttributeTypeFactory {
041: private FilterFactory ff;
042:
043: public DefaultAttributeTypeFactory() {
044: this (CommonFactoryFinder.getFilterFactory(null));
045: }
046:
047: public DefaultAttributeTypeFactory(FilterFactory factory) {
048: ff = factory;
049: }
050:
051: public void setFilterFactory(FilterFactory factory) {
052: ff = factory;
053: }
054:
055: /**
056: * Create an AttributeType with the given name, Class, nillability, and
057: * fieldLength meta-data. This method will itself call <code>
058: * createAttributeType(String,Class,boolean,int,Object) </code> with null
059: * as the default value. To use your own default value, use the above
060: * method, providing your default value.
061: *
062: * @param name The name of the AttributeType to create.
063: * @param clazz the class of the AttributeType to create.
064: * @param isNillable whether the AttributeType should allow nulls.
065: *
066: * @return the newly created AttributeType
067: */
068: protected AttributeType createAttributeType(String name,
069: Class clazz, boolean isNillable, int fieldLength) {
070:
071: return createAttributeType(name, clazz, isNillable,
072: fieldLength, null);
073: }
074:
075: /**
076: * Creates the DefaultAttributeType.Feature
077: *
078: * @param name The name of the AttributeType to create.
079: * @param type To use for validation.
080: * @param isNillable whether the AttributeType should allow nulls.
081: *
082: * @return the newly created feature AttributeType.
083: */
084: protected AttributeType createAttributeType(String name,
085: FeatureType type, boolean isNillable) {
086:
087: return new FeatureAttributeType(name, type, isNillable,
088: minOccurs(isNillable), 1);
089: }
090:
091: protected Filter length(int fieldLength, String attributeXPath) {
092: LengthFunction length = (LengthFunction) ff.function(
093: "LengthFunction", new Expression[] { ff
094: .property(attributeXPath) });
095: if (length == null) {
096: // TODO: Help Richard! ff.createFunctionExpression cannot find Length!
097: return null;
098: }
099:
100: Filter cf = null;
101: try {
102: cf = ff.equals(length, ff.literal(fieldLength));
103: } catch (IllegalFilterException e) {
104: // TODO something
105: }
106: return cf == null ? Filter.EXCLUDE : cf;
107: }
108:
109: /**
110: * Implementation of AttributeType creation.
111: */
112: protected AttributeType createAttributeType(String name,
113: Class clazz, boolean isNillable, int fieldLength,
114: Object defaultValue) {
115: Filter f = length(fieldLength, name);
116:
117: int minOccurs = minOccurs(isNillable);
118: int maxOccurs = 1;
119: if (Number.class.isAssignableFrom(clazz)) {
120: return new NumericAttributeType(name, clazz, isNillable,
121: minOccurs, maxOccurs, defaultValue, f);
122: } else if (CharSequence.class.isAssignableFrom(clazz)) {
123: return new TextualAttributeType(name, isNillable,
124: minOccurs, maxOccurs, defaultValue, f);
125: } else if (java.util.Date.class.isAssignableFrom(clazz)) {
126: return new TemporalAttributeType(name, clazz, isNillable,
127: minOccurs, maxOccurs, defaultValue, f);
128: } else if (Geometry.class.isAssignableFrom(clazz)) {
129: return new GeometricAttributeType(name, clazz, isNillable,
130: minOccurs, maxOccurs, defaultValue, null, f);
131: }
132: return new DefaultAttributeType(name, clazz, isNillable,
133: minOccurs, maxOccurs, defaultValue, f);
134: }
135:
136: /**
137: * Implementation of AttributeType creation.
138: */
139: protected AttributeType createAttributeType(String name,
140: Class clazz, boolean isNillable, Filter filter,
141: Object defaultValue, Object metadata) {
142:
143: return createAttributeType(name, clazz, isNillable, filter,
144: defaultValue, metadata, minOccurs(isNillable), 1);
145: }
146:
147: protected AttributeType createAttributeType(String name,
148: Class clazz, boolean isNillable, Filter filter,
149: Object defaultValue, Object metadata, int min, int max) {
150:
151: if (Number.class.isAssignableFrom(clazz)) {
152: return new NumericAttributeType(name, clazz, isNillable,
153: min, max, defaultValue, filter);
154: } else if (CharSequence.class.isAssignableFrom(clazz)) {
155: return new TextualAttributeType(name, isNillable, min, max,
156: defaultValue, filter);
157: } else if (java.util.Date.class.isAssignableFrom(clazz)) {
158: //return new TemporalAttributeType(name,isNillable,min,max,defaultValue,filter);
159: return new TemporalAttributeType(name, clazz, isNillable,
160: min, max, defaultValue, filter);
161: } else if (Geometry.class.isAssignableFrom(clazz)) {
162: if (metadata instanceof CoordinateReferenceSystem)
163: return new GeometricAttributeType(name, clazz,
164: isNillable, min, max, defaultValue,
165: (CoordinateReferenceSystem) metadata, filter);
166: else
167: return new GeometricAttributeType(name, clazz,
168: isNillable, min, max, defaultValue, null,
169: filter);
170: }
171: return new DefaultAttributeType(name, clazz, isNillable, min,
172: max, defaultValue, filter);
173: }
174:
175: protected AttributeType createAttributeType(String name,
176: Class clazz, boolean isNillable, int fieldLength,
177: Object defaultValue, Object metaData) {
178:
179: if (Geometry.class.isAssignableFrom(clazz)
180: && metaData instanceof CoordinateReferenceSystem) {
181: LengthFunction length = (LengthFunction) ff.function(
182: "LengthFunction", new Expression[] { ff
183: .property(name) });
184: Filter cf = null;
185: try {
186: cf = ff.equals(length, ff.literal(fieldLength));
187: } catch (IllegalFilterException e) {
188: // TODO something
189: }
190: Filter f = cf == null ? Filter.EXCLUDE : cf;
191: return new GeometricAttributeType(name, clazz, isNillable,
192: minOccurs(isNillable), 1, defaultValue,
193: (CoordinateReferenceSystem) metaData, f);
194: }
195: return createAttributeType(name, clazz, isNillable,
196: fieldLength, defaultValue);
197: }
198:
199: private int minOccurs(boolean nillable) {
200: return nillable ? 0 : 1;
201: }
202: }
|