use of org.openmuc.framework.config.ParseException in project OpenMUC by isc-konstanz.
the class OptionValue method getFromDomNode.
static OptionValue getFromDomNode(String id, Node node) throws ParseException {
OptionValue option = new OptionValue(id);
Node valueDefaultNode = null;
Node valueSelectionNode = null;
NodeList childNodes = node.getChildNodes();
try {
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
String childNodeName = childNode.getNodeName();
if (childNodeName.equals("#text")) {
continue;
} else if (childNodeName.equals("name")) {
option.name = childNode.getTextContent().trim();
} else if (childNodeName.equals("description")) {
option.description = Options.trimDomNodeText(childNode);
} else if (childNodeName.equals("mandatory")) {
String mandatoryString = childNode.getTextContent().trim().toLowerCase();
if (mandatoryString.equals("true")) {
option.mandatory = true;
} else if (mandatoryString.equals("false")) {
option.mandatory = false;
} else {
throw new ParseException("Option \"mandatory\" contains neither \"true\" nor \"false\"");
}
} else if (childNodeName.equals("type")) {
String valueTypeString = childNode.getTextContent().trim().toUpperCase();
try {
option.type = ValueType.valueOf(valueTypeString);
} catch (IllegalArgumentException e) {
throw new ParseException("Unknown option value type found:" + valueTypeString);
}
} else if (childNodeName.equals("default")) {
valueDefaultNode = childNode;
} else if (childNodeName.equals("selection")) {
valueSelectionNode = childNode;
} else {
throw new ParseException("Unknown tag found:" + childNodeName);
}
}
if (option.name == null) {
option.name = id;
}
if (valueDefaultNode != null) {
Value valueDefault = new StringValue(valueDefaultNode.getTextContent().trim());
// Verify default values to be of the specified value type
switch(option.type) {
case FLOAT:
option.valueDefault = new FloatValue(valueDefault.asFloat());
break;
case DOUBLE:
option.valueDefault = new DoubleValue(valueDefault.asDouble());
break;
case SHORT:
option.valueDefault = new ShortValue(valueDefault.asShort());
break;
case INTEGER:
option.valueDefault = new IntValue(valueDefault.asInt());
break;
case LONG:
option.valueDefault = new LongValue(valueDefault.asLong());
break;
case BYTE:
option.valueDefault = new ByteValue(valueDefault.asByte());
break;
case BYTE_ARRAY:
byte[] arr;
if (!valueDefault.asString().startsWith("0x")) {
arr = valueDefault.asByteArray();
} else {
try {
arr = OptionValue.hexToBytes(valueDefault.asString().substring(2).trim());
} catch (IllegalArgumentException e) {
throw new ParseException(e);
}
}
option.valueDefault = new ByteArrayValue(arr);
break;
case BOOLEAN:
option.valueDefault = new BooleanValue(valueDefault.asBoolean());
break;
case STRING:
option.valueDefault = valueDefault;
break;
default:
break;
}
}
if (valueSelectionNode != null) {
option.valueSelection = OptionSelection.getFromDomNode(valueSelectionNode, option.type);
}
} catch (IllegalArgumentException e) {
throw new ParseException(e);
}
return option;
}
use of org.openmuc.framework.config.ParseException in project OpenMUC by isc-konstanz.
the class DriverConfigImpl method parseDiverNode.
private static void parseDiverNode(Node driverConfigNode, DriverConfigImpl config) throws ParseException {
NodeList driverChildren = driverConfigNode.getChildNodes();
try {
for (int j = 0; j < driverChildren.getLength(); j++) {
Node childNode = driverChildren.item(j);
String childName = childNode.getNodeName();
switch(childName) {
case "#text":
continue;
case "device":
DeviceConfigImpl.addDeviceFromDomNode(childNode, config);
break;
case "samplingTimeout":
config.setSamplingTimeout(ChannelConfigImpl.timeStringToMillis(childNode.getTextContent()));
break;
case "connectRetryInterval":
config.setConnectRetryInterval(ChannelConfigImpl.timeStringToMillis(childNode.getTextContent()));
break;
case "disabled":
String disabledString = childNode.getTextContent();
config.disabled = Boolean.parseBoolean(disabledString);
break;
default:
throw new ParseException("found unknown tag:" + childName);
}
}
} catch (IllegalArgumentException e) {
throw new ParseException(e);
}
}
use of org.openmuc.framework.config.ParseException in project OpenMUC by isc-konstanz.
the class DriverConfigImpl method addDriverFromDomNode.
static void addDriverFromDomNode(Node driverConfigNode, RootConfigImpl parentConfig) throws ParseException {
String id = ChannelConfigImpl.getAttributeValue(driverConfigNode, "id");
if (id == null) {
throw new ParseException("driver has no id attribute");
}
DriverConfigImpl config;
try {
config = parentConfig.addDriver(id);
} catch (IdCollisionException e) {
throw new ParseException(e);
}
parseDiverNode(driverConfigNode, config);
}
use of org.openmuc.framework.config.ParseException in project OpenMUC by isc-konstanz.
the class ChannelConfigImpl method timeStringToMillis.
static Integer timeStringToMillis(String timeString) throws ParseException {
if (timeString == null || timeString.isEmpty()) {
return null;
}
Matcher timeMatcher = timePattern.matcher(timeString);
if (!timeMatcher.matches()) {
throw new ParseException(MessageFormat.format("Unknown time string: ''{0}''.", timeString));
}
String timeNumStr = timeMatcher.group(1);
Long timeNum = parseTimeNumFrom(timeNumStr);
String timeUnit = timeMatcher.group(2);
final TimeUnit milliseconds = TimeUnit.MILLISECONDS;
if (timeUnit == null) {
return timeNum.intValue();
}
switch(timeUnit) {
case "s":
return (int) milliseconds.convert(timeNum, TimeUnit.SECONDS);
case "m":
return (int) milliseconds.convert(timeNum, TimeUnit.MINUTES);
case "h":
return (int) milliseconds.convert(timeNum, TimeUnit.HOURS);
case "ms":
return timeNum.intValue();
default:
// can not reach this case: string pattern does not allow this.
throw new ParseException("Unknown time unit: " + timeUnit);
}
}
use of org.openmuc.framework.config.ParseException in project OpenMUC by isc-konstanz.
the class RootConfigImpl method createFromFile.
public static RootConfigImpl createFromFile(File configFile) throws ParseException, FileNotFoundException {
if (configFile == null) {
throw new NullPointerException("configFileName is null or the empty string.");
}
if (!configFile.exists()) {
throw new FileNotFoundException("Config file not found.");
}
DocumentBuilderFactory docBFac = DocumentBuilderFactory.newInstance();
docBFac.setIgnoringComments(true);
Document doc = parseDocument(configFile, docBFac);
Node rootNode = doc.getDocumentElement();
if (!rootNode.getNodeName().equals("configuration")) {
throw new ParseException("root node in configuration is not of type \"configuration\"");
}
return loadRootConfigFrom(rootNode);
}
Aggregations