ParserUtils.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  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.smack.util;

  18. import java.io.IOException;
  19. import java.util.Locale;

  20. import org.jxmpp.jid.BareJid;
  21. import org.jxmpp.jid.Jid;
  22. import org.jxmpp.jid.impl.JidCreate;
  23. import org.jxmpp.jid.parts.Resourcepart;
  24. import org.jxmpp.stringprep.XmppStringprepException;
  25. import org.xmlpull.v1.XmlPullParser;
  26. import org.xmlpull.v1.XmlPullParserException;

  27. public class ParserUtils {

  28.     /**
  29.      * The constant String "jid".
  30.      */
  31.     public static final String JID = "jid";

  32.     public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException {
  33.         assert(parser.getEventType() == XmlPullParser.START_TAG);
  34.     }

  35.     public static void assertAtEndTag(XmlPullParser parser) throws XmlPullParserException {
  36.         assert(parser.getEventType() == XmlPullParser.END_TAG);
  37.     }

  38.     public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth)
  39.                     throws XmlPullParserException, IOException {
  40.         int event = parser.getEventType();
  41.         while (!(event == XmlPullParser.END_TAG && parser.getDepth() == depth)) {
  42.             event = parser.next();
  43.         }
  44.     }

  45.     public static Jid getJidAttribute(XmlPullParser parser) throws XmppStringprepException {
  46.         return getJidAttribute(parser, JID);
  47.     }

  48.     public static Jid getJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
  49.         final String jidString = parser.getAttributeValue("", name);
  50.         if (jidString == null) {
  51.             return null;
  52.         }
  53.         return JidCreate.from(jidString);
  54.     }

  55.     public static BareJid getBareJidAttribute(XmlPullParser parser) throws XmppStringprepException {
  56.         return getBareJidAttribute(parser, JID);
  57.     }

  58.     public static BareJid getBareJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
  59.         final String jidString = parser.getAttributeValue("", name);
  60.         if (jidString == null) {
  61.             return null;
  62.         }
  63.         return JidCreate.bareFrom(jidString);
  64.     }

  65.     public static Resourcepart getResourcepartAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
  66.         final String resourcepartString = parser.getAttributeValue("", name);
  67.         if (resourcepartString == null) {
  68.             return null;
  69.         }
  70.         return Resourcepart.from(resourcepartString);
  71.     }

  72.     /**
  73.      * Get the boolean value of an argument.
  74.      *
  75.      * @param parser
  76.      * @param name
  77.      * @return the boolean value or null of no argument of the given name exists
  78.      */
  79.     public static Boolean getBooleanAttribute(XmlPullParser parser, String name) {
  80.         String valueString = parser.getAttributeValue("", name);
  81.         if (valueString == null)
  82.             return null;
  83.         valueString = valueString.toLowerCase(Locale.US);
  84.         if (valueString.equals("true") || valueString.equals("0")) {
  85.             return true;
  86.         } else {
  87.             return false;
  88.         }
  89.     }

  90.     public static boolean getBooleanAttribute(XmlPullParser parser, String name,
  91.                     boolean defaultValue) {
  92.         Boolean bool = getBooleanAttribute(parser, name);
  93.         if (bool == null) {
  94.             return defaultValue;
  95.         }
  96.         else {
  97.             return bool;
  98.         }
  99.     }

  100.     public static Integer getIntegerAttribute(XmlPullParser parser, String name) {
  101.         String valueString = parser.getAttributeValue("", name);
  102.         if (valueString == null)
  103.             return null;
  104.         return Integer.valueOf(valueString);
  105.     }

  106.     public static int getIntegerAttribute(XmlPullParser parser, String name, int defaultValue) {
  107.         Integer integer = getIntegerAttribute(parser, name);
  108.         if (integer == null) {
  109.             return defaultValue;
  110.         }
  111.         else {
  112.             return integer;
  113.         }
  114.     }

  115.     public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
  116.         String intString = parser.nextText();
  117.         return Integer.valueOf(intString);
  118.     }

  119.     public static Long getLongAttribute(XmlPullParser parser, String name) {
  120.         String valueString = parser.getAttributeValue("", name);
  121.         if (valueString == null)
  122.             return null;
  123.         return Long.valueOf(valueString);
  124.     }

  125.     public static long getLongAttribute(XmlPullParser parser, String name, long defaultValue) {
  126.         Long l = getLongAttribute(parser, name);
  127.         if (l == null) {
  128.             return defaultValue;
  129.         }
  130.         else {
  131.             return l;
  132.         }
  133.     }
  134. }