1 /*
2  * Copyright (C) 2008 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.graphics.drawable.cts;
18 
19 import android.content.res.XmlResourceParser;
20 import android.util.AttributeSet;
21 import android.util.Xml;
22 
23 import java.io.IOException;
24 
25 import org.xmlpull.v1.XmlPullParser;
26 import org.xmlpull.v1.XmlPullParserException;
27 
28 /**
29  * The useful methods for graphics.drawable test.
30  */
31 public class DrawableTestUtils {
32 
skipCurrentTag(XmlPullParser parser)33     public static void skipCurrentTag(XmlPullParser parser)
34             throws XmlPullParserException, IOException {
35         int outerDepth = parser.getDepth();
36         int type;
37         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
38                && (type != XmlPullParser.END_TAG
39                        || parser.getDepth() > outerDepth)) {
40         }
41     }
42 
43     /**
44      * Retrieve an AttributeSet from a XML.
45      *
46      * @param parser the XmlPullParser to use for the xml parsing.
47      * @param searchedNodeName the name of the target node.
48      * @return the AttributeSet retrieved from specified node.
49      * @throws IOException
50      * @throws XmlPullParserException
51      */
getAttributeSet(XmlResourceParser parser, String searchedNodeName)52     public static AttributeSet getAttributeSet(XmlResourceParser parser, String searchedNodeName)
53             throws XmlPullParserException, IOException {
54         AttributeSet attrs = null;
55         int type;
56         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
57                 && type != XmlPullParser.START_TAG) {
58         }
59         String nodeName = parser.getName();
60         if (!"alias".equals(nodeName)) {
61             throw new RuntimeException();
62         }
63         int outerDepth = parser.getDepth();
64         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
65                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
66             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
67                 continue;
68             }
69             nodeName = parser.getName();
70             if (searchedNodeName.equals(nodeName)) {
71                 outerDepth = parser.getDepth();
72                 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
73                         && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
74                     if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
75                         continue;
76                     }
77                     nodeName = parser.getName();
78                     attrs = Xml.asAttributeSet(parser);
79                     break;
80                 }
81                 break;
82             } else {
83                 skipCurrentTag(parser);
84             }
85         }
86         return attrs;
87     }
88 }
89