UserTuneProvider.java

  1. /**
  2.  *
  3.  * Copyright 2019 Aditya Borikar.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.usertune.provider;

  18. import java.io.IOException;
  19. import java.net.URI;

  20. import org.jivesoftware.smack.packet.XmlEnvironment;
  21. import org.jivesoftware.smack.parsing.SmackParsingException;
  22. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  23. import org.jivesoftware.smack.util.ParserUtils;
  24. import org.jivesoftware.smack.xml.XmlPullParser;
  25. import org.jivesoftware.smack.xml.XmlPullParserException;

  26. import org.jivesoftware.smackx.usertune.element.UserTuneElement;

  27. /**
  28.  * This is the Provider Class for {@link UserTuneElement}.
  29.  */
  30. public class UserTuneProvider extends ExtensionElementProvider<UserTuneElement> {

  31.     public static final UserTuneProvider INSTANCE = new UserTuneProvider();

  32.     @Override
  33.     public UserTuneElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
  34.             throws XmlPullParserException, IOException, SmackParsingException {

  35.         UserTuneElement.Builder builder = UserTuneElement.getBuilder();
  36.         XmlPullParser.TagEvent tag = parser.nextTag();
  37.         outerloop: while (true) {
  38.             switch (tag) {
  39.             case START_ELEMENT:
  40.                 String name = parser.getName();
  41.                 String namespace = parser.getNamespace();
  42.                 if (!UserTuneElement.NAMESPACE.equals(namespace)) {
  43.                     continue outerloop;
  44.                 }
  45.                 while (tag == XmlPullParser.TagEvent.START_ELEMENT) {
  46.                     switch (name) {
  47.                     case "artist":
  48.                         builder.setArtist(parser.nextText());
  49.                         break;
  50.                     case "length":
  51.                         builder.setLength(ParserUtils.getIntegerFromNextText(parser));
  52.                         break;
  53.                     case "rating":
  54.                         builder.setRating(ParserUtils.getIntegerFromNextText(parser));
  55.                         break;
  56.                     case "source":
  57.                         builder.setSource(parser.nextText());
  58.                         break;
  59.                     case "title":
  60.                         builder.setTitle(parser.nextText());
  61.                         break;
  62.                     case "track":
  63.                         builder.setTrack(parser.nextText());
  64.                         break;
  65.                     case "uri":
  66.                         URI uri = ParserUtils.getUriFromNextText(parser);
  67.                         builder.setUri(uri);
  68.                         break;
  69.                     }
  70.                     tag = parser.nextTag();
  71.                     name = parser.getName();
  72.                 }
  73.                 break;
  74.             case END_ELEMENT:
  75.                 if (parser.getDepth() == initialDepth) {
  76.                     break outerloop;
  77.                 }
  78.                 break;
  79.             }
  80.         }
  81.         return builder.build();
  82.     }
  83. }