001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2003-2004, Julian J. Ray, All Rights Reserved
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.data.tiger;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.util.NoSuchElementException;
024:
025: import org.geotools.data.AttributeReader;
026: import org.geotools.data.DataSourceException;
027: import org.geotools.data.DataUtilities;
028: import org.geotools.feature.AttributeType;
029: import org.geotools.feature.FeatureType;
030: import org.geotools.feature.SchemaException;
031:
032: /**
033: * <p>
034: * Title: GeoTools2 Development
035: * </p>
036: *
037: * <p>
038: * Description:
039: * </p>
040: *
041: * <p>
042: * Copyright: Copyright (c) 2003
043: * </p>
044: *
045: * <p>
046: * Company:
047: * </p>
048: *
049: * @author Julian J. Ray
050: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/tiger/src/main/java/org/geotools/data/tiger/TigerAttributeReader.java $
051: * @version 1.0
052: */
053: public class TigerAttributeReader implements AttributeReader {
054: /** DOCUMENT ME! */
055: private BufferedReader rt1Reader;
056:
057: /** DOCUMENT ME! */
058: private BufferedReader rt2Reader;
059:
060: /** DOCUMENT ME! */
061: private FeatureType featureType;
062:
063: /** DOCUMENT ME! */
064: private String currentLine;
065:
066: /** DOCUMENT ME! */
067: private String nextLine;
068:
069: /** DOCUMENT ME! */
070: private TigerSchemaManager schemaManager;
071:
072: /** DOCUMENT ME! */
073: private TigerGeometryAdapter geometryAdapter;
074:
075: /** DOCUMENT ME! */
076: private String typeName;
077:
078: /** DOCUMENT ME! */
079: private String namespace;
080:
081: /** DOCUMENT ME! */
082: private String typeKey;
083:
084: /**
085: * TigerAttributeReader
086: *
087: * @param file File
088: * @param namespace String
089: * @param typeName String
090: *
091: * @throws IOException
092: * @throws DataSourceException DOCUMENT ME!
093: */
094: public TigerAttributeReader(File file, String namespace,
095: String typeName) throws IOException {
096: this .schemaManager = new TigerSchemaManager();
097: this .typeName = typeName;
098: this .namespace = namespace;
099: this .typeKey = schemaManager.getTypeKey(typeName);
100:
101: String fileName1 = file.getPath();
102: String fileName = fileName1.substring(0, fileName1
103: .lastIndexOf("1"))
104: + "2";
105:
106: rt1Reader = new BufferedReader(new FileReader(file)); // RT1 records
107: rt2Reader = new BufferedReader(new FileReader(
108: new File(fileName))); // RT2 records
109:
110: geometryAdapter = new TigerGeometryAdapter(rt2Reader);
111:
112: String typeSpec = schemaManager.getTypeSpec(typeName);
113:
114: try {
115: featureType = DataUtilities.createType(this .typeName,
116: typeSpec);
117: } catch (SchemaException e) {
118: throw new DataSourceException(this .typeName
119: + " schema not available", e);
120: }
121: }
122:
123: /**
124: * getFeatureType
125: *
126: * @return FeatureType
127: */
128: public FeatureType getFeatureType() {
129: return featureType;
130: }
131:
132: /**
133: * getTypeName
134: *
135: * @param file File
136: *
137: * @return String
138: */
139: private static String getTypeName(File file) {
140: String name = file.getName();
141: int split = name.lastIndexOf('.');
142:
143: return (split == -1) ? name : name.substring(split);
144: }
145:
146: /**
147: * getNamespace
148: *
149: * @param file File
150: *
151: * @return String
152: */
153: private static String getNamespace(File file) {
154: String name = file.getName();
155: int split = name.lastIndexOf('.');
156:
157: return (split == -1) ? name : name.substring(split);
158: }
159:
160: /**
161: * getAttributeCount
162: *
163: * @return int
164: */
165: public int getAttributeCount() {
166: return featureType.getAttributeCount();
167: }
168:
169: /**
170: * getAttributeType
171: *
172: * @param index int
173: *
174: * @return AttributeType
175: *
176: * @throws ArrayIndexOutOfBoundsException
177: */
178: public AttributeType getAttributeType(int index)
179: throws ArrayIndexOutOfBoundsException {
180: return featureType.getAttributeType(index);
181: }
182:
183: /**
184: * close
185: *
186: * @throws IOException
187: */
188: public void close() throws IOException {
189: rt1Reader.close();
190: rt1Reader = null;
191:
192: // Close the shape points reader as well
193: rt2Reader.close();
194: rt2Reader = null;
195: }
196:
197: /**
198: * hasNext
199: *
200: * @return boolean
201: *
202: * @throws IOException
203: */
204: public boolean hasNext() throws IOException {
205: if (nextLine != null) {
206: return true;
207: }
208:
209: while (true) {
210: // We keep reading from the file until we either hit the end (null is returned) or we find a record with
211: // the correct key type
212: nextLine = rt1Reader.readLine();
213:
214: if (nextLine == null) {
215: break;
216: }
217:
218: if (isKeyTypeRecord()) {
219: break;
220: }
221: }
222:
223: return nextLine != null;
224: }
225:
226: /**
227: * next
228: *
229: * @throws IOException
230: * @throws NoSuchElementException DOCUMENT ME!
231: */
232: public void next() throws IOException {
233: if (hasNext()) {
234: currentLine = nextLine;
235: nextLine = null;
236: } else {
237: throw new NoSuchElementException();
238: }
239: }
240:
241: /**
242: * DOCUMENT ME!
243: *
244: * @return DOCUMENT ME!
245: */
246: private boolean isKeyTypeRecord() {
247: if (nextLine == null) {
248: return false;
249: } else {
250: return nextLine.substring(55, 56).equals(typeKey);
251: }
252: }
253:
254: /**
255: * DOCUMENT ME!
256: *
257: * @param index DOCUMENT ME!
258: *
259: * @return DOCUMENT ME!
260: *
261: * @throws IOException DOCUMENT ME!
262: * @throws ArrayIndexOutOfBoundsException DOCUMENT ME!
263: */
264: public Object read(int index) throws IOException,
265: ArrayIndexOutOfBoundsException {
266: if (currentLine == null) {
267: throw new IOException("No data available.");
268: }
269:
270: TigerSchemaElement[] elements = schemaManager
271: .getSchema(typeName);
272:
273: if ((index < 0) || (index >= elements.length)) {
274: throw new ArrayIndexOutOfBoundsException();
275: }
276:
277: // Process geometry
278: if (elements[index].getClassType() == "Geometry") {
279: Object geom = geometryAdapter.deSerialize(getFeatureID(),
280: currentLine);
281:
282: return featureType.getAttributeType(index).parse(geom);
283: } else {
284: String elemData = currentLine.substring(
285: elements[index].getStartPos(),
286: elements[index].getEndPos()).trim();
287:
288: // Always return a null if the string is 0 length
289: if (elemData.length() == 0) {
290: return featureType.getAttributeType(index).parse(null);
291: }
292:
293: if (elements[index].getClassType() == "String") {
294: return featureType.getAttributeType(index).parse(
295: elemData);
296: } else if (elements[index].getClassType() == "Integer") {
297: Integer ival = new Integer(elemData.trim());
298:
299: return featureType.getAttributeType(index).parse(ival);
300: } else {
301: return featureType.getAttributeType(index).parse(
302: elemData);
303: }
304: }
305: }
306:
307: /**
308: * DOCUMENT ME!
309: *
310: * @return DOCUMENT ME!
311: */
312: public String getFeatureID() {
313: if (currentLine == null) {
314: return null;
315: }
316:
317: return currentLine.substring(5, 15).trim();
318: }
319: }
|