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