VersionProvider.java

  1. /**
  2.  *
  3.  * Copyright 2014 Georg Lukas.
  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.iqversion.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.provider.IQProvider;
  21. import org.jivesoftware.smackx.iqversion.packet.Version;
  22. import org.xmlpull.v1.XmlPullParser;
  23. import org.xmlpull.v1.XmlPullParserException;

  24. public class VersionProvider extends IQProvider<Version> {

  25.     @Override
  26.     public Version parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  27.         String name = null, version = null, os = null;

  28.         outerloop: while (true) {
  29.             int eventType = parser.next();
  30.             switch (eventType) {
  31.             case XmlPullParser.START_TAG:
  32.                 String tagName = parser.getName();
  33.                 switch (tagName) {
  34.                 case "name":
  35.                     name = parser.nextText();
  36.                     break;
  37.                 case "version":
  38.                     version = parser.nextText();
  39.                     break;
  40.                 case "os":
  41.                     os = parser.nextText();
  42.                     break;
  43.                 }
  44.                 break;
  45.             case XmlPullParser.END_TAG:
  46.                 if (parser.getDepth() == initialDepth && parser.getName().equals(IQ.QUERY_ELEMENT)) {
  47.                     break outerloop;
  48.                 }
  49.             }
  50.         }
  51.         if (name == null && version == null && os == null) {
  52.             return new Version();
  53.         }
  54.         return new Version(name, version, os);
  55.     }
  56. }