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.oracle.tools.packager;
  27 
  28 import java.io.File;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 
  32 public class JreUtils {
  33 
  34     public static class Rule {
  35         String regex;
  36         boolean includeRule;
  37         Type type;
  38         enum Type {SUFFIX, PREFIX, SUBSTR, REGEX}
  39 
  40         private Rule(String regex, boolean includeRule, Type type) {
  41             this.regex = regex;
  42             this.type = type;
  43             this.includeRule = includeRule;
  44         }
  45 
  46         boolean match(String str) {
  47             if (type == Type.SUFFIX) {
  48                 return str.endsWith(regex);
  49             }
  50             if (type == Type.PREFIX) {
  51                 return str.startsWith(regex);
  52             }
  53             if (type == Type.SUBSTR) {
  54                 return str.contains(regex);
  55             }
  56             return str.matches(regex);
  57         }
  58 
  59         boolean treatAsAccept() {return includeRule;}
  60 
  61         public static Rule suffix(String s) {
  62             return new Rule(s, true, Type.SUFFIX);
  63         }
  64         public static Rule suffixNeg(String s) {
  65             return new Rule(s, false, Type.SUFFIX);
  66         }
  67         static Rule prefix(String s) {
  68             return new Rule(s, true, Type.PREFIX);
  69         }
  70         public static Rule prefixNeg(String s) {
  71             return new Rule(s, false, Type.PREFIX);
  72         }
  73         static Rule substr(String s) {
  74             return new Rule(s, true, Type.SUBSTR);
  75         }
  76         public static Rule substrNeg(String s) {
  77             return new Rule(s, false, Type.SUBSTR);
  78         }
  79     }
  80 
  81     public static boolean shouldExclude(File baseDir, File f, Rule ruleset[]) {
  82         if (ruleset == null) {
  83             return false;
  84         }
  85 
  86         String fname = f.getAbsolutePath().toLowerCase().substring(
  87                 baseDir.getAbsolutePath().length());
  88         //first rule match defines the answer
  89         for (Rule r: ruleset) {
  90             if (r.match(fname)) {
  91                 return !r.treatAsAccept();
  92             }
  93         }
  94         //default is include
  95         return false;
  96     }
  97 
  98     public static void walk(File base, File root, Rule ruleset[], Set<File> files) {
  99         walk(base, root, ruleset, files, false);
 100     }
 101 
 102     public static void walk(File base, File root, Rule ruleset[], Set<File> files, boolean acceptSymlinks) {
 103         if (!root.isDirectory()) {
 104             if (root.isFile()) {
 105                 files.add(root);
 106             }
 107             return;
 108         }
 109 
 110         File[] lst = root.listFiles();
 111         if (lst != null) {
 112             for (File f : lst) {
 113                 if ((acceptSymlinks || IOUtils.isNotSymbolicLink(f)) && !shouldExclude(base, f, ruleset)) {
 114                     if (f.isDirectory()) {
 115                         walk(base, f, ruleset, files, acceptSymlinks);
 116                     } else if (f.isFile()) {
 117                         //add to list
 118                         files.add(f);
 119                     }
 120                 }
 121             }
 122         }
 123     }
 124 
 125     public static RelativeFileSet extractJreAsRelativeFileSet(String root, JreUtils.Rule[] ruleset) {
 126         return extractJreAsRelativeFileSet(root, ruleset, false);
 127     }
 128 
 129     public static RelativeFileSet extractJreAsRelativeFileSet(String root, JreUtils.Rule[] ruleset, boolean acceptSymlinks) {
 130         if (root.isEmpty()) return null;
 131 
 132         File baseDir = new File(root);
 133 
 134         Set<File> lst = new HashSet<>();
 135 
 136         walk(baseDir, baseDir, ruleset, lst, acceptSymlinks);
 137 
 138         return new RelativeFileSet(baseDir, lst);
 139     }
 140 
 141 }