1 /* 2 * Copyright (C) 2020 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 import android.util.TypedXmlPullParser; 18 import android.util.TypedXmlSerializer; 19 20 import com.android.internal.util.XmlUtils; 21 22 import com.google.errorprone.refaster.annotation.AfterTemplate; 23 import com.google.errorprone.refaster.annotation.BeforeTemplate; 24 25 /** 26 * Refaster templates that migrate callers to equivalent and more efficient 27 * {@link TypedXmlSerializer} and {@link TypedXmlPullParser} methods. 28 */ 29 public class EfficientXml { 30 class IntToString { 31 @BeforeTemplate beforeToString(TypedXmlSerializer out, String n, int v)32 void beforeToString(TypedXmlSerializer out, String n, int v) throws Exception { 33 out.attribute(null, n, Integer.toString(v)); 34 } 35 36 @BeforeTemplate beforeValueOf(TypedXmlSerializer out, String n, int v)37 void beforeValueOf(TypedXmlSerializer out, String n, int v) throws Exception { 38 out.attribute(null, n, String.valueOf(v)); 39 } 40 41 @BeforeTemplate beforeUtils(TypedXmlSerializer out, String n, int v)42 void beforeUtils(TypedXmlSerializer out, String n, int v) throws Exception { 43 XmlUtils.writeIntAttribute(out, n, v); 44 } 45 46 @BeforeTemplate beforeRadix(TypedXmlSerializer out, String n, int v)47 void beforeRadix(TypedXmlSerializer out, String n, int v) throws Exception { 48 out.attribute(null, n, Integer.toString(v, 10)); 49 } 50 51 @AfterTemplate after(TypedXmlSerializer out, String n, int v)52 void after(TypedXmlSerializer out, String n, int v) throws Exception { 53 out.attributeInt(null, n, v); 54 } 55 } 56 57 class IntToStringHex { 58 @BeforeTemplate beforeToHexString(TypedXmlSerializer out, String n, int v)59 void beforeToHexString(TypedXmlSerializer out, String n, int v) throws Exception { 60 out.attribute(null, n, Integer.toHexString(v)); 61 } 62 63 @BeforeTemplate beforeRadix(TypedXmlSerializer out, String n, int v)64 void beforeRadix(TypedXmlSerializer out, String n, int v) throws Exception { 65 out.attribute(null, n, Integer.toString(v, 16)); 66 } 67 68 @AfterTemplate after(TypedXmlSerializer out, String n, int v)69 void after(TypedXmlSerializer out, String n, int v) throws Exception { 70 out.attributeIntHex(null, n, v); 71 } 72 } 73 74 class IntFromString { 75 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)76 int beforeParse(TypedXmlPullParser in, String n) throws Exception { 77 return Integer.parseInt(in.getAttributeValue(null, n)); 78 } 79 80 @BeforeTemplate beforeUtils(TypedXmlPullParser in, String n)81 int beforeUtils(TypedXmlPullParser in, String n) throws Exception { 82 return XmlUtils.readIntAttribute(in, n); 83 } 84 85 @BeforeTemplate beforeRadix(TypedXmlPullParser in, String n)86 int beforeRadix(TypedXmlPullParser in, String n) throws Exception { 87 return Integer.parseInt(in.getAttributeValue(null, n), 10); 88 } 89 90 @AfterTemplate after(TypedXmlPullParser in, String n)91 int after(TypedXmlPullParser in, String n) throws Exception { 92 return in.getAttributeInt(null, n); 93 } 94 } 95 96 class IntFromStringDefault { 97 @BeforeTemplate before(TypedXmlPullParser in, String n, int d)98 int before(TypedXmlPullParser in, String n, int d) throws Exception { 99 return XmlUtils.readIntAttribute(in, n, d); 100 } 101 102 @AfterTemplate after(TypedXmlPullParser in, String n, int d)103 int after(TypedXmlPullParser in, String n, int d) throws Exception { 104 return in.getAttributeInt(null, n, d); 105 } 106 } 107 108 class IntFromStringHex { 109 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)110 int beforeParse(TypedXmlPullParser in, String n) throws Exception { 111 return Integer.parseInt(in.getAttributeValue(null, n), 16); 112 } 113 114 @AfterTemplate after(TypedXmlPullParser in, String n)115 int after(TypedXmlPullParser in, String n) throws Exception { 116 return in.getAttributeIntHex(null, n); 117 } 118 } 119 120 class LongToString { 121 @BeforeTemplate beforeToString(TypedXmlSerializer out, String n, long v)122 void beforeToString(TypedXmlSerializer out, String n, long v) throws Exception { 123 out.attribute(null, n, Long.toString(v)); 124 } 125 126 @BeforeTemplate beforeValueOf(TypedXmlSerializer out, String n, long v)127 void beforeValueOf(TypedXmlSerializer out, String n, long v) throws Exception { 128 out.attribute(null, n, String.valueOf(v)); 129 } 130 131 @BeforeTemplate beforeUtils(TypedXmlSerializer out, String n, long v)132 void beforeUtils(TypedXmlSerializer out, String n, long v) throws Exception { 133 XmlUtils.writeLongAttribute(out, n, v); 134 } 135 136 @BeforeTemplate beforeRadix(TypedXmlSerializer out, String n, long v)137 void beforeRadix(TypedXmlSerializer out, String n, long v) throws Exception { 138 out.attribute(null, n, Long.toString(v, 10)); 139 } 140 141 @AfterTemplate after(TypedXmlSerializer out, String n, long v)142 void after(TypedXmlSerializer out, String n, long v) throws Exception { 143 out.attributeLong(null, n, v); 144 } 145 } 146 147 class LongToStringHex { 148 @BeforeTemplate beforeToHexString(TypedXmlSerializer out, String n, long v)149 void beforeToHexString(TypedXmlSerializer out, String n, long v) throws Exception { 150 out.attribute(null, n, Long.toHexString(v)); 151 } 152 153 @BeforeTemplate beforeRadix(TypedXmlSerializer out, String n, long v)154 void beforeRadix(TypedXmlSerializer out, String n, long v) throws Exception { 155 out.attribute(null, n, Long.toString(v, 16)); 156 } 157 158 @AfterTemplate after(TypedXmlSerializer out, String n, long v)159 void after(TypedXmlSerializer out, String n, long v) throws Exception { 160 out.attributeLongHex(null, n, v); 161 } 162 } 163 164 class LongFromString { 165 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)166 long beforeParse(TypedXmlPullParser in, String n) throws Exception { 167 return Long.parseLong(in.getAttributeValue(null, n)); 168 } 169 170 @BeforeTemplate beforeUtils(TypedXmlPullParser in, String n)171 long beforeUtils(TypedXmlPullParser in, String n) throws Exception { 172 return XmlUtils.readLongAttribute(in, n); 173 } 174 175 @BeforeTemplate beforeRadix(TypedXmlPullParser in, String n)176 long beforeRadix(TypedXmlPullParser in, String n) throws Exception { 177 return Long.parseLong(in.getAttributeValue(null, n), 10); 178 } 179 180 @AfterTemplate after(TypedXmlPullParser in, String n)181 long after(TypedXmlPullParser in, String n) throws Exception { 182 return in.getAttributeLong(null, n); 183 } 184 } 185 186 class LongFromStringDefault { 187 @BeforeTemplate before(TypedXmlPullParser in, String n, long d)188 long before(TypedXmlPullParser in, String n, long d) throws Exception { 189 return XmlUtils.readLongAttribute(in, n, d); 190 } 191 192 @AfterTemplate after(TypedXmlPullParser in, String n, long d)193 long after(TypedXmlPullParser in, String n, long d) throws Exception { 194 return in.getAttributeLong(null, n, d); 195 } 196 } 197 198 class LongFromStringHex { 199 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)200 long beforeParse(TypedXmlPullParser in, String n) throws Exception { 201 return Long.parseLong(in.getAttributeValue(null, n), 16); 202 } 203 204 @AfterTemplate after(TypedXmlPullParser in, String n)205 long after(TypedXmlPullParser in, String n) throws Exception { 206 return in.getAttributeLongHex(null, n); 207 } 208 } 209 210 class FloatToString { 211 @BeforeTemplate beforeToString(TypedXmlSerializer out, String n, float v)212 void beforeToString(TypedXmlSerializer out, String n, float v) throws Exception { 213 out.attribute(null, n, Float.toString(v)); 214 } 215 216 @BeforeTemplate beforeValueOf(TypedXmlSerializer out, String n, float v)217 void beforeValueOf(TypedXmlSerializer out, String n, float v) throws Exception { 218 out.attribute(null, n, String.valueOf(v)); 219 } 220 221 @BeforeTemplate beforeUtils(TypedXmlSerializer out, String n, float v)222 void beforeUtils(TypedXmlSerializer out, String n, float v) throws Exception { 223 XmlUtils.writeFloatAttribute(out, n, v); 224 } 225 226 @AfterTemplate after(TypedXmlSerializer out, String n, float v)227 void after(TypedXmlSerializer out, String n, float v) throws Exception { 228 out.attributeFloat(null, n, v); 229 } 230 } 231 232 class FloatFromString { 233 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)234 float beforeParse(TypedXmlPullParser in, String n) throws Exception { 235 return Float.parseFloat(in.getAttributeValue(null, n)); 236 } 237 238 @BeforeTemplate beforeUtils(TypedXmlPullParser in, String n)239 float beforeUtils(TypedXmlPullParser in, String n) throws Exception { 240 return XmlUtils.readFloatAttribute(in, n); 241 } 242 243 @AfterTemplate after(TypedXmlPullParser in, String n)244 float after(TypedXmlPullParser in, String n) throws Exception { 245 return in.getAttributeFloat(null, n); 246 } 247 } 248 249 class DoubleToString { 250 @BeforeTemplate beforeToString(TypedXmlSerializer out, String n, double v)251 void beforeToString(TypedXmlSerializer out, String n, double v) throws Exception { 252 out.attribute(null, n, Double.toString(v)); 253 } 254 255 @BeforeTemplate beforeValueOf(TypedXmlSerializer out, String n, double v)256 void beforeValueOf(TypedXmlSerializer out, String n, double v) throws Exception { 257 out.attribute(null, n, String.valueOf(v)); 258 } 259 260 @AfterTemplate after(TypedXmlSerializer out, String n, double v)261 void after(TypedXmlSerializer out, String n, double v) throws Exception { 262 out.attributeDouble(null, n, v); 263 } 264 } 265 266 class DoubleFromString { 267 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)268 double beforeParse(TypedXmlPullParser in, String n) throws Exception { 269 return Double.parseDouble(in.getAttributeValue(null, n)); 270 } 271 272 @AfterTemplate after(TypedXmlPullParser in, String n)273 double after(TypedXmlPullParser in, String n) throws Exception { 274 return in.getAttributeDouble(null, n); 275 } 276 } 277 278 class BooleanToString { 279 @BeforeTemplate beforeToString(TypedXmlSerializer out, String n, boolean v)280 void beforeToString(TypedXmlSerializer out, String n, boolean v) throws Exception { 281 out.attribute(null, n, Boolean.toString(v)); 282 } 283 284 @BeforeTemplate beforeValueOf(TypedXmlSerializer out, String n, boolean v)285 void beforeValueOf(TypedXmlSerializer out, String n, boolean v) throws Exception { 286 out.attribute(null, n, String.valueOf(v)); 287 } 288 289 @AfterTemplate after(TypedXmlSerializer out, String n, boolean v)290 void after(TypedXmlSerializer out, String n, boolean v) throws Exception { 291 out.attributeBoolean(null, n, v); 292 } 293 } 294 295 class BooleanToStringTrue { 296 @BeforeTemplate before(TypedXmlSerializer out, String n)297 void before(TypedXmlSerializer out, String n) throws Exception { 298 out.attribute(null, n, "true"); 299 } 300 301 @AfterTemplate after(TypedXmlSerializer out, String n)302 void after(TypedXmlSerializer out, String n) throws Exception { 303 out.attributeBoolean(null, n, true); 304 } 305 } 306 307 class BooleanToStringFalse { 308 @BeforeTemplate before(TypedXmlSerializer out, String n)309 void before(TypedXmlSerializer out, String n) throws Exception { 310 out.attribute(null, n, "false"); 311 } 312 313 @AfterTemplate after(TypedXmlSerializer out, String n)314 void after(TypedXmlSerializer out, String n) throws Exception { 315 out.attributeBoolean(null, n, false); 316 } 317 } 318 319 class BooleanFromString { 320 @BeforeTemplate beforeParse(TypedXmlPullParser in, String n)321 boolean beforeParse(TypedXmlPullParser in, String n) throws Exception { 322 return Boolean.parseBoolean(in.getAttributeValue(null, n)); 323 } 324 325 @BeforeTemplate beforeUtils(TypedXmlPullParser in, String n)326 boolean beforeUtils(TypedXmlPullParser in, String n) throws Exception { 327 return XmlUtils.readBooleanAttribute(in, n); 328 } 329 330 @AfterTemplate after(TypedXmlPullParser in, String n)331 boolean after(TypedXmlPullParser in, String n) throws Exception { 332 return in.getAttributeBoolean(null, n, false); 333 } 334 } 335 336 class BooleanFromStringDefault { 337 @BeforeTemplate before(TypedXmlPullParser in, String n, boolean d)338 boolean before(TypedXmlPullParser in, String n, boolean d) throws Exception { 339 return XmlUtils.readBooleanAttribute(in, n, d); 340 } 341 342 @AfterTemplate after(TypedXmlPullParser in, String n, boolean d)343 boolean after(TypedXmlPullParser in, String n, boolean d) throws Exception { 344 return in.getAttributeBoolean(null, n, d); 345 } 346 } 347 } 348