if (!configFile.exists()) {
return null;
} else{
Configuration configuration = new Configuration(configuredClass);
try {
//get a xml-document object from the file
Builder builder = new Builder();
Document configurationDocument = builder.build(configFile);
//get configuration set from the xml document
Element xmlRootElement = configurationDocument.getRootElement();
if( xmlRootElement == null ){
System.out.println("error: file \"" + configFile + "\" is not a valid configuration file: <configuration>-tags missing");
}
else{
Elements xmlConfigurationElements = xmlRootElement.getChildElements("conf");
for( int xmlConfigurationElementIndex = 0; xmlConfigurationElementIndex < xmlConfigurationElements.size(); ++ xmlConfigurationElementIndex ){
Element xmlConfigurationElement = xmlConfigurationElements.get(xmlConfigurationElementIndex);
String name = null;
String type = null;
boolean list = false;
Attribute attributeName = xmlConfigurationElement.getAttribute("name");
if( attributeName != null ){
name = attributeName.getValue();
}
else{
System.out.println("warning: configuration data partially invalid: no name attribute at \"conf\" child element " + String.valueOf(xmlConfigurationElementIndex) );
}
Attribute attributeType = xmlConfigurationElement.getAttribute("type");
if( attributeType != null ){
type = attributeType.getValue();
}
else{
System.out.println("warning: configuration data partially invalid: no type attribute at \"conf\" child element " + String.valueOf(xmlConfigurationElementIndex) );
}
Attribute attributeList = xmlConfigurationElement.getAttribute("list");
if( attributeList != null ){
list = stringToBoolean(attributeList.getValue());
}
if (name != null && type != null) {
Field field = null;
for (Field f : configuration.getConfigurableFields()) {
if (f.getName().equals(name)) {
field = f;
break;
}
}
if (field != null) {
if (!list) {
String data = null;
if( xmlConfigurationElement.getChildCount() > 0 ){
Node dataText = xmlConfigurationElement.getChild(0);
if( dataText != null ){
data = ((Text)dataText).getValue();
}
}
if (data == null) {
data = "";
}
if (type.toLowerCase().equals("integer")) {
configuration.setValue(field, Integer.parseInt(data));
} else if (type.toLowerCase().equals("float")) {
configuration.setValue(field, Float.parseFloat(data));
} else if (type.toLowerCase().equals("string")) {
configuration.setValue(field, data);
} else if (type.toLowerCase().equals("boolean")) {
configuration.setValue(field, stringToBoolean(data));
}
} else { // list
List<String> dataList = new LinkedList<String>();
Elements childs = xmlConfigurationElement.getChildElements("entry");
if (childs.size() > 0) {
for (int i = 0; i < childs.size(); i++) {
dataList.add(childs.get(i).getValue());
}
} else {
//note: getChild also returns the element's text
Text text = (Text)xmlConfigurationElement.getChild(0);
if (text != null) {
dataList.add(text.getValue());
}
}
if (type.toLowerCase().equals("integer")) {
List<Integer> ints = new LinkedList<Integer>();
for (String data : dataList) {
ints.add(Integer.parseInt(data));
}
configuration.setValue(field,
ints.toArray(new Integer[ints.size()]));
} else if (type.toLowerCase().equals("float")) {
List<Float> floats = new LinkedList<Float>();
for (String data : dataList) {
floats.add(Float.parseFloat(data));
}
configuration.setValue(field,
floats.toArray(new Float[floats.size()]));
} else if (type.toLowerCase().equals("string")) {
configuration.setValue(field,
dataList.toArray(new String[dataList.size()]));
} else if (type.toLowerCase().equals("boolean")) {
List<Boolean> bools = new LinkedList<Boolean>();
for (String data : dataList) {
bools.add(stringToBoolean(data));
}
configuration.setValue(field,
bools.toArray(new Boolean[bools.size()]));
}
}