001/*
002 *
003 * Copyright 2019 Aditya Borikar.
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.usertune.provider;
018
019import java.io.IOException;
020import java.net.URI;
021
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.parsing.SmackParsingException;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.usertune.element.UserTuneElement;
030
031import org.jxmpp.JxmppContext;
032
033/**
034 * This is the Provider Class for {@link UserTuneElement}.
035 */
036public class UserTuneProvider extends ExtensionElementProvider<UserTuneElement> {
037
038    public static final UserTuneProvider INSTANCE = new UserTuneProvider();
039
040    @Override
041    public UserTuneElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
042            throws XmlPullParserException, IOException, SmackParsingException {
043
044        UserTuneElement.Builder builder = UserTuneElement.getBuilder();
045        XmlPullParser.TagEvent tag = parser.nextTag();
046        outerloop: while (true) {
047            switch (tag) {
048            case START_ELEMENT:
049                String name = parser.getName();
050                String namespace = parser.getNamespace();
051                if (!UserTuneElement.NAMESPACE.equals(namespace)) {
052                    continue outerloop;
053                }
054                while (tag == XmlPullParser.TagEvent.START_ELEMENT) {
055                    switch (name) {
056                    case "artist":
057                        builder.setArtist(parser.nextText());
058                        break;
059                    case "length":
060                        builder.setLength(ParserUtils.getIntegerFromNextText(parser));
061                        break;
062                    case "rating":
063                        builder.setRating(ParserUtils.getIntegerFromNextText(parser));
064                        break;
065                    case "source":
066                        builder.setSource(parser.nextText());
067                        break;
068                    case "title":
069                        builder.setTitle(parser.nextText());
070                        break;
071                    case "track":
072                        builder.setTrack(parser.nextText());
073                        break;
074                    case "uri":
075                        URI uri = ParserUtils.getUriFromNextText(parser);
076                        builder.setUri(uri);
077                        break;
078                    }
079                    tag = parser.nextTag();
080                    name = parser.getName();
081                }
082                break;
083            case END_ELEMENT:
084                if (parser.getDepth() == initialDepth) {
085                    break outerloop;
086                }
087                break;
088            }
089        }
090        return builder.build();
091    }
092}