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 17 package com.android.inputmethod.latin.utils; 18 19 import android.content.res.TypedArray; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 24 import java.io.IOException; 25 26 public final class XmlParseUtils { XmlParseUtils()27 private XmlParseUtils() { 28 // This utility class is not publicly instantiable. 29 } 30 31 @SuppressWarnings("serial") 32 public static class ParseException extends XmlPullParserException { ParseException(final String msg, final XmlPullParser parser)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 { IllegalStartTag(final XmlPullParser parser, final String tag, final String parent)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 { IllegalEndTag(final XmlPullParser parser, final String tag, final String parent)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 { IllegalAttribute(final XmlPullParser parser, final String tag, final String attribute)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{ NonEmptyTag(final XmlPullParser parser, final String tag)62 public NonEmptyTag(final XmlPullParser parser, final String tag) { 63 super(tag + " must be empty tag", parser); 64 } 65 } 66 checkEndTag(final String tag, final XmlPullParser parser)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 checkAttributeExists(final TypedArray attr, final int attrId, final String attrName, final String tag, final XmlPullParser parser)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