001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    
016    
017    package lapisx.util;
018    
019    import java.util.*;
020    import java.io.*;
021    
022    public class Config extends Properties {
023        File file;
024        IOException lastException;
025    
026        public Config (String fileBaseName) {
027            this (fileBaseName, null);
028        }
029    
030        public Config (File file) {
031            this (file, null);
032        }
033    
034        public Config (String fileBaseName, Config defaults) {
035            this (new File (getHomeDirectory (), fileBaseName),
036                  defaults);
037        }
038    
039        public Config (File file, Config defaults) {
040            super (defaults);
041            this.file = file;
042            if(file.getParentFile()!=null)
043                file.getParentFile().mkdirs();
044            try {
045                FileInputStream in = 
046                    new FileInputStream (file);
047                load (in);
048                in.close ();
049            } catch (IOException e) {
050                lastException = e;
051            }
052        }
053    
054        public IOException getLastException () {
055            return lastException;
056        }
057    
058    //     public String getProperty (String key, String defaultValue) {
059    //         String val = super.getProperty (key, defaultValue);
060    //         if (val != null && val == defaultValue)
061    //             put (key, defaultValue);
062    //         return val;
063    //     }
064    
065        public void save () {
066            try {
067                FileOutputStream out = new FileOutputStream (file);
068                store (out, "");
069                out.close ();
070                lastException = null;
071            } catch (IOException e) {
072                lastException = e;
073            }
074        }
075    
076        public void rename(String fileName) {
077            rename(new File (getHomeDirectory(), fileName));
078        }
079    
080        public void rename(File newFile) {
081            file.delete();
082            this.file = newFile;
083            if(file.getParentFile()!=null)
084                file.getParentFile().mkdirs();
085        }
086            
087        public int countKeysStartingWith (String prefix) {
088            int n = 0;
089            for (Enumeration e = propertyNames (); e.hasMoreElements (); ) {
090                String name = (String)e.nextElement ();
091                if (name.startsWith (prefix))
092                    ++n;
093            }
094            return n;
095        }
096    
097        public void removeAllKeysStartingWith (String prefix) {
098            Vector keysToDelete = new Vector ();
099            for (Enumeration e = propertyNames (); e.hasMoreElements (); ) {
100                String name = (String)e.nextElement ();
101                if (name.startsWith (prefix))
102                    keysToDelete.addElement (name);
103            }
104            
105            for (Enumeration e = keysToDelete.elements ();
106                 e.hasMoreElements (); )
107                remove (e.nextElement ());
108        }
109        
110        public static File getHomeDirectory () {
111            String homedir;
112            if ((homedir = System.getProperty ("user.home")) == null
113                && (homedir = System.getProperty ("user.dir")) == null)
114                homedir = ".";
115            
116            return new File (homedir);
117        }
118    }