001: package org.geotools.feature.iso.simple;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.geotools.feature.iso.AttributeFactoryImpl;
008: import org.opengis.feature.Attribute;
009: import org.opengis.feature.FeatureFactory;
010: import org.opengis.feature.simple.SimpleFeature;
011: import org.opengis.feature.simple.SimpleFeatureCollection;
012: import org.opengis.feature.simple.SimpleFeatureCollectionType;
013: import org.opengis.feature.simple.SimpleFeatureFactory;
014: import org.opengis.feature.simple.SimpleFeatureType;
015: import org.opengis.feature.type.AttributeDescriptor;
016: import org.opengis.feature.type.AttributeType;
017: import org.opengis.feature.type.GeometryType;
018:
019: /**
020: * This builder will help you put together a SimpleFeature.
021: * <p>
022: * Since the simple feature is well simple, this class is not very complicated
023: * either! You are required to provide a SimpleFeatureFactory in order to use
024: * this builder.
025: * </p>
026: *
027: * @author Justin
028: */
029: public class SimpleFeatureBuilder {
030:
031: List attributes = new ArrayList();
032:
033: private SimpleFeatureFactory factory;
034:
035: private FeatureFactory typeFactory;
036:
037: private SimpleFeatureType featureType;
038:
039: private SimpleFeatureCollectionType collectionType;
040:
041: public SimpleFeatureBuilder(SimpleFeatureFactory factory) {
042: this .factory = factory;
043: this .typeFactory = new AttributeFactoryImpl();
044: }
045:
046: /**
047: * Setter injection for SimpleFeatureFactory. XXX Review? If you do not mean
048: * for Setter injection please factory final
049: *
050: * @param factory
051: */
052: public void setSimpleFeatureFactory(SimpleFeatureFactory factory) {
053: this .factory = factory;
054: }
055:
056: public void init() {
057: attributes.clear();
058: featureType = null;
059: collectionType = null;
060: }
061:
062: public void setType(SimpleFeatureType featureType) {
063: this .featureType = featureType;
064: }
065:
066: public void setType(SimpleFeatureCollectionType collectionType) {
067: this .collectionType = collectionType;
068: }
069:
070: /** Call to add the next attribute to the builder. */
071: public void add(Object value) {
072: attributes.add(value);
073: }
074:
075: public Object build(String id) {
076: if (featureType != null) {
077: return feature(id);
078: }
079: if (collectionType != null) {
080: return collection(id);
081: }
082: return null;
083: }
084:
085: public SimpleFeature feature(String id) {
086: return factory.createSimpleFeature(properties(), featureType,
087: id);
088: }
089:
090: private List properties() {
091: List properties = new ArrayList(attributes.size());
092: List descriptors = featureType.getAttributes();
093: int attCount = featureType.getAttributeCount();
094: for (int i = 0; i < attCount; i++) {
095: Object value = attributes.get(i);
096: AttributeDescriptor descriptor = (AttributeDescriptor) descriptors
097: .get(i);
098: Attribute attribute = create(value, descriptor);
099: properties.add(attribute);
100: }
101: return properties;
102: }
103:
104: public SimpleFeatureCollection collection(String id) {
105: return factory
106: .createSimpleFeatureCollection(collectionType, id);
107: }
108:
109: protected Attribute create(Object value,
110: AttributeDescriptor descriptor) {
111: // if (descriptor != null) {
112: // type = descriptor.getType();
113: // }
114: AttributeType type = (AttributeType) descriptor.type();
115:
116: Attribute attribute = null;
117: if (type instanceof GeometryType) {
118: attribute = factory.createGeometryAttribute(value,
119: descriptor, null, null);
120: } else {
121: attribute = factory
122: .createAttribute(value, descriptor, null);
123: }
124:
125: return attribute;
126: }
127:
128: /**
129: * Initialize the builder with the provided feature.
130: * <p>
131: * This is used to quickly create a "clone", can be used to change between
132: * one SimpleFeatureImplementation and another.
133: * </p>
134: *
135: * @param feature
136: */
137: public void init(SimpleFeature feature) {
138: init();
139: this .featureType = (SimpleFeatureType) feature.getType();
140: for (Iterator i = feature.attributes().iterator(); i.hasNext();) {
141: this .attributes.add(i.next()); // TODO: copy
142: }
143: }
144: }
|