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.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IQProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.iqversion.packet.Version;
029
030public class VersionProvider extends IQProvider<Version> {
031
032    @Override
033    public Version parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
034        String name = null, version = null, os = null;
035
036        outerloop: while (true) {
037            XmlPullParser.Event eventType = parser.next();
038            switch (eventType) {
039            case START_ELEMENT:
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 END_ELEMENT:
054                if (parser.getDepth() == initialDepth && parser.getName().equals(IQ.QUERY_ELEMENT)) {
055                    break outerloop;
056                }
057                break;
058            default:
059                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
060                break;
061            }
062        }
063        if (name == null && version == null && os == null) {
064            return new Version();
065        }
066        return new Version(name, version, os);
067    }
068}