001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.htmltowiki;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.TreeMap;
026:
027: /**
028: * some usefull methods for properties
029: *
030: * @author Sebastian Baltes (sbaltes@gmx.com)
031: * @version 1.0
032: */
033: public final class PropertiesUtils {
034: private static final String OTHER_WHITESPACE = "\t\r\n\014";
035: private static final char[] HEXDIGIT = { '0', '1', '2', '3', '4',
036: '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
037:
038: /** Private constructor to prevent instantiation. */
039: private PropertiesUtils() {
040: }
041:
042: /**
043: * <p>
044: * like Properties.store, but stores the properties in sorted order
045: * </p>
046: *
047: * @param properties the properties object
048: * @return String the properties, nicely formatted
049: */
050: public static String toSortedString(Properties properties) {
051: TreeMap treemap = new TreeMap(properties);
052: String string = "";
053: Iterator iterator = treemap.entrySet().iterator();
054: while (iterator.hasNext()) {
055: Map.Entry entry = (Map.Entry) iterator.next();
056: String key = (String) entry.getKey();
057: String value = entry.getValue() == null ? "null" : entry
058: .getValue().toString();
059: string += toLine(key, value) + '\n';
060: }
061: return string;
062: }
063:
064: /**
065: * Generates a property file line from a supplied key and value.
066: * @param key the property's key
067: * @param value the property's value
068: * @return the converted string
069: */
070: public static String toLine(String key, String value) {
071: return saveConvert(key, true) + '=' + saveConvert(value, false);
072: }
073:
074: /**
075: * Encodes a property file string from a supplied key/value line.
076: * @param string the string to encode
077: * @param encodeWhiteSpace <code>true</code> if whitespace should be encoded also
078: * @return the converted string
079: */
080: public static String saveConvert(String string,
081: boolean encodeWhiteSpace) {
082: int i = string.length();
083: StringBuffer stringbuffer = new StringBuffer(i * 2);
084: for (int i3 = 0; i3 < i; i3++) {
085: char c = string.charAt(i3);
086: switch (c) {
087: case ' ':
088: if (i3 == 0 || encodeWhiteSpace) {
089: stringbuffer.append('\\');
090: }
091: stringbuffer.append(' ');
092: break;
093: case '\\':
094: stringbuffer.append('\\');
095: stringbuffer.append('\\');
096: break;
097: case '\t':
098: stringbuffer.append('\\');
099: stringbuffer.append('t');
100: break;
101: case '\n':
102: stringbuffer.append('\\');
103: stringbuffer.append('n');
104: break;
105: case '\r':
106: stringbuffer.append('\\');
107: stringbuffer.append('r');
108: break;
109: case '\014':
110: stringbuffer.append('\\');
111: stringbuffer.append('f');
112: break;
113: default:
114: if (c < 32 || c > 126) {
115: stringbuffer.append('\\');
116: stringbuffer.append('u');
117: stringbuffer.append(toHex(c >> 12 & 0xf));
118: stringbuffer.append(toHex(c >> 8 & 0xf));
119: stringbuffer.append(toHex(c >> 4 & 0xf));
120: stringbuffer.append(toHex(c & 0xf));
121: } else {
122: if (OTHER_WHITESPACE.indexOf(c) != -1) {
123: stringbuffer.append('\\');
124: }
125: stringbuffer.append(c);
126: }
127: }
128: }
129: return stringbuffer.toString();
130: }
131:
132: private static char toHex(int i) {
133: return HEXDIGIT[i & 0xf];
134: }
135: }
|