001/**
002 * Copyright 2010-2016 Boxfuse GmbH
003 * <p/>
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 * <p/>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p/>
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 */
016package org.avaje.classpath.scanner.internal;
017
018import java.io.File;
019import java.io.UnsupportedEncodingException;
020import java.net.URL;
021import java.net.URLDecoder;
022
023/**
024 * Collection of utility methods for working with URLs.
025 */
026public class UrlUtils {
027  /**
028   * Prevent instantiation.
029   */
030  private UrlUtils() {
031    // Do nothing
032  }
033
034  /**
035   * Retrieves the file path of this URL, with any trailing slashes removed.
036   *
037   * @param url The URL to get the file path for.
038   * @return The file path.
039   */
040  public static String toFilePath(URL url) {
041    try {
042      String filePath = new File(URLDecoder.decode(url.getPath().replace("+", "%2b"), "UTF-8")).getAbsolutePath();
043      if (filePath.endsWith("/")) {
044        return filePath.substring(0, filePath.length() - 1);
045      }
046      return filePath;
047    } catch (UnsupportedEncodingException e) {
048      throw new IllegalStateException("Can never happen", e);
049    }
050  }
051}