1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util; 18 19 import org.xmlpull.v1.XmlPullParser; 20 import org.xmlpull.v1.XmlPullParserException; 21 22 import java.io.IOException; 23 24 /** 25 * Bits and pieces copied from hidden API of 26 * frameworks/base/core/java/com/android/internal/util/XmlUtils.java 27 * 28 * @hide 29 */ 30 public class XmlUtils { 31 32 /** @hide */ beginDocument(XmlPullParser parser, String firstElementName)33 public static final void beginDocument(XmlPullParser parser, String firstElementName) 34 throws XmlPullParserException, IOException { 35 int type; 36 while ((type = parser.next()) != parser.START_TAG 37 && type != parser.END_DOCUMENT) { 38 // Do nothing 39 } 40 41 if (type != parser.START_TAG) { 42 throw new XmlPullParserException("No start tag found"); 43 } 44 45 if (!parser.getName().equals(firstElementName)) { 46 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() 47 + ", expected " + firstElementName); 48 } 49 } 50 51 /** @hide */ nextElementWithin(XmlPullParser parser, int outerDepth)52 public static boolean nextElementWithin(XmlPullParser parser, int outerDepth) 53 throws IOException, XmlPullParserException { 54 for (;;) { 55 int type = parser.next(); 56 if (type == XmlPullParser.END_DOCUMENT 57 || (type == XmlPullParser.END_TAG && parser.getDepth() == outerDepth)) { 58 return false; 59 } 60 if (type == XmlPullParser.START_TAG 61 && parser.getDepth() == outerDepth + 1) { 62 return true; 63 } 64 } 65 } 66 } 67