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.File;
020: import java.io.FilenameFilter;
021: import java.io.IOException;
022: import java.util.ArrayList;
023:
024: import org.geotools.data.AbstractDataStore;
025: import org.geotools.data.DataSourceException;
026: import org.geotools.data.DataUtilities;
027: import org.geotools.data.FeatureReader;
028: import org.geotools.feature.FeatureType;
029: import org.geotools.feature.SchemaException;
030:
031: /**
032: * <p>
033: * Title: GeoTools2 Development
034: * </p>
035: *
036: * <p>
037: * Description:
038: * </p>
039: *
040: * <p>
041: * Copyright: Copyright (c) 2003
042: * </p>
043: *
044: * <p>
045: * Company:
046: * </p>
047: *
048: * @author Julian J. Ray
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/tiger/src/main/java/org/geotools/data/tiger/TigerDataStore.java $
050: * @version 1.0
051: */
052: public class TigerDataStore extends AbstractDataStore {
053: /** DOCUMENT ME! */
054: protected File directory;
055:
056: /**
057: * Creates a new TigerDataStore object.
058: *
059: * @param dirName DOCUMENT ME!
060: *
061: * @throws IllegalArgumentException DOCUMENT ME!
062: */
063: public TigerDataStore(String dirName) {
064: this (new File(dirName));
065: }
066:
067: public TigerDataStore(File dir) {
068: // Do not allow writes
069: super (false);
070: directory = dir;
071:
072: if (!dir.exists() || !dir.isDirectory()) {
073: throw new IllegalArgumentException(directory
074: + " is not a directory!");
075: }
076:
077: }
078:
079: /**
080: * Returns a list of logical tiger files. This routine searches for tiger Type1 (RT1) files in the data store
081: * directory and returns the file names. File search is case insensetive so RT1 and rt1 work equally well.
082: *
083: * @return String[]
084: */
085: public String[] getTypeNames() {
086: String[] list = directory.list(new TigerFilenameFilter());
087: String[] schemaTypeNames = TigerSchemaManager.getTypeNames();
088:
089: ArrayList array = new ArrayList();
090:
091: for (int i = 0; i < list.length; i++) {
092: list[i] = list[i].substring(0, list[i].lastIndexOf('.'));
093:
094: for (int j = 0; j < schemaTypeNames.length; j++) {
095: array.add(list[i] + "_" + schemaTypeNames[j]);
096: }
097: }
098:
099: String[] typeNames = new String[array.size()];
100:
101: for (int i = 0; i < array.size(); i++) {
102: typeNames[i] = (String) array.get(i);
103: }
104:
105: return typeNames;
106: }
107:
108: /**
109: * getSchema
110: *
111: * @param typeName String
112: *
113: * @return FeatureType
114: *
115: * @throws IOException
116: * @throws DataSourceException DOCUMENT ME!
117: */
118: public FeatureType getSchema(String typeName) throws IOException {
119: try {
120: TigerSchemaManager manager = new TigerSchemaManager();
121:
122: FeatureType featureType = DataUtilities.createType(
123: directory.getName() + "." + typeName, manager
124: .getTypeSpec(typeName));
125:
126: return featureType;
127: } catch (SchemaException e) {
128: e.printStackTrace();
129: throw new DataSourceException(typeName
130: + " schema format error", e);
131: }
132: }
133:
134: /**
135: * getFeatureReader
136: *
137: * @param typeName String
138: *
139: * @return FeatureReader
140: *
141: * @throws IOException
142: */
143: protected FeatureReader getFeatureReader(String typeName)
144: throws IOException {
145: return new TigerFeatureReader(directory, typeName);
146: }
147:
148: /**
149: * <p>
150: * Title: GeoTools2 Development
151: * </p>
152: *
153: * <p>
154: * Description:
155: * </p>
156: *
157: * <p>
158: * Copyright: Copyright (c) 2003
159: * </p>
160: *
161: * <p>
162: * Company:
163: * </p>
164: *
165: * @author Julian J. Ray
166: * @version 1.0
167: */
168: private class TigerFilenameFilter implements FilenameFilter {
169: //~ Constructors -----------------------------------------------------------------------------------------------
170:
171: /**
172: * Creates a new TigerFilenameFilter object.
173: */
174: TigerFilenameFilter() {
175: }
176:
177: //~ Methods ----------------------------------------------------------------------------------------------------
178:
179: /**
180: * DOCUMENT ME!
181: *
182: * @param dir DOCUMENT ME!
183: * @param name DOCUMENT ME!
184: *
185: * @return DOCUMENT ME!
186: */
187: public boolean accept(File dir, String name) {
188: if (name.endsWith("RT1") || name.endsWith("rt1")) {
189: return true;
190: } else {
191: return false;
192: }
193: }
194: }
195: }
|