001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with CleanSheets; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.io;
022    
023    import java.io.File;
024    import java.io.FileInputStream;
025    import java.io.FileOutputStream;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.io.OutputStream;
029    import java.util.Properties;
030    
031    /**
032     * An extension of properties, predetermined to be saved in a specified file.
033     * If the file exists, any properties therein are loaded.
034     * @author Einar Pehrson
035     */
036    public class NamedProperties extends Properties {
037    
038            /** The serialVersionUID of the NamedProperties.java */
039            private static final long serialVersionUID = 5734781797863986818L;
040    
041            /** The file in which the properties are and should be stored */
042            private File file;
043    
044            /**
045             * Creates the named properties.
046             * @param file the file in which the properties are and should be stored
047             * @throws IOException if the file could not be read from
048             */
049            public NamedProperties(File file) throws IOException {
050                    this(file, null);
051            }
052    
053            /**
054             * Creates the named properties.
055             * @param defaults the properties to use as defaults
056             * @param file the file in which the properties are and should be stored
057             * @throws IOException if the file could not be read from
058             */
059            public NamedProperties(File file, Properties defaults) {
060                    super(defaults);
061    
062                    if (file.exists()) {
063                            // Loads properties from file
064                            InputStream stream = null;
065                            try {
066                                    stream = new FileInputStream(file);
067                                    loadFromXML(stream);
068                            } catch (IOException e) {
069                            } finally {
070                                    try {
071                                            if (stream != null)
072                                                    stream.close();
073                                    } catch (IOException e) {}
074                            }
075                    }
076                    this.file = file;
077            }
078    
079            /**
080             * Invokes store() using the file that belongs to the properties.
081             * @param comment a description of the property list, or null if no comment is desired. 
082             * @throws IOException if the file could not be written to
083             */
084            public void store(String comment) throws IOException {
085                    OutputStream stream = null;
086                    try {
087                            stream = new FileOutputStream(file);
088                            super.store(stream, comment);
089                    } finally {
090                            try {
091                                    if (stream != null)
092                                            stream.close();
093                            } catch (IOException e) {}
094                    }
095            }
096    
097            /**
098             * Invokes storeToXML() using the file that belongs to the properties.
099             * @param comment a description of the property list, or null if no comment is desired. 
100             * @throws IOException if the file could not be written to
101             */
102            public void storeToXML(String comment) throws IOException {
103                    OutputStream stream = null;
104                    try {
105                            stream = new FileOutputStream(file);
106                            super.storeToXML(stream, comment);
107                    } finally {
108                            try {
109                                    if (stream != null)
110                                            stream.close();
111                            } catch (IOException e) {}
112                    }
113            }
114    }