1 /* 2 * Copyright (c) 2011, 2013, 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.ipack.resources; 27 28 import java.io.File; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.Collections; 32 import java.util.HashSet; 33 import java.util.List; 34 import java.util.Set; 35 36 public final class ResourceRules { 37 private final List<Exclude> excludes; 38 private final Set<String> excludesLookup; 39 40 public ResourceRules() { 41 this.excludes = new ArrayList<Exclude>(); 42 this.excludesLookup = new HashSet<String>(); 43 } 44 45 public void addExclude(final String name, final int weight) { 46 excludes.add(new Exclude(name, weight)); 47 excludesLookup.add(name); 48 } 49 50 public List<Exclude> getExcludes() { 51 return Collections.unmodifiableList(excludes); 52 } 53 54 public List<String> collectResources(final File baseDirectory) { 55 final List<String> resources = new ArrayList<String>(); 56 collectResources(resources, baseDirectory, ""); 57 58 return Collections.unmodifiableList(resources); 59 } 60 61 private void collectResources(final List<String> resources, 62 final File resourceFile, 63 final String resourceName) { 64 if (excludesLookup.contains(resourceName)) { 65 return; 66 } 67 68 if (resourceFile.isDirectory()) { 69 if (!resourceName.isEmpty()) { 70 resources.add(resourceName + '/'); 71 } 72 73 final String[] childNames = resourceFile.list(); 74 for (final String childName: childNames) { 75 collectResources(resources, 76 new File(resourceFile, childName), 77 constructResourceName( 78 resourceName, childName)); 79 } 80 81 return; 82 } 83 84 if (resourceFile.isFile()) { 85 resources.add(resourceName); 86 return; 87 } 88 89 System.err.println("Skipping " + resourceName); 90 } 91 92 public static final class Exclude { 93 private final String name; 94 private final int weight; 95 96 public Exclude(final String name, final int weight) { 97 this.name = name; 98 this.weight = weight; 99 } 100 101 public String getName() { 102 return name; 103 } 104 105 public int getWeight() { 106 return weight; 107 } 108 } 109 110 private static String constructResourceName(final String parentResource, 111 final String childResource) { 112 return parentResource.isEmpty() 113 ? childResource 114 : parentResource + '/' + childResource; 115 } 116 }