下面列出了android.content.res.XmlResourceParser#END_DOCUMENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@CheckResult
@NonNull
public List<BottomBarTab> parseTabs() {
if (tabs == null) {
tabs = new ArrayList<>(AVG_NUMBER_OF_TABS);
try {
int eventType;
do {
eventType = parser.next();
if (eventType == XmlResourceParser.START_TAG && TAB_TAG.equals(parser.getName())) {
BottomBarTab bottomBarTab = parseNewTab(parser, tabs.size());
tabs.add(bottomBarTab);
}
} while (eventType != XmlResourceParser.END_DOCUMENT);
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
throw new TabParserException();
}
}
return tabs;
}
public List<String[]> parse(XmlResourceParser parser) throws IOException, XmlPullParserException {
ArrayList<String[]> data = new ArrayList<>();
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
String defaultValue = parser.getAttributeValue(NAMESPACE, DEFAULT_VALUE);
String key = parser.getAttributeValue(NAMESPACE, KEY);
if (defaultValue != null) {
String[] keyValue = new String[2];
keyValue[0] = key;
keyValue[1] = defaultValue;
data.add(keyValue);
}
}
eventType = parser.next();
}
return data;
}
private void skipToEndOfRow(XmlResourceParser parser)
throws XmlPullParserException, IOException {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG
&& parser.getName().equals(TAG_ROW)) {
break;
}
}
}
private void skipToEndOfRow(XmlResourceParser parser)
throws XmlPullParserException, IOException {
if (DEBUG) Log.v(TAG, "skipToEndOfRow:");
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG
&& parser.getName().equals(TAG_ROW)) {
break;
}
}
}
private Map<String, List<CallerInfo>> readValidCertificates(XmlResourceParser parser) {
HashMap<String, List<CallerInfo>> validCertificates = new HashMap<>();
try {
int eventType = parser.next();
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG
&& parser.getName().equals("signing_certificate")) {
String name = parser.getAttributeValue(null, "name");
String packageName = parser.getAttributeValue(null, "package");
boolean isRelease = parser.getAttributeBooleanValue(null, "release", false);
String certificate = parser.nextText().replaceAll("\\s|\\n", "");
CallerInfo info = new CallerInfo(name, packageName, isRelease);
List<CallerInfo> infos = validCertificates.get(certificate);
if (infos == null) {
infos = new ArrayList<>();
validCertificates.put(certificate, infos);
}
if (Log.logVEnabled()) {
Log.v(TAG, "Adding allowed caller: " + info.name
+ " package=" + info.packageName + " release=" + info.release
+ " certificate=" + certificate);
}
infos.add(info);
}
eventType = parser.next();
}
} catch (XmlPullParserException | IOException e) {
Log.w(TAG, "Could not read allowed callers from XML", e);
}
return validCertificates;
}
private void skipToEndOfRow(XmlResourceParser parser)
throws XmlPullParserException, IOException {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG
&& parser.getName().equals(TAG_ROW)) {
break;
}
}
}
private static void skipCurrentTag(XmlResourceParser parser)
throws XmlPullParserException, IOException {
int outerDepth = parser.getDepth();
int type;
while ((type=parser.next()) != XmlResourceParser.END_DOCUMENT
&& (type != XmlResourceParser.END_TAG
|| parser.getDepth() > outerDepth)) {
}
}
/**
* Check to make sure global_tracker.xml was configured correctly (this function only needed
* for sample apps).
*/
private boolean checkConfiguration() {
XmlResourceParser parser = getResources().getXml(R.xml.global_tracker);
boolean foundTag = false;
try {
while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (parser.getEventType() == XmlResourceParser.START_TAG) {
String tagName = parser.getName();
String nameAttr = parser.getAttributeValue(null, "name");
foundTag = "string".equals(tagName) && "ga_trackingId".equals(nameAttr);
}
if (parser.getEventType() == XmlResourceParser.TEXT) {
if (foundTag && parser.getText().contains("REPLACE_ME")) {
return false;
}
}
parser.next();
}
} catch (Exception e) {
Log.w(TAG, "checkConfiguration", e);
return false;
}
return true;
}
private Map<String, ArrayList<CallerInfo>> readValidCertificates(XmlResourceParser parser) {
HashMap<String, ArrayList<CallerInfo>> validCertificates = new HashMap<>();
try {
int eventType = parser.next();
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG
&& parser.getName().equals("signing_certificate")) {
String name = parser.getAttributeValue(null, "name");
String packageName = parser.getAttributeValue(null, "package");
boolean isRelease = parser.getAttributeBooleanValue(null, "release", false);
String certificate = parser.nextText().replaceAll("\\s|\\n", "");
CallerInfo info = new CallerInfo(name, packageName, isRelease);
ArrayList<CallerInfo> infos = validCertificates.get(certificate);
if (infos == null) {
infos = new ArrayList<>();
validCertificates.put(certificate, infos);
}
Timber.v("Adding allowed caller: %s package=%s release=%b certificate=%s",
info.name, info.packageName, info.release, certificate);
infos.add(info);
}
eventType = parser.next();
}
} catch (XmlPullParserException | IOException e) {
Timber.e(e, "Could not read allowed callers from XML.");
}
return validCertificates;
}
/**
* 读取XML配置文件
* @param id 配置文件索引
*/
private void readXML(int id){
String name;
String className = getClass().getSimpleName();
XmlResourceParser xrp = getResources().getXml(id);
try {
while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
if (xrp.getEventType() == XmlResourceParser.START_TAG) {
name = xrp.getName();
if (name.equals("BackgroundColor")) {
mNavigationView.setBackgroundColor(xrp.getAttributeIntValue(0, mNavigationView.getBackgroundColor()));
}
if (name.equals("StatusType")) {
mNavigationView.setStatusType(xrp.getAttributeIntValue(0, mNavigationView.getStatusType()));
}
if (name.equals("TitleColor")) {
mNavigationView.setTitleColor(xrp.getAttributeIntValue(0, mNavigationView.getTitleColor()));
}
if (name.equals("LineBackgroundColor")) {
mNavigationView.setLineBackgroundColor(xrp.getAttributeIntValue(0, mNavigationView.getTitleColor()));
}
if (name.equals("NavigationTextColor")) {
mNavigationView.setNavigationTextColor(xrp.getAttributeIntValue(0, mNavigationView.getNavigationTextColor()));
}
if (name.equals("Title") && xrp.getAttributeValue(0).equals(className)) {
mNavigationView.setTitle(xrp.getAttributeValue(1));
}
}
xrp.next();
}
} catch (Exception e) {
Log.d(UE.TAG, "UEConfig配置出错"+e.toString());
}
}
/**
* 读取XML配置文件
* @param id 配置文件索引
*/
private void readXML(int id){
String name;
String className = getClass().getSimpleName();
XmlResourceParser xrp = getResources().getXml(id);
try {
while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
if (xrp.getEventType() == XmlResourceParser.START_TAG) {
name = xrp.getName();
if (name.equals("BackgroundColor")) {
mNavigationView.setBackgroundColor(xrp.getAttributeIntValue(0, mNavigationView.getBackgroundColor()));
}
if (name.equals("StatusType")) {
mNavigationView.setStatusType(xrp.getAttributeIntValue(0, mNavigationView.getStatusType()));
}
if (name.equals("TitleColor")) {
mNavigationView.setTitleColor(xrp.getAttributeIntValue(0, mNavigationView.getTitleColor()));
}
if (name.equals("LineBackgroundColor")) {
mNavigationView.setLineBackgroundColor(xrp.getAttributeIntValue(0, mNavigationView.getTitleColor()));
}
if (name.equals("NavigationTextColor")) {
mNavigationView.setNavigationTextColor(xrp.getAttributeIntValue(0, mNavigationView.getNavigationTextColor()));
}
if (name.equals("Title") && xrp.getAttributeValue(0).equals(className)) {
mNavigationView.setTitle(xrp.getAttributeValue(1));
}
}
xrp.next();
}
} catch (Exception e) {
Log.d(UE.TAG, "UEConfig配置出错"+e.toString());
}
}
private void loadKeyboard(Context context, XmlResourceParser parser) {
boolean inKey = false;
boolean inRow = false;
boolean leftMostKey = false;
int row = 0;
int x = 0;
int y = 0;
Key key = null;
Row currentRow = null;
Resources res = context.getResources();
boolean skipRow = false;
try {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
String tag = parser.getName();
if (TAG_ROW.equals(tag)) {
inRow = true;
x = 0;
currentRow = createRowFromXml(res, parser);
rows.add(currentRow);
skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;
if (skipRow) {
skipToEndOfRow(parser);
inRow = false;
}
} else if (TAG_KEY.equals(tag)) {
inKey = true;
key = createKeyFromXml(res, currentRow, x, y, parser);
mKeys.add(key);
if (key.codes[0] == KEYCODE_SHIFT) {
// Find available shift key slot and put this shift key in it
for (int i = 0; i < mShiftKeys.length; i++) {
if (mShiftKeys[i] == null) {
mShiftKeys[i] = key;
mShiftKeyIndices[i] = mKeys.size()-1;
break;
}
}
mModifierKeys.add(key);
} else if (key.codes[0] == KEYCODE_ALT) {
mModifierKeys.add(key);
}
currentRow.mKeys.add(key);
} else if (TAG_KEYBOARD.equals(tag)) {
parseKeyboardAttributes(res, parser);
}
} else if (event == XmlResourceParser.END_TAG) {
if (inKey) {
inKey = false;
x += key.gap + key.width;
if (x > mTotalWidth) {
mTotalWidth = x;
}
} else if (inRow) {
inRow = false;
y += currentRow.verticalGap;
y += currentRow.defaultHeight;
row++;
} else {
// TODO: error or extend?
}
}
}
} catch (Exception e) {
Log.e(TAG, "Parse error:" + e);
e.printStackTrace();
}
mTotalHeight = y - mDefaultVerticalGap;
}
private void loadKeyboard(Context context, XmlResourceParser parser) {
if (DEBUG) Log.v(TAG, "loadKeyboard:");
boolean inKey = false;
boolean inRow = false;
boolean leftMostKey = false;
int row = 0;
int x = 0;
int y = 0;
Key key = null;
Row currentRow = null;
Resources res = context.getResources();
boolean skipRow = false;
try {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
String tag = parser.getName();
if (TAG_ROW.equals(tag)) {
inRow = true;
x = 0;
currentRow = createRowFromXml(res, parser);
rows.add(currentRow);
skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;
if (skipRow) {
skipToEndOfRow(parser);
inRow = false;
}
} else if (TAG_KEY.equals(tag)) {
inKey = true;
key = createKeyFromXml(res, currentRow, x, y, parser);
Log.d(TAG, "loadKeyboard:key=" + key);
mKeys.add(key);
if (key.codes[0] == KEYCODE_SHIFT) {
// Find available shift key slot and put this shift key in it
for (int i = 0; i < mShiftKeys.length; i++) {
if (mShiftKeys[i] == null) {
mShiftKeys[i] = key;
mShiftKeyIndices[i] = mKeys.size() - 1;
break;
}
}
mModifierKeys.add(key);
} else if (key.codes[0] == KEYCODE_ALT) {
mModifierKeys.add(key);
}
currentRow.mKeys.add(key);
} else if (TAG_KEYBOARD.equals(tag)) {
parseKeyboardAttributes(res, parser);
}
} else if (event == XmlResourceParser.END_TAG) {
if (inKey) {
inKey = false;
x += key.gap + key.width;
if (x > mTotalWidth) {
mTotalWidth = x;
}
} else if (inRow) {
inRow = false;
y += currentRow.verticalGap;
y += currentRow.defaultHeight;
row++;
} else {
// TODO: error or extend?
}
}
}
} catch (Exception e) {
Log.e(TAG, "Parse error:" + e);
e.printStackTrace();
}
mTotalHeight = y - mDefaultVerticalGap;
}
private boolean isAtDocumentEnd() {
return currentEventType == XmlResourceParser.END_DOCUMENT;
}
private void loadKeyboard(Context context, XmlResourceParser parser) {
boolean inKey = false;
boolean inRow = false;
float x = 0;
int y = 0;
Key key = null;
Row currentRow = null;
Resources res = context.getResources();
boolean skipRow = false;
mRowCount = 0;
try {
int event;
Key prevKey = null;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
String tag = parser.getName();
if (TAG_ROW.equals(tag)) {
inRow = true;
x = 0;
currentRow = createRowFromXml(res, parser);
skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;
if (currentRow.extension) {
if (mUseExtension) {
++mExtensionRowCount;
} else {
skipRow = true;
}
}
if (skipRow) {
skipToEndOfRow(parser);
inRow = false;
}
} else if (TAG_KEY.equals(tag)) {
inKey = true;
key = createKeyFromXml(res, currentRow, Math.round(x), y, parser);
key.realX = x;
if (key.codes == null) {
// skip this key, adding its width to the previous one
if (prevKey != null) {
prevKey.width += key.width;
}
} else {
mKeys.add(key);
prevKey = key;
if (key.codes[0] == KEYCODE_SHIFT) {
if (mShiftKeyIndex == -1) {
mShiftKey = key;
mShiftKeyIndex = mKeys.size()-1;
}
mModifierKeys.add(key);
} else if (key.codes[0] == KEYCODE_ALT_SYM) {
mModifierKeys.add(key);
} else if (key.codes[0] == LatinKeyboardView.KEYCODE_CTRL_LEFT) {
mCtrlKey = key;
} else if (key.codes[0] == LatinKeyboardView.KEYCODE_ALT_LEFT) {
mAltKey = key;
} else if (key.codes[0] == LatinKeyboardView.KEYCODE_META_LEFT) {
mMetaKey = key;
}
}
} else if (TAG_KEYBOARD.equals(tag)) {
parseKeyboardAttributes(res, parser);
}
} else if (event == XmlResourceParser.END_TAG) {
if (inKey) {
inKey = false;
x += key.realGap + key.realWidth;
if (x > mTotalWidth) {
mTotalWidth = Math.round(x);
}
} else if (inRow) {
inRow = false;
y += currentRow.verticalGap;
y += currentRow.defaultHeight;
mRowCount++;
} else {
// TODO: error or extend?
}
}
}
} catch (Exception e) {
Log.e(TAG, "Parse error:" + e);
e.printStackTrace();
}
mTotalHeight = y - mDefaultVerticalGap;
}
public static List<License> Parse(XmlResourceParser parser)
throws XmlPullParserException, IOException {
List<License> licenses = new ArrayList<License>();
int event = parser.getEventType();
String name = null;
String type = null;
String license = null;
while (event != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
if (!parser.getName().equals(TAG_ROOT)
&& !parser.getName().equals(TAG_CHILD))
throw new XmlPullParserException(
"Error in xml: tag isn't '" + TAG_ROOT + "' or '"
+ TAG_CHILD + "' at line:"
+ parser.getLineNumber());
name = parser.getAttributeValue(null, ATTR_NAME);
type = parser.getAttributeValue(null, ATTR_TYPE);
} else if (event == XmlResourceParser.TEXT) {
license = parser.getText();
} else if (event == XmlResourceParser.END_TAG) {
if (name != null && type != null && license != null
&& !parser.getName().equals(TAG_ROOT)) {
if (type.equals(VALUE_FILE)) {
licenses.add(new License(name, License.TYPE_FILE,
license));
System.out.println(name);
} else if (type.equals(VALUE_LIBRARY)) {
licenses.add(new License(name, License.TYPE_LIBRARY,
license));
System.out.println(name);
} else {
throw new XmlPullParserException(
"Error in xml: 'type' isn't valid at line:"
+ parser.getLineNumber());
}
} else if (name == null) {
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'name' at line:"
+ parser.getLineNumber());
} else if (type == null) {
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'type' at line:"
+ parser.getLineNumber());
} else if (license == null){
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'license text' at line:"
+ parser.getLineNumber());
}
}
event = parser.next();
}
parser.close();
return licenses;
}