/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils.wga;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
public class SortedProperties extends Properties {
@SuppressWarnings("unchecked")
public synchronized Enumeration keys() {
Enumeration keysEnum = super.keys();
Vector genericKeyList = new Vector();
Vector prefixKeyList = new Vector();
while (keysEnum.hasMoreElements()) {
String key = (String) keysEnum.nextElement();
if (key.contains(".")) {
prefixKeyList.add(key);
} else {
genericKeyList.add(key);
}
}
Collections.sort(genericKeyList);
Collections.sort(prefixKeyList);
Vector finalKeyList = new Vector();
finalKeyList.addAll(genericKeyList);
finalKeyList.addAll(prefixKeyList);
return finalKeyList.elements();
}
public void store(OutputStream out, String comments) throws IOException {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
bw.write("#" + new Date().toString());
bw.newLine();
String lastkeyPrefix = "";
String key = null;
String val = null;
for (Enumeration e = keys(); e.hasMoreElements();) {
key = (String) e.nextElement();
val = (String) get(key);
key = LabelFileEncodingHelper.saveConvert(key, true);
val = LabelFileEncodingHelper.saveConvert(val, false);
if (!lastkeyPrefix.equals(key.substring(0, key.indexOf(".") + 1))) {
bw.newLine();
}
bw.write(key + "=" + val);
lastkeyPrefix = key.substring(0, key.indexOf(".") + 1);
bw.newLine();
}
bw.flush();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
}
}
}
}
}