2011-12-14 09:11:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-12-14 09:11:27 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-14 09:11:27 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-12-14 09:11:27 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-23 16:11:32 +00:00
|
|
|
package com.android.inputmethod.latin.utils;
|
2011-12-14 09:11:27 +00:00
|
|
|
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2012-08-29 08:26:00 +00:00
|
|
|
public final class XmlParseUtils {
|
2012-03-16 15:50:51 +00:00
|
|
|
private XmlParseUtils() {
|
|
|
|
// This utility class is not publicly instantiable.
|
|
|
|
}
|
|
|
|
|
2011-12-14 09:11:27 +00:00
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public static class ParseException extends XmlPullParserException {
|
2013-03-26 03:03:07 +00:00
|
|
|
public ParseException(final String msg, final XmlPullParser parser) {
|
2012-02-17 07:14:14 +00:00
|
|
|
super(msg + " at " + parser.getPositionDescription());
|
2011-12-14 09:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class IllegalStartTag extends ParseException {
|
2013-03-26 03:03:07 +00:00
|
|
|
public IllegalStartTag(final XmlPullParser parser, final String tag, final String parent) {
|
|
|
|
super("Illegal start tag " + tag + " in " + parent, parser);
|
2011-12-14 09:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class IllegalEndTag extends ParseException {
|
2013-03-26 03:03:07 +00:00
|
|
|
public IllegalEndTag(final XmlPullParser parser, final String tag, final String parent) {
|
|
|
|
super("Illegal end tag " + tag + " in " + parent, parser);
|
2011-12-14 09:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class IllegalAttribute extends ParseException {
|
2013-03-26 03:03:07 +00:00
|
|
|
public IllegalAttribute(final XmlPullParser parser, final String tag,
|
|
|
|
final String attribute) {
|
|
|
|
super("Tag " + tag + " has illegal attribute " + attribute, parser);
|
2011-12-14 09:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class NonEmptyTag extends ParseException{
|
2013-03-26 03:03:07 +00:00
|
|
|
public NonEmptyTag(final XmlPullParser parser, final String tag) {
|
2011-12-14 09:11:27 +00:00
|
|
|
super(tag + " must be empty tag", parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 03:03:07 +00:00
|
|
|
public static void checkEndTag(final String tag, final XmlPullParser parser)
|
2011-12-14 09:11:27 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
|
|
|
|
return;
|
2013-03-26 03:03:07 +00:00
|
|
|
throw new NonEmptyTag(parser, tag);
|
2011-12-14 09:11:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 03:03:07 +00:00
|
|
|
public static void checkAttributeExists(final TypedArray attr, final int attrId,
|
|
|
|
final String attrName, final String tag, final XmlPullParser parser)
|
|
|
|
throws XmlPullParserException {
|
|
|
|
if (attr.hasValue(attrId)) {
|
2011-12-14 09:11:27 +00:00
|
|
|
return;
|
2013-03-26 03:03:07 +00:00
|
|
|
}
|
2011-12-14 09:11:27 +00:00
|
|
|
throw new ParseException(
|
|
|
|
"No " + attrName + " attribute found in <" + tag + "/>", parser);
|
|
|
|
}
|
|
|
|
}
|