1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* ------------------------------
28
* SortedConfigurationWriter.java
29
* ------------------------------
30
* (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
35
* $Id: SortedConfigurationWriter.java,v 1.4 2005/11/03 09:55:27 mungady Exp $
42
package org.jfree.util;
44
import java.io.BufferedOutputStream;
46
import java.io.FileOutputStream;
47
import java.io.IOException;
48
import java.io.OutputStream;
49
import java.io.OutputStreamWriter;
50
import java.io.Writer;
51
import java.util.ArrayList;
52
import java.util.Collections;
53
import java.util.Iterator;
56
* Writes a <code>Configuration</code> instance into a property file, where
57
* the keys are sorted by their name. Writing sorted keys make it easier for
58
* users to find and change properties in the file.
60
* @author Thomas Morgner
62
public class SortedConfigurationWriter {
64
* A constant defining that text should be escaped in a way
65
* which is suitable for property keys.
67
private static final int ESCAPE_KEY = 0;
69
* A constant defining that text should be escaped in a way
70
* which is suitable for property values.
72
private static final int ESCAPE_VALUE = 1;
74
* A constant defining that text should be escaped in a way
75
* which is suitable for property comments.
77
private static final int ESCAPE_COMMENT = 2;
79
/** The system-dependent End-Of-Line separator. */
80
private static final String END_OF_LINE = StringUtils.getLineSeparator();
83
* The default constructor, does nothing.
85
public SortedConfigurationWriter() {
89
* Returns a description for the given key. This implementation returns
90
* null to indicate that no description should be written. Subclasses can
91
* overwrite this method to provide comments for every key. These descriptions
92
* will be included as inline comments.
94
* @param key the key for which a description should be printed.
95
* @return the description or null if no description should be printed.
97
protected String getDescription(final String key) {
102
* Saves the given configuration into a file specified by the given
105
* @param filename the filename
106
* @param config the configuration
107
* @throws IOException if an IOError occurs.
109
public void save(final String filename, final Configuration config)
111
save(new File(filename), config);
115
* Saves the given configuration into a file specified by the given
118
* @param file the target file
119
* @param config the configuration
120
* @throws IOException if an IOError occurs.
122
public void save(final File file, final Configuration config)
124
final BufferedOutputStream out =
125
new BufferedOutputStream(new FileOutputStream(file));
132
* Writes the configuration into the given output stream.
134
* @param outStream the target output stream
135
* @param config the configuration
136
* @throws IOException if writing fails.
138
public void save(final OutputStream outStream, final Configuration config)
140
final ArrayList names = new ArrayList();
142
// clear all previously set configuration settings ...
143
final Iterator defaults = config.findPropertyKeys("");
144
while (defaults.hasNext()) {
145
final String key = (String) defaults.next();
149
Collections.sort(names);
151
final OutputStreamWriter out =
152
new OutputStreamWriter(outStream, "iso-8859-1");
154
for (int i = 0; i < names.size(); i++) {
155
final String key = (String) names.get(i);
156
final String value = config.getConfigProperty(key);
158
final String description = getDescription(key);
159
if (description != null) {
160
writeDescription(description, out);
162
saveConvert(key, ESCAPE_KEY, out);
164
saveConvert(value, ESCAPE_VALUE, out);
165
out.write(END_OF_LINE);
172
* Writes a descriptive comment into the given print writer.
174
* @param text the text to be written. If it contains more than
175
* one line, every line will be prepended by the comment character.
176
* @param writer the writer that should receive the content.
177
* @throws IOException if writing fails
179
private void writeDescription(final String text, final Writer writer)
181
// check if empty content ... this case is easy ...
182
if (text.length() == 0) {
187
writer.write(END_OF_LINE);
188
final LineBreakIterator iterator = new LineBreakIterator(text);
189
while (iterator.hasNext()) {
191
saveConvert((String) iterator.next(), ESCAPE_COMMENT, writer);
192
writer.write(END_OF_LINE);
197
* Performs the necessary conversion of an java string into a property
200
* @param text the text to be escaped
201
* @param escapeMode the mode that should be applied.
202
* @param writer the writer that should receive the content.
203
* @throws IOException if writing fails
205
private void saveConvert(final String text, final int escapeMode,
208
final char[] string = text.toCharArray();
210
for (int x = 0; x < string.length; x++) {
211
final char aChar = string[x];
215
if ((escapeMode != ESCAPE_COMMENT)
216
&& (x == 0 || escapeMode == ESCAPE_KEY)) {
230
if (escapeMode == ESCAPE_COMMENT) {
253
if (escapeMode == ESCAPE_COMMENT) {
268
if (escapeMode == ESCAPE_COMMENT) {
278
if ((aChar < 0x0020) || (aChar > 0x007e)) {
281
writer.write(HEX_CHARS[(aChar >> 12) & 0xF]);
282
writer.write(HEX_CHARS[(aChar >> 8) & 0xF]);
283
writer.write(HEX_CHARS[(aChar >> 4) & 0xF]);
284
writer.write(HEX_CHARS[aChar & 0xF]);
293
/** A lookup-table. */
294
private static final char[] HEX_CHARS =
295
{'0', '1', '2', '3', '4', '5', '6', '7',
296
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};