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