001    /**
002     * Copyright (c) 2000-present 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 com.liferay.portal.kernel.process.ClassPathUtil#_buildClassPath(
029             *      ClassLoader, String)
030             */
031            public static URL normalizeURL(URL url) throws MalformedURLException {
032                    String urlString = url.toString();
033    
034                    if (urlString.startsWith("vfsfile:")) {
035                            urlString = StringUtil.replaceFirst(urlString, "vfsfile:", "file:");
036                    }
037                    else if (urlString.startsWith("vfsjar:")) {
038                            urlString = StringUtil.replaceFirst(urlString, "vfsjar:", "file:");
039                    }
040                    else if (urlString.startsWith("vfszip:")) {
041                            urlString = StringUtil.replaceFirst(urlString, "vfszip:", "file:");
042                    }
043    
044                    if (urlString.contains(".jar/")) {
045                            urlString = StringUtil.replaceFirst(urlString, ".jar/", ".jar!/");
046    
047                            if (urlString.startsWith("file:")) {
048                                    urlString = "jar:" + urlString;
049                            }
050                    }
051    
052                    urlString = urlString.replace('\\', '/');
053    
054                    int index = urlString.indexOf("file:");
055    
056                    if (index != -1) {
057                            index += 5;
058    
059                            if (urlString.charAt(index) != '/') {
060                                    urlString =
061                                            urlString.substring(0, index) + '/' +
062                                                    urlString.substring(index);
063                            }
064                    }
065    
066                    return new URL(urlString);
067            }
068    
069    }