KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > SavableConfiguration


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import java.io.*;
37 import java.util.Iterator JavaDoc;
38 import java.util.Date JavaDoc;
39
40 /** A Configuration object that can be read and saved from a Stream.
41  * @version $Id: SavableConfiguration.java 3903 2006-07-05 20:03:06Z rcartwright $
42  */

43 public class SavableConfiguration extends Configuration {
44   /**
45    * Creates a new Configuration based on the given OptionMap.
46    * @param map an empty OptionMap
47    */

48   public SavableConfiguration(OptionMap map) { super(map); }
49
50   /**
51    * Creates an OptionMapLoader with the values loaded from the InputStream
52    * (and defaults where values weren't specified) and loads them into
53    * this Configuration's OptionMap.
54    * @param is InputStream containing properties-style keys and values
55    */

56   public void loadConfiguration(InputStream is) throws IOException {
57     new OptionMapLoader(is).loadInto(map);
58   }
59
60   /**
61    * Used to save the values from this Configuration into the given OutputStream
62    * as a Properties file. The elements weren't ordered, so now the properties
63    * are written in the same way as the about dialog.
64    * Values equal to their defaults are not written to disk.
65    */

66   public void saveConfiguration(OutputStream os, String JavaDoc header) throws IOException {
67     Writer w = new BufferedWriter(new OutputStreamWriter(os));
68     Iterator JavaDoc<OptionParser<?>> keys = map.keys();
69     //Properties p = new Properties();
70
// String tmpString;
71
// StringBuffer buff;
72
// OptionParser<?> key;
73

74     // Write the header
75
Date JavaDoc date = new Date JavaDoc();
76     w.write((int)'#');
77     w.write(header, 0, header.length());
78     w.write((int)'\n');
79     w.write((int)'#');
80     w.write(date.toString(), 0, date.toString().length());
81     w.write((int)'\n');
82
83     // Write each option
84
while (keys.hasNext()) {
85       
86       OptionParser<?> key = keys.next();
87
88       if (!key.getDefault().equals(map.getOption(key))) {
89
90         // Write name
91
String JavaDoc tmpString = key.getName();
92         w.write(tmpString, 0, tmpString.length());
93
94         // Write equals sign
95
tmpString = " = ";
96         w.write(tmpString, 0, 3);
97
98         // Write value
99
tmpString = map.getString(key);
100         // This replaces all backslashes with two backslashes for windows
101
int index = 0;
102         int pos;
103         while (index < tmpString.length() &&
104                ((pos = tmpString.indexOf('\\', index)) >= 0)) {
105           final StringBuilder JavaDoc buff = new StringBuilder JavaDoc(tmpString); // should use StringBuilder, but not 1.4 compatible
106
buff.insert(pos, '\\');
107           index = pos + 2;
108           tmpString = buff.toString();
109         }
110         w.write(tmpString, 0, tmpString.length());
111         w.write((int)'\n');
112
113         // p.setProperty(key.getName(),map.getString(key));
114
}
115     }
116     w.close();
117     //p.store(os,header)
118
}
119 }
120
Popular Tags