1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.latin.utils; 18 19import android.content.res.TypedArray; 20 21import org.xmlpull.v1.XmlPullParser; 22import org.xmlpull.v1.XmlPullParserException; 23 24import java.io.IOException; 25 26public final class XmlParseUtils { 27 private XmlParseUtils() { 28 // This utility class is not publicly instantiable. 29 } 30 31 @SuppressWarnings("serial") 32 public static class ParseException extends XmlPullParserException { 33 public ParseException(final String msg, final XmlPullParser parser) { 34 super(msg + " at " + parser.getPositionDescription()); 35 } 36 } 37 38 @SuppressWarnings("serial") 39 public static final class IllegalStartTag extends ParseException { 40 public IllegalStartTag(final XmlPullParser parser, final String tag, final String parent) { 41 super("Illegal start tag " + tag + " in " + parent, parser); 42 } 43 } 44 45 @SuppressWarnings("serial") 46 public static final class IllegalEndTag extends ParseException { 47 public IllegalEndTag(final XmlPullParser parser, final String tag, final String parent) { 48 super("Illegal end tag " + tag + " in " + parent, parser); 49 } 50 } 51 52 @SuppressWarnings("serial") 53 public static final class IllegalAttribute extends ParseException { 54 public IllegalAttribute(final XmlPullParser parser, final String tag, 55 final String attribute) { 56 super("Tag " + tag + " has illegal attribute " + attribute, parser); 57 } 58 } 59 60 @SuppressWarnings("serial") 61 public static final class NonEmptyTag extends ParseException{ 62 public NonEmptyTag(final XmlPullParser parser, final String tag) { 63 super(tag + " must be empty tag", parser); 64 } 65 } 66 67 public static void checkEndTag(final String tag, final XmlPullParser parser) 68 throws XmlPullParserException, IOException { 69 if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName())) 70 return; 71 throw new NonEmptyTag(parser, tag); 72 } 73 74 public static void checkAttributeExists(final TypedArray attr, final int attrId, 75 final String attrName, final String tag, final XmlPullParser parser) 76 throws XmlPullParserException { 77 if (attr.hasValue(attrId)) { 78 return; 79 } 80 throw new ParseException( 81 "No " + attrName + " attribute found in <" + tag + "/>", parser); 82 } 83} 84