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