1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package us.paulevans.basicxslt; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.OutputStream; |
20 | |
import java.util.Arrays; |
21 | |
import java.util.Enumeration; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
import java.util.Properties; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
28 | |
import org.apache.log4j.Logger; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 12 | public class UserPreferences extends Properties { |
36 | |
|
37 | |
|
38 | 1 | private static final Logger logger = Logger.getLogger( |
39 | |
UserPreferences.class); |
40 | |
|
41 | |
|
42 | |
private String currentConfiguration; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public String getProperty(String aName, String aDefaultValue) { |
51 | |
|
52 | |
String value; |
53 | |
|
54 | 1 | value = getProperty(aName); |
55 | 1 | if (StringUtils.isBlank(value)) { |
56 | 1 | value = aDefaultValue; |
57 | |
} |
58 | 1 | return value; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public String getProperty(String aName) { |
66 | 4 | return super.getProperty(currentConfiguration + "." + aName); |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public Object setProperty(String aName, String aValue) { |
75 | 6 | if (aName == null) { |
76 | 2 | throw new NullPointerException(); |
77 | |
} |
78 | 4 | return super.setProperty(currentConfiguration + "." + aName, aValue); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public String getPropertyNoPrefix(String aName) { |
88 | 52 | return super.getProperty(aName); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public Object setPropertyNoPrefix(String aName, String aValue) { |
99 | 61 | return super.setProperty(aName, aValue); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public void setConfiguration(String aConfiguration, |
109 | |
boolean aMakeAsDefault) { |
110 | 11 | currentConfiguration = aConfiguration; |
111 | 11 | if (aMakeAsDefault) { |
112 | 10 | setPropertyNoPrefix(AppConstants.DEFAULT_CONFIGURATION_PROP, |
113 | |
currentConfiguration); |
114 | |
} |
115 | 11 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public String getConfiguration() { |
122 | 2 | return currentConfiguration; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public String loadDefaultConfiguration() { |
131 | 2 | currentConfiguration = getPropertyNoPrefix( |
132 | |
AppConstants.DEFAULT_CONFIGURATION_PROP); |
133 | 2 | if (StringUtils.isBlank(currentConfiguration)) { |
134 | 1 | currentConfiguration = AppConstants.DEFAULT_CONFIGURATION; |
135 | |
} |
136 | 2 | return currentConfiguration; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void copyCurrentPreferences(String aNewConfig) { |
145 | |
|
146 | |
String fullPropName, propNameSuffix; |
147 | |
Enumeration propertyNames; |
148 | |
|
149 | 1 | propertyNames = propertyNames(); |
150 | 134 | while (propertyNames.hasMoreElements()) { |
151 | 133 | fullPropName = (String)propertyNames.nextElement(); |
152 | 133 | if (fullPropName.startsWith(currentConfiguration + ".")) { |
153 | 46 | propNameSuffix = fullPropName.substring( |
154 | |
fullPropName.indexOf(".")+1); |
155 | 46 | setPropertyNoPrefix(aNewConfig + "." + propNameSuffix, |
156 | |
getPropertyNoPrefix(fullPropName)); |
157 | 46 | } |
158 | |
} |
159 | 1 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public void clearDynamicPrefs() { |
167 | |
|
168 | |
Enumeration propertyNames; |
169 | |
String propertyName; |
170 | |
|
171 | 1 | propertyNames = propertyNames(); |
172 | 134 | while(propertyNames.hasMoreElements()) { |
173 | 133 | propertyName = (String)propertyNames.nextElement(); |
174 | 133 | if (propertyName.startsWith(currentConfiguration + ".xsl_") || |
175 | |
propertyName.startsWith( |
176 | |
currentConfiguration + ".xml_identity_transform")) { |
177 | 26 | remove(propertyName); |
178 | 26 | } |
179 | |
} |
180 | 1 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public void persistUserPrefs() throws IOException { |
186 | |
|
187 | |
OutputStream out; |
188 | |
|
189 | 0 | out = null; |
190 | |
try { |
191 | 0 | store(out = Utils.getUserPrefsOutputStream(), |
192 | |
LabelStringFactory.getInstance().getString( |
193 | |
LabelStringFactory.TOOL_USERPREFERENCES_TITLE)); |
194 | 0 | out.flush(); |
195 | 0 | } catch (IOException aException) { |
196 | |
|
197 | 0 | logger.error(ExceptionUtils.getFullStackTrace(aException)); |
198 | 0 | throw aException; |
199 | |
} finally { |
200 | 0 | Utils.closeQuietly(out); |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public String[] getAllConfigurations() { |
209 | |
|
210 | |
Map<String,String> configurations; |
211 | |
Enumeration propertyNames; |
212 | |
String propertyName, configuration; |
213 | |
String configs[]; |
214 | |
int index; |
215 | |
|
216 | 1 | configurations = new HashMap<String,String>(); |
217 | 1 | propertyNames = propertyNames(); |
218 | 134 | while(propertyNames.hasMoreElements()) { |
219 | 133 | propertyName = (String)propertyNames.nextElement(); |
220 | 133 | if ((!propertyName.startsWith(currentConfiguration)) && |
221 | |
(index = propertyName.indexOf(".")) != -1) { |
222 | 84 | configuration = propertyName.substring(0, index); |
223 | 84 | configurations.put(configuration, configuration); |
224 | 84 | } |
225 | |
} |
226 | 1 | configs = (String[])configurations.values().toArray( |
227 | |
new String[configurations.size()]); |
228 | 1 | Arrays.sort(configs); |
229 | 1 | return configs; |
230 | |
} |
231 | |
} |