1 32 33 package com.jeantessier.diff; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.oro.text.perl.*; 39 40 public class PackageValidator implements Validator { 41 private static final Perl5Util perl = new Perl5Util(); 42 43 private Collection allowedPackages = new HashSet(); 44 45 public PackageValidator(String filename) throws IOException { 46 try { 47 init(new BufferedReader(new InputStreamReader(new FileInputStream(filename)))); 48 } catch (FileNotFoundException ex) { 49 } 51 } 52 53 public PackageValidator(BufferedReader in) throws IOException { 54 init(in); 55 } 56 57 private void init(BufferedReader in) throws IOException { 58 try { 59 String line; 60 while ((line = in.readLine()) != null) { 61 if (line.length() > 0) { 62 allowedPackages.add(line.trim()); 63 } 64 } 65 } catch (FileNotFoundException ex) { 66 } finally { 68 if (in != null) { 69 in.close(); 70 } 71 } 72 } 73 74 public boolean isPackageAllowed(String name) { 75 return isAllowed(name); 76 } 77 78 public boolean isClassAllowed(String name) { 79 String packageName = ""; 80 int pos = name.lastIndexOf('.'); 81 if (pos != -1) { 82 packageName = name.substring(0, pos); 83 } 84 85 return isPackageAllowed(packageName); 86 } 87 88 public boolean isFeatureAllowed(String name) { 89 boolean result = false; 90 91 String className = ""; 92 synchronized (perl) { 93 if (perl.match("/^(.+)\\.[^\\.]+\\(.*\\)$/", name)) { 94 className = perl.group(1); 95 } else if (perl.match("/^(.+)\\.[^\\.]+$/", name)) { 96 className = perl.group(1); 97 } 98 } 99 100 if (!className.equals("")) { 101 result = isClassAllowed(className); 102 } 103 104 return result; 105 } 106 107 public boolean isAllowed(String name) { 108 return allowedPackages.size() == 0 || allowedPackages.contains(name); 109 } 110 } 111 | Popular Tags |