001/*
002 *
003 * Copyright © 2016-2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.iot.control.provider;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.IqData;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.iot.control.element.IoTSetRequest;
030import org.jivesoftware.smackx.iot.control.element.SetBoolData;
031import org.jivesoftware.smackx.iot.control.element.SetData;
032import org.jivesoftware.smackx.iot.control.element.SetDoubleData;
033import org.jivesoftware.smackx.iot.control.element.SetIntData;
034import org.jivesoftware.smackx.iot.control.element.SetLongData;
035
036import org.jxmpp.JxmppContext;
037
038public class IoTSetRequestProvider extends IqProvider<IoTSetRequest> {
039
040    @Override
041    public IoTSetRequest parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
042        List<SetData> data = new ArrayList<>(4);
043        outerloop: while (true) {
044            final XmlPullParser.Event eventType = parser.next();
045            switch (eventType) {
046            case START_ELEMENT:
047                final String name = parser.getName();
048                switch (name) {
049                case "bool": {
050                    String valueName = parser.getAttributeValue(null, "name");
051                    String valueString = parser.getAttributeValue(null, "value");
052                    boolean value = Boolean.parseBoolean(valueString);
053                    data.add(new SetBoolData(valueName, value));
054                }
055                    break;
056                case "double": {
057                    String valueName = parser.getAttributeValue(null, "name");
058                    String valueString = parser.getAttributeValue(null, "value");
059                    double value = Double.parseDouble(valueString);
060                    data.add(new SetDoubleData(valueName, value));
061                }
062                    break;
063                case "int": {
064                    String valueName = parser.getAttributeValue(null, "name");
065                    String valueString = parser.getAttributeValue(null, "value");
066                    int value = Integer.parseInt(valueString);
067                    data.add(new SetIntData(valueName, value));
068                }
069                    break;
070                case "long": {
071                    String valueName = parser.getAttributeValue(null, "name");
072                    String valueString = parser.getAttributeValue(null, "value");
073                    long value = Long.parseLong(valueString);
074                    data.add(new SetLongData(valueName, value));
075                }
076                    break;
077                }
078                break;
079            case END_ELEMENT:
080                if (parser.getDepth() == initialDepth) {
081                    break outerloop;
082                }
083                break;
084            default:
085                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
086                break;
087            }
088        }
089        return new IoTSetRequest(data);
090    }
091
092}