001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.property;
017:
018: import java.io.BufferedReader;
019: import java.io.BufferedWriter;
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.FileWriter;
023: import java.io.FilenameFilter;
024: import java.io.IOException;
025:
026: import org.geotools.data.AbstractDataStore;
027: import org.geotools.data.DataSourceException;
028: import org.geotools.data.DataUtilities;
029: import org.geotools.data.FeatureReader;
030: import org.geotools.data.FeatureSource;
031: import org.geotools.data.FeatureWriter;
032: import org.geotools.feature.FeatureType;
033: import org.geotools.feature.SchemaException;
034:
035: /**
036: * Sample DataStore implementation, please see formal tutorial included
037: * with users docs.
038: *
039: * @author Jody Garnett, Refractions Research Inc.
040: */
041: public class PropertyDataStore extends AbstractDataStore {
042: protected File directory;
043: protected String namespaceURI;
044:
045: public PropertyDataStore(File dir) {
046: this (dir, null);
047: }
048:
049: public PropertyDataStore(File dir, String namespaceURI) {
050: if (!dir.isDirectory()) {
051: throw new IllegalArgumentException(dir
052: + " is not a directory");
053: }
054: if (namespaceURI == null) {
055: namespaceURI = dir.getName();
056: }
057: directory = dir;
058: this .namespaceURI = namespaceURI;
059: }
060:
061: public void setNamespaceURI(String namespaceURI) {
062: this .namespaceURI = namespaceURI;
063: }
064:
065: public String[] getTypeNames() {
066: String list[] = directory.list(new FilenameFilter() {
067: public boolean accept(File dir, String name) {
068: return name.endsWith(".properties");
069: }
070: });
071: for (int i = 0; i < list.length; i++) {
072: list[i] = list[i].substring(0, list[i].lastIndexOf('.'));
073: }
074: return list;
075: }
076:
077: // START SNIPPET: getSchema
078: public FeatureType getSchema(String typeName) throws IOException {
079: //look for type name
080: String typeSpec = property(typeName, "_");
081: try {
082: return DataUtilities.createType(namespaceURI + "."
083: + typeName, typeSpec);
084: } catch (SchemaException e) {
085: e.printStackTrace();
086: throw new DataSourceException(typeName
087: + " schema not available", e);
088: }
089: }
090:
091: // END SNIPPET: getSchema
092:
093: private String property(String typeName, String key)
094: throws IOException {
095: File file = new File(directory, typeName + ".properties");
096: BufferedReader reader = new BufferedReader(new FileReader(file));
097: try {
098: for (String line = reader.readLine(); line != null; line = reader
099: .readLine()) {
100: if (line.startsWith(key + "=")) {
101: return line.substring(key.length() + 1);
102: }
103: }
104: } finally {
105: reader.close();
106: }
107: return null;
108: }
109:
110: protected FeatureReader getFeatureReader(String typeName)
111: throws IOException {
112: return new PropertyFeatureReader(directory, typeName);
113: }
114:
115: protected FeatureWriter getFeatureWriter(String typeName)
116: throws IOException {
117: return new PropertyFeatureWriter(this , typeName);
118: }
119:
120: public void createSchema(FeatureType featureType)
121: throws IOException {
122: String typeName = featureType.getTypeName();
123: File file = new File(directory, typeName + ".properties");
124: BufferedWriter writer = new BufferedWriter(new FileWriter(file));
125: writer.write("_=");
126: writer.write(DataUtilities.spec(featureType));
127: writer.close();
128: }
129:
130: //
131: // Access to Optimiations
132: //
133: public FeatureSource getFeatureSource(final String typeName)
134: throws IOException {
135: return new PropertyFeatureSource(this, typeName);
136: }
137: }
|