001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.catalog.internal.worldimage;
010:
011: import java.io.File;
012: import java.io.Serializable;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: import net.refractions.udig.catalog.IService;
019: import net.refractions.udig.catalog.ServiceExtension2;
020: import net.refractions.udig.catalog.URLUtils;
021: import net.refractions.udig.catalog.worldimage.internal.Messages;
022:
023: import org.geotools.gce.image.WorldImageFormat;
024: import org.geotools.gce.image.WorldImageFormatFactory;
025:
026: /**
027: * Provides the interface to the catalog service extension point.
028: * <p>
029: * This class is responsible for ensuring that only those services that the
030: * WorldImage plugin is capable of processing are created.
031: * </p>
032: * @author mleslie
033: * @since 0.6.0
034: */
035: public class WorldImageServiceExtension implements ServiceExtension2 {
036: /** <code>URL_PARAM</code> field */
037: public final static String URL_PARAM = "URL"; //$NON-NLS-1$
038:
039: private static WorldImageFormatFactory factory;
040:
041: /**
042: * Construct <code>WorldImageServiceExtension</code>.
043: *
044: */
045: public WorldImageServiceExtension() {
046: super ();
047: }
048:
049: public IService createService(URL id,
050: Map<String, Serializable> params) {
051: URL id2 = id;
052: // if( !canProcess(id))
053: // return null;
054:
055: if (id2 == null) {
056: id2 = getID(params);
057: }
058: if (!canProcess(id2)) {
059: return null;
060: }
061: WorldImageServiceImpl service = new WorldImageServiceImpl(id2,
062: getFactory());
063: return service;
064: }
065:
066: private URL getID(Map<String, Serializable> params) {
067: if (params.containsKey(URL_PARAM)) {
068: Object param = params.get(URL_PARAM);
069: if (param instanceof String) {
070: try {
071: return new URL((String) param);
072: } catch (MalformedURLException ex) {
073: return null;
074: }
075: } else if (param instanceof URL) {
076: return (URL) param;
077: } else {
078: return null;
079: }
080: } else {
081: return null;
082: }
083: }
084:
085: /**
086: * Finds or creates a WorldImageFormatFactory.
087: *
088: * @return Default instance of WorldImageFormatFactory
089: */
090: public static WorldImageFormatFactory getFactory() {
091: if (factory == null) {
092: factory = new WorldImageFormatFactory();
093: }
094: return factory;
095: }
096:
097: private boolean canProcess(URL id) {
098: return reasonForFailure(id) == null;
099: }
100:
101: public Map<String, Serializable> createParams(URL url) {
102: if (!canProcess(url))
103: return null;
104:
105: if (url != null) {
106: Map<String, Serializable> params = new HashMap<String, Serializable>();
107: params.put(URL_PARAM, url);
108: return params;
109: }
110:
111: return null;
112: }
113:
114: public String reasonForFailure(Map<String, Serializable> params) {
115: return reasonForFailure(getID(params));
116: }
117:
118: public String reasonForFailure(URL id) {
119: if (id == null) {
120: return Messages.WorldImageServiceExtension_noID;
121: }
122: String fileExt = id.getFile().substring(
123: id.getFile().indexOf('.') + 1);
124: if (fileExt.compareToIgnoreCase("PNG") != 0 && //$NON-NLS-1$
125: fileExt.compareToIgnoreCase("GIF") != 0 && //$NON-NLS-1$
126: fileExt.compareToIgnoreCase("JPG") != 0 && //$NON-NLS-1$
127: fileExt.compareToIgnoreCase("JPEG") != 0 && //&& //$NON-NLS-1$
128: fileExt.compareToIgnoreCase("TIF") != 0 && //$NON-NLS-1$
129: fileExt.compareToIgnoreCase("TIFF") != 0) { //$NON-NLS-1$
130: return Messages.WorldImageServiceExtension_badFileExtension
131: + fileExt;
132: }
133: File file = URLUtils.urlToFile(id);
134: String fileName = file.getAbsolutePath();
135: int index = fileName.indexOf('.');
136: String fileRoot = fileName.substring(0, index);
137: File worldFile = new File(fileRoot + ".wld"); //$NON-NLS-1$
138: if (!worldFile.exists()) {
139: String wldExt = WorldImageFormat.getWorldExtension(fileExt);
140: File otherWorldFile = new File(fileRoot + wldExt);
141: if (!otherWorldFile.exists()) {
142: return Messages.WorldImageServiceExtension_needsFile
143: + "\n\t-" + worldFile + "\n" + Messages.WorldImageServiceExtension_or + "\n\t-" + otherWorldFile; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
144: }
145: }
146:
147: if (!id.getProtocol().equals(
148: Messages.WorldImageServiceExtension_file)) {
149: return Messages.WorldImageServiceExtension_mustBeFIle;
150: }
151: try {
152: @SuppressWarnings("unused")
153: File fileTest = URLUtils.urlToFile(id);
154: } catch (IllegalArgumentException ex) {
155: return Messages.WorldImageServiceExtension_IllegalFilePart1
156: + id.getFile()
157: + Messages.WorldImageServiceExtension_IllegalFilePart2;
158: }
159:
160: if (!(((WorldImageFormat) getFactory().createFormat())
161: .accepts(id))) {
162: return Messages.WorldImageServiceExtension_geotoolsDisagrees;
163: }
164: return null;
165: }
166: }
|