1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| package org.ini4j; |
18 |
| |
19 |
| import java.util.prefs.PreferencesFactory; |
20 |
| import java.util.prefs.Preferences; |
21 |
| import java.util.Properties; |
22 |
| import java.io.InputStream; |
23 |
| |
24 |
| import java.net.URI; |
25 |
| import java.net.URL; |
26 |
| |
27 |
| public class IniPreferencesFactory implements PreferencesFactory |
28 |
| { |
29 |
| public static final String PROPERTIES = "ini4j.properties"; |
30 |
| |
31 |
| public static final String KEY_USER = "org.ini4j.prefs.user"; |
32 |
| public static final String KEY_SYSTEM = "org.ini4j.prefs.system"; |
33 |
| |
34 |
| private Preferences _system; |
35 |
| private Preferences _user; |
36 |
| |
37 |
2
| public synchronized Preferences systemRoot()
|
38 |
| { |
39 |
2
| if ( _system == null )
|
40 |
| { |
41 |
1
| _system = newIniPreferences(KEY_SYSTEM);
|
42 |
| } |
43 |
| |
44 |
2
| return _system;
|
45 |
| } |
46 |
| |
47 |
2
| public synchronized Preferences userRoot()
|
48 |
| { |
49 |
2
| if ( _user == null )
|
50 |
| { |
51 |
1
| _user = newIniPreferences(KEY_USER);
|
52 |
| } |
53 |
| |
54 |
2
| return _user;
|
55 |
| } |
56 |
| |
57 |
2
| protected Preferences newIniPreferences(String key)
|
58 |
| { |
59 |
2
| Ini ini = new Ini();
|
60 |
| |
61 |
2
| String location = getIniLocation(key);
|
62 |
| |
63 |
2
| if ( location != null )
|
64 |
| { |
65 |
1
| try
|
66 |
| { |
67 |
1
| ini.load(getResourceAsStream(location));
|
68 |
| } |
69 |
| catch (Exception x) |
70 |
| { |
71 |
0
| throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
|
72 |
| } |
73 |
| } |
74 |
| |
75 |
2
| return new IniPreferences(ini);
|
76 |
| } |
77 |
| |
78 |
2
| protected String getIniLocation(String key)
|
79 |
| { |
80 |
2
| String location = System.getProperty(key);
|
81 |
| |
82 |
2
| if ( location == null )
|
83 |
| { |
84 |
1
| try
|
85 |
| { |
86 |
1
| Properties props = new Properties();
|
87 |
1
| props.load(getClass().getClassLoader().getResourceAsStream(PROPERTIES));
|
88 |
0
| location = props.getProperty(key);
|
89 |
| } |
90 |
| catch (Exception x) |
91 |
| { |
92 |
| ; |
93 |
| } |
94 |
| } |
95 |
| |
96 |
2
| return location;
|
97 |
| } |
98 |
| |
99 |
5
| protected URL getResource(String location) throws IllegalArgumentException
|
100 |
| { |
101 |
5
| try
|
102 |
| { |
103 |
5
| URI uri = new URI(location);
|
104 |
4
| URL url;
|
105 |
| |
106 |
4
| if ( uri.getScheme() == null )
|
107 |
| { |
108 |
3
| url = getClass().getClassLoader().getResource(location);
|
109 |
| } |
110 |
| else |
111 |
| { |
112 |
1
| url = uri.toURL();
|
113 |
| } |
114 |
| |
115 |
4
| return url;
|
116 |
| } |
117 |
| catch (Exception x) |
118 |
| { |
119 |
1
| throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
|
120 |
| } |
121 |
| } |
122 |
| |
123 |
5
| protected InputStream getResourceAsStream(String location) throws IllegalArgumentException
|
124 |
| { |
125 |
5
| try
|
126 |
| { |
127 |
5
| return getResource(location).openStream();
|
128 |
| } |
129 |
| catch (Exception x) |
130 |
| { |
131 |
1
| throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
|
132 |
| } |
133 |
| } |
134 |
| } |