am b8c2c6e2: Merge "Add XmlParseUtils class"

* commit 'b8c2c6e2ecdac2c170e17a100a007aea04aafa1a':
  Add XmlParseUtils class
This commit is contained in:
Tadashi G. Takaoka 2011-12-15 10:08:32 -08:00 committed by Android Git Automerger
commit 5661e4e4a3
5 changed files with 140 additions and 99 deletions

View file

@ -28,13 +28,14 @@ import android.util.Xml;
import com.android.inputmethod.keyboard.internal.KeyStyles; import com.android.inputmethod.keyboard.internal.KeyStyles;
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle; import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder.ParseException;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams; import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.keyboard.internal.MoreKeySpecParser; import com.android.inputmethod.keyboard.internal.MoreKeySpecParser;
import com.android.inputmethod.keyboard.internal.XmlParseUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -212,9 +213,10 @@ public class Key {
* this key. * this key.
* @param parser the XML parser containing the attributes for this key * @param parser the XML parser containing the attributes for this key
* @param keyStyles active key styles set * @param keyStyles active key styles set
* @throws XmlPullParserException
*/ */
public Key(Resources res, KeyboardParams params, KeyboardBuilder.Row row, public Key(Resources res, KeyboardParams params, KeyboardBuilder.Row row,
XmlPullParser parser, KeyStyles keyStyles) { XmlPullParser parser, KeyStyles keyStyles) throws XmlPullParserException {
final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap; final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
final int keyHeight = row.mRowHeight; final int keyHeight = row.mRowHeight;
mVerticalGap = params.mVerticalGap; mVerticalGap = params.mVerticalGap;
@ -228,7 +230,8 @@ public class Key {
String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle); String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle);
style = keyStyles.getKeyStyle(styleName); style = keyStyles.getKeyStyle(styleName);
if (style == null) if (style == null)
throw new ParseException("Unknown key style: " + styleName, parser); throw new XmlParseUtils.ParseException(
"Unknown key style: " + styleName, parser);
} else { } else {
style = KeyStyles.getEmptyKeyStyle(); style = KeyStyles.getEmptyKeyStyle();
} }
@ -468,9 +471,9 @@ public class Key {
* Detects if a point falls on this key. * Detects if a point falls on this key.
* @param x the x-coordinate of the point * @param x the x-coordinate of the point
* @param y the y-coordinate of the point * @param y the y-coordinate of the point
* @return whether or not the point falls on the key. If the key is attached to an edge, it will * @return whether or not the point falls on the key. If the key is attached to an edge, it
* assume that all points between the key and the edge are considered to be on the key. * will assume that all points between the key and the edge are considered to be on the key.
* @see {@link #markAsLeftEdge(KeyboardParams)} etc. * @see #markAsLeftEdge(KeyboardParams) etc.
*/ */
public boolean isOnKey(int x, int y) { public boolean isOnKey(int x, int y) {
return mHitBox.contains(x, y); return mHitBox.contains(x, y);
@ -569,7 +572,7 @@ public class Key {
public static class Spacer extends Key { public static class Spacer extends Key {
public Spacer(Resources res, KeyboardParams params, KeyboardBuilder.Row row, public Spacer(Resources res, KeyboardParams params, KeyboardBuilder.Row row,
XmlPullParser parser, KeyStyles keyStyles) { XmlPullParser parser, KeyStyles keyStyles) throws XmlPullParserException {
super(res, params, row, parser, keyStyles); super(res, params, row, parser, keyStyles);
} }

View file

@ -25,10 +25,7 @@ import android.util.DisplayMetrics;
import android.util.Xml; import android.util.Xml;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.XmlParseUtils;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder.IllegalEndTag;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder.IllegalStartTag;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder.ParseException;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
@ -153,7 +150,7 @@ public class KeyboardSet {
if (TAG_KEYBOARD_SET.equals(tag)) { if (TAG_KEYBOARD_SET.equals(tag)) {
parseKeyboardSetContent(parser); parseKeyboardSetContent(parser);
} else { } else {
throw new IllegalStartTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET);
} }
} }
} }
@ -171,14 +168,14 @@ public class KeyboardSet {
if (TAG_ELEMENT.equals(tag)) { if (TAG_ELEMENT.equals(tag)) {
parseKeyboardSetElement(parser); parseKeyboardSetElement(parser);
} else { } else {
throw new IllegalStartTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
if (TAG_KEYBOARD_SET.equals(tag)) { if (TAG_KEYBOARD_SET.equals(tag)) {
break; break;
} else { } else {
throw new IllegalEndTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEYBOARD_SET);
} }
} }
} }
@ -189,15 +186,13 @@ public class KeyboardSet {
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.KeyboardSet_Element); R.styleable.KeyboardSet_Element);
try { try {
if (!a.hasValue(R.styleable.KeyboardSet_Element_elementName)) { XmlParseUtils.checkAttributeExists(a,
throw new ParseException( R.styleable.KeyboardSet_Element_elementName, "elementName",
"No elementName attribute in <" + TAG_ELEMENT + "/>", parser); TAG_ELEMENT, parser);
} XmlParseUtils.checkAttributeExists(a,
if (!a.hasValue(R.styleable.KeyboardSet_Element_elementKeyboard)) { R.styleable.KeyboardSet_Element_elementKeyboard, "elementKeyboard",
throw new ParseException( TAG_ELEMENT, parser);
"No elementKeyboard attribute in <" + TAG_ELEMENT + "/>", parser); XmlParseUtils.checkEndTag(TAG_ELEMENT, parser);
}
KeyboardBuilder.checkEndTag(TAG_ELEMENT, parser);
final int elementName = a.getInt( final int elementName = a.getInt(
R.styleable.KeyboardSet_Element_elementName, 0); R.styleable.KeyboardSet_Element_elementName, 0);
@ -226,7 +221,7 @@ public class KeyboardSet {
keyboardSetAttr.recycle(); keyboardSetAttr.recycle();
return locale; return locale;
} else { } else {
throw new IllegalStartTag(parser, TAG_KEYBOARD_SET); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD_SET);
} }
} }
} }

View file

@ -19,10 +19,10 @@ package com.android.inputmethod.keyboard.internal;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder.ParseException;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -43,7 +43,7 @@ public class KeyStyles {
} }
/* package */ static class EmptyKeyStyle implements KeyStyle { /* package */ static class EmptyKeyStyle implements KeyStyle {
private EmptyKeyStyle() { EmptyKeyStyle() {
// Nothing to do. // Nothing to do.
} }
@ -118,7 +118,7 @@ public class KeyStyles {
} }
} }
private static class DeclaredKeyStyle extends EmptyKeyStyle { /* package */ static class DeclaredKeyStyle extends EmptyKeyStyle {
private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>(); private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>();
@Override @Override
@ -145,11 +145,11 @@ public class KeyStyles {
return super.getFlag(a, index, defaultValue) | (value != null ? value : 0); return super.getFlag(a, index, defaultValue) | (value != null ? value : 0);
} }
private DeclaredKeyStyle() { DeclaredKeyStyle() {
super(); super();
} }
private void parseKeyStyleAttributes(TypedArray keyAttr) { void parseKeyStyleAttributes(TypedArray keyAttr) {
// TODO: Currently not all Key attributes can be declared as style. // TODO: Currently not all Key attributes can be declared as style.
readInt(keyAttr, R.styleable.Keyboard_Key_code); readInt(keyAttr, R.styleable.Keyboard_Key_code);
readInt(keyAttr, R.styleable.Keyboard_Key_altCode); readInt(keyAttr, R.styleable.Keyboard_Key_altCode);
@ -188,18 +188,19 @@ public class KeyStyles {
mAttributes.put(index, value); mAttributes.put(index, value);
} }
private void addParent(DeclaredKeyStyle parentStyle) { void addParent(DeclaredKeyStyle parentStyle) {
mAttributes.putAll(parentStyle.mAttributes); mAttributes.putAll(parentStyle.mAttributes);
} }
} }
public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs, public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs,
XmlPullParser parser) { XmlPullParser parser) throws XmlPullParserException {
final String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName); final String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName);
if (DEBUG) Log.d(TAG, String.format("<%s styleName=%s />", if (DEBUG) Log.d(TAG, String.format("<%s styleName=%s />",
KeyboardBuilder.TAG_KEY_STYLE, styleName)); KeyboardBuilder.TAG_KEY_STYLE, styleName));
if (mStyles.containsKey(styleName)) if (mStyles.containsKey(styleName))
throw new ParseException("duplicate key style declared: " + styleName, parser); throw new XmlParseUtils.ParseException(
"duplicate key style declared: " + styleName, parser);
final DeclaredKeyStyle style = new DeclaredKeyStyle(); final DeclaredKeyStyle style = new DeclaredKeyStyle();
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) { if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
@ -207,7 +208,8 @@ public class KeyStyles {
R.styleable.Keyboard_KeyStyle_parentStyle); R.styleable.Keyboard_KeyStyle_parentStyle);
final DeclaredKeyStyle parent = mStyles.get(parentStyle); final DeclaredKeyStyle parent = mStyles.get(parentStyle);
if (parent == null) if (parent == null)
throw new ParseException("Unknown parentStyle " + parentStyle, parser); throw new XmlParseUtils.ParseException(
"Unknown parentStyle " + parentStyle, parser);
style.addParent(parent); style.addParent(parent);
} }
style.parseKeyStyleAttributes(keyAttrs); style.parseKeyStyleAttributes(keyAttrs);

View file

@ -305,7 +305,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
parseKeyboardContent(parser, false); parseKeyboardContent(parser, false);
break; break;
} else { } else {
throw new IllegalStartTag(parser, TAG_KEYBOARD); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEYBOARD);
} }
} }
} }
@ -393,7 +393,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip); parseKeyStyle(parser, skip);
} else { } else {
throw new IllegalStartTag(parser, TAG_ROW); throw new XmlParseUtils.IllegalStartTag(parser, TAG_ROW);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -407,20 +407,20 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
continue; continue;
} else { } else {
throw new IllegalEndTag(parser, TAG_ROW); throw new XmlParseUtils.IllegalEndTag(parser, TAG_ROW);
} }
} }
} }
} }
private Row parseRowAttributes(XmlPullParser parser) { private Row parseRowAttributes(XmlPullParser parser) throws XmlPullParserException {
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard); R.styleable.Keyboard);
try { try {
if (a.hasValue(R.styleable.Keyboard_horizontalGap)) if (a.hasValue(R.styleable.Keyboard_horizontalGap))
throw new IllegalAttribute(parser, "horizontalGap"); throw new XmlParseUtils.IllegalAttribute(parser, "horizontalGap");
if (a.hasValue(R.styleable.Keyboard_verticalGap)) if (a.hasValue(R.styleable.Keyboard_verticalGap))
throw new IllegalAttribute(parser, "verticalGap"); throw new XmlParseUtils.IllegalAttribute(parser, "verticalGap");
return new Row(mResources, mParams, parser, mCurrentY); return new Row(mResources, mParams, parser, mCurrentY);
} finally { } finally {
a.recycle(); a.recycle();
@ -444,7 +444,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
parseKeyStyle(parser, skip); parseKeyStyle(parser, skip);
} else { } else {
throw new IllegalStartTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -460,7 +460,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_KEY_STYLE.equals(tag)) { } else if (TAG_KEY_STYLE.equals(tag)) {
continue; continue;
} else { } else {
throw new IllegalEndTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY);
} }
} }
} }
@ -469,13 +469,13 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
private void parseKey(XmlPullParser parser, Row row, boolean skip) private void parseKey(XmlPullParser parser, Row row, boolean skip)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
if (skip) { if (skip) {
checkEndTag(TAG_KEY, parser); XmlParseUtils.checkEndTag(TAG_KEY, parser);
} else { } else {
final Key key = new Key(mResources, mParams, row, parser, mKeyStyles); final Key key = new Key(mResources, mParams, row, parser, mKeyStyles);
if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d moreKeys=%s />", if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d moreKeys=%s />",
TAG_KEY, (key.isEnabled() ? "" : " disabled"), key.mLabel, key.mCode, TAG_KEY, (key.isEnabled() ? "" : " disabled"), key.mLabel, key.mCode,
Arrays.toString(key.mMoreKeys))); Arrays.toString(key.mMoreKeys)));
checkEndTag(TAG_KEY, parser); XmlParseUtils.checkEndTag(TAG_KEY, parser);
endKey(key); endKey(key);
} }
} }
@ -483,11 +483,11 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
private void parseSpacer(XmlPullParser parser, Row row, boolean skip) private void parseSpacer(XmlPullParser parser, Row row, boolean skip)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
if (skip) { if (skip) {
checkEndTag(TAG_SPACER, parser); XmlParseUtils.checkEndTag(TAG_SPACER, parser);
} else { } else {
final Key.Spacer spacer = new Key.Spacer(mResources, mParams, row, parser, mKeyStyles); final Key.Spacer spacer = new Key.Spacer(mResources, mParams, row, parser, mKeyStyles);
if (DEBUG) Log.d(TAG, String.format("<%s />", TAG_SPACER)); if (DEBUG) Log.d(TAG, String.format("<%s />", TAG_SPACER));
checkEndTag(TAG_SPACER, parser); XmlParseUtils.checkEndTag(TAG_SPACER, parser);
endKey(spacer); endKey(spacer);
} }
} }
@ -505,17 +505,21 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
private void parseIncludeInternal(XmlPullParser parser, Row row, boolean skip) private void parseIncludeInternal(XmlPullParser parser, Row row, boolean skip)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
if (skip) { if (skip) {
checkEndTag(TAG_INCLUDE, parser); XmlParseUtils.checkEndTag(TAG_INCLUDE, parser);
} else { } else {
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_Include); R.styleable.Keyboard_Include);
final int keyboardLayout = a.getResourceId( int keyboardLayout = 0;
R.styleable.Keyboard_Include_keyboardLayout, 0); try {
a.recycle(); XmlParseUtils.checkAttributeExists(a,
R.styleable.Keyboard_Include_keyboardLayout, "keyboardLayout",
TAG_INCLUDE, parser);
keyboardLayout = a.getResourceId(R.styleable.Keyboard_Include_keyboardLayout, 0);
} finally {
a.recycle();
}
checkEndTag(TAG_INCLUDE, parser); XmlParseUtils.checkEndTag(TAG_INCLUDE, parser);
if (keyboardLayout == 0)
throw new ParseException("No keyboardLayout attribute in <include/>", parser);
if (DEBUG) Log.d(TAG, String.format("<%s keyboardLayout=%s />", if (DEBUG) Log.d(TAG, String.format("<%s keyboardLayout=%s />",
TAG_INCLUDE, mResources.getResourceEntryName(keyboardLayout))); TAG_INCLUDE, mResources.getResourceEntryName(keyboardLayout)));
final XmlResourceParser parserForInclude = mResources.getXml(keyboardLayout); final XmlResourceParser parserForInclude = mResources.getXml(keyboardLayout);
@ -541,7 +545,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} }
break; break;
} else { } else {
throw new ParseException( throw new XmlParseUtils.ParseException(
"Included keyboard layout must have <merge> root element", parser); "Included keyboard layout must have <merge> root element", parser);
} }
} }
@ -571,7 +575,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} else if (TAG_DEFAULT.equals(tag)) { } else if (TAG_DEFAULT.equals(tag)) {
selected |= parseDefault(parser, row, selected ? true : skip); selected |= parseDefault(parser, row, selected ? true : skip);
} else { } else {
throw new IllegalStartTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalStartTag(parser, TAG_KEY);
} }
} else if (event == XmlPullParser.END_TAG) { } else if (event == XmlPullParser.END_TAG) {
final String tag = parser.getName(); final String tag = parser.getName();
@ -579,7 +583,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
if (DEBUG) Log.d(TAG, String.format("</%s>", TAG_SWITCH)); if (DEBUG) Log.d(TAG, String.format("</%s>", TAG_SWITCH));
break; break;
} else { } else {
throw new IllegalEndTag(parser, TAG_KEY); throw new XmlParseUtils.IllegalEndTag(parser, TAG_KEY);
} }
} }
} }
@ -716,14 +720,15 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
return true; return true;
} }
private void parseKeyStyle(XmlPullParser parser, boolean skip) { private void parseKeyStyle(XmlPullParser parser, boolean skip)
throws XmlPullParserException {
TypedArray keyStyleAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), TypedArray keyStyleAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_KeyStyle); R.styleable.Keyboard_KeyStyle);
TypedArray keyAttrs = mResources.obtainAttributes(Xml.asAttributeSet(parser), TypedArray keyAttrs = mResources.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_Key); R.styleable.Keyboard_Key);
try { try {
if (!keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_styleName)) if (!keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_styleName))
throw new ParseException("<" + TAG_KEY_STYLE throw new XmlParseUtils.ParseException("<" + TAG_KEY_STYLE
+ "/> needs styleName attribute", parser); + "/> needs styleName attribute", parser);
if (!skip) if (!skip)
mKeyStyles.parseKeyStyleAttributes(keyStyleAttr, keyAttrs, parser); mKeyStyles.parseKeyStyleAttributes(keyStyleAttr, keyAttrs, parser);
@ -733,13 +738,6 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} }
} }
public static void checkEndTag(String tag, XmlPullParser parser)
throws XmlPullParserException, IOException {
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
return;
throw new NonEmptyTag(tag, parser);
}
private void startKeyboard() { private void startKeyboard() {
mCurrentY += mParams.mTopPadding; mCurrentY += mParams.mTopPadding;
mTopEdge = true; mTopEdge = true;
@ -778,6 +776,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
} }
private void endKeyboard() { private void endKeyboard() {
// nothing to do here.
} }
private void addEdgeSpace(float width, Row row) { private void addEdgeSpace(float width, Row row) {
@ -824,41 +823,6 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
return v.type == TypedValue.TYPE_STRING; return v.type == TypedValue.TYPE_STRING;
} }
@SuppressWarnings("serial")
public static class ParseException extends InflateException {
public ParseException(String msg, XmlPullParser parser) {
super(msg + " at line " + parser.getLineNumber());
}
}
@SuppressWarnings("serial")
public static class IllegalStartTag extends ParseException {
public IllegalStartTag(XmlPullParser parser, String parent) {
super("Illegal start tag " + parser.getName() + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
public static class IllegalEndTag extends ParseException {
public IllegalEndTag(XmlPullParser parser, String parent) {
super("Illegal end tag " + parser.getName() + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
private static class IllegalAttribute extends ParseException {
public IllegalAttribute(XmlPullParser parser, String attribute) {
super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser);
}
}
@SuppressWarnings("serial")
private static class NonEmptyTag extends ParseException {
public NonEmptyTag(String tag, XmlPullParser parser) {
super(tag + " must be empty tag", parser);
}
}
private static String textAttr(String value, String name) { private static String textAttr(String value, String name) {
return value != null ? String.format(" %s=%s", name, value) : ""; return value != null ? String.format(" %s=%s", name, value) : "";
} }

View file

@ -0,0 +1,77 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.android.inputmethod.keyboard.internal;
import android.content.res.TypedArray;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class XmlParseUtils {
@SuppressWarnings("serial")
public static class ParseException extends XmlPullParserException {
public ParseException(String msg, XmlPullParser parser) {
super(msg + " at line " + parser.getLineNumber()
+ ", column " + parser.getColumnNumber());
}
}
@SuppressWarnings("serial")
public static class IllegalStartTag extends ParseException {
public IllegalStartTag(XmlPullParser parser, String parent) {
super("Illegal start tag " + parser.getName() + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
public static class IllegalEndTag extends ParseException {
public IllegalEndTag(XmlPullParser parser, String parent) {
super("Illegal end tag " + parser.getName() + " in " + parent, parser);
}
}
@SuppressWarnings("serial")
static class IllegalAttribute extends ParseException {
public IllegalAttribute(XmlPullParser parser, String attribute) {
super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser);
}
}
@SuppressWarnings("serial")
static class NonEmptyTag extends ParseException{
public NonEmptyTag(String tag, XmlPullParser parser) {
super(tag + " must be empty tag", parser);
}
}
public static void checkEndTag(String tag, XmlPullParser parser)
throws XmlPullParserException, IOException {
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
return;
throw new NonEmptyTag(tag, parser);
}
public static void checkAttributeExists(TypedArray attr, int attrId, String attrName,
String tag, XmlPullParser parser) throws XmlPullParserException {
if (attr.hasValue(attrId))
return;
throw new ParseException(
"No " + attrName + " attribute found in <" + tag + "/>", parser);
}
}