Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2006 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 
     21 import android.annotation.UnsupportedAppUsage;
     22 import android.util.AttributeSet;
     23 
     24 import com.android.internal.util.XmlUtils;
     25 
     26 /**
     27  * Provides an implementation of AttributeSet on top of an XmlPullParser.
     28  */
     29 class XmlPullAttributes implements AttributeSet {
     30     @UnsupportedAppUsage
     31     public XmlPullAttributes(XmlPullParser parser) {
     32         mParser = parser;
     33     }
     34 
     35     public int getAttributeCount() {
     36         return mParser.getAttributeCount();
     37     }
     38 
     39     public String getAttributeNamespace (int index) {
     40         return mParser.getAttributeNamespace(index);
     41     }
     42 
     43     public String getAttributeName(int index) {
     44         return mParser.getAttributeName(index);
     45     }
     46 
     47     public String getAttributeValue(int index) {
     48         return mParser.getAttributeValue(index);
     49     }
     50 
     51     public String getAttributeValue(String namespace, String name) {
     52         return mParser.getAttributeValue(namespace, name);
     53     }
     54 
     55     public String getPositionDescription() {
     56         return mParser.getPositionDescription();
     57     }
     58 
     59     public int getAttributeNameResource(int index) {
     60         return 0;
     61     }
     62 
     63     public int getAttributeListValue(String namespace, String attribute,
     64             String[] options, int defaultValue) {
     65         return XmlUtils.convertValueToList(
     66             getAttributeValue(namespace, attribute), options, defaultValue);
     67     }
     68 
     69     public boolean getAttributeBooleanValue(String namespace, String attribute,
     70             boolean defaultValue) {
     71         return XmlUtils.convertValueToBoolean(
     72             getAttributeValue(namespace, attribute), defaultValue);
     73     }
     74 
     75     public int getAttributeResourceValue(String namespace, String attribute,
     76             int defaultValue) {
     77         return XmlUtils.convertValueToInt(
     78             getAttributeValue(namespace, attribute), defaultValue);
     79     }
     80 
     81     public int getAttributeIntValue(String namespace, String attribute,
     82             int defaultValue) {
     83         return XmlUtils.convertValueToInt(
     84             getAttributeValue(namespace, attribute), defaultValue);
     85     }
     86 
     87     public int getAttributeUnsignedIntValue(String namespace, String attribute,
     88             int defaultValue) {
     89         return XmlUtils.convertValueToUnsignedInt(
     90             getAttributeValue(namespace, attribute), defaultValue);
     91     }
     92 
     93     public float getAttributeFloatValue(String namespace, String attribute,
     94             float defaultValue) {
     95         String s = getAttributeValue(namespace, attribute);
     96         if (s != null) {
     97             return Float.parseFloat(s);
     98         }
     99         return defaultValue;
    100     }
    101 
    102     public int getAttributeListValue(int index,
    103             String[] options, int defaultValue) {
    104         return XmlUtils.convertValueToList(
    105             getAttributeValue(index), options, defaultValue);
    106     }
    107 
    108     public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
    109         return XmlUtils.convertValueToBoolean(
    110             getAttributeValue(index), defaultValue);
    111     }
    112 
    113     public int getAttributeResourceValue(int index, int defaultValue) {
    114         return XmlUtils.convertValueToInt(
    115             getAttributeValue(index), defaultValue);
    116     }
    117 
    118     public int getAttributeIntValue(int index, int defaultValue) {
    119         return XmlUtils.convertValueToInt(
    120             getAttributeValue(index), defaultValue);
    121     }
    122 
    123     public int getAttributeUnsignedIntValue(int index, int defaultValue) {
    124         return XmlUtils.convertValueToUnsignedInt(
    125             getAttributeValue(index), defaultValue);
    126     }
    127 
    128     public float getAttributeFloatValue(int index, float defaultValue) {
    129         String s = getAttributeValue(index);
    130         if (s != null) {
    131             return Float.parseFloat(s);
    132         }
    133         return defaultValue;
    134     }
    135 
    136     public String getIdAttribute() {
    137         return getAttributeValue(null, "id");
    138     }
    139 
    140     public String getClassAttribute() {
    141         return getAttributeValue(null, "class");
    142     }
    143 
    144     public int getIdAttributeResourceValue(int defaultValue) {
    145         return getAttributeResourceValue(null, "id", defaultValue);
    146     }
    147 
    148     public int getStyleAttribute() {
    149         return getAttributeResourceValue(null, "style", 0);
    150     }
    151 
    152     @UnsupportedAppUsage
    153     /*package*/ XmlPullParser mParser;
    154 }
    155