01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2006-2006, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.catalog.shapefile;
17:
18: import java.net.MalformedURLException;
19: import java.net.URI;
20: import java.net.URISyntaxException;
21: import java.net.URL;
22: import java.util.HashMap;
23: import java.util.Map;
24:
25: import org.geotools.catalog.Catalog;
26: import org.geotools.catalog.Service;
27: import org.geotools.catalog.ServiceFactory;
28: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
29:
30: public class ShapefileServiceFactory implements ServiceFactory {
31:
32: private static ShapefileDataStoreFactory shpDSFactory = new ShapefileDataStoreFactory();
33:
34: public Service createService(Catalog parent, URI id, Map params) {
35: if (params.containsKey(ShapefileDataStoreFactory.URLP.key)) {
36: // shapefile ...
37:
38: URL url = null;
39: if (params.get(ShapefileDataStoreFactory.URLP.key) instanceof URL) {
40: url = (URL) params
41: .get(ShapefileDataStoreFactory.URLP.key);
42: } else {
43: try {
44: String surl = params.get(
45: ShapefileDataStoreFactory.URLP.key)
46: .toString();
47: url = (URL) ShapefileDataStoreFactory.URLP
48: .parse(surl);
49: params.put(ShapefileDataStoreFactory.URLP.key, url);
50: } catch (Throwable e) {
51: //TODO: log exception
52: return null;
53: }
54: }
55: if (!shpDSFactory.canProcess(url))
56: return null;
57:
58: if (id == null) {
59: try {
60: id = new URI(url.toExternalForm());
61: } catch (URISyntaxException e) {
62: }
63:
64: return new ShapefileService(parent, id, params);
65: }
66:
67: return new ShapefileService(parent, id, params);
68: }
69:
70: return null;
71: }
72:
73: public boolean canProcess(URI uri) {
74: try {
75: return shpDSFactory.canProcess(uri.toURL());
76: } catch (MalformedURLException e) {
77: return false;
78: }
79: }
80:
81: public Map createParams(URI uri) {
82: URL url = null;
83: try {
84: url = uri.toURL();
85: } catch (MalformedURLException e) {
86: return null;
87: }
88:
89: if (shpDSFactory.canProcess(url)) {
90: // shapefile
91: HashMap params = new HashMap();
92: params.put(ShapefileDataStoreFactory.URLP.key, url);
93: return params;
94: }
95: return null;
96: }
97: }
|