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