You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
4.4 KiB
206 lines
4.4 KiB
11 years ago
|
package mightypork.utils.config;
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.LinkedHashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.Map.Entry;
|
||
|
|
||
11 years ago
|
import mightypork.utils.files.FileUtils;
|
||
11 years ago
|
import mightypork.utils.logging.Log;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Utility for parsing simple config files<br>
|
||
|
* # and // mark a comment<br>
|
||
|
* empty lines and lines without "=" are ignored<br>
|
||
|
* lines with "=" must have "key = value" format, or a warning is logged.<br>
|
||
|
* use "NULL" to create empty value.
|
||
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
|
public class SimpleConfig {
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Load list from file
|
||
|
*
|
||
11 years ago
|
* @param file file
|
||
11 years ago
|
* @return map of keys and values
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
public static List<String> listFromFile(File file) throws IOException
|
||
|
{
|
||
11 years ago
|
final String fileText = FileUtils.fileToString(file);
|
||
11 years ago
|
|
||
11 years ago
|
return listFromString(fileText);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Load map from file
|
||
|
*
|
||
11 years ago
|
* @param file file
|
||
11 years ago
|
* @return map of keys and values
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
public static Map<String, String> mapFromFile(File file) throws IOException
|
||
|
{
|
||
11 years ago
|
final String fileText = FileUtils.fileToString(file);
|
||
11 years ago
|
|
||
11 years ago
|
return mapFromString(fileText);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Load list from string
|
||
|
*
|
||
11 years ago
|
* @param text text of the file
|
||
11 years ago
|
* @return map of keys and values
|
||
|
*/
|
||
|
public static List<String> listFromString(String text)
|
||
|
{
|
||
11 years ago
|
final List<String> list = new ArrayList<String>();
|
||
11 years ago
|
|
||
11 years ago
|
final String[] groupsLines = text.split("\n");
|
||
11 years ago
|
|
||
11 years ago
|
for (String s : groupsLines) {
|
||
|
// ignore invalid lines
|
||
|
if (s.length() == 0) continue;
|
||
|
if (s.startsWith("#") || s.startsWith("//")) continue;
|
||
11 years ago
|
|
||
11 years ago
|
// NULL value
|
||
|
if (s.equalsIgnoreCase("NULL")) s = null;
|
||
11 years ago
|
|
||
11 years ago
|
if (s != null) s = s.replace("\\n", "\n");
|
||
11 years ago
|
|
||
11 years ago
|
// save extracted key-value pair
|
||
|
list.add(s);
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return list;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Load map from string
|
||
|
*
|
||
11 years ago
|
* @param text text of the file
|
||
11 years ago
|
* @return map of keys and values
|
||
|
*/
|
||
|
public static Map<String, String> mapFromString(String text)
|
||
|
{
|
||
11 years ago
|
final LinkedHashMap<String, String> pairs = new LinkedHashMap<String, String>();
|
||
11 years ago
|
|
||
11 years ago
|
final String[] groupsLines = text.split("\n");
|
||
11 years ago
|
|
||
11 years ago
|
for (final String s : groupsLines) {
|
||
11 years ago
|
// ignore invalid lines
|
||
|
if (s.length() == 0) continue;
|
||
|
if (s.startsWith("#") || s.startsWith("//")) continue;
|
||
|
if (!s.contains("=")) continue;
|
||
11 years ago
|
|
||
11 years ago
|
// split and trim
|
||
|
String[] parts = s.split("=");
|
||
|
for (int i = 0; i < parts.length; i++) {
|
||
|
parts[i] = parts[i].trim();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// check if both parts are valid
|
||
|
if (parts.length == 0) {
|
||
|
Log.w("Bad line in config file: " + s);
|
||
|
continue;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (parts.length == 1) {
|
||
|
parts = new String[] { parts[0], "" };
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (parts.length != 2) {
|
||
|
Log.w("Bad line in config file: " + s);
|
||
|
continue;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// NULL value
|
||
|
if (parts[0].equalsIgnoreCase("NULL")) parts[0] = null;
|
||
|
if (parts[1].equalsIgnoreCase("NULL")) parts[1] = null;
|
||
11 years ago
|
|
||
11 years ago
|
if (parts[0] != null) parts[0] = parts[0].replace("\\n", "\n");
|
||
|
if (parts[1] != null) parts[1] = parts[1].replace("\\n", "\n");
|
||
11 years ago
|
|
||
11 years ago
|
// save extracted key-value pair
|
||
|
pairs.put(parts[0], parts[1]);
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return pairs;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Save map to file
|
||
|
*
|
||
|
* @param target
|
||
|
* @param data
|
||
11 years ago
|
* @param allowNulls allow nulls.
|
||
11 years ago
|
* @throws IOException
|
||
|
*/
|
||
|
public static void mapToFile(File target, Map<String, String> data, boolean allowNulls) throws IOException
|
||
|
{
|
||
11 years ago
|
final List<String> lines = new ArrayList<String>();
|
||
11 years ago
|
|
||
11 years ago
|
for (final Entry<String, String> e : data.entrySet()) {
|
||
11 years ago
|
String key = e.getKey();
|
||
|
String value = e.getValue();
|
||
11 years ago
|
|
||
11 years ago
|
if (!allowNulls && (key == null || value == null || key.length() == 0 || value.length() == 0)) continue;
|
||
11 years ago
|
|
||
11 years ago
|
if (key == null) key = "NULL";
|
||
|
if (value == null) value = "NULL";
|
||
11 years ago
|
|
||
11 years ago
|
key = key.replace("\n", "\\n");
|
||
|
value = value.replace("\n", "\\n");
|
||
11 years ago
|
|
||
11 years ago
|
lines.add(key + " = " + value);
|
||
|
}
|
||
11 years ago
|
|
||
|
String text = ""; // # File written by SimpleConfig
|
||
|
|
||
11 years ago
|
for (final String s : lines) {
|
||
11 years ago
|
if (text.length() > 0) text += "\n";
|
||
11 years ago
|
|
||
11 years ago
|
text += s;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
FileUtils.stringToFile(target, text);
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Save list to file
|
||
|
*
|
||
|
* @param target
|
||
|
* @param data
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
public static void listToFile(File target, List<String> data) throws IOException
|
||
|
{
|
||
11 years ago
|
String text = ""; // # File written by SimpleConfig
|
||
|
|
||
11 years ago
|
for (String s : data) {
|
||
|
if (text.length() > 0) text += "\n";
|
||
11 years ago
|
|
||
11 years ago
|
if (s == null) s = "NULL";
|
||
11 years ago
|
|
||
11 years ago
|
s = s.replace("\n", "\\n");
|
||
11 years ago
|
|
||
11 years ago
|
text += s;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
FileUtils.stringToFile(target, text);
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
|
}
|