001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    
019    import java.net.MalformedURLException;
020    import java.net.URL;
021    
022    /**
023     * @author Igor Spasic
024     */
025    public class URLUtil {
026    
027            /**
028             * @see {@link
029             *      com.liferay.portal.kernel.process.ClassPathUtil#_buildClassPath(
030             *      ClassLoader, String)}
031             */
032            public static URL normalizeURL(URL url) throws MalformedURLException {
033                    String urlString = url.toString();
034    
035                    if (urlString.startsWith("vfsfile:")) {
036                            urlString = StringUtil.replaceFirst(urlString, "vfsfile:", "file:");
037                    }
038                    else if (urlString.startsWith("vfsjar:")) {
039                            urlString = StringUtil.replaceFirst(urlString, "vfsjar:", "file:");
040                    }
041                    else if (urlString.startsWith("vfszip:")) {
042                            urlString = StringUtil.replaceFirst(urlString, "vfszip:", "file:");
043                    }
044    
045                    if (urlString.contains(".jar/")) {
046                            urlString = StringUtil.replaceFirst(urlString, ".jar/", ".jar!/");
047    
048                            if (urlString.startsWith("file:")) {
049                                    urlString = "jar:" + urlString;
050                            }
051                    }
052    
053                    urlString = urlString.replace('\\', '/');
054    
055                    int index = urlString.indexOf("file:");
056    
057                    if (index != -1) {
058                            index += 5;
059    
060                            if (urlString.charAt(index) != '/') {
061                                    urlString =
062                                            urlString.substring(0, index) + '/' +
063                                                    urlString.substring(index);
064                            }
065                    }
066    
067                    return new URL(urlString);
068            }
069    
070    }