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
031/**
032 * This is the Provider Class for {@link UserTuneElement}.
033 */
034public class UserTuneProvider extends ExtensionElementProvider<UserTuneElement> {
035
036    public static final UserTuneProvider INSTANCE = new UserTuneProvider();
037
038    @Override
039    public UserTuneElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
040            throws XmlPullParserException, IOException, SmackParsingException {
041
042        UserTuneElement.Builder builder = UserTuneElement.getBuilder();
043        XmlPullParser.TagEvent tag = parser.nextTag();
044        outerloop: while (true) {
045            switch (tag) {
046            case START_ELEMENT:
047                String name = parser.getName();
048                String namespace = parser.getNamespace();
049                if (!UserTuneElement.NAMESPACE.equals(namespace)) {
050                    continue outerloop;
051                }
052                while (tag == XmlPullParser.TagEvent.START_ELEMENT) {
053                    switch (name) {
054                    case "artist":
055                        builder.setArtist(parser.nextText());
056                        break;
057                    case "length":
058                        builder.setLength(ParserUtils.getIntegerFromNextText(parser));
059                        break;
060                    case "rating":
061                        builder.setRating(ParserUtils.getIntegerFromNextText(parser));
062                        break;
063                    case "source":
064                        builder.setSource(parser.nextText());
065                        break;
066                    case "title":
067                        builder.setTitle(parser.nextText());
068                        break;
069                    case "track":
070                        builder.setTrack(parser.nextText());
071                        break;
072                    case "uri":
073                        URI uri = ParserUtils.getUriFromNextText(parser);
074                        builder.setUri(uri);
075                        break;
076                    }
077                    tag = parser.nextTag();
078                    name = parser.getName();
079                }
080                break;
081            case END_ELEMENT:
082                if (parser.getDepth() == initialDepth) {
083                    break outerloop;
084                }
085                break;
086            }
087        }
088        return builder.build();
089    }
090}