001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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: */
017: package net.refractions.udig.catalog.internal.shp;
018:
019: import java.io.File;
020: import java.io.Serializable;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import net.refractions.udig.catalog.AbstractDataStoreServiceExtension;
027: import net.refractions.udig.catalog.IService;
028: import net.refractions.udig.catalog.ServiceExtension;
029: import net.refractions.udig.catalog.shp.internal.Messages;
030:
031: import org.geotools.data.DataStoreFactorySpi;
032: import org.geotools.data.shapefile.indexed.IndexedShapefileDataStoreFactory;
033:
034: /**
035: * Service Extension implementation for Shapefiles.
036: *
037: * @author David Zwiers, Refractions Research
038: * @since 0.6
039: */
040: public class ShpServiceExtension extends
041: AbstractDataStoreServiceExtension implements ServiceExtension {
042:
043: private static IndexedShapefileDataStoreFactory shpDSFactory;
044:
045: public static IndexedShapefileDataStoreFactory getSHPDSFactory() {
046: if (shpDSFactory == null)
047: shpDSFactory = new IndexedShapefileDataStoreFactory();
048: return shpDSFactory;
049: }
050:
051: public IService createService(URL id,
052: Map<String, Serializable> params) {
053: if (params
054: .containsKey(IndexedShapefileDataStoreFactory.URLP.key)) {
055: // shapefile ...
056:
057: URL url = null;
058: if (params.get(IndexedShapefileDataStoreFactory.URLP.key) instanceof URL) {
059: url = (URL) params
060: .get(IndexedShapefileDataStoreFactory.URLP.key);
061: } else {
062: try {
063: url = (URL) IndexedShapefileDataStoreFactory.URLP
064: .parse(params
065: .get(
066: IndexedShapefileDataStoreFactory.URLP.key)
067: .toString());
068: params.put(
069: IndexedShapefileDataStoreFactory.URLP.key,
070: url);
071: } catch (Throwable e1) {
072: // log this?
073: e1.printStackTrace();
074: return null;
075: }
076: }
077: String file = url.getFile();
078: file = file.toLowerCase();
079: if (!(file.endsWith(".shp") || file.endsWith(".shx") || file.endsWith(".qix") //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
080: || file.endsWith(".dbf"))) //$NON-NLS-1$
081: return null;
082: if (id == null) {
083: return new ShpServiceImpl(url, params);
084: }
085: return new ShpServiceImpl(id, params);
086: }
087: return null;
088: }
089:
090: public Map<String, Serializable> createParams(URL url) {
091: URL url2 = url;
092: if (!isSupportedExtension(url))
093: return null;
094:
095: url2 = toShpURL(url2);
096: if (url2 == null)
097: return null;
098: if (getSHPDSFactory().canProcess(url2)) {
099: // shapefile
100: HashMap<String, Serializable> params = new HashMap<String, Serializable>();
101: params.put(IndexedShapefileDataStoreFactory.URLP.key, url2);
102: return params;
103: }
104: return null;
105: }
106:
107: private boolean isSupportedExtension(URL url) {
108: String file = url.getFile();
109: file = file.toLowerCase();
110:
111: return (file.endsWith(".shp") || file.endsWith(".shx") || file.endsWith(".qix") //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
112: || file.endsWith(".dbf")); //$NON-NLS-1$
113: }
114:
115: private URL toShpURL(URL url) {
116: URL url2 = url;
117:
118: String auth = url.getAuthority();
119: String urlFile = url2.getPath();
120: if (auth != null && !auth.equals("")) {
121: urlFile = "//" + auth + urlFile;
122: }
123: if (!urlFile.endsWith(".shp")) { //$NON-NLS-1$
124: urlFile = urlFile.substring(0, urlFile.lastIndexOf('.'))
125: + ".shp"; //$NON-NLS-1$
126: }
127: try {
128: File file = new File(urlFile);
129: url2 = file.toURL();
130: } catch (MalformedURLException e) {
131: return null;
132: }
133: return url2;
134: }
135:
136: @Override
137: protected String doOtherChecks(Map<String, Serializable> params) {
138: return null;
139: }
140:
141: @Override
142: protected DataStoreFactorySpi getDataStoreFactory() {
143: return getSHPDSFactory();
144: }
145:
146: public String reasonForFailure(URL url) {
147: if (!isSupportedExtension(url))
148: return Messages.ShpServiceExtension_badExtension;
149: if (toShpURL(url) == null)
150: return Messages.ShpServiceExtension_cantCreateURL;
151: return reasonForFailure(createParams(url));
152: }
153:
154: }
|