1 package com.explosion.utilities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /***
24 * This is a utility class to handle all properties related functions
25 */
26
27 import java.awt.Font;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Enumeration;
33 import java.util.Properties;
34
35 import javax.swing.JFrame;
36
37 import org.apache.log4j.LogManager;
38 import org.apache.log4j.Logger;
39
40 public class PropertiesUtils extends Exception
41 {
42
43 private static Logger log = LogManager.getLogger(PropertiesUtils.class);
44
45 /***
46 * This method will load a properties file from a given location and return
47 * that properties object to the caller.
48 */
49 public static Properties loadProperties(String filePath) throws Exception
50 {
51 Properties properties = new Properties();
52 if (filePath == null)
53 throw new Exception("Preferences path supplied to load properties is null.");
54
55 File file = new File(filePath);
56 if (file.exists())
57 {
58 if (file.isDirectory())
59 throw new Exception("The properties path '" + filePath + "' supplied is a directory and not a file.");
60
61 properties.load(new FileInputStream(file));
62 }
63 else
64 return new Properties();
65
66 return properties;
67 }
68
69 /***
70 * Loads a property set with the given name up from the classpath
71 *
72 * @throws IOException
73 */
74 public static Properties loadPropertiesFromClasspath(String name, ClassLoader loader) throws Exception
75 {
76 Properties properties = new Properties();
77 InputStream stream = loader.getResourceAsStream(name);
78 if (stream == null)
79 throw new Exception("Unable to lead properties '"+name+"' from the classpath.");
80 properties.load(loader.getResourceAsStream(name));
81 return properties;
82 }
83
84 /***
85 * Loads a property set with the given name up from the classpath
86 *
87 * @throws IOException
88 */
89 public static Properties loadPropertiesFromClasspath(String name) throws Exception
90 {
91 return loadPropertiesFromClasspath(name, name.getClass().getClassLoader());
92 }
93
94 /***
95 * This method extracts a subset of properties with the prefix provided from
96 * the properties object provided. The names of the resulting properties
97 * are sunstringed to exclude the prefix
98 *
99 * @param prefix
100 * @param properties
101 * @return
102 */
103 public static Properties subset(String prefix, Properties properties)
104 {
105 Properties newProperties = new Properties();
106 Enumeration e = properties.propertyNames();
107 while (e.hasMoreElements())
108 {
109 String itemName = (String) e.nextElement();
110 if (itemName.startsWith(prefix))
111 newProperties.put(itemName.substring(prefix.length(), itemName.length()), properties.getProperty(itemName).trim());
112 }
113 return newProperties;
114 }
115
116 /***
117 * This method appends one set of properties to another.
118 *
119 * @param appendFrom
120 * @param appendTo
121 * @throws InvalidCOnfigurationException if one of the properties already
122 * exists
123 */
124 private static void append(Properties appendFrom, Properties appendTo)
125 {
126 Enumeration e = appendFrom.propertyNames();
127 while (e.hasMoreElements())
128 {
129 String itemName = (String) e.nextElement();
130 appendTo.put(itemName, appendFrom.getProperty(itemName).trim());
131 }
132 }
133
134 /***
135 * This method returns a properties path. Of the format
136 * "[user.home]/.[prefix]/[prefix].prop" If te directory does not exist, it
137 * will create it.
138 */
139 public static String getStandardPropertiesPath(String prefix)
140 {
141 File propertiesDirectory = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "." + prefix);
142 if (!propertiesDirectory.exists())
143 propertiesDirectory.mkdir();
144
145 return System.getProperty("user.home") + System.getProperty("file.separator") + "." + prefix + System.getProperty("file.separator") + prefix + ".properties";
146 }
147
148 /***
149 * Returns a font from the properties object if there is one or a default
150 * ifthere isnt
151 *
152 * @deprecated does nothing
153 */
154 public static Font getPropertiesFont(Properties properties)
155 {
156 log.debug("getPropertiesFont has been called andis not correctly implemented");
157
158
159
160
161
162
163
164 return null;
165 }
166
167 /***
168 * This method sets the frame size of the given frame. <br>
169 * If the property ExpConstants.STARTMAXIMISED is true then it will start
170 * maximised <br>
171 * otherwise it will set the frame size to the sizes given by the properties
172 * ExpConstants.HEIGHT and ExpConstants.WIDTH <br>
173 * if these are notfoundthenitwilldefaulttothedefaultsizesprovided
174 *
175 * @deprecated does nothing
176 */
177 public static void setFrameSizeFromProperties(Properties properties, JFrame frame, String defaultHeight, String defaultWidth) throws Exception
178 {
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 }
194
195 }