• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.latin;
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(String msg, XmlPullParser parser)33         public ParseException(String msg, XmlPullParser parser) {
34             super(msg + " at " + parser.getPositionDescription());
35         }
36     }
37 
38     @SuppressWarnings("serial")
39     public static final class IllegalStartTag extends ParseException {
IllegalStartTag(XmlPullParser parser, String parent)40         public IllegalStartTag(XmlPullParser parser, String parent) {
41             super("Illegal start tag " + parser.getName() + " in " + parent, parser);
42         }
43     }
44 
45     @SuppressWarnings("serial")
46     public static final class IllegalEndTag extends ParseException {
IllegalEndTag(XmlPullParser parser, String parent)47         public IllegalEndTag(XmlPullParser parser, String parent) {
48             super("Illegal end tag " + parser.getName() + " in " + parent, parser);
49         }
50     }
51 
52     @SuppressWarnings("serial")
53     public static final class IllegalAttribute extends ParseException {
IllegalAttribute(XmlPullParser parser, String attribute)54         public IllegalAttribute(XmlPullParser parser, String attribute) {
55             super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser);
56         }
57     }
58 
59     @SuppressWarnings("serial")
60     public static final class NonEmptyTag extends ParseException{
NonEmptyTag(String tag, XmlPullParser parser)61         public NonEmptyTag(String tag, XmlPullParser parser) {
62             super(tag + " must be empty tag", parser);
63         }
64     }
65 
checkEndTag(String tag, XmlPullParser parser)66     public static void checkEndTag(String tag, XmlPullParser parser)
67             throws XmlPullParserException, IOException {
68         if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
69             return;
70         throw new NonEmptyTag(tag, parser);
71     }
72 
checkAttributeExists(TypedArray attr, int attrId, String attrName, String tag, XmlPullParser parser)73     public static void checkAttributeExists(TypedArray attr, int attrId, String attrName,
74             String tag, XmlPullParser parser) throws XmlPullParserException {
75         if (attr.hasValue(attrId))
76             return;
77         throw new ParseException(
78                 "No " + attrName + " attribute found in <" + tag + "/>", parser);
79     }
80 }
81