• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.google.android.exoplayer2.util;
17 
18 import androidx.annotation.Nullable;
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 
22 /**
23  * {@link XmlPullParser} utility methods.
24  */
25 public final class XmlPullParserUtil {
26 
XmlPullParserUtil()27   private XmlPullParserUtil() {}
28 
29   /**
30    * Returns whether the current event is an end tag with the specified name.
31    *
32    * @param xpp The {@link XmlPullParser} to query.
33    * @param name The specified name.
34    * @return Whether the current event is an end tag with the specified name.
35    * @throws XmlPullParserException If an error occurs querying the parser.
36    */
isEndTag(XmlPullParser xpp, String name)37   public static boolean isEndTag(XmlPullParser xpp, String name) throws XmlPullParserException {
38     return isEndTag(xpp) && xpp.getName().equals(name);
39   }
40 
41   /**
42    * Returns whether the current event is an end tag.
43    *
44    * @param xpp The {@link XmlPullParser} to query.
45    * @return Whether the current event is an end tag.
46    * @throws XmlPullParserException If an error occurs querying the parser.
47    */
isEndTag(XmlPullParser xpp)48   public static boolean isEndTag(XmlPullParser xpp) throws XmlPullParserException {
49     return xpp.getEventType() == XmlPullParser.END_TAG;
50   }
51 
52   /**
53    * Returns whether the current event is a start tag with the specified name.
54    *
55    * @param xpp The {@link XmlPullParser} to query.
56    * @param name The specified name.
57    * @return Whether the current event is a start tag with the specified name.
58    * @throws XmlPullParserException If an error occurs querying the parser.
59    */
isStartTag(XmlPullParser xpp, String name)60   public static boolean isStartTag(XmlPullParser xpp, String name) throws XmlPullParserException {
61     return isStartTag(xpp) && xpp.getName().equals(name);
62   }
63 
64   /**
65    * Returns whether the current event is a start tag.
66    *
67    * @param xpp The {@link XmlPullParser} to query.
68    * @return Whether the current event is a start tag.
69    * @throws XmlPullParserException If an error occurs querying the parser.
70    */
isStartTag(XmlPullParser xpp)71   public static boolean isStartTag(XmlPullParser xpp) throws XmlPullParserException {
72     return xpp.getEventType() == XmlPullParser.START_TAG;
73   }
74 
75   /**
76    * Returns whether the current event is a start tag with the specified name. If the current event
77    * has a raw name then its prefix is stripped before matching.
78    *
79    * @param xpp The {@link XmlPullParser} to query.
80    * @param name The specified name.
81    * @return Whether the current event is a start tag with the specified name.
82    * @throws XmlPullParserException If an error occurs querying the parser.
83    */
isStartTagIgnorePrefix(XmlPullParser xpp, String name)84   public static boolean isStartTagIgnorePrefix(XmlPullParser xpp, String name)
85       throws XmlPullParserException {
86     return isStartTag(xpp) && stripPrefix(xpp.getName()).equals(name);
87   }
88 
89   /**
90    * Returns the value of an attribute of the current start tag.
91    *
92    * @param xpp The {@link XmlPullParser} to query.
93    * @param attributeName The name of the attribute.
94    * @return The value of the attribute, or null if the current event is not a start tag or if no
95    *     such attribute was found.
96    */
getAttributeValue(XmlPullParser xpp, String attributeName)97   public static @Nullable String getAttributeValue(XmlPullParser xpp, String attributeName) {
98     int attributeCount = xpp.getAttributeCount();
99     for (int i = 0; i < attributeCount; i++) {
100       if (xpp.getAttributeName(i).equals(attributeName)) {
101         return xpp.getAttributeValue(i);
102       }
103     }
104     return null;
105   }
106 
107   /**
108    * Returns the value of an attribute of the current start tag. Any raw attribute names in the
109    * current start tag have their prefixes stripped before matching.
110    *
111    * @param xpp The {@link XmlPullParser} to query.
112    * @param attributeName The name of the attribute.
113    * @return The value of the attribute, or null if the current event is not a start tag or if no
114    *     such attribute was found.
115    */
getAttributeValueIgnorePrefix( XmlPullParser xpp, String attributeName)116   public static @Nullable String getAttributeValueIgnorePrefix(
117       XmlPullParser xpp, String attributeName) {
118     int attributeCount = xpp.getAttributeCount();
119     for (int i = 0; i < attributeCount; i++) {
120       if (stripPrefix(xpp.getAttributeName(i)).equals(attributeName)) {
121         return xpp.getAttributeValue(i);
122       }
123     }
124     return null;
125   }
126 
stripPrefix(String name)127   private static String stripPrefix(String name) {
128     int prefixSeparatorIndex = name.indexOf(':');
129     return prefixSeparatorIndex == -1 ? name : name.substring(prefixSeparatorIndex + 1);
130   }
131 }
132