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