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