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 getAttributeNamespace (int index) { 38 return mParser.getAttributeNamespace(index); 39 } 40 41 public String getAttributeName(int index) { 42 return mParser.getAttributeName(index); 43 } 44 45 public String getAttributeValue(int index) { 46 return mParser.getAttributeValue(index); 47 } 48 49 public String getAttributeValue(String namespace, String name) { 50 return mParser.getAttributeValue(namespace, name); 51 } 52 53 public String getPositionDescription() { 54 return mParser.getPositionDescription(); 55 } 56 57 public int getAttributeNameResource(int index) { 58 return 0; 59 } 60 61 public int getAttributeListValue(String namespace, String attribute, 62 String[] options, int defaultValue) { 63 return XmlUtils.convertValueToList( 64 getAttributeValue(namespace, attribute), options, defaultValue); 65 } 66 67 public boolean getAttributeBooleanValue(String namespace, String attribute, 68 boolean defaultValue) { 69 return XmlUtils.convertValueToBoolean( 70 getAttributeValue(namespace, attribute), defaultValue); 71 } 72 73 public int getAttributeResourceValue(String namespace, String attribute, 74 int defaultValue) { 75 return XmlUtils.convertValueToInt( 76 getAttributeValue(namespace, attribute), defaultValue); 77 } 78 79 public int getAttributeIntValue(String namespace, String attribute, 80 int defaultValue) { 81 return XmlUtils.convertValueToInt( 82 getAttributeValue(namespace, attribute), defaultValue); 83 } 84 85 public int getAttributeUnsignedIntValue(String namespace, String attribute, 86 int defaultValue) { 87 return XmlUtils.convertValueToUnsignedInt( 88 getAttributeValue(namespace, attribute), defaultValue); 89 } 90 91 public float getAttributeFloatValue(String namespace, String attribute, 92 float defaultValue) { 93 String s = getAttributeValue(namespace, attribute); 94 if (s != null) { 95 return Float.parseFloat(s); 96 } 97 return defaultValue; 98 } 99 100 public int getAttributeListValue(int index, 101 String[] options, int defaultValue) { 102 return XmlUtils.convertValueToList( 103 getAttributeValue(index), options, defaultValue); 104 } 105 106 public boolean getAttributeBooleanValue(int index, boolean defaultValue) { 107 return XmlUtils.convertValueToBoolean( 108 getAttributeValue(index), defaultValue); 109 } 110 111 public int getAttributeResourceValue(int index, int defaultValue) { 112 return XmlUtils.convertValueToInt( 113 getAttributeValue(index), defaultValue); 114 } 115 116 public int getAttributeIntValue(int index, int defaultValue) { 117 return XmlUtils.convertValueToInt( 118 getAttributeValue(index), defaultValue); 119 } 120 121 public int getAttributeUnsignedIntValue(int index, int defaultValue) { 122 return XmlUtils.convertValueToUnsignedInt( 123 getAttributeValue(index), defaultValue); 124 } 125 126 public float getAttributeFloatValue(int index, float defaultValue) { 127 String s = getAttributeValue(index); 128 if (s != null) { 129 return Float.parseFloat(s); 130 } 131 return defaultValue; 132 } 133 134 public String getIdAttribute() { 135 return getAttributeValue(null, "id"); 136 } 137 138 public String getClassAttribute() { 139 return getAttributeValue(null, "class"); 140 } 141 142 public int getIdAttributeResourceValue(int defaultValue) { 143 return getAttributeResourceValue(null, "id", defaultValue); 144 } 145 146 public int getStyleAttribute() { 147 return getAttributeResourceValue(null, "style", 0); 148 } 149 150 /*package*/ XmlPullParser mParser; 151} 152