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.ui;
018:
019: import java.io.File;
020: import java.net.URL;
021:
022: import net.refractions.udig.catalog.ui.internal.Messages;
023:
024: /**
025: * URLs are used for resolve.getId(), this class contains utility methods to help work with urls
026: * used in this manner.
027: *
028: * @author jgarnett
029: * @since 0.9.0
030: */
031: public class Identifier {
032:
033: public static final boolean isFile(URL url) {
034: return "file".equals(url.getProtocol()); //$NON-NLS-1$
035: }
036:
037: public static boolean isGraphic(URL url) {
038: // http://localhost/mapgraphic
039: String HOST = url.getHost();
040: String PROTOCOL = url.getProtocol();
041: String PATH = url.getPath();
042: if (!"http".equals(PROTOCOL))return false; //$NON-NLS-1$
043: if (!"localhost".equals(HOST))return false; //$NON-NLS-1$
044:
045: if (!"/mapgraphic".equals(PATH))return false; //$NON-NLS-1$
046:
047: return true;
048: }
049:
050: public static boolean isMemory(URL url) {
051: String HOST = url.getHost();
052: String PROTOCOL = url.getProtocol();
053: String PATH = url.getPath();
054: if (!"http".equals(PROTOCOL))return false; //$NON-NLS-1$
055: if (!"localhost".equals(HOST))return false; //$NON-NLS-1$
056:
057: if (!"/scratch".equals(PATH))return false; //$NON-NLS-1$
058:
059: return true;
060: }
061:
062: public static boolean isWMS(URL url) {
063: String PATH = url.getPath();
064: String QUERY = url.getQuery();
065: String PROTOCOL = url.getProtocol();
066: if (!"http".equals(PROTOCOL)) { //$NON-NLS-1$
067: return false;
068: }
069: if (QUERY != null
070: && QUERY.toUpperCase().indexOf("SERVICE=WMS") != -1) { //$NON-NLS-1$
071: return true;
072: } else if (PATH != null
073: && PATH.toUpperCase().indexOf("GEOSERVER/WMS") != -1) { //$NON-NLS-1$
074: return true;
075: }
076: return false;
077: }
078:
079: public static final boolean isWFS(URL url) {
080: String PATH = url.getPath();
081: String QUERY = url.getQuery();
082: String PROTOCOL = url.getProtocol();
083:
084: if (!"http".equals(PROTOCOL)) { //$NON-NLS-1$
085: return false;
086: }
087: if (QUERY != null
088: && QUERY.toUpperCase().indexOf("SERVICE=WFS") != -1) { //$NON-NLS-1$
089: return true;
090: } else if (PATH != null
091: && PATH.toUpperCase().indexOf("GEOSERVER/WFS") != -1) { //$NON-NLS-1$
092: return true;
093: }
094: return false;
095: }
096:
097: public static final boolean isJDBC(URL url) {
098: String PROTOCOL = url.getProtocol();
099: String HOST = url.getHost();
100: return "http".equals(PROTOCOL) && HOST != null && HOST.indexOf(".jdbc") != -1; //$NON-NLS-1$ //$NON-NLS-2$
101: }
102:
103: /**
104: * Aquire "resource " similar to "data/a.shp" from url.
105: * <p>
106: * According to the followng breakdown:
107: * <ul>
108: * <li>http path is returned
109: * <li>last entry in the file path is returned
110: * <li>last two elements of the file path is returned
111: * <li>jdbc table name is returned
112: * <li>WFS typeName is returned
113: * </ul>
114: * </p>
115: * <p>
116: * Here are some examples:
117: * <ul>
118: * <li>file:///C:/java/workspace/shapefiles/a.shp becomes "a.shp"
119: * <li>http://www.refractions.net:8080/geoserver/wfs?REQUEST=GetCapabilities&SERVICE=WFS&type=topp:ROAD
120: * becomes "topp:ROAD"
121: * <li>http://www.refractions.net:8080/data/a.shp becomes "data/a.shp"
122: * <li>ftp://ftp.refractions.net/data/a.shp becomes "data/a.shp"
123: * <li>http://kraken.postgis.jdbc:5432/production.... becomes road
124: * </ul>
125: * <li>
126: *
127: * @param url
128: * @return label describing the URL as a resource (ie file or content)
129: */
130: final static public String labelResource(URL url) {
131: if (url == null)
132: return Messages.ResolveLabelProvider_missingText;
133:
134: String HOST = url.getHost();
135: String QUERY = url.getQuery();
136: String PATH = url.getPath();
137: String PROTOCOL = url.getProtocol();
138: String REF = url.getRef();
139:
140: if (REF != null) {
141: return REF;
142: }
143: if (PROTOCOL == null) {
144: return ""; // we do not represent a server (local host does not cut it) //$NON-NLS-1$
145: }
146: StringBuffer label = new StringBuffer();
147: if ("file".equals(PROTOCOL)) { //$NON-NLS-1$
148: int split = PATH.lastIndexOf('/');
149: if (split == -1) {
150: label.append(PATH);
151: } else {
152: String file = PATH.substring(split + 1);
153: int dot = file.lastIndexOf('.');
154: if (dot != -1) {
155: file = file.substring(0, dot);
156: }
157: label.append(file);
158: }
159: } else if ("http".equals(PROTOCOL) && HOST.indexOf(".jdbc") != -1) { //$NON-NLS-1$ //$NON-NLS-2$
160: if (QUERY != null) {
161: label.append(QUERY);
162: } else {
163: label.append(PATH);
164: }
165: } else if ("http".equals(PROTOCOL)) { //$NON-NLS-1$
166: if (QUERY != null
167: && QUERY.toUpperCase().indexOf("SERVICE=WFS") != -1) { //$NON-NLS-1$
168: for (String split : QUERY.split("&")) { //$NON-NLS-1$
169: if (split.toLowerCase().startsWith("type=")) { //$NON-NLS-1$
170: label.append(split.substring(5));
171: }
172: }
173: } else if (QUERY != null
174: && QUERY.toUpperCase().indexOf("SERVICE=WMS") != -1) { //$NON-NLS-1$
175: for (String split : QUERY.split("&")) { //$NON-NLS-1$
176: if (split.startsWith("LAYER=")) { //$NON-NLS-1$
177: label.append(split.substring(6));
178: }
179: }
180: } else {
181: int split = PATH.lastIndexOf('/');
182: if (split == -1) {
183: label.append(PATH);
184: } else {
185: label.append(PATH.substring(split + 1));
186: }
187: }
188: } else {
189: int split = PATH.lastIndexOf('/');
190: if (split == -1) {
191: label.append(PATH);
192: } else {
193: label.append(PATH.substring(split + 1));
194: }
195: }
196: return label.toString();
197: }
198:
199: /**
200: * Aquire "server" similar to "protocol://host:port" from url.
201: * <p>
202: * According to the followng breakdown:
203: * <ul>
204: * <li>Protocol http and file is ignored (or assumed)
205: * <li>File is reduced to HOST and last path entry
206: * <li>jdbc stuff is encoded as *majic* see, this method sorts it out
207: * <li>default ports are not displayed
208: * <li>service=WFS and service=WMS are *magic* as well
209: * </ul>
210: * </p>
211: * <p>
212: * Here are some examples:
213: * <ul>
214: * <li>file:///C:/java/workspace/shapefiles/a.shp becomes "shapefiles/a.shp"
215: * <li>http://www.refractions.net:8080/geoserver/wfs?REQUEST=GetCapabilities&SERVICE=WFS
216: * becomes "wfs://www.refractions.net:8080"
217: * <li>http://www.refractions.net:8080/data/a.shp becomes "www.refractions.net:8080"
218: * <li>ftp://ftp.refractions.net/data/a.shp becomes "ftp://ftp.refractions.net"
219: * <li>http://kraken.postgis.jdbc:5432/production becomes postgis://kraken:5432
220: * </ul>
221: * <li>
222: *
223: * @param server
224: * @return label as if url points to just a server
225: */
226: final static public String labelServer(URL url) {
227: if (url == null)
228: return Messages.ResolveLabelProvider_missingText;
229:
230: String HOST = url.getHost();
231: int PORT = url.getPort();
232: String PATH = url.getPath();
233: // String QUERY = url.getQuery();
234: String PROTOCOL = url.getProtocol();
235:
236: if (PROTOCOL == null) {
237: return ""; // we do not represent a server (local host does not cut it) //$NON-NLS-1$
238: }
239: StringBuffer label = new StringBuffer();
240: if (isFile(url)) {
241: String split[] = PATH.split("\\/"); //$NON-NLS-1$
242:
243: if (split.length == 0) {
244: label.append(File.separatorChar);
245: } else {
246: if (split.length < 2) {
247: label.append(File.separatorChar);
248: label.append(split[0]);
249: label.append(File.separatorChar);
250: } else {
251: label.append(split[split.length - 2]);
252: label.append(File.separatorChar);
253: }
254: label.append(split[split.length - 1]);
255: }
256: } else if (isJDBC(url)) {
257: int split2 = HOST.lastIndexOf('.');
258: int split1 = HOST.lastIndexOf('.', split2 - 1);
259: label.append(HOST.substring(split1 + 1, split2));
260: label.append("://"); //$NON-NLS-1$
261: label.append(HOST.subSequence(0, split1));
262: } else if ("http".equals(PROTOCOL) || "https".equals(PROTOCOL)) { //$NON-NLS-1$ //$NON-NLS-2$
263: if (isWMS(url)) {
264: label.append("wms://"); //$NON-NLS-1$
265: } else if (isWFS(url)) {
266: label.append("wfs://"); //$NON-NLS-1$
267: }
268: label.append(HOST);
269: } else {
270: label.append(PROTOCOL);
271: label.append("://"); //$NON-NLS-1$
272: label.append(HOST);
273: }
274: if (PORT != -1) {
275: label.append(":"); //$NON-NLS-1$
276: label.append(PORT);
277: }
278: return label.toString();
279: }
280: }
|