001/*
002 *
003 * Copyright 2014 Georg Lukas, 2021 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.iqversion.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.packet.IqData;
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;
029import org.jivesoftware.smackx.iqversion.packet.VersionBuilder;
030
031import org.jxmpp.JxmppContext;
032
033public class VersionProvider extends IqProvider<Version> {
034
035    @Override
036    public Version parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
037                    throws XmlPullParserException, IOException {
038        VersionBuilder versionBuilder = Version.builder(iqData);
039
040        outerloop: while (true) {
041            XmlPullParser.Event eventType = parser.next();
042            switch (eventType) {
043            case START_ELEMENT:
044                String tagName = parser.getName();
045                switch (tagName) {
046                case "name":
047                    String name = parser.nextText();
048                    versionBuilder.setName(name);
049                    break;
050                case "version":
051                    String version = parser.nextText();
052                    versionBuilder.setVersion(version);
053                    break;
054                case "os":
055                    String os = parser.nextText();
056                    versionBuilder.setOs(os);
057                    break;
058                }
059                break;
060            case END_ELEMENT:
061                if (parser.getDepth() == initialDepth && parser.getName().equals(IQ.QUERY_ELEMENT)) {
062                    break outerloop;
063                }
064                break;
065            default:
066                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
067                break;
068            }
069        }
070
071        return versionBuilder.build();
072    }
073}