1 /* 2 * Copyright (c) 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.tools.jaotc.collect; 24 25 import java.nio.file.FileSystem; 26 import java.nio.file.Path; 27 import java.nio.file.Paths; 28 import java.util.ArrayList; 29 import java.util.List; 30 31 public class SearchPath { 32 private final List<Path> searchPaths = new ArrayList<>(); 33 private final FileSupport fileSupport; 34 35 public SearchPath() { 36 this(new FileSupport()); 37 } 38 39 public SearchPath(FileSupport fileSupport) { 40 this.fileSupport = fileSupport; 41 } 42 43 public Path find(FileSystem fileSystem, Path entry, String... defaults) { 44 if (isAbsolute(entry)) { 45 if (exists(entry)) { 46 return entry; 47 } 48 return null; 49 } 50 51 if (exists(entry)) { 52 return entry; 53 } 54 55 for (String searchPath : defaults) { 56 Path newPath = fileSystem.getPath(searchPath, entry.toString()); 57 if (exists(newPath)) { 58 return newPath; 59 } 60 } 61 62 for (Path searchPath : searchPaths) { 63 Path newPath = fileSystem.getPath(searchPath.toString(), entry.toString()); 64 if (exists(newPath)) { 65 return newPath; 66 } 67 } 68 69 return null; 70 } 71 72 private boolean isAbsolute(Path entry) { 73 return fileSupport.isAbsolute(entry); 74 } 75 76 private boolean exists(Path entry) { 77 return fileSupport.exists(entry); 78 } 79 80 public void add(String... paths) { 81 for (String name : paths) { 82 Path path = Paths.get(name); 83 searchPaths.add(path); 84 } 85 } 86 } 87