001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.utils; 018 019 import com.intellij.openapi.util.SystemInfo; 020 import com.intellij.openapi.util.text.StringUtil; 021 import com.intellij.util.io.URLUtil; 022 import org.jetbrains.annotations.NotNull; 023 024 import java.net.MalformedURLException; 025 import java.net.URL; 026 027 /* 028 * This code is essentially copied from IntelliJ IDEA's VfsUtil, and slightly restructured 029 */ 030 public class KotlinVfsUtil { 031 private static final String FILE = "file"; 032 private static final String JAR = "jar"; 033 private static final String PROTOCOL_DELIMITER = ":"; 034 035 @NotNull 036 public static String convertFromUrl(@NotNull URL url) throws MalformedURLException { 037 String protocol = url.getProtocol(); 038 String path = url.getPath(); 039 if (JAR.equals(protocol)) { 040 if (StringUtil.startsWithConcatenationOf(path, FILE, PROTOCOL_DELIMITER)) { 041 URL subURL = new URL(path); 042 path = subURL.getPath(); 043 } 044 else { 045 throw new MalformedURLException("Can't parse url: " + url.toExternalForm()); 046 } 047 } 048 if (SystemInfo.isWindows || SystemInfo.isOS2) { 049 while (path.length() > 0 && path.charAt(0) == '/') { 050 path = path.substring(1, path.length()); 051 } 052 } 053 054 path = URLUtil.unescapePercentSequences(path); 055 return protocol + "://" + path; 056 } 057 058 private KotlinVfsUtil() {} 059 }