1 /* 2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.tools; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.net.MalformedURLException; 31 import java.net.URL; 32 import java.nio.file.Paths; 33 34 /** 35 * A utility class for handle path list 36 * 37 */ 38 public class PathList { 39 /** 40 * Utility method for appending path from pathFrom to pathTo. 41 * 42 * @param pathTo the target path 43 * @param pathFrom the path to be appended to pathTo 44 * @return the resulting path 45 */ 46 public static String appendPath(String pathTo, String pathFrom) { 47 if (pathTo == null || pathTo.length() == 0) { 48 return pathFrom; 49 } else if (pathFrom == null || pathFrom.length() == 0) { 50 return pathTo; 51 } else { 52 return pathTo + File.pathSeparator + pathFrom; 53 } 54 } 55 56 /** 57 * Utility method for converting a search path string to an array 58 * of directory and JAR file URLs. 59 * 60 * @param path the search path string 61 * @return the resulting array of directory and JAR file URLs 62 */ 63 public static URL[] pathToURLs(String path) { 64 return Paths.pathToStrings(path) 65 .stream() 66 .map(s -> fileToURL(new File(s))) 67 .toArray(URL[]::new); 68 } 69 70 /** 71 * Returns the directory or JAR file URL corresponding to the specified 72 * local file name. 73 * 74 * @param file the File object 75 * @return the resulting directory or JAR file URL, or null if unknown 76 */ 77 private static URL fileToURL(File file) { 78 String name; 79 try { 80 name = file.getCanonicalPath(); 81 } catch (IOException e) { 82 name = file.getAbsolutePath(); 83 } 84 name = name.replace(File.separatorChar, '/'); 85 if (!name.startsWith("/")) { 86 name = "/" + name; 87 } 88 // If the file does not exist, then assume that it's a directory 89 if (!file.isFile()) { 90 name = name + "/"; 91 } 92 try { 93 return new URL("file", "", name); 94 } catch (MalformedURLException e) { 95 throw new IllegalArgumentException("file"); 96 } 97 } 98 }