001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy 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
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.network;
020
021import icy.util.StringUtil;
022
023import java.io.File;
024import java.net.MalformedURLException;
025import java.net.URL;
026
027/**
028 * @author Stephane
029 */
030public class URLUtil
031{
032    public static final String PROTOCOL_FILE = "file";
033    public static final String PROTOCOL_FTP = "ftp";
034    public static final String PROTOCOL_GOPHER = "gopher";
035    public static final String PROTOCOL_HTTP = "http";
036    public static final String PROTOCOL_JAR = "jar";
037
038    /**
039     * Convert a network path to a URL
040     */
041    public static URL getURL(String path)
042    {
043        if (path == null)
044            return null;
045
046        // first we try as network URL
047        try
048        {
049            return new URL(path);
050        }
051        catch (MalformedURLException e)
052        {
053            // then we try to get it as file URL
054            try
055            {
056                return new File(path).toURI().toURL();
057            }
058            catch (MalformedURLException e2)
059            {
060                return null;
061            }
062        }
063    }
064
065    public static boolean isURL(String path)
066    {
067        return getURL(path) != null;
068    }
069
070    public static boolean isNetworkURL(String path)
071    {
072        return isNetworkURL(getURL(path));
073    }
074
075    public static boolean isNetworkURL(URL url)
076    {
077        return (url != null) && !url.getProtocol().equals(PROTOCOL_FILE);
078    }
079
080    public static boolean isFileURL(String path)
081    {
082        return isFileURL(getURL(path));
083    }
084
085    public static boolean isFileURL(URL url)
086    {
087        return (url != null) && url.getProtocol().equals(PROTOCOL_FILE);
088    }
089
090    /**
091     * Returns <code>true</code> if the url defines an absolute address and <code>false</code> if it
092     * defines a relative address.
093     */
094    public static boolean isAbsolute(String path)
095    {
096        if (!StringUtil.isEmpty(path))
097        {
098            int index = path.indexOf(':');
099
100            // protocol or drive letter
101            if (index != -1)
102            {
103                if ((index + 1) < path.length())
104                    return (path.charAt(index + 1) == '/');
105
106                return false;
107            }
108
109            return (path.charAt(0) == '/');
110        }
111
112        return false;
113    }
114
115    public static String getNetworkURLString(String base, String path)
116    {
117        if (StringUtil.isEmpty(base))
118            return path;
119        if (StringUtil.isEmpty(path))
120            return base;
121
122        if (isNetworkURL(path))
123            return path;
124
125        return base + path;
126    }
127
128    public static String getURLProtocol(URL url)
129    {
130        if (url == null)
131            return null;
132
133        return url.getProtocol();
134    }
135
136    public static String getURLHost(URL url)
137    {
138        if (url == null)
139            return null;
140
141        return url.getHost();
142    }
143
144    public static String getURLDirectory(String url)
145    {
146        return getURLDirectory(getURL(url));
147    }
148
149    public static String getURLDirectory(URL url)
150    {
151        if (url != null)
152        {
153            final String path = url.getPath();
154
155            if (!StringUtil.isEmpty(path))
156            {
157                int index = path.lastIndexOf('/');
158                if (index != -1)
159                    return path.substring(0, index + 1);
160
161                index = path.lastIndexOf(':');
162                if (index != -1)
163                    return path.substring(0, index + 1);
164            }
165        }
166
167        return "";
168    }
169
170    public static String getURLFileName(String url)
171    {
172        return getURLFileName(getURL(url));
173    }
174
175    public static String getURLFileName(String url, boolean withExtension)
176    {
177        return getURLFileName(getURL(url), withExtension);
178    }
179
180    public static String getURLFileName(URL url)
181    {
182        return getURLFileName(url, true);
183    }
184
185    public static String getURLFileName(URL url, boolean withExtension)
186    {
187        if (url == null)
188            return "";
189
190        final String path = url.getPath();
191
192        if (StringUtil.isEmpty(path))
193            return "";
194
195        int index = path.lastIndexOf('/');
196        final String fileName;
197
198        if (index != -1)
199            fileName = path.substring(index + 1);
200        else
201        {
202            index = path.lastIndexOf(':');
203
204            if (index != -1)
205                fileName = path.substring(index + 1);
206            else
207                fileName = path;
208        }
209
210        if (withExtension)
211            return fileName;
212
213        index = fileName.lastIndexOf('.');
214
215        if (index == 0)
216            return "";
217        else if (index != -1)
218            return fileName.substring(0, index);
219        else
220            return fileName;
221    }
222
223    public static String getURLFileExtension(String url, boolean withDot)
224    {
225        return getURLFileExtension(getURL(url), withDot);
226    }
227
228    public static String getURLFileExtension(URL url, boolean withDot)
229    {
230        if (url == null)
231            return "";
232
233        final String path = url.getPath();
234
235        if (StringUtil.isEmpty(path))
236            return "";
237
238        final int index = path.lastIndexOf('.');
239
240        if (index == -1)
241            return "";
242
243        if (withDot)
244            return path.substring(index);
245
246        return path.substring(index + 1);
247    }
248
249    public static String getURLQuery(URL url)
250    {
251        if (url == null)
252            return null;
253
254        return url.getQuery();
255    }
256
257    /**
258     * Build a URL from a base path and specified url.<br>
259     * If the url is a relative address then result is the concatenation of base path and url.<br>
260     * If the specified url is an absolute address then the url is returned as it is.
261     */
262    public static URL buildURL(String basePath, String url)
263    {
264        if (!isAbsolute(url) && !StringUtil.isEmpty(basePath))
265            return getURL(basePath + url);
266
267        return getURL(url);
268    }
269}