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