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 24 package jdk.tools.jaotc.collect; 25 26 import java.nio.file.FileSystem; 27 import java.nio.file.Path; 28 import java.nio.file.Paths; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 public class SearchPath { 33 private final List<Path> searchPaths = new ArrayList<>(); 34 private final FileSupport fileSupport; 35 SearchPath()36 public SearchPath() { 37 this(new FileSupport()); 38 } 39 SearchPath(FileSupport fileSupport)40 public SearchPath(FileSupport fileSupport) { 41 this.fileSupport = fileSupport; 42 } 43 find(FileSystem fileSystem, Path entry, String... defaults)44 public Path find(FileSystem fileSystem, Path entry, String... defaults) { 45 if (isAbsolute(entry)) { 46 if (exists(entry)) { 47 return entry; 48 } 49 return null; 50 } 51 52 if (exists(entry)) { 53 return entry; 54 } 55 56 for (String searchPath : defaults) { 57 Path newPath = fileSystem.getPath(searchPath, entry.toString()); 58 if (exists(newPath)) { 59 return newPath; 60 } 61 } 62 63 for (Path searchPath : searchPaths) { 64 Path newPath = fileSystem.getPath(searchPath.toString(), entry.toString()); 65 if (exists(newPath)) { 66 return newPath; 67 } 68 } 69 70 return null; 71 } 72 isAbsolute(Path entry)73 private boolean isAbsolute(Path entry) { 74 return fileSupport.isAbsolute(entry); 75 } 76 exists(Path entry)77 private boolean exists(Path entry) { 78 return fileSupport.exists(entry); 79 } 80 add(String... paths)81 public void add(String... paths) { 82 for (String name : paths) { 83 Path path = Paths.get(name); 84 searchPaths.add(path); 85 } 86 } 87 } 88