1 /*
   2  * Copyright (c) 2012, 2014, 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 com.sun.javafx.tools.packager.bundlers;
  27 
  28 import com.sun.javafx.tools.packager.Log;
  29 import java.io.File;
  30 
  31 import java.util.LinkedHashSet;
  32 import java.util.Set;
  33 
  34 public class RelativeFileSet {
  35     private File basedir;
  36     Set<String> files = new LinkedHashSet<>();
  37 
  38     public RelativeFileSet(File base, Set<File> files) {
  39         basedir = base;
  40         String baseAbsolute = basedir.getAbsolutePath();
  41         for (File f: files) {
  42             String absolute = f.getAbsolutePath();
  43             if (!absolute.startsWith(baseAbsolute)) {
  44                 throw new RuntimeException("File " + f.getAbsolutePath() +
  45                         " does not belong to "+baseAbsolute);
  46             }
  47             if (!absolute.equals(baseAbsolute)) { //possible in javafxpackager case
  48                this.files.add(absolute.substring(baseAbsolute.length()+1));
  49             }
  50         }
  51     }
  52 
  53     public boolean contains(String[] requiredFiles) {
  54         boolean result = true;
  55 
  56         for(String fname: requiredFiles) {
  57             if (!files.contains(fname)) {
  58                 Log.debug("  Runtime does not contain [" + fname + "]");
  59                 result = false;
  60             }
  61         }
  62 
  63         return result;
  64     }
  65 
  66     public boolean contains(String requiredFile) {
  67         if (files.contains(requiredFile)) {
  68             return true;
  69         } else {
  70             Log.debug("  Runtime does not contain [" + requiredFile + "]");
  71             return false;
  72         }
  73     }
  74 
  75     public File getBaseDirectory() {
  76         return basedir;
  77     }
  78 
  79     public Set<String> getIncludedFiles() {
  80         return files;
  81     }
  82 
  83     public void dump() {
  84         Log.verbose("\n=========\nBasedir: " + basedir + "\n");
  85         for (String fname : files) {
  86             Log.verbose("  " + fname);
  87         }
  88         Log.verbose("\n========");
  89     }
  90     
  91     @Override
  92     public String toString() {
  93         return "RelativeFileSet{basedir:" + basedir + ", files:" + files + "}";
  94     }
  95 
  96 }