1 24 package org.riotfamily.common.util; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Comparator ; 30 import java.util.HashMap ; 31 32 import org.springframework.beans.support.PropertyComparator; 33 34 public final class PackageLister { 35 36 private static final Comparator PACKAGE_COMPARATOR = 37 new PropertyComparator("implementationTitle", true, true); 38 39 private PackageLister() { 40 } 41 42 public static Collection listPackages(String [] patterns) { 43 HashMap map = new HashMap (); 44 Package [] packages = Package.getPackages(); 45 for (int i = 0; i < packages.length; i++) { 46 String name = packages[i].getImplementationTitle(); 47 if (name != null && matches(packages[i].getName(), patterns)) { 48 map.put(name, packages[i]); 49 } 50 } 51 ArrayList result = new ArrayList (map.values()); 52 Collections.sort(result, PACKAGE_COMPARATOR); 53 return result; 54 } 55 56 private static boolean matches(String name, String [] patterns) { 57 if (patterns == null) { 58 return true; 59 } 60 for (int i = 0; i < patterns.length; i++) { 61 String pattern = patterns[i]; 62 if (pattern.indexOf('*') != -1) { 63 pattern = pattern.substring(0, pattern.indexOf('*')); 64 if (name.startsWith(pattern)) { 65 return true; 66 } 67 } 68 else { 69 if (name.equals(pattern)) { 70 return true; 71 } 72 } 73 } 74 return false; 75 } 76 } 77 | Popular Tags |