001: package org.geotools.data.store;
002:
003: import java.util.Iterator;
004:
005: import org.geotools.feature.AttributeType;
006: import org.geotools.feature.Feature;
007: import org.geotools.feature.FeatureType;
008: import org.geotools.feature.IllegalAttributeException;
009:
010: /**
011: * Iterator wrapper which re-types features on the fly based on a target
012: * feature type.
013: *
014: * @author Justin Deoliveira, The Open Planning Project
015: *
016: */
017: public class ReTypingIterator implements Iterator {
018:
019: /**
020: * The delegate iterator
021: */
022: Iterator delegate;
023: /**
024: * The target feature type
025: */
026: FeatureType target;
027: /**
028: * The matching types from target
029: */
030: AttributeType[] types;
031:
032: public ReTypingIterator(Iterator delegate, FeatureType source,
033: FeatureType target) {
034: this .delegate = delegate;
035: this .target = target;
036: types = typeAttributes(source, target);
037: }
038:
039: public Iterator getDelegate() {
040: return delegate;
041: }
042:
043: public void remove() {
044: delegate.remove();
045: }
046:
047: public boolean hasNext() {
048: return delegate.hasNext();
049: }
050:
051: public Object next() {
052: Feature next = (Feature) delegate.next();
053: String id = next.getID();
054:
055: Object[] attributes = new Object[types.length];
056: String xpath;
057:
058: try {
059: for (int i = 0; i < types.length; i++) {
060: xpath = types[i].getName();
061: //attributes[i] = types[i].duplicate(next.getAttribute(xpath));
062: attributes[i] = next.getAttribute(xpath);
063: }
064:
065: return target.create(attributes, id);
066: } catch (IllegalAttributeException e) {
067: throw new RuntimeException(e);
068: }
069: }
070:
071: /**
072: * Supplies mapping from origional to target FeatureType.
073: *
074: * <p>
075: * Will also ensure that origional can cover target
076: * </p>
077: *
078: * @param target Desired FeatureType
079: * @param origional Origional FeatureType
080: *
081: * @return Mapping from originoal to target FeatureType
082: *
083: * @throws IllegalArgumentException if unable to provide a mapping
084: */
085: protected AttributeType[] typeAttributes(FeatureType original,
086: FeatureType target) {
087: if (target.equals(original)) {
088: throw new IllegalArgumentException(
089: "FeatureReader allready produces contents with the correct schema");
090: }
091:
092: if (target.getAttributeCount() > original.getAttributeCount()) {
093: throw new IllegalArgumentException(
094: "Unable to retype FeatureReader (origional does not cover requested type)");
095: }
096:
097: String xpath;
098: AttributeType[] types = new AttributeType[target
099: .getAttributeCount()];
100:
101: for (int i = 0; i < target.getAttributeCount(); i++) {
102: AttributeType attrib = target.getAttributeType(i);
103: xpath = attrib.getName();
104: types[i] = attrib;
105:
106: if (!attrib.equals(original.getAttributeType(xpath))) {
107: throw new IllegalArgumentException(
108: "Unable to retype FeatureReader (origional does not cover "
109: + xpath + ")");
110: }
111: }
112:
113: return types;
114: }
115:
116: }
|