Search in sources :

Example 1 with Pair

use of org.openhab.binding.insteonplm.internal.utils.Pair in project openhab1-addons by openhab.

the class XMLMessageReader method s_readMessageDefinition.

private static Pair<String, Msg> s_readMessageDefinition(Element msg) throws FieldException, ParsingException {
    int length = 0;
    int hlength = 0;
    LinkedHashMap<Field, Object> fieldMap = new LinkedHashMap<Field, Object>();
    String dir = msg.getAttribute("direction");
    String name = msg.getAttribute("name");
    Msg.Direction direction = Msg.Direction.s_getDirectionFromString(dir);
    if (msg.hasAttribute("length")) {
        length = Integer.parseInt(msg.getAttribute("length"));
    }
    NodeList nodes = msg.getChildNodes();
    int offset = 0;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals("header")) {
                int o = s_readHeaderElement((Element) node, fieldMap);
                hlength = o;
                // Increment the offset by the header length
                offset += o;
            } else {
                Pair<Field, Object> field = s_readField((Element) node, offset);
                fieldMap.put(field.getKey(), field.getValue());
                // Increment the offset
                offset += field.getKey().getType().getSize();
            }
        }
    }
    if (offset != length) {
        throw new ParsingException("Actual msg length " + offset + " differs from given msg length " + length + "!");
    }
    if (length == 0) {
        length = offset;
    }
    return new Pair<String, Msg>(name, s_createMsg(fieldMap, length, hlength, direction));
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) LinkedHashMap(java.util.LinkedHashMap) ParsingException(org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException) Pair(org.openhab.binding.insteonplm.internal.utils.Pair)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Pair (org.openhab.binding.insteonplm.internal.utils.Pair)1 ParsingException (org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1