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.dir;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.URL;
021: import java.util.Map;
022: import java.util.Collections;
023:
024: import org.geotools.data.DataStore;
025: import org.geotools.data.DataStoreFactorySpi;
026:
027: /**
028: * Creates a Directory DataStore following the DataStoreFactorySpi interface.
029: *
030: * @author David Zwiers, Refractions Research, Inc.
031: *
032: * @see DataStoreFactorySpi
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/directory/src/main/java/org/geotools/data/dir/DirectoryDataStoreFactory.java $
034: */
035: public class DirectoryDataStoreFactory implements DataStoreFactorySpi {
036: /** The Directory parameter which should contain some files to read */
037: public static final Param DIRECTORY = new Param(
038: "data_directory url", URL.class,
039: "Directory containing geospatial vector files", true);
040:
041: /**
042: * The suffix parameter to specify the order of creation for new
043: * featureTypes
044: */
045: public static final Param CREATE_SUFFIX_ORDER = new Param(
046: "suffix_list",
047: String[].class,
048: "space delimited list of prefixes to attempt and create new files for.",
049: true) {
050:
051: /**
052: * Overide text to unwap the array of strings back into a space
053: * limited text string.
054: * TODO: (JD) perhaps this should be done by param? I guess we would have to
055: * agree on the delimiter for collection or array based parameters.
056: */
057: public String text(Object value) {
058: if (value instanceof String[]) {
059: String[] values = (String[]) value;
060: StringBuffer sb = new StringBuffer();
061: for (int i = 0; i < values.length; i++) {
062: sb.append(values[i] + " ");
063: }
064: sb.setLength(sb.length() - 1);
065: return sb.toString();
066: }
067:
068: return super .text(value);
069: }
070: };
071:
072: /**
073: * @see org.geotools.data.DataStoreFactorySpi#createDataStore(java.util.Map)
074: */
075: public DataStore createDataStore(Map params) throws IOException {
076: if (!canProcess(params)) {
077: throw new IOException(
078: "Invalid parameters for DirectoryDataStore");
079: }
080:
081: URL url = (URL) DIRECTORY.lookUp(params);
082: File f = new File(url.getFile());
083: String[] strs = (String[]) CREATE_SUFFIX_ORDER.lookUp(params);
084:
085: if (strs == null) {
086: throw new IOException("Invalid parameter "
087: + CREATE_SUFFIX_ORDER.key + " : is null");
088: }
089:
090: if (!f.isDirectory()) {
091: throw new IOException("Invalid parameter " + DIRECTORY.key
092: + " : is not a valid directory");
093: }
094:
095: return new DirectoryDataStore(f, strs);
096: }
097:
098: // /**
099: // * @see org.geotools.data.DataStoreFactorySpi#createMetadata(java.util.Map)
100: // */
101: // public DataSourceMetadataEnity createMetadata(Map params)
102: // throws IOException {
103: // File url = (File) DIRECTORY.lookUp(params);
104: // String parent = url.getParent();
105: // String name = url.getName();
106: //
107: // return new DataSourceMetadataEnity(parent, name,
108: // "Access to Directory " + url.toString());
109: // }
110:
111: /**
112: * @see org.geotools.data.DataStoreFactorySpi#createNewDataStore(java.util.Map)
113: */
114: public DataStore createNewDataStore(Map params) throws IOException {
115: if (!canProcess(params)) {
116: throw new IOException(
117: "Invalid parameters for DirectoryDataStore");
118: }
119:
120: URL url = (URL) DIRECTORY.lookUp(params);
121: File f = new File(url.getFile());
122:
123: if (f.exists()) {
124: throw new IOException("Invalid parameter " + DIRECTORY.key
125: + " : directory already exists");
126: }
127:
128: if (!f.isDirectory()) {
129: throw new IOException("Invalid parameter " + DIRECTORY.key
130: + " : is not a valid directory");
131: }
132:
133: if (!f.createNewFile()) {
134: throw new IOException("Invalid parameter " + DIRECTORY.key
135: + " : cannot create directory");
136: }
137:
138: String[] strs = (String[]) CREATE_SUFFIX_ORDER.lookUp(params);
139:
140: if (strs == null) {
141: throw new IOException("Invalid parameter "
142: + CREATE_SUFFIX_ORDER.key + " : is null");
143: }
144:
145: return new DirectoryDataStore(f, strs);
146: }
147:
148: /**
149: * @see org.geotools.data.DataStoreFactorySpi#getDisplayName()
150: */
151: public String getDisplayName() {
152: return "Directory DataStore";
153: }
154:
155: /**
156: * @see org.geotools.data.DataStoreFactorySpi#getDescription()
157: */
158: public String getDescription() {
159: return "Propagates to multiple file types representing a directory";
160: }
161:
162: /**
163: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
164: */
165: public Param[] getParametersInfo() {
166: return new Param[] { DIRECTORY, CREATE_SUFFIX_ORDER };
167: }
168:
169: /**
170: * @see org.geotools.data.DataStoreFactorySpi#canProcess(java.util.Map)
171: */
172: public boolean canProcess(Map params) {
173: try {
174: URL url = (URL) DIRECTORY.lookUp(params);
175: File f = new File(url.getFile());
176: String[] str = (String[]) CREATE_SUFFIX_ORDER
177: .lookUp(params);
178:
179: //return ((f != null) && (str != null) && f.isDirectory());
180: return (f != null) && (str != null);
181: } catch (Exception e) {
182: return false;
183: }
184: }
185:
186: /**
187: * @see org.geotools.data.DataStoreFactorySpi#isAvailable()
188: */
189: public boolean isAvailable() {
190: return true;
191: }
192:
193: /**
194: * Returns the implementation hints. The default implementation returns en empty map.
195: */
196: public Map getImplementationHints() {
197: return Collections.EMPTY_MAP;
198: }
199: }
|