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.discovery.provider;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smack.provider.IQProvider;
023import org.jivesoftware.smack.util.ParserUtils;
024
025import org.jivesoftware.smackx.iot.discovery.element.IoTRegister;
026import org.jivesoftware.smackx.iot.discovery.element.Tag;
027import org.jivesoftware.smackx.iot.element.NodeInfo;
028import org.jivesoftware.smackx.iot.parser.NodeInfoParser;
029
030import org.xmlpull.v1.XmlPullParser;
031
032public class IoTRegisterProvider extends IQProvider<IoTRegister> {
033
034    @Override
035    public IoTRegister parse(XmlPullParser parser, int initialDepth) throws Exception {
036        boolean selfOwned = ParserUtils.getBooleanAttribute(parser, "selfOwned", false);
037        NodeInfo nodeInfo = NodeInfoParser.parse(parser);
038        List<Tag> tags = new ArrayList<>();
039        while (parser.getDepth() != initialDepth) {
040            int event = parser.next();
041            if (event != XmlPullParser.START_TAG) {
042                continue;
043            }
044            final String element = parser.getName();
045            Tag.Type type = null;
046            switch (element) {
047            case "str":
048                type = Tag.Type.str;
049                break;
050            case "num":
051                type = Tag.Type.num;
052                break;
053            }
054            if (type == null) {
055                continue;
056            }
057            String name = parser.getAttributeValue(null, "name");
058            String value = parser.getAttributeValue(null, "value");
059            tags.add(new Tag(name, type, value));
060        }
061        return new IoTRegister(tags, nodeInfo, selfOwned);
062    }
063
064}