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