11package javaxt .express ;
22import javaxt .json .JSONObject ;
33import javaxt .encryption .AES256 ;
4- import javaxt .json .JSONValue ;
54
65//******************************************************************************
76//** ConfigFile
87//******************************************************************************
98/**
10- * Used to access and save configuration information to an encrypted JSON
11- * file.
9+ * Used to access and save configuration information stored in a JSON file.
10+ * Provides an option to encrypt the JSON file.
1211 *
1312 ******************************************************************************/
1413
1514public class ConfigFile {
1615
1716 private javaxt .io .File file ;
17+ private JSONObject config ;
1818
1919
2020 //**************************************************************************
@@ -43,7 +43,9 @@ public boolean exists(){
4343 }
4444
4545
46-
46+ //**************************************************************************
47+ //** getDirectory
48+ //**************************************************************************
4749 public javaxt .io .Directory getDirectory (){
4850 return file .getDirectory ();
4951 }
@@ -59,20 +61,52 @@ public void delete(){
5961 }
6062
6163
64+ //**************************************************************************
65+ //** getConfig
66+ //**************************************************************************
67+ /** Returns the current config (json document).
68+ */
69+ public JSONObject getConfig (){
70+ if (config ==null ) config = file .getJSONObject ();
71+ return config ;
72+ }
73+
74+
6275 //**************************************************************************
6376 //** getConfig
6477 //**************************************************************************
6578 /** Used to decrypt and parse a config file (json document).
6679 */
6780 public JSONObject getConfig (String username , String password )
6881 throws java .security .InvalidKeyException , Exception {
69-
70- return new JSONObject (
82+ if (config ==null ) config = new JSONObject (
7183 AES256 .decrypt (
7284 file .getBytes ().toByteArray (),
7385 generatePassword (username , password )
7486 )
7587 );
88+ return config ;
89+ }
90+
91+
92+ //**************************************************************************
93+ //** save
94+ //**************************************************************************
95+ /** Used to save the config file.
96+ */
97+ public void save (){
98+ file .write (getConfig ());
99+ }
100+
101+
102+ //**************************************************************************
103+ //** save
104+ //**************************************************************************
105+ /** Used to encrypt and save the config file.
106+ */
107+ public void save (String username , String password )
108+ throws java .security .InvalidKeyException , Exception {
109+ save (getConfig (), username , password );
76110 }
77111
78112
@@ -94,7 +128,7 @@ public void save(JSONObject config, String username, String password)
94128 //**************************************************************************
95129 //** generatePassword
96130 //**************************************************************************
97- /** Used to generate a password/key used to encrypt/decrypt the config file.
131+ /** Used to generate a password/key used to encrypt/decrypt a config file.
98132 */
99133 private static String generatePassword (String username , String password ) throws Exception {
100134 return ( javaxt .utils .Base64 .encode (
@@ -103,28 +137,32 @@ private static String generatePassword(String username, String password) throws
103137 }
104138
105139
140+ //**************************************************************************
141+ //** updateDir
142+ //**************************************************************************
143+ /** Used to update a path to a directory defined in the config file.
144+ * Resolves both canonical and relative paths (relative to the configFile).
145+ */
146+ public void updateDir (String key , boolean create ){
147+ updateDir (key , getConfig (), file , create );
148+ }
106149
107150
108151 //**************************************************************************
109- //** getFile
152+ //** updateDir
110153 //**************************************************************************
111- /** Returns a File for a given path
112- * @param path Full canonical path to a file or a relative path (relative
113- * to the jarFile)
154+ /** Used to update a path to a directory defined in a given config. Resolves
155+ * both canonical and relative paths (relative to the configFile).
114156 */
115- public static javaxt .io .File getFile (String path , javaxt .io .File jarFile ){
116- javaxt .io .File file = new javaxt .io .File (path );
117- if (!file .exists ()){
118- file = new javaxt .io .File (jarFile .MapPath (path ));
119- }
120- return file ;
157+ public void updateDir (String key , JSONObject config , boolean create ){
158+ updateDir (key , config , file , create );
121159 }
122160
123161
124162 //**************************************************************************
125163 //** updateDir
126164 //**************************************************************************
127- /** Used to update a path to a directory defined in a config file . Resolves
165+ /** Used to update a path to a directory defined in a given config . Resolves
128166 * both canonical and relative paths (relative to the configFile).
129167 */
130168 public static void updateDir (String key , JSONObject config , javaxt .io .File configFile , boolean create ){
@@ -140,26 +178,34 @@ public static void updateDir(String key, JSONObject config, javaxt.io.File confi
140178 }
141179 else {
142180
143- javaxt .io .Directory dir = new javaxt .io .Directory (path );
181+ //Get directory
182+ javaxt .io .Directory dir ;
183+ if (isRelativePath (path )){
184+ path = configFile .MapPath (path );
185+ dir = new javaxt .io .Directory (new java .io .File (path ));
186+ }
187+ else {
188+ dir = new javaxt .io .Directory (path );
189+ }
190+
191+
192+ //Get canonical path
144193 if (dir .exists ()){
145194 try {
146- java .io .File f = new java .io .File (path );
147- javaxt .io .Directory d = new javaxt .io .Directory (f .getCanonicalFile ());
195+ javaxt .io .Directory d = new javaxt .io .Directory (dir .toFile ().getCanonicalFile ());
148196 if (!dir .toString ().equals (d .toString ())){
149197 dir = d ;
150198 }
151199 }
152- catch (Exception e ){
153- }
154- }
155- else {
156- dir = new javaxt .io .Directory (new java .io .File (configFile .MapPath (path )));
200+ catch (Exception e ){}
157201 }
158202
159203
204+ //Create directory as needed
160205 if (!dir .exists () && create ) dir .create ();
161206
162207
208+ //Update config
163209 if (dir .exists ()){
164210 config .set (key , dir .toString ());
165211 }
@@ -175,8 +221,30 @@ public static void updateDir(String key, JSONObject config, javaxt.io.File confi
175221 //**************************************************************************
176222 //** updateFile
177223 //**************************************************************************
178- /** Used to update a path to a file defined in a config file. Resolves
224+ /** Used to update a path to a file defined in the config file. Resolves
179225 * both canonical and relative paths (relative to the configFile).
226+ */
227+ public void updateFile (String key ){
228+ updateFile (key , getConfig (), file );
229+ }
230+
231+
232+ //**************************************************************************
233+ //** updateFile
234+ //**************************************************************************
235+ /** Used to update a path to a file defined a given config. Resolves both
236+ * canonical and relative paths (relative to the configFile).
237+ */
238+ public void updateFile (String key , JSONObject config ){
239+ updateFile (key , config , file );
240+ }
241+
242+
243+ //**************************************************************************
244+ //** updateFile
245+ //**************************************************************************
246+ /** Used to update a path to a file defined in a config. Resolves both
247+ * canonical and relative paths (relative to a configFile).
180248 */
181249 public static void updateFile (String key , JSONObject config , javaxt .io .File configFile ){
182250 if (config .has (key )){
@@ -191,32 +259,61 @@ public static void updateFile(String key, JSONObject config, javaxt.io.File conf
191259 }
192260 else {
193261
194- javaxt .io .File file = new javaxt .io .File (path );
262+ //Get file
263+ javaxt .io .File file = getFile (path , configFile );
264+
265+
266+ //Get canonical path
195267 if (file .exists ()){
196268 try {
197- java .io .File f = new java .io .File (path );
198- javaxt .io .File _file = new javaxt .io .File (f .getCanonicalFile ());
199- if (!file .toString ().equals (_file .toString ())){
200- file = _file ;
269+ javaxt .io .File f = new javaxt .io .File (file .toFile ().getCanonicalFile ());
270+ if (!file .toString ().equals (f .toString ())){
271+ file = f ;
201272 }
202273 }
203- catch (Exception e ){
204- }
205- }
206- else {
207- file = new javaxt .io .File (configFile .MapPath (path ));
274+ catch (Exception e ){}
208275 }
209276
277+
278+ //Update config
210279 config .set (key , file .toString ());
211- // if (file.exists()){
212- // config.set(key, file.toString());
213- // }
214- // else{
215- // config.remove(key);
216- // }
217280 }
218281 }
219282 }
220283 }
221284
285+
286+ //**************************************************************************
287+ //** getFile
288+ //**************************************************************************
289+ /** Returns a File for a given path. Resolves both canonical and relative
290+ * paths (relative to the given file).
291+ * @param path Full canonical path or a relative path
292+ * @param file If a relative path is given, the path is resolved to the
293+ * given file
294+ */
295+ public static javaxt .io .File getFile (String path , javaxt .io .File file ){
296+ if (isRelativePath (path )){
297+ return new javaxt .io .File (file .MapPath (path ));
298+ }
299+ else {
300+ return new javaxt .io .File (path );
301+ }
302+ }
303+
304+
305+ //**************************************************************************
306+ //** isRelativePath
307+ //**************************************************************************
308+ /** Returns true is the given path appears to be relative
309+ */
310+ private static boolean isRelativePath (String path ){
311+ path = path .replace ("\\ " , "/" );
312+ if (path .startsWith ("/" )) return false ;
313+ if (System .getProperty ("os.name" ).toLowerCase ().startsWith ("windows" )){
314+ if (path .indexOf (":" )==1 ) return false ;
315+ }
316+ return true ;
317+ }
318+
222319}
0 commit comments