KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > PropertyConfigurator


1 package jdepend.framework;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14  * The <code>PropertyConfigurator</code> class contains configuration
15  * information contained in the <code>jdepend.properties</code> file,
16  * if such a file exists either in the user's home directory or somewhere
17  * in the classpath.
18  *
19  * @author <b>Mike Clark</b>
20  * @author Clarkware Consulting, Inc.
21  */

22
23 public class PropertyConfigurator {
24
25     private Properties JavaDoc properties;
26
27     public static final String JavaDoc DEFAULT_PROPERTY_FILE = "jdepend.properties";
28
29     /**
30      * Constructs a <code>PropertyConfigurator</code> instance
31      * containing the properties specified in the file
32      * <code>jdepend.properties</code>, if it exists.
33      */

34     public PropertyConfigurator() {
35         this(getDefaultPropertyFile());
36     }
37
38     /**
39      * Constructs a <code>PropertyConfigurator</code> instance
40      * with the specified property set.
41      *
42      * @param p Property set.
43      */

44     public PropertyConfigurator(Properties JavaDoc p) {
45         this.properties = p;
46     }
47
48     /**
49      * Constructs a <code>PropertyConfigurator</code> instance
50      * with the specified property file.
51      *
52      * @param f Property file.
53      */

54     public PropertyConfigurator(File JavaDoc f) {
55         this(loadProperties(f));
56     }
57
58     public Collection JavaDoc getFilteredPackages() {
59
60         Collection JavaDoc packages = new ArrayList JavaDoc();
61
62         Enumeration JavaDoc e = properties.propertyNames();
63         while (e.hasMoreElements()) {
64             String JavaDoc key = (String JavaDoc) e.nextElement();
65             if (key.startsWith("ignore")) {
66                 String JavaDoc path = properties.getProperty(key);
67                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ",");
68                 while (st.hasMoreTokens()) {
69                     String JavaDoc name = (String JavaDoc) st.nextToken();
70                     name = name.trim();
71                     packages.add(name);
72                 }
73             }
74         }
75
76         return packages;
77     }
78
79     public Collection JavaDoc getConfiguredPackages() {
80
81         Collection JavaDoc packages = new ArrayList JavaDoc();
82
83         Enumeration JavaDoc e = properties.propertyNames();
84         while (e.hasMoreElements()) {
85             String JavaDoc key = (String JavaDoc)e.nextElement();
86             if (!key.startsWith("ignore")
87                     && (!key.equals("analyzeInnerClasses"))) {
88                 String JavaDoc v = properties.getProperty(key);
89                 packages.add(new JavaPackage(key, new Integer JavaDoc(v).intValue()));
90             }
91         }
92
93         return packages;
94     }
95
96     public boolean getAnalyzeInnerClasses() {
97
98         String JavaDoc key = "analyzeInnerClasses";
99         if (properties.containsKey(key)) {
100             String JavaDoc value = properties.getProperty(key);
101             return new Boolean JavaDoc(value).booleanValue();
102         }
103
104         return true;
105     }
106
107     public static File JavaDoc getDefaultPropertyFile() {
108         String JavaDoc home = System.getProperty("user.home");
109         return new File JavaDoc(home, DEFAULT_PROPERTY_FILE);
110     }
111
112     public static Properties JavaDoc loadProperties(File JavaDoc file) {
113
114         Properties JavaDoc p = new Properties JavaDoc();
115
116         InputStream JavaDoc is = null;
117
118         try {
119
120             is = new FileInputStream JavaDoc(file);
121
122         } catch (Exception JavaDoc 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 JavaDoc ignore) {
132         } finally {
133             try {
134                 if (is != null) {
135                     is.close();
136                 }
137             } catch (IOException JavaDoc ignore) {
138             }
139         }
140
141         return p;
142     }
143 }
Popular Tags