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
036public class IoTSetRequestProvider extends IqProvider<IoTSetRequest> {
037
038    @Override
039    public IoTSetRequest parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
040        List<SetData> data = new ArrayList<>(4);
041        outerloop: while (true) {
042            final XmlPullParser.Event eventType = parser.next();
043            switch (eventType) {
044            case START_ELEMENT:
045                final String name = parser.getName();
046                switch (name) {
047                case "bool": {
048                    String valueName = parser.getAttributeValue(null, "name");
049                    String valueString = parser.getAttributeValue(null, "value");
050                    boolean value = Boolean.parseBoolean(valueString);
051                    data.add(new SetBoolData(valueName, value));
052                }
053                    break;
054                case "double": {
055                    String valueName = parser.getAttributeValue(null, "name");
056                    String valueString = parser.getAttributeValue(null, "value");
057                    double value = Double.parseDouble(valueString);
058                    data.add(new SetDoubleData(valueName, value));
059                }
060                    break;
061                case "int": {
062                    String valueName = parser.getAttributeValue(null, "name");
063                    String valueString = parser.getAttributeValue(null, "value");
064                    int value = Integer.parseInt(valueString);
065                    data.add(new SetIntData(valueName, value));
066                }
067                    break;
068                case "long": {
069                    String valueName = parser.getAttributeValue(null, "name");
070                    String valueString = parser.getAttributeValue(null, "value");
071                    long value = Long.parseLong(valueString);
072                    data.add(new SetLongData(valueName, value));
073                }
074                    break;
075                }
076                break;
077            case END_ELEMENT:
078                if (parser.getDepth() == initialDepth) {
079                    break outerloop;
080                }
081                break;
082            default:
083                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
084                break;
085            }
086        }
087        return new IoTSetRequest(data);
088    }
089
090}