001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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; either
009: * version 2.1 of the License, or (at your option) any later version.
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.vpf;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.logging.Logger;
021: import java.util.Collections;
022: import java.util.Map;
023: import java.net.URL;
024: import java.net.URI;
025:
026: import org.geotools.data.DataStore;
027: import org.geotools.data.DataStoreFactorySpi;
028: import org.geotools.data.vpf.ifc.FileConstants;
029: import org.geotools.feature.SchemaException;
030:
031: /**
032: * Class VPFDataSourceFactory.java is responsible for constructing appropriate
033: * VPFDataStore (actually VPFLibrary) objects.
034: * VPFDataStoreFactory
035: * - factory for VPFLibrary
036: * - factory for VPFCoverage
037: * - factory for VPFFeatureClass
038: * - implements FeatureType by delegation to contained DefaultFeatureType
039: * - contains VPFFiles
040: * - retrieves VPFColumns from VPFFiles for use in constructing DefaultFeatureType
041: * - contains joins (column pairs)
042: * - factory for VPFFeatureType
043: * - implements FeatureType by delegation to contained VPFFeatureClass
044: *
045: * VPFFile
046: * - contains VPFInputStream
047: * - factory for VPFColumn
048: * - implements AttributeType by delegation to contained DefaultFeatureType
049: * <p>
050: * Created: Fri Mar 28 15:54:32 2003
051: * </p>
052: *
053: * @author <a href="mailto:kobit@users.sourceforge.net">Artur Hefczyc</a>
054: * @author <a href="mailto:knuterik@onemap.org">Knut-Erik Johnsen</a>, Project OneMap
055: * @author Chris Holmes, Fulbright
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/vpf/src/main/java/org/geotools/data/vpf/VPFDataStoreFactory.java $
057: * @version 2.1.0
058: */
059: public class VPFDataStoreFactory implements DataStoreFactorySpi {
060: /** The logger for the vpf module. */
061: protected static final Logger LOGGER = org.geotools.util.logging.Logging
062: .getLogger("org.geotools.data.vpf");
063:
064: /**
065: * Default Constructor
066: *
067: */
068: public VPFDataStoreFactory() {
069: }
070:
071: /*
072: * (non-Javadoc)
073: * @see org.geotools.data.DataStoreFactorySpi#getDisplayName()
074: */
075: public String getDisplayName() {
076: return "Vector Product Format Library";
077: }
078:
079: /*
080: * (non-Javadoc)
081: * @see org.geotools.data.DataStoreFactorySpi#getDescription()
082: */
083: public String getDescription() {
084: return "Vector Product Format Library data store implementation.";
085: }
086:
087: /*
088: * (non-Javadoc)
089: * @see org.geotools.data.DataStoreFactorySpi#canProcess(java.util.Map)
090: */
091: public boolean canProcess(Map params) {
092: boolean result = false;
093: try {
094:
095: File file = getLhtFile(params);
096: //if getLhtFile didn't throw an exception then we're good.
097: result = true;
098:
099: } catch (IOException exc) {
100: //catch io exception, false will return
101: }
102: return result;
103: }
104:
105: /*
106: * (non-Javadoc)
107: * @see org.geotools.data.DataStoreFactorySpi#createDataStore(java.util.Map)
108: */
109: public DataStore createDataStore(Map params) throws IOException {
110: return create(params);
111: }
112:
113: /**
114: * Creates a data store.
115: * @param params A <code>Map</code> of parameters which must be verified and
116: * @throws IOException
117: */
118: private DataStore create(Map params) throws IOException {
119: DataStore result = null;
120: File file = getLhtFile(params);
121: //CH I'd like to check existence here, so that geoserver can get
122: //a better error message, but I've spent way too long on this, so
123: //I'm giving up for now. Ideally canProcess just figures out if
124: //the params are valid, and doesn't throw the not existing error, but
125: //since we need a directory, not the actual file, it's hard to check
126: //for anything.
127: if (!file.exists() || !file.canRead()) {
128: throw new IOException(
129: "File either doesn't exist or is unreadable : "
130: + file);
131: }
132: URI namespace = (URI) NAMESPACEP.lookUp(params); //null if not exist
133: LOGGER.finer("creating new vpf datastore with params: "
134: + params);
135: try {
136: result = new VPFLibrary(file, namespace);
137: } catch (SchemaException exc) {
138: throw new IOException("There was a problem making one of "
139: + "the feature classes as a FeatureType.");
140: }
141:
142: return result;
143: }
144:
145: /*
146: * private method to get the lht file from the map of params, to avoid
147: * code duplication in canProcess and create, since they both need the
148: * file - canProcess just returns true if it's there, and eats the
149: * exception, create makes the store.
150: */
151: private File getLhtFile(Map params) throws IOException {
152: URL url = (URL) DIR.lookUp(params);
153: File file = null;
154: if (url.getProtocol().equals("file")) {
155:
156: if (url.getHost() != null && !url.getHost().equals("")) {
157: //win
158: file = new File(url.getHost() + ":" + url.getFile());
159: } else {
160: //linux
161: file = new File(url.getFile());
162: }
163: File lhtFile;
164: if (file.isDirectory()) {
165: lhtFile = new File(file,
166: FileConstants.LIBRARY_HEADER_TABLE);
167: } else {
168: lhtFile = file;
169: if (!lhtFile.getName().equalsIgnoreCase(
170: FileConstants.LIBRARY_HEADER_TABLE)) {
171: throw new IOException("File: " + file
172: + "is not a lht file");
173: }
174: }
175:
176: } else {
177: throw new IOException("only file protocol supported");
178: }
179: return file;
180: }
181:
182: /*
183: *
184: * (non-Javadoc)
185: * @see org.geotools.data.DataStoreFactorySpi#createNewDataStore(java.util.Map)
186: */
187: public DataStore createNewDataStore(Map params)
188: throws java.io.IOException {
189: return create(params);
190: }
191:
192: /**
193: * A parameter which is the directory containing the LHT file
194: */
195: public static final Param DIR = new Param("url", URL.class,
196: "Directory containing lht file", true);
197:
198: public static final Param NAMESPACEP = new Param("namespace",
199: URI.class, "uri to a the namespace", false); //not required
200:
201: /*
202: * (non-Javadoc)
203: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
204: */
205: public Param[] getParametersInfo() {
206: return new Param[] { DIR, };
207: }
208:
209: /*
210: * (non-Javadoc)
211: * @see org.geotools.data.DataStoreFactorySpi#isAvailable()
212: */
213: public boolean isAvailable() {
214: return true;
215: }
216:
217: /**
218: * Returns the implementation hints. The default implementation returns en empty map.
219: */
220: public Map getImplementationHints() {
221: return Collections.EMPTY_MAP;
222: }
223: }
|