se.l4.commons.config.internal.RawFormatReader Maven / Gradle / Ivy
The newest version!
package se.l4.commons.config.internal;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import se.l4.commons.config.internal.streaming.ConfigJsonInput;
import se.l4.commons.config.internal.streaming.MapInput;
import se.l4.commons.serialization.SerializationException;
import se.l4.commons.serialization.format.StreamingInput;
import se.l4.commons.serialization.format.Token;
/**
* Reader of configuration values. Uses {@link ConfigJsonInput} and outputs
* objects that can be used with {@link MapInput}.
*
* @author Andreas Holstenson
*
*/
public class RawFormatReader
{
private RawFormatReader()
{
}
/**
* Convert the given stream to a map.
*
* @param stream
* @return
* @throws IOException
*/
public static Map read(InputStream stream)
throws IOException
{
ConfigJsonInput input = new ConfigJsonInput(new InputStreamReader(stream));
return read(input);
}
/**
* Convert the given stream to a map.
*
* @param input
* @return
* @throws IOException
*/
public static Map read(StreamingInput input)
throws IOException
{
Token next = input.peek();
if(next == null) return Collections.emptyMap();
switch(next)
{
case LIST_START:
case LIST_END:
case OBJECT_END:
case VALUE:
case NULL:
throw new SerializationException("Error in configuration file, should be in in key, value format");
}
return readMap(input);
}
/**
* Read a single map from the input, optionally while reading object
* start and end tokens.
*
* @param input
* @return
* @throws IOException
*/
private static Map readMap(StreamingInput input)
throws IOException
{
boolean readEnd = false;
if(input.peek() == Token.OBJECT_START)
{
// Check if the object is wrapped
readEnd = true;
input.next();
}
Token t;
Map result = new HashMap();
while((t = input.peek()) != Token.OBJECT_END && t != null)
{
// Read the key
input.next(Token.KEY);
String key = input.getString();
// Read the value
Object value = readDynamic(input);
result.put(key, value);
}
if(readEnd)
{
input.next(Token.OBJECT_END);
}
return result;
}
/**
* Read a list from the input.
*
* @param input
* @return
* @throws IOException
*/
private static List