001/** 002 * 003 * Copyright © 2014 Florian Schmaus 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.smack.util; 018 019import java.io.IOException; 020import java.util.Locale; 021 022import org.xmlpull.v1.XmlPullParser; 023import org.xmlpull.v1.XmlPullParserException; 024 025public class ParserUtils { 026 public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException { 027 assert(parser.getEventType() == XmlPullParser.START_TAG); 028 } 029 030 public static void assertAtEndTag(XmlPullParser parser) throws XmlPullParserException { 031 assert(parser.getEventType() == XmlPullParser.END_TAG); 032 } 033 034 public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth) 035 throws XmlPullParserException, IOException { 036 int event = parser.getEventType(); 037 while (!(event == XmlPullParser.END_TAG && parser.getDepth() == depth)) { 038 event = parser.next(); 039 } 040 } 041 042 /** 043 * Get the boolean value of an argument. 044 * 045 * @param parser 046 * @param name 047 * @return the boolean value or null of no argument of the given name exists 048 */ 049 public static Boolean getBooleanAttribute(XmlPullParser parser, String name) { 050 String valueString = parser.getAttributeValue("", name); 051 if (valueString == null) 052 return null; 053 valueString = valueString.toLowerCase(Locale.US); 054 if (valueString.equals("true") || valueString.equals("0")) { 055 return true; 056 } else { 057 return false; 058 } 059 } 060 061 public static boolean getBooleanAttribute(XmlPullParser parser, String name, 062 boolean defaultValue) { 063 Boolean bool = getBooleanAttribute(parser, name); 064 if (bool == null) { 065 return defaultValue; 066 } 067 else { 068 return bool; 069 } 070 } 071 072 public static Integer getIntegerAttribute(XmlPullParser parser, String name) { 073 String valueString = parser.getAttributeValue("", name); 074 if (valueString == null) 075 return null; 076 return Integer.valueOf(valueString); 077 } 078 079 public static int getIntegerAttribute(XmlPullParser parser, String name, int defaultValue) { 080 Integer integer = getIntegerAttribute(parser, name); 081 if (integer == null) { 082 return defaultValue; 083 } 084 else { 085 return integer; 086 } 087 } 088 089 public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException { 090 String intString = parser.nextText(); 091 return Integer.valueOf(intString); 092 } 093 094 public static Long getLongAttribute(XmlPullParser parser, String name) { 095 String valueString = parser.getAttributeValue("", name); 096 if (valueString == null) 097 return null; 098 return Long.valueOf(valueString); 099 } 100 101 public static long getLongAttribute(XmlPullParser parser, String name, long defaultValue) { 102 Long l = getLongAttribute(parser, name); 103 if (l == null) { 104 return defaultValue; 105 } 106 else { 107 return l; 108 } 109 } 110}