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.discovery.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.util.ParserUtils; 027import org.jivesoftware.smack.xml.XmlPullParser; 028import org.jivesoftware.smack.xml.XmlPullParserException; 029 030import org.jivesoftware.smackx.iot.discovery.element.IoTRegister; 031import org.jivesoftware.smackx.iot.discovery.element.Tag; 032import org.jivesoftware.smackx.iot.element.NodeInfo; 033import org.jivesoftware.smackx.iot.parser.NodeInfoParser; 034 035public class IoTRegisterProvider extends IqProvider<IoTRegister> { 036 037 @Override 038 public IoTRegister parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 039 boolean selfOwned = ParserUtils.getBooleanAttribute(parser, "selfOwned", false); 040 NodeInfo nodeInfo = NodeInfoParser.parse(parser); 041 List<Tag> tags = new ArrayList<>(); 042 while (parser.getDepth() != initialDepth) { 043 XmlPullParser.Event event = parser.next(); 044 if (event != XmlPullParser.Event.START_ELEMENT) { 045 continue; 046 } 047 final String element = parser.getName(); 048 Tag.Type type = null; 049 switch (element) { 050 case "str": 051 type = Tag.Type.str; 052 break; 053 case "num": 054 type = Tag.Type.num; 055 break; 056 } 057 if (type == null) { 058 continue; 059 } 060 String name = parser.getAttributeValue(null, "name"); 061 String value = parser.getAttributeValue(null, "value"); 062 tags.add(new Tag(name, type, value)); 063 } 064 return new IoTRegister(tags, nodeInfo, selfOwned); 065 } 066 067}