001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.config;
038:
039: import java.io.*;
040: import java.util.Iterator;
041: import java.util.Date;
042:
043: /** A Configuration object that can be read and saved from a Stream.
044: * @version $Id: SavableConfiguration.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public class SavableConfiguration extends Configuration {
047: /**
048: * Creates a new Configuration based on the given OptionMap.
049: * @param map an empty OptionMap
050: */
051: public SavableConfiguration(OptionMap map) {
052: super (map);
053: }
054:
055: /**
056: * Creates an OptionMapLoader with the values loaded from the InputStream
057: * (and defaults where values weren't specified) and loads them into
058: * this Configuration's OptionMap.
059: * @param is InputStream containing properties-style keys and values
060: */
061: public void loadConfiguration(InputStream is) throws IOException {
062: new OptionMapLoader(is).loadInto(map);
063: }
064:
065: /**
066: * Used to save the values from this Configuration into the given OutputStream
067: * as a Properties file. The elements weren't ordered, so now the properties
068: * are written in the same way as the about dialog.
069: * Values equal to their defaults are not written to disk.
070: */
071: public void saveConfiguration(OutputStream os, String header)
072: throws IOException {
073: Writer w = new BufferedWriter(new OutputStreamWriter(os));
074: Iterator<OptionParser<?>> keys = map.keys();
075: //Properties p = new Properties();
076: // String tmpString;
077: // StringBuffer buff;
078: // OptionParser<?> key;
079:
080: // Write the header
081: Date date = new Date();
082: w.write((int) '#');
083: w.write(header, 0, header.length());
084: w.write((int) '\n');
085: w.write((int) '#');
086: w.write(date.toString(), 0, date.toString().length());
087: w.write((int) '\n');
088:
089: // Write each option
090: while (keys.hasNext()) {
091:
092: OptionParser<?> key = keys.next();
093:
094: if (!key.getDefault().equals(map.getOption(key))) {
095:
096: // Write name
097: String tmpString = key.getName();
098: w.write(tmpString, 0, tmpString.length());
099:
100: // Write equals sign
101: tmpString = " = ";
102: w.write(tmpString, 0, 3);
103:
104: // Write value
105: tmpString = map.getString(key);
106: // This replaces all backslashes with two backslashes for windows
107: int index = 0;
108: int pos;
109: while (index < tmpString.length()
110: && ((pos = tmpString.indexOf('\\', index)) >= 0)) {
111: final StringBuilder buff = new StringBuilder(
112: tmpString); // should use StringBuilder, but not 1.4 compatible
113: buff.insert(pos, '\\');
114: index = pos + 2;
115: tmpString = buff.toString();
116: }
117: w.write(tmpString, 0, tmpString.length());
118: w.write((int) '\n');
119:
120: // p.setProperty(key.getName(),map.getString(key));
121: }
122: }
123: w.close();
124: //p.store(os,header)
125: }
126: }
|