1 package jdepend.framework; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Enumeration ; 10 import java.util.Properties ; 11 import java.util.StringTokenizer ; 12 13 22 23 public class PropertyConfigurator { 24 25 private Properties properties; 26 27 public static final String DEFAULT_PROPERTY_FILE = "jdepend.properties"; 28 29 34 public PropertyConfigurator() { 35 this(getDefaultPropertyFile()); 36 } 37 38 44 public PropertyConfigurator(Properties p) { 45 this.properties = p; 46 } 47 48 54 public PropertyConfigurator(File f) { 55 this(loadProperties(f)); 56 } 57 58 public Collection getFilteredPackages() { 59 60 Collection packages = new ArrayList (); 61 62 Enumeration e = properties.propertyNames(); 63 while (e.hasMoreElements()) { 64 String key = (String ) e.nextElement(); 65 if (key.startsWith("ignore")) { 66 String path = properties.getProperty(key); 67 StringTokenizer st = new StringTokenizer (path, ","); 68 while (st.hasMoreTokens()) { 69 String name = (String ) st.nextToken(); 70 name = name.trim(); 71 packages.add(name); 72 } 73 } 74 } 75 76 return packages; 77 } 78 79 public Collection getConfiguredPackages() { 80 81 Collection packages = new ArrayList (); 82 83 Enumeration e = properties.propertyNames(); 84 while (e.hasMoreElements()) { 85 String key = (String )e.nextElement(); 86 if (!key.startsWith("ignore") 87 && (!key.equals("analyzeInnerClasses"))) { 88 String v = properties.getProperty(key); 89 packages.add(new JavaPackage(key, new Integer (v).intValue())); 90 } 91 } 92 93 return packages; 94 } 95 96 public boolean getAnalyzeInnerClasses() { 97 98 String key = "analyzeInnerClasses"; 99 if (properties.containsKey(key)) { 100 String value = properties.getProperty(key); 101 return new Boolean (value).booleanValue(); 102 } 103 104 return true; 105 } 106 107 public static File getDefaultPropertyFile() { 108 String home = System.getProperty("user.home"); 109 return new File (home, DEFAULT_PROPERTY_FILE); 110 } 111 112 public static Properties loadProperties(File file) { 113 114 Properties p = new Properties (); 115 116 InputStream is = null; 117 118 try { 119 120 is = new FileInputStream (file); 121 122 } catch (Exception e) { 123 is = PropertyConfigurator.class.getResourceAsStream("/" 124 + DEFAULT_PROPERTY_FILE); 125 } 126 127 try { 128 if (is != null) { 129 p.load(is); 130 } 131 } catch (IOException ignore) { 132 } finally { 133 try { 134 if (is != null) { 135 is.close(); 136 } 137 } catch (IOException ignore) { 138 } 139 } 140 141 return p; 142 } 143 } | Popular Tags |