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