March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -0,0 +1,70 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConfigReader {
|
||||
private static final Pattern LINE_PATTERN = Pattern.compile( "([^ =]+)[ =]?(.*)" );
|
||||
private static final Map<String, Object> DEFAULTS = new HashMap<String, Object>() {{
|
||||
put( "needspeeling", false );
|
||||
put( "seedsremoved", false );
|
||||
}};
|
||||
|
||||
public static void main( final String[] args ) {
|
||||
System.out.println( parseFile( args[ 0 ] ) );
|
||||
}
|
||||
|
||||
public static Map<String, Object> parseFile( final String fileName ) {
|
||||
final Map<String, Object> result = new HashMap<String, Object>( DEFAULTS );
|
||||
/*v*/ BufferedReader reader = null;
|
||||
|
||||
try {
|
||||
reader = new BufferedReader( new FileReader( fileName ) );
|
||||
for ( String line; null != ( line = reader.readLine() ); ) {
|
||||
parseLine( line, result );
|
||||
}
|
||||
} catch ( final IOException x ) {
|
||||
throw new RuntimeException( "Oops: " + x, x );
|
||||
} finally {
|
||||
if ( null != reader ) try {
|
||||
reader.close();
|
||||
} catch ( final IOException x2 ) {
|
||||
System.err.println( "Could not close " + fileName + " - " + x2 );
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void parseLine( final String line, final Map<String, Object> map ) {
|
||||
if ( "".equals( line.trim() ) || line.startsWith( "#" ) || line.startsWith( ";" ) )
|
||||
return;
|
||||
|
||||
final Matcher matcher = LINE_PATTERN.matcher( line );
|
||||
|
||||
if ( ! matcher.matches() ) {
|
||||
System.err.println( "Bad config line: " + line );
|
||||
return;
|
||||
}
|
||||
|
||||
final String key = matcher.group( 1 ).trim().toLowerCase();
|
||||
final String value = matcher.group( 2 ).trim();
|
||||
|
||||
if ( "".equals( value ) ) {
|
||||
map.put( key, true );
|
||||
} else if ( -1 == value.indexOf( ',' ) ) {
|
||||
map.put( key, value );
|
||||
} else {
|
||||
final String[] values = value.split( "," );
|
||||
|
||||
for ( int i = 0; i < values.length; i++ ) {
|
||||
values[ i ] = values[ i ].trim();
|
||||
}
|
||||
map.put( key, Arrays.asList( values ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
data class Configuration(val map: Map<String, Any?>) {
|
||||
val fullName: String by Delegates.mapVal(map)
|
||||
val favoriteFruit: String by Delegates.mapVal(map)
|
||||
val needsPeeling: Boolean by Delegates.mapVal(map)
|
||||
val otherFamily: List<String> by Delegates.mapVal(map)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val configurationPath = Paths.get(args[0])!!
|
||||
|
||||
val configurables = Files.readAllLines(configurationPath, StandardCharsets.UTF_8)
|
||||
.map { it.trim() }
|
||||
.filterNot { it.isEmpty() }
|
||||
.filterNot(::commentedOut)
|
||||
.map(::toKeyValuePairs)
|
||||
|
||||
val configurationMap: MutableMap<String, Any?> = hashMapOf("needsPeeling" to false)
|
||||
for (configurable in configurables) {
|
||||
val (key, value) = configurable
|
||||
when (key) {
|
||||
"FULLNAME" -> configurationMap.put("fullName", value)
|
||||
"FAVOURITEFRUIT" -> configurationMap.put("favoriteFruit", value)
|
||||
"NEEDSPEELING" -> configurationMap.put("needsPeeling", true)
|
||||
"OTHERFAMILY" -> configurationMap.put("otherFamily", value.split(" , ").map { it.trim() })
|
||||
else -> println("Encountered unexpected key ${key}=${value}")
|
||||
}
|
||||
}
|
||||
|
||||
val configuration = Configuration(configurationMap)
|
||||
}
|
||||
|
||||
private fun commentedOut(line: String): Boolean = (line.indexOf("#") == 0 || line.indexOf(";") == 0)
|
||||
|
||||
private fun toKeyValuePairs(line: String): Pair<String, String> {
|
||||
return line.split(" ", 2).let {
|
||||
Pair(it[0], if (it.size == 1) "" else it[1])
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue