001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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.data;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020:
021: import org.geotools.feature.Feature;
022: import org.geotools.feature.FeatureType;
023: import org.geotools.feature.FeatureTypeFactory;
024: import org.geotools.feature.IllegalAttributeException;
025: import org.geotools.feature.SchemaException;
026:
027: /**
028: * Basic support for reading Features from an AttributeReader.
029: *
030: * @author Ian Schneider
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/DefaultFeatureReader.java $
032: * @version $Id: DefaultFeatureReader.java 20651 2006-07-21 07:51:54Z jgarnett $
033: */
034: public class DefaultFeatureReader implements FeatureReader {
035: private final AttributeReader attributeReader;
036: private final FeatureType schema;
037: protected final Object[] attributes;
038:
039: /**
040: * Creates a new instance of AbstractFeatureReader
041: *
042: * @param attributeReader AttributeReader for contents
043: * @param schema FeatureType to use, <code>null</code> if not provided
044: *
045: * @throws SchemaException If Schema could not be obtained
046: */
047: public DefaultFeatureReader(AttributeReader attributeReader,
048: FeatureType schema) throws SchemaException {
049: this .attributeReader = attributeReader;
050:
051: if (schema == null) {
052: schema = createSchema();
053: }
054:
055: this .schema = schema;
056: this .attributes = new Object[attributeReader
057: .getAttributeCount()];
058: }
059:
060: public DefaultFeatureReader(AttributeReader attributeReader)
061: throws SchemaException {
062: this (attributeReader, null);
063: }
064:
065: public Feature next() throws IOException,
066: IllegalAttributeException, NoSuchElementException {
067: Feature f = null;
068:
069: if (attributeReader.hasNext()) {
070: attributeReader.next();
071: f = readFeature(attributeReader);
072: }
073:
074: return f;
075: }
076:
077: protected FeatureType createSchema() throws SchemaException {
078: FeatureTypeFactory factory = FeatureTypeFactory
079: .newInstance("xxx");
080:
081: for (int i = 0, ii = attributeReader.getAttributeCount(); i < ii; i++) {
082: factory.addType(attributeReader.getAttributeType(i));
083: }
084:
085: return factory.getFeatureType();
086: }
087:
088: protected Feature readFeature(AttributeReader atts)
089: throws IllegalAttributeException, IOException {
090: for (int i = 0, ii = atts.getAttributeCount(); i < ii; i++) {
091: attributes[i] = atts.read(i);
092: }
093:
094: return schema.create(attributes);
095: }
096:
097: public void close() throws IOException {
098: attributeReader.close();
099: }
100:
101: public FeatureType getFeatureType() {
102: return schema;
103: }
104:
105: public boolean hasNext() throws IOException {
106: return attributeReader.hasNext();
107: }
108: }
|