Search in sources :

Example 1 with ParsingException

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

the class FeatureTemplateLoader method s_makeHandlerEntry.

private static HandlerEntry s_makeHandlerEntry(Element e) throws ParsingException {
    String handler = e.getTextContent();
    if (handler == null) {
        throw new ParsingException("Could not find Handler for: " + e.getTextContent());
    }
    NamedNodeMap attributes = e.getAttributes();
    HashMap<String, String> params = new HashMap<String, String>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node n = attributes.item(i);
        params.put(n.getNodeName(), n.getNodeValue());
    }
    return new HandlerEntry(handler, params);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) ParsingException(org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException) Node(org.w3c.dom.Node)

Example 2 with ParsingException

use of org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException 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)

Example 3 with ParsingException

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

the class XMLMessageReader method s_readMessageDefinitions.

/**
     * Reads the message definitions from an xml file
     * 
     * @param input input stream from which to read
     * @return what was read from file: the map between clear text string and Msg objects
     * @throws IOException couldn't read file etc
     * @throws ParsingException something wrong with the file format
     * @throws FieldException something wrong with the field definition
     */
public static HashMap<String, Msg> s_readMessageDefinitions(InputStream input) throws IOException, ParsingException, FieldException {
    HashMap<String, Msg> messageMap = new HashMap<String, Msg>();
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        // Parse it!
        Document doc = dBuilder.parse(input);
        doc.getDocumentElement().normalize();
        Node root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (node.getNodeName().equals("msg")) {
                    Pair<String, Msg> msgDef = s_readMessageDefinition((Element) node);
                    messageMap.put(msgDef.getKey(), msgDef.getValue());
                }
            }
        }
    } catch (SAXException e) {
        throw new ParsingException("Failed to parse XML!", e);
    } catch (ParserConfigurationException e) {
        throw new ParsingException("Got parser config exception! ", e);
    }
    return messageMap;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParsingException(org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with ParsingException

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

the class XMLMessageReader method s_readHeaderElement.

private static int s_readHeaderElement(Element header, LinkedHashMap<Field, Object> fields) throws ParsingException {
    int offset = 0;
    int headerLen = Integer.parseInt(header.getAttribute("length"));
    NodeList nodes = header.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Pair<Field, Object> definition = s_readField((Element) node, offset);
            if (definition != null) {
                offset += definition.getKey().getType().getSize();
                fields.put(definition.getKey(), definition.getValue());
            }
        }
    }
    if (headerLen != offset) {
        throw new ParsingException("Actual header length " + offset + " differs from given length " + headerLen + "!");
    }
    return headerLen;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ParsingException(org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException)

Example 5 with ParsingException

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

the class FeatureTemplateLoader method s_readTemplates.

public static ArrayList<FeatureTemplate> s_readTemplates(InputStream input) throws IOException, ParsingException {
    ArrayList<FeatureTemplate> features = new ArrayList<FeatureTemplate>();
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        // Parse it!
        Document doc = dBuilder.parse(input);
        doc.getDocumentElement().normalize();
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) node;
                if (e.getTagName().equals("feature")) {
                    features.add(s_parseFeature(e));
                }
            }
        }
    } catch (SAXException e) {
        throw new ParsingException("Failed to parse XML!", e);
    } catch (ParserConfigurationException e) {
        throw new ParsingException("Got parser config exception! ", e);
    }
    return features;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParsingException(org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ParsingException (org.openhab.binding.insteonplm.internal.utils.Utils.ParsingException)5 Node (org.w3c.dom.Node)5 NodeList (org.w3c.dom.NodeList)4 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Document (org.w3c.dom.Document)2 SAXException (org.xml.sax.SAXException)2 ArrayList (java.util.ArrayList)1 Pair (org.openhab.binding.insteonplm.internal.utils.Pair)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1