001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.type;
017:
018: import org.geotools.feature.AttributeType;
019: import org.geotools.feature.Feature;
020: import org.geotools.feature.FeatureType;
021: import org.geotools.feature.IllegalAttributeException;
022: import org.opengis.filter.Filter;
023:
024: /**
025: * Represents an ordered list of AttributeTypes. For SFS this will be a
026: * FeatureType. For GML this is the same as an element whose complexType
027: * contains a Sequence of Attributes.
028: *
029: * @author dzwiers
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/type/FeatureAttributeType.java $
031: */
032: public class FeatureAttributeType implements AttributeType {
033:
034: private final boolean nill;
035: private final int min, max;
036: private final String name;
037: private final FeatureType featureType;
038:
039: /**
040: * @param copy
041: */
042: public FeatureAttributeType(FeatureAttributeType copy) {
043: nill = copy.isNillable();
044: min = copy.getMinOccurs();
045: max = copy.getMaxOccurs();
046: this .name = copy.getName();
047: featureType = copy.getFeatureType();
048: }
049:
050: // The field for 'Class type' should be added when GT has moved to java 1.5
051: public FeatureAttributeType(String name, FeatureType featureType,
052: boolean nillable, int min, int max) {
053: nill = nillable;
054: this .min = min;
055: this .max = max;
056: this .name = name;
057: this .featureType = featureType;
058: }
059:
060: public FeatureAttributeType(String name, FeatureType featureType,
061: boolean nillable) {
062: this (name, featureType, nillable, 1, 1);
063: }
064:
065: public Filter getRestriction() {
066: return Filter.INCLUDE;
067: }
068:
069: protected FeatureType getFeatureType() {
070: return featureType;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.geotools.feature.AttributeType#getName()
075: */
076: public final String getName() {
077: return getLocalName();
078: }
079:
080: /**
081: * {@inheritDoc}
082: */
083: public String getLocalName() {
084: return name;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.geotools.feature.AttributeType#getType()
089: */
090: public final Class getType() {
091: return getBinding();
092: }
093:
094: /**
095: * {@inheritDoc}
096: */
097: public Class getBinding() {
098: return Feature.class;
099: }
100:
101: /* (non-Javadoc)
102: * @see org.geotools.feature.AttributeType#isNillable()
103: */
104: public boolean isNillable() {
105: return nill;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.geotools.feature.AttributeType#getMinOccurs()
110: */
111: public int getMinOccurs() {
112: return min;
113: }
114:
115: /* (non-Javadoc)
116: * @see org.geotools.feature.AttributeType#getMaxOccurs()
117: */
118: public int getMaxOccurs() {
119: return max;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.geotools.feature.AttributeType#isGeometry()
124: */
125: public boolean isGeometry() {
126: return false;
127: }
128:
129: /* (non-Javadoc)
130: * @see org.geotools.feature.AttributeType#parse(java.lang.Object)
131: */
132: public Object parse(Object value) throws IllegalArgumentException {
133: if (value instanceof Feature) {
134: return value;
135: }
136: if (value instanceof Object[])
137: try {
138: return featureType.create((Object[]) value);
139: } catch (IllegalAttributeException e) {
140: IllegalArgumentException ee = new IllegalArgumentException(
141: e.getMessage());
142: ee.initCause(e);
143: throw ee;
144: }
145: throw new IllegalArgumentException(getName()
146: + " needs to parse an array of Objects");
147: }
148:
149: /* (non-Javadoc)
150: * @see org.geotools.feature.AttributeType#validate(java.lang.Object)
151: */
152: public void validate(Object obj) throws IllegalArgumentException {
153: if (obj instanceof Feature) {
154: try {
155: featureType.duplicate((Feature) obj);
156: return;
157: } catch (IllegalAttributeException e) {
158: IllegalArgumentException ee = new IllegalArgumentException(
159: e.getMessage());
160: ee.initCause(e);
161: throw ee;
162: }
163: }
164: //users should call parse first - ch.
165: /*if(obj instanceof Object[]){
166: try {
167: featureType.create((Object[])obj);
168: return;
169: } catch (IllegalAttributeException e) {
170: IllegalArgumentException ee = new IllegalArgumentException(e.getMessage());
171: ee.initCause(e);
172: throw ee;
173: }
174: }*/
175: throw new IllegalArgumentException("Not a Feature");
176:
177: }
178:
179: /* (non-Javadoc)
180: * @see org.geotools.feature.AttributeType#duplicate(java.lang.Object)
181: */
182: public Object duplicate(Object src)
183: throws IllegalAttributeException {
184: if (src instanceof Feature) {
185: return featureType.duplicate((Feature) src);
186: }
187: throw new IllegalArgumentException("Not an Object []");
188: }
189:
190: /* (non-Javadoc)
191: * @see org.geotools.feature.AttributeType#createDefaultValue()
192: */
193: public Object createDefaultValue() {
194: Object[] t = new Object[featureType.getAttributeCount()];
195: for (int i = 0; i < t.length; i++)
196: t[i] = null;
197: try {
198: return featureType.create(t);
199: } catch (IllegalAttributeException e) {
200: // do nothing
201: return null;
202: }
203: }
204:
205: /**
206: * This is only used twice in the whole geotools code base, and one of
207: * those is for a test, so we're removing it from the interface. If
208: * getAttributeType does not have the AttributeType it will just return
209: * null. Gets the number of occurrences of this attribute.
210: *
211: * @param xPath XPath pointer to attribute type.
212: *
213: * @return Number of occurrences.
214: */
215: public boolean hasAttributeType(String xPath) {
216: return getAttributeType(xPath) != null;
217: }
218:
219: /**
220: * Returns the number of attributes at the first 'level' of the schema.
221: *
222: * @return equivalent value to getAttributeTypes().length
223: */
224: public int getAttributeCount() {
225: return featureType.getAttributeCount();
226: }
227:
228: /**
229: * Gets the attributeType at this xPath, if the specified attributeType
230: * does not exist then null is returned.
231: *
232: * @param xPath XPath pointer to attribute type.
233: *
234: * @return True if attribute exists.
235: */
236: public AttributeType getAttributeType(String xPath) {
237: return featureType.getAttributeType(xPath);
238: }
239:
240: /**
241: * Find the position of a given AttributeType.
242: *
243: * @param type The type to search for.
244: *
245: * @return -1 if not found, a zero-based index if found.
246: */
247: public int find(AttributeType type) {
248: return featureType.find(type);
249: }
250:
251: /**
252: * Find the position of an AttributeType which matches the given String.
253: * @param attName the name to look for
254: * @return -1 if not found, zero-based index otherwise
255: */
256: public int find(String attName) {
257: return featureType.find(attName);
258: }
259:
260: /**
261: * Gets the attributeType at the specified index.
262: *
263: * @param position the position of the attribute to check.
264: *
265: * @return The attribute type at the specified position.
266: */
267: public AttributeType getAttributeType(int position) {
268: return featureType.getAttributeType(position);
269: }
270:
271: public AttributeType[] getAttributeTypes() {
272: return featureType.getAttributeTypes();
273: }
274: }
|