Refactor ConfigHandler to use EntryHandler
This commit is contained in:
parent
8bf63fff0c
commit
47af6fbb9f
@ -1,12 +1,9 @@
|
|||||||
package info.openrocket.core.file.openrocket.importt;
|
package info.openrocket.core.file.openrocket.importt;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import info.openrocket.core.logging.WarningSet;
|
import info.openrocket.core.logging.WarningSet;
|
||||||
import info.openrocket.core.file.simplesax.AbstractElementHandler;
|
|
||||||
import info.openrocket.core.file.simplesax.ElementHandler;
|
import info.openrocket.core.file.simplesax.ElementHandler;
|
||||||
import info.openrocket.core.file.simplesax.PlainTextHandler;
|
import info.openrocket.core.file.simplesax.PlainTextHandler;
|
||||||
import info.openrocket.core.util.ArrayList;
|
import info.openrocket.core.util.ArrayList;
|
||||||
@ -14,7 +11,7 @@ import info.openrocket.core.util.Config;
|
|||||||
|
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
public class ConfigHandler extends AbstractElementHandler {
|
public class ConfigHandler extends EntryHandler {
|
||||||
|
|
||||||
private ConfigHandler listHandler;
|
private ConfigHandler listHandler;
|
||||||
private final Config config = new Config();
|
private final Config config = new Config();
|
||||||
@ -36,17 +33,7 @@ public class ConfigHandler extends AbstractElementHandler {
|
|||||||
throws SAXException {
|
throws SAXException {
|
||||||
if (element.equals("entry")) {
|
if (element.equals("entry")) {
|
||||||
String key = attributes.get("key");
|
String key = attributes.get("key");
|
||||||
String type = attributes.get("type");
|
Object value = EntryHelper.getValueFromEntry(ConfigHandler.this, attributes, content);
|
||||||
Object value = null;
|
|
||||||
if ("boolean".equals(type)) {
|
|
||||||
value = Boolean.valueOf(content);
|
|
||||||
} else if ("string".equals(type)) {
|
|
||||||
value = content;
|
|
||||||
} else if ("number".equals(type)) {
|
|
||||||
value = parseNumber(content);
|
|
||||||
} else if ("list".equals(type)) {
|
|
||||||
value = listHandler.list;
|
|
||||||
}
|
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
if (key != null) {
|
if (key != null) {
|
||||||
config.put(key, value);
|
config.put(key, value);
|
||||||
@ -59,31 +46,6 @@ public class ConfigHandler extends AbstractElementHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Number parseNumber(String str) {
|
|
||||||
try {
|
|
||||||
str = str.trim();
|
|
||||||
if (str.matches("^[+-]?[0-9]+$")) {
|
|
||||||
BigInteger value = new BigInteger(str, 10);
|
|
||||||
if (value.equals(BigInteger.valueOf(value.intValue()))) {
|
|
||||||
return value.intValue();
|
|
||||||
} else if (value.equals(BigInteger.valueOf(value.longValue()))) {
|
|
||||||
return value.longValue();
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
BigDecimal value = new BigDecimal(str);
|
|
||||||
if (value.equals(BigDecimal.valueOf(value.doubleValue()))) {
|
|
||||||
return value.doubleValue();
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Config getConfig() {
|
public Config getConfig() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package info.openrocket.core.file.openrocket.importt;
|
||||||
|
|
||||||
|
import info.openrocket.core.file.simplesax.AbstractElementHandler;
|
||||||
|
import info.openrocket.core.util.ArrayList;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for entries that have a key and type attribute, and a value.
|
||||||
|
* For example <entry key="foo" type="string">bar</entry>
|
||||||
|
*/
|
||||||
|
public abstract class EntryHandler extends AbstractElementHandler {
|
||||||
|
protected EntryHandler listHandler;
|
||||||
|
protected final List<Object> list = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Object> getNestedList() {
|
||||||
|
if (listHandler != null) {
|
||||||
|
return listHandler.list;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package info.openrocket.core.file.openrocket.importt;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class for parsing entry elements that have a key and type attribute, and a value.
|
||||||
|
* For example: <entry key="key" type="string">value</entry>
|
||||||
|
*/
|
||||||
|
public abstract class EntryHelper {
|
||||||
|
public static Object getValueFromEntry(EntryHandler handler, HashMap<String, String> attributes, String content) {
|
||||||
|
String type = attributes.get("type");
|
||||||
|
Object value = null;
|
||||||
|
if ("boolean".equals(type)) {
|
||||||
|
value = Boolean.valueOf(content);
|
||||||
|
} else if ("string".equals(type)) {
|
||||||
|
value = content;
|
||||||
|
} else if ("number".equals(type)) {
|
||||||
|
value = parseNumber(content);
|
||||||
|
} else if ("list".equals(type)) {
|
||||||
|
value = handler.getNestedList();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Number parseNumber(String str) {
|
||||||
|
try {
|
||||||
|
str = str.trim();
|
||||||
|
if (str.matches("^[+-]?[0-9]+$")) {
|
||||||
|
BigInteger value = new BigInteger(str, 10);
|
||||||
|
if (value.equals(BigInteger.valueOf(value.intValue()))) {
|
||||||
|
return value.intValue();
|
||||||
|
} else if (value.equals(BigInteger.valueOf(value.longValue()))) {
|
||||||
|
return value.longValue();
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
BigDecimal value = new BigDecimal(str);
|
||||||
|
if (value.equals(BigDecimal.valueOf(value.doubleValue()))) {
|
||||||
|
return value.doubleValue();
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user