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.gml;
018:
019: import java.io.Serializable;
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import net.refractions.udig.catalog.AbstractDataStoreServiceExtension;
025: import net.refractions.udig.catalog.IService;
026: import net.refractions.udig.catalog.ServiceExtension;
027: import net.refractions.udig.catalog.gml.internal.Messages;
028:
029: import org.geotools.data.DataStoreFactorySpi;
030: import org.geotools.data.DataStoreFactorySpi.Param;
031: import org.geotools.data.gml.GMLDataStoreFactory;
032:
033: /**
034: * GML Service Extension implementation.
035: *
036: * @author David Zwiers, Refractions Research
037: * @since 0.6
038: */
039: public class GMLServiceExtension extends
040: AbstractDataStoreServiceExtension implements ServiceExtension {
041:
042: private static Param URLP;
043: private static GMLDataStoreFactory gmlDSFactory;
044:
045: public static GMLDataStoreFactory getGMLDSFactory() {
046: if (gmlDSFactory == null)
047: gmlDSFactory = new GMLDataStoreFactory();
048: return gmlDSFactory;
049: }
050:
051: /**
052: * Returns the GMLDataStoreFactory URL Param
053: *
054: * @return
055: */
056: public static Param getURLP() {
057: if (URLP == null) {
058: URLP = getGMLDSFactory().getParametersInfo()[0];
059: }
060: return URLP;
061: }
062:
063: public IService createService(URL id,
064: Map<String, Serializable> params) {
065: if (params.containsKey(getURLP().key)) {
066: URL url = null;
067: try {
068: if (params.get(getURLP().key) instanceof String) {
069: url = new URL((String) params.get(getURLP().key));
070: params.put(getURLP().key, url);
071: } else {
072: url = (URL) getURLP().parse(
073: params.get(getURLP().key).toString());
074: params.put(getURLP().key, url);
075: }
076: } catch (Throwable e1) {
077: GmlPlugin.log("", e1); //$NON-NLS-1$
078: return null;
079: }
080:
081: // shapefile ...
082: try {
083: if (!canProcess((URL) params.get(getURLP().key)))
084: return null;
085: } catch (Exception e) {
086: return null;
087: }
088:
089: if (id == null) {
090: return new GMLServiceImpl(url, params);
091: }
092: return new GMLServiceImpl(id, params);
093: }
094: return null;
095: }
096:
097: public Map<String, Serializable> createParams(URL url) {
098: if (getGMLDSFactory().canProcess(url)) {
099: // shapefile
100: HashMap<String, Serializable> params = new HashMap<String, Serializable>();
101: params.put(GMLDataStoreFactory.URLP.key, url);
102: params.put(GMLDataStoreFactory.TIMEOUT.key, 20000);
103: return params;
104: }
105: return null;
106: }
107:
108: private boolean canProcess(URL id) {
109: if (id == null) {
110: return false;
111: }
112:
113: return id.toExternalForm().toUpperCase().endsWith(".GML"); //$NON-NLS-1$
114: }
115:
116: @Override
117: protected String doOtherChecks(Map<String, Serializable> params) {
118: return null;
119: }
120:
121: @Override
122: protected DataStoreFactorySpi getDataStoreFactory() {
123: return getGMLDSFactory();
124: }
125:
126: public String reasonForFailure(URL url) {
127: if (!canProcess(url)) {
128: return Messages.GMLServiceExtension_notGMLExt;
129: }
130: return reasonForFailure(createParams(url));
131: }
132:
133: }
|