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