001/** 002 * 003 * Copyright 2014 Georg Lukas. 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 */ 017 018package org.jivesoftware.smackx.iqversion.provider; 019 020import java.io.IOException; 021 022import org.jivesoftware.smack.packet.IQ; 023import org.jivesoftware.smack.provider.IQProvider; 024 025import org.jivesoftware.smackx.iqversion.packet.Version; 026 027import org.xmlpull.v1.XmlPullParser; 028import org.xmlpull.v1.XmlPullParserException; 029 030public class VersionProvider extends IQProvider<Version> { 031 032 @Override 033 public Version parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 034 String name = null, version = null, os = null; 035 036 outerloop: while (true) { 037 int eventType = parser.next(); 038 switch (eventType) { 039 case XmlPullParser.START_TAG: 040 String tagName = parser.getName(); 041 switch (tagName) { 042 case "name": 043 name = parser.nextText(); 044 break; 045 case "version": 046 version = parser.nextText(); 047 break; 048 case "os": 049 os = parser.nextText(); 050 break; 051 } 052 break; 053 case XmlPullParser.END_TAG: 054 if (parser.getDepth() == initialDepth && parser.getName().equals(IQ.QUERY_ELEMENT)) { 055 break outerloop; 056 } 057 } 058 } 059 if (name == null && version == null && os == null) { 060 return new Version(); 061 } 062 return new Version(name, version, os); 063 } 064}