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.google;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URI;
022: import java.net.URL;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import net.refractions.udig.catalog.CatalogPlugin;
027: import net.refractions.udig.catalog.IGeoResource;
028: import net.refractions.udig.catalog.IGeoResourceInfo;
029: import net.refractions.udig.catalog.IResolveChangeEvent;
030: import net.refractions.udig.catalog.IResolveDelta;
031: import net.refractions.udig.catalog.IService;
032: import net.refractions.udig.catalog.internal.CatalogImpl;
033: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
034: import net.refractions.udig.catalog.internal.ResolveDelta;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037: import org.eclipse.jface.resource.ImageDescriptor;
038: import org.geotools.data.wfs.WFSDataStore;
039: import org.geotools.data.wms.WebMapServer;
040: import org.geotools.data.wms.xml.WMSSchema;
041: import org.geotools.xml.wfs.WFSSchema;
042:
043: import com.vividsolutions.jts.geom.Envelope;
044:
045: /**
046: * summary sentence.
047: * <p>
048: * Paragraph ...
049: * </p><p>
050: * Responsibilities:
051: * <ul>
052: * <li>
053: * <li>
054: * </ul>
055: * </p><p>
056: * Example:<pre><code>
057: * GoogleResource x = new GoogleResource( ... );
058: * TODO code example
059: * </code></pre>
060: * </p>
061: * @author David Zwiers, Refractions Research
062: */
063: abstract class GoogleResource extends IGeoResource {
064:
065: public static GoogleResource getResource(OGCLayer layer) {
066: if (layer.getServertype().equalsIgnoreCase("WMS")) { //$NON-NLS-1$
067: return getWMSResource(layer);
068: }
069: if (layer.getServertype().equalsIgnoreCase("WFS")) { //$NON-NLS-1$
070: return getWFSResource(layer);
071: }
072: return null;
073: }
074:
075: public static GoogleResource getWMSResource(OGCLayer layer) {
076: return new GoogleWMSResource(layer);
077: }
078:
079: public static GoogleResource getWFSResource(OGCLayer layer) {
080: return new GoogleWFSResource(layer);
081: }
082:
083: protected Throwable msg = null;
084: protected IGeoResourceInfo info = null;
085: private URL id = null;
086:
087: public GoogleResource(OGCLayer layer) {
088: id = layer.getId();
089: info = new IGeoResourceInfo(layer.getTitle(), layer.getName(),
090: layer.getDescription(), getSchema(), new Envelope(),
091: null, new String[] { layer.getServertype(),
092: layer.getName(), layer.getTitle() }, getIcon());
093: }
094:
095: protected abstract URI getSchema();
096:
097: protected abstract ImageDescriptor getIcon();
098:
099: /*
100: * Required adaptions:
101: * <ul>
102: * <li>IGeoResourceInfo.class
103: * <li>IService.class
104: * </ul>
105: *
106: * @see net.refractions.udig.catalog.IResolve#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
107: */
108: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
109: throws IOException {
110: if (adaptee == null)
111: return null;
112:
113: if (real != null)
114: return real.resolve(adaptee, monitor);
115:
116: if (adaptee.isAssignableFrom(IGeoResourceInfo.class))
117: return adaptee.cast(this .getInfo(monitor));
118:
119: loadReal(monitor);
120: if (real != null)
121: return real.resolve(adaptee, monitor);
122: return super .resolve(adaptee, monitor);
123: }
124:
125: public IService service(IProgressMonitor monitor)
126: throws IOException {
127: if (service == null)
128: loadReal(monitor);
129: //System.out.println("SERVICE == NULL? "+(service==null));
130: if (service != null)
131: return service;
132: return null;
133: }
134:
135: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
136: throws IOException {
137: if (real != null)
138: return real.getInfo(monitor);
139: return info;
140: }
141:
142: protected IService service = null;
143: protected IGeoResource real = null;
144:
145: protected abstract void loadReal(IProgressMonitor monitor)
146: throws IOException;
147:
148: /*
149: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
150: */
151: public <T> boolean canResolve(Class<T> adaptee) {
152: if (adaptee == null)
153: return false;
154: return (adaptee.isAssignableFrom(IGeoResourceInfo.class) || adaptee
155: .isAssignableFrom(IService.class))
156: || super .canResolve(adaptee);
157: }
158:
159: /*
160: * @see net.refractions.udig.catalog.IResolve#getStatus()
161: */
162: public Status getStatus() {
163: return msg == null ? Status.CONNECTED : Status.BROKEN;
164: }
165:
166: /*
167: * @see net.refractions.udig.catalog.IResolve#getMessage()
168: */
169: public Throwable getMessage() {
170: return msg;
171: }
172:
173: /*
174: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
175: */
176: public URL getIdentifier() {
177: if (real != null)
178: return real.getIdentifier();
179: return id;
180: }
181:
182: static class GoogleWMSResource extends GoogleResource {
183:
184: /**
185: * Construct <code>GoogleWMSResource</code>.
186: *
187: * @param layer
188: * @throws MalformedURLException
189: */
190: public GoogleWMSResource(OGCLayer layer) {
191: super (layer);
192: url = layer.getOnlineresource();
193: }
194:
195: private URL url = null;
196:
197: /*
198: * @see net.refractions.udig.catalog.google.GoogleResource#getSchema()
199: */
200: protected URI getSchema() {
201: return WMSSchema.NAMESPACE;
202: }
203:
204: /*
205: * @see net.refractions.udig.catalog.google.GoogleResource#getIcon()
206: */
207: protected ImageDescriptor getIcon() {
208: return null;
209: }
210:
211: @Override
212: public <T> boolean canResolve(Class<T> adaptee) {
213: if (adaptee == null)
214: return false;
215: return super .canResolve(adaptee)
216: || adaptee.isAssignableFrom(WebMapServer.class)
217: || adaptee
218: .isAssignableFrom(org.geotools.data.ows.Layer.class)
219: || super .canResolve(adaptee);
220:
221: }
222:
223: /*
224: * @see net.refractions.udig.catalog.google.GoogleResource#loadReal(org.eclipse.core.runtime.IProgressMonitor)
225: */
226: protected void loadReal(IProgressMonitor monitor)
227: throws IOException {
228: getInfo(monitor); // load me
229: if (info == null || url == null || info.getName() == null)
230: return;
231:
232: URL id = getIdentifier();
233: if (id == null)
234: return;
235:
236: if (service == null) {
237: List<IService> services = CatalogPlugin.getDefault()
238: .getServiceFactory().acquire(url);
239: Iterator<IService> srv = services.iterator();
240: while (srv.hasNext()) {
241: IService s = srv.next();
242: if (s.canResolve(WebMapServer.class)) {
243: service = s;
244: }
245: }
246:
247: if (service == null) {
248: return;
249: }
250: }
251: if (real == null) {
252: CatalogPlugin.getDefault().getLocalCatalog().add(
253: service);
254: // search for the child
255: List<? extends IGeoResource> resL = service
256: .members(monitor);
257: if (resL == null) {
258: return;
259: }
260: Iterator<? extends IGeoResource> res = resL.iterator();
261: while (res.hasNext() && real == null) {
262: IGeoResource restmp = res.next();
263: if (restmp != null
264: && restmp.getIdentifier() != null
265: && info.getName().equals(
266: restmp.getIdentifier().getRef()))
267: real = restmp;
268: }
269: }
270: IResolveDelta delta = new ResolveDelta(this , real, null);
271: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
272: .fire(new ResolveChangeEvent(this ,
273: IResolveChangeEvent.Type.POST_CHANGE, delta));
274: }
275: }
276:
277: static class GoogleWFSResource extends GoogleResource {
278:
279: /**
280: * Construct <code>GoogleWMSResource</code>.
281: *
282: * @param layer
283: * @throws MalformedURLException
284: */
285: public GoogleWFSResource(OGCLayer layer) {
286: super (layer);
287: url = layer.getOnlineresource();
288: }
289:
290: private URL url = null;
291:
292: /*
293: * @see net.refractions.udig.catalog.google.GoogleResource#getSchema()
294: */
295: protected URI getSchema() {
296: return WFSSchema.NAMESPACE;
297: }
298:
299: /*
300: * @see net.refractions.udig.catalog.google.GoogleResource#getIcon()
301: */
302: protected ImageDescriptor getIcon() {
303: return null;
304: }
305:
306: /*
307: * @see net.refractions.udig.catalog.google.GoogleResource#loadReal(org.eclipse.core.runtime.IProgressMonitor)
308: */
309: protected void loadReal(IProgressMonitor monitor)
310: throws IOException {
311: getInfo(monitor); // load me
312: if (info == null || url == null || info.getName() == null)
313: return;
314:
315: URL id = getIdentifier();
316: if (id == null)
317: return;
318:
319: if (service == null) {
320:
321: List<IService> services = CatalogPlugin.getDefault()
322: .getServiceFactory().acquire(url);
323: Iterator<IService> srv = services.iterator();
324: while (srv.hasNext()) {
325: IService s = srv.next();
326: if (s.canResolve(WFSDataStore.class)) {
327: service = s;
328: }
329: }
330:
331: if (service == null) {
332: return;
333: }
334: }
335:
336: if (real == null) {
337: // search for the child
338: List<? extends IGeoResource> resL = service
339: .members(monitor);
340: if (resL == null)
341: return;
342: Iterator<? extends IGeoResource> res = resL.iterator();
343: while (res.hasNext() && real == null) {
344: IGeoResource restmp = res.next();
345: if (restmp != null
346: && restmp.getIdentifier() != null
347: && info.getName().equals(
348: restmp.getIdentifier().getRef()))
349: real = restmp;
350: }
351: }
352: IResolveDelta delta = new ResolveDelta(this , real, null);
353: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
354: .fire(new ResolveChangeEvent(this,
355: IResolveChangeEvent.Type.POST_CHANGE, delta));
356: }
357: }
358:
359: }
|