001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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: */
017: package org.geotools.data.feature.adapter;
018:
019: import java.net.URI;
020: import java.net.URISyntaxException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.LinkedHashMap;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import org.geotools.feature.AttributeType;
029: import org.geotools.feature.Feature;
030: import org.geotools.feature.FeatureType;
031: import org.geotools.feature.GeometryAttributeType;
032: import org.geotools.feature.IllegalAttributeException;
033: import org.opengis.feature.type.AttributeDescriptor;
034: import org.opengis.feature.type.ComplexType;
035: import org.opengis.feature.type.GeometryType;
036:
037: /**
038: * Adapts an ISO complex feature type to a GeoTools FeatureType by eliminating
039: * complex properties.
040: * <p>
041: * This is of almost any use but to allow the geoserver Data module to deal with
042: * ComplexDataStore as a normal DataStore instead of FeatureAccess, while we're
043: * not allowed to merge Feature implementations
044: * </p>
045: *
046: * @author Gabriel Roldan, Axios Engineering
047: * @version $Id: GTComlexFeatureTypeAdapter.java 28577 2008-01-03 15:44:29Z groldan $
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/adapter/GTComlexFeatureTypeAdapter.java $
049: * @since 2.4
050: */
051: public class GTComlexFeatureTypeAdapter implements FeatureType {
052:
053: private AttributeDescriptor adaptee;
054:
055: private LinkedHashMap properties;
056:
057: private GeometryAttributeType defaultGeom;
058:
059: public GTComlexFeatureTypeAdapter(
060: AttributeDescriptor featureDescriptor) {
061: if (featureDescriptor.type() instanceof ISOFeatureTypeAdapter) {
062: throw new IllegalArgumentException(
063: "No need to adapt ISOFEatureTypeAdapter, use getAdaptee() instead");
064: }
065: this .adaptee = featureDescriptor;
066: this .properties = new LinkedHashMap();
067:
068: org.opengis.feature.type.FeatureType type;
069: type = (org.opengis.feature.type.FeatureType) adaptee.type();
070:
071: List props = new LinkedList(type.getProperties());
072:
073: for (Iterator it = props.iterator(); it.hasNext();) {
074: AttributeDescriptor att = (AttributeDescriptor) it.next();
075: if (att.type() instanceof ComplexType) {
076: it.remove();
077: } else {
078: AttributeType gtAttType;
079: gtAttType = GTAttributeTypeAdapter.adapter(att);
080:
081: if (att.type() instanceof GeometryType) {
082: defaultGeom = (GeometryAttributeType) gtAttType;
083: }
084: properties.put(att.getName().getLocalPart(), gtAttType);
085: }
086: }
087:
088: }
089:
090: public AttributeDescriptor getAdaptee() {
091: return adaptee;
092: }
093:
094: public Feature create(Object[] attributes)
095: throws IllegalAttributeException {
096: throw new UnsupportedOperationException();
097: }
098:
099: public Feature create(Object[] attributes, String featureID)
100: throws IllegalAttributeException {
101: throw new UnsupportedOperationException();
102: }
103:
104: public Feature duplicate(Feature feature)
105: throws IllegalAttributeException {
106: throw new UnsupportedOperationException();
107: }
108:
109: public int find(AttributeType type) {
110: return find(type.getName());
111: }
112:
113: public int find(String attName) {
114: int index = 0;
115: for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
116: String name = (String) it.next();
117: if (name.equals(attName)) {
118: return index;
119: }
120: index++;
121: }
122: return -1;
123: }
124:
125: public FeatureType[] getAncestors() {
126: return new FeatureType[0];
127: }
128:
129: public int getAttributeCount() {
130: return properties.size();
131: }
132:
133: public AttributeType getAttributeType(String xPath) {
134: AttributeType att = (AttributeType) properties.get(xPath);
135: return att;
136: }
137:
138: public AttributeType getAttributeType(int position) {
139: ArrayList arrayList = new ArrayList(properties.values());
140: Object object = arrayList.get(position);
141: return (AttributeType) object;
142: }
143:
144: public AttributeType[] getAttributeTypes() {
145: Collection collection = properties.values();
146: AttributeType[] types = (AttributeType[]) collection
147: .toArray(new AttributeType[0]);
148: return types;
149: }
150:
151: public final GeometryAttributeType getDefaultGeometry() {
152: return getPrimaryGeometry();
153: }
154:
155: public GeometryAttributeType getPrimaryGeometry() {
156: return defaultGeom;
157: }
158:
159: public URI getNamespace() {
160: try {
161: URI ns = new URI(adaptee.getName().getNamespaceURI());
162: return ns;
163: } catch (URISyntaxException e) {
164: throw (RuntimeException) new RuntimeException()
165: .initCause(e);
166: }
167: }
168:
169: public String getTypeName() {
170: return adaptee.getName().getLocalPart();
171: }
172:
173: public boolean hasAttributeType(String xPath) {
174: return properties.containsKey(xPath);
175: }
176:
177: public boolean isAbstract() {
178: return adaptee.type().isAbstract();
179: }
180:
181: public boolean isDescendedFrom(URI nsURI, String typeName) {
182: return false;
183: }
184:
185: public boolean isDescendedFrom(FeatureType type) {
186: return false;
187: }
188:
189: }
|