Search in sources :

Example 1 with ParseException

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;
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) BooleanValue(org.openmuc.framework.data.BooleanValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue) LongValue(org.openmuc.framework.data.LongValue) ParseException(org.openmuc.framework.config.ParseException) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue)

Example 2 with ParseException

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);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ParseException(org.openmuc.framework.config.ParseException)

Example 3 with ParseException

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);
}
Also used : IdCollisionException(org.openmuc.framework.config.IdCollisionException) ParseException(org.openmuc.framework.config.ParseException)

Example 4 with ParseException

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) TimeUnit(java.util.concurrent.TimeUnit) ParseException(org.openmuc.framework.config.ParseException)

Example 5 with ParseException

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.openmuc.framework.config.ParseException) Document(org.w3c.dom.Document)

Aggregations

ParseException (org.openmuc.framework.config.ParseException)12 Node (org.w3c.dom.Node)9 NodeList (org.w3c.dom.NodeList)7 IdCollisionException (org.openmuc.framework.config.IdCollisionException)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 FileNotFoundException (java.io.FileNotFoundException)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Document (org.w3c.dom.Document)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matcher (java.util.regex.Matcher)1 ServerMapping (org.openmuc.framework.config.ServerMapping)1 BooleanValue (org.openmuc.framework.data.BooleanValue)1 ByteArrayValue (org.openmuc.framework.data.ByteArrayValue)1 ByteValue (org.openmuc.framework.data.ByteValue)1 DoubleValue (org.openmuc.framework.data.DoubleValue)1 FloatValue (org.openmuc.framework.data.FloatValue)1 IntValue (org.openmuc.framework.data.IntValue)1