Path: blob/master/test/functional/cmdLineTests/shareClassTests/utils/src/Utilities/URLClassPathCreator.java
6005 views
/*******************************************************************************1* Copyright (c) 2005, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package Utilities;2223import java.io.File;24import java.io.IOException;25import java.net.MalformedURLException;26import java.net.URL;2728/**29* @author Matthew Kilner30*/31public class URLClassPathCreator {3233String classpathString;3435/**36*37*/38public URLClassPathCreator(String cp) {39super();4041classpathString = prepareString(cp);42}4344private String prepareString(String string){45String newString = null;46newString = string.replace(';',File.pathSeparatorChar);47return newString;48}4950public URL[] createURLClassPath(){51int index = 0, count = 0, end = classpathString.length();52while (index < end) {53int next = classpathString.indexOf(File.pathSeparatorChar, index);54if (next == -1) next = end;55if (next - index > 0) count++;56index = next + 1;57}58URL[] urlPath = new URL[count];59index = count = 0;60while (index < end) {61int next = classpathString.indexOf(File.pathSeparatorChar, index);62if (next == -1) next = end;63if (next - index > 0) {64String path = classpathString.substring(index, next);65try {66File f = new File(path);67path = f.getCanonicalPath();68if (File.separatorChar != '/')69path = path.replace(File.separatorChar, '/');70if (f.isDirectory()) {71if (!path.endsWith("/"))72path = new StringBuffer(path.length() + 1).73append(path).append('/').toString();74}75if (!path.startsWith("/"))76path = new StringBuffer(path.length() + 1).77append('/').append(path).toString();78if (!path.startsWith("//")) {79try {80urlPath[count++] = new URL("file", null, -1, path, null);81} catch (MalformedURLException e) {}82index = next + 1;83continue;84}85path = new StringBuffer(path.length() + 5).86append("file:").append(path).toString();8788urlPath[count] = new URL(path);89count++;90} catch (IOException e) {}91}92index = next + 1;93}94if (count < urlPath.length) {95URL[] paths = new URL[count];96System.arraycopy(urlPath, 0, paths, 0, count);97urlPath = paths;98}99return urlPath;100}101}102103104