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