001: package jdepend.framework;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.Enumeration;
010: import java.util.Properties;
011: import java.util.StringTokenizer;
012:
013: /**
014: * The <code>PropertyConfigurator</code> class contains configuration
015: * information contained in the <code>jdepend.properties</code> file,
016: * if such a file exists either in the user's home directory or somewhere
017: * in the classpath.
018: *
019: * @author <b>Mike Clark</b>
020: * @author Clarkware Consulting, Inc.
021: */
022:
023: public class PropertyConfigurator {
024:
025: private Properties properties;
026:
027: public static final String DEFAULT_PROPERTY_FILE = "jdepend.properties";
028:
029: /**
030: * Constructs a <code>PropertyConfigurator</code> instance
031: * containing the properties specified in the file
032: * <code>jdepend.properties</code>, if it exists.
033: */
034: public PropertyConfigurator() {
035: this (getDefaultPropertyFile());
036: }
037:
038: /**
039: * Constructs a <code>PropertyConfigurator</code> instance
040: * with the specified property set.
041: *
042: * @param p Property set.
043: */
044: public PropertyConfigurator(Properties p) {
045: this .properties = p;
046: }
047:
048: /**
049: * Constructs a <code>PropertyConfigurator</code> instance
050: * with the specified property file.
051: *
052: * @param f Property file.
053: */
054: public PropertyConfigurator(File f) {
055: this (loadProperties(f));
056: }
057:
058: public Collection getFilteredPackages() {
059:
060: Collection packages = new ArrayList();
061:
062: Enumeration e = properties.propertyNames();
063: while (e.hasMoreElements()) {
064: String key = (String) e.nextElement();
065: if (key.startsWith("ignore")) {
066: String path = properties.getProperty(key);
067: StringTokenizer st = new StringTokenizer(path, ",");
068: while (st.hasMoreTokens()) {
069: String name = (String) st.nextToken();
070: name = name.trim();
071: packages.add(name);
072: }
073: }
074: }
075:
076: return packages;
077: }
078:
079: public Collection getConfiguredPackages() {
080:
081: Collection packages = new ArrayList();
082:
083: Enumeration e = properties.propertyNames();
084: while (e.hasMoreElements()) {
085: String key = (String) e.nextElement();
086: if (!key.startsWith("ignore")
087: && (!key.equals("analyzeInnerClasses"))) {
088: String v = properties.getProperty(key);
089: packages.add(new JavaPackage(key, new Integer(v)
090: .intValue()));
091: }
092: }
093:
094: return packages;
095: }
096:
097: public boolean getAnalyzeInnerClasses() {
098:
099: String key = "analyzeInnerClasses";
100: if (properties.containsKey(key)) {
101: String value = properties.getProperty(key);
102: return new Boolean(value).booleanValue();
103: }
104:
105: return true;
106: }
107:
108: public static File getDefaultPropertyFile() {
109: String home = System.getProperty("user.home");
110: return new File(home, DEFAULT_PROPERTY_FILE);
111: }
112:
113: public static Properties loadProperties(File file) {
114:
115: Properties p = new Properties();
116:
117: InputStream is = null;
118:
119: try {
120:
121: is = new FileInputStream(file);
122:
123: } catch (Exception e) {
124: is = PropertyConfigurator.class.getResourceAsStream("/"
125: + DEFAULT_PROPERTY_FILE);
126: }
127:
128: try {
129: if (is != null) {
130: p.load(is);
131: }
132: } catch (IOException ignore) {
133: } finally {
134: try {
135: if (is != null) {
136: is.close();
137: }
138: } catch (IOException ignore) {
139: }
140: }
141:
142: return p;
143: }
144: }
|