Clean up XmlParserUtils

Change-Id: I636e8f359d47f39e61a7f77e3e686f2721779b6f
main
Tadashi G. Takaoka 2013-03-26 12:03:07 +09:00
parent 5f976d1cef
commit d17b69ec00
3 changed files with 30 additions and 27 deletions

View File

@ -301,8 +301,8 @@ public final class KeyboardLayoutSet {
final int xmlId = mResources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
try {
parseKeyboardLayoutSet(mResources, xmlId);
} catch (Exception e) {
throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName);
} catch (final Exception e) {
throw new RuntimeException(e.getMessage() + " in " + keyboardLayoutSetName, e);
}
return new KeyboardLayoutSet(mContext, mParams);
}
@ -318,7 +318,7 @@ public final class KeyboardLayoutSet {
if (TAG_KEYBOARD_SET.equals(tag)) {
parseKeyboardLayoutSetContent(parser);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
}
}
}
@ -336,14 +336,14 @@ public final class KeyboardLayoutSet {
if (TAG_ELEMENT.equals(tag)) {
parseKeyboardLayoutSetElement(parser);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
}
} else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName();
if (TAG_KEYBOARD_SET.equals(tag)) {
break;
} else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEYBOARD_SET);
throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_KEYBOARD_SET);
}
}
}

View File

@ -220,7 +220,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
parseKeyboardContent(parser, false);
break;
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD);
}
}
}
@ -321,7 +321,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_ROW);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_ROW);
}
} else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName();
@ -333,7 +333,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
|| TAG_MERGE.equals(tag)) {
break;
} else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_ROW);
throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_ROW);
}
}
}
@ -345,10 +345,10 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
R.styleable.Keyboard);
try {
if (a.hasValue(R.styleable.Keyboard_horizontalGap)) {
throw new XmlParseUtils.IllegalAttribute(parser, "horizontalGap");
throw new XmlParseUtils.IllegalAttribute(parser, TAG_ROW, "horizontalGap");
}
if (a.hasValue(R.styleable.Keyboard_verticalGap)) {
throw new XmlParseUtils.IllegalAttribute(parser, "verticalGap");
throw new XmlParseUtils.IllegalAttribute(parser, TAG_ROW, "verticalGap");
}
return new KeyboardRow(mResources, mParams, parser, mCurrentY);
} finally {
@ -373,7 +373,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_ROW);
}
} else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName();
@ -387,7 +387,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
|| TAG_MERGE.equals(tag)) {
break;
} else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY);
throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_ROW);
}
}
}
@ -548,7 +548,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_DEFAULT.equals(tag)) {
selected |= parseDefault(parser, row, selected ? true : skip);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY);
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_SWITCH);
}
} else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName();
@ -556,7 +556,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (DEBUG) endTag("</%s>", TAG_SWITCH);
break;
} else {
throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY);
throw new XmlParseUtils.IllegalEndTag(parser, tag, TAG_SWITCH);
}
}
}

View File

@ -30,50 +30,53 @@ public final class XmlParseUtils {
@SuppressWarnings("serial")
public static class ParseException extends XmlPullParserException {
public ParseException(String msg, XmlPullParser parser) {
public ParseException(final String msg, final XmlPullParser parser) {
super(msg + " at " + parser.getPositionDescription());
}
}
@SuppressWarnings("serial")
public static final class IllegalStartTag extends ParseException {
public IllegalStartTag(XmlPullParser parser, String parent) {
super("Illegal start tag " + parser.getName() + " in " + parent, parser);
public IllegalStartTag(final XmlPullParser parser, final String tag, final String parent) {
super("Illegal start tag " + tag + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
public static final class IllegalEndTag extends ParseException {
public IllegalEndTag(XmlPullParser parser, String parent) {
super("Illegal end tag " + parser.getName() + " in " + parent, parser);
public IllegalEndTag(final XmlPullParser parser, final String tag, final String parent) {
super("Illegal end tag " + tag + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
public static final class IllegalAttribute extends ParseException {
public IllegalAttribute(XmlPullParser parser, String attribute) {
super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser);
public IllegalAttribute(final XmlPullParser parser, final String tag,
final String attribute) {
super("Tag " + tag + " has illegal attribute " + attribute, parser);
}
}
@SuppressWarnings("serial")
public static final class NonEmptyTag extends ParseException{
public NonEmptyTag(String tag, XmlPullParser parser) {
public NonEmptyTag(final XmlPullParser parser, final String tag) {
super(tag + " must be empty tag", parser);
}
}
public static void checkEndTag(String tag, XmlPullParser parser)
public static void checkEndTag(final String tag, final XmlPullParser parser)
throws XmlPullParserException, IOException {
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
return;
throw new NonEmptyTag(tag, parser);
throw new NonEmptyTag(parser, tag);
}
public static void checkAttributeExists(TypedArray attr, int attrId, String attrName,
String tag, XmlPullParser parser) throws XmlPullParserException {
if (attr.hasValue(attrId))
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)) {
return;
}
throw new ParseException(
"No " + attrName + " attribute found in <" + tag + "/>", parser);
}