1 /* 2 * Copyright (C) 2023 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 com.android.internal.vibrator.persistence; 18 19 import static java.util.Objects.requireNonNull; 20 21 import android.annotation.NonNull; 22 import android.text.TextUtils; 23 24 import com.android.internal.util.ArrayUtils; 25 import com.android.modules.utils.TypedXmlPullParser; 26 27 import org.xmlpull.v1.XmlPullParser; 28 import org.xmlpull.v1.XmlPullParserException; 29 30 import java.util.Objects; 31 32 /** 33 * Helper methods for validating elements from a {@link XmlPullParser}. 34 * 35 * @hide 36 */ 37 public final class XmlValidator { 38 39 /** 40 * Check parser is currently at {@link XmlPullParser#START_TAG} and that it has the expected 41 * name. 42 */ checkStartTag(TypedXmlPullParser parser, String expectedTag)43 public static void checkStartTag(TypedXmlPullParser parser, String expectedTag) 44 throws XmlParserException { 45 checkStartTag(parser); 46 checkParserCondition( 47 expectedTag.equals(parser.getName()), 48 "Unexpected start tag found %s, expected %s", parser.getName(), expectedTag); 49 } 50 51 /** Check parser is currently at {@link XmlPullParser#START_TAG}. */ checkStartTag(TypedXmlPullParser parser)52 public static void checkStartTag(TypedXmlPullParser parser) throws XmlParserException { 53 try { 54 checkParserCondition( 55 parser.getEventType() == parser.START_TAG, 56 "Expected start tag, got " + parser.getEventType()); 57 } catch (XmlPullParserException e) { 58 throw XmlParserException.createFromPullParserException(parser.getName(), e); 59 } 60 } 61 62 /** Check current tag only has attributes from the expected list */ checkTagHasNoUnexpectedAttributes( TypedXmlPullParser parser, String... expectedAttributes)63 public static void checkTagHasNoUnexpectedAttributes( 64 TypedXmlPullParser parser, String... expectedAttributes) throws XmlParserException { 65 if (expectedAttributes == null || expectedAttributes.length == 0) { 66 checkParserCondition(parser.getAttributeCount() == 0, 67 "Unexpected attributes in tag %s, expected no attributes", parser.getName()); 68 return; 69 } 70 71 String tagName = parser.getName(); 72 int attributeCount = parser.getAttributeCount(); 73 74 for (int i = 0; i < attributeCount; i++) { 75 String attributeName = parser.getAttributeName(i); 76 checkParserCondition(ArrayUtils.contains(expectedAttributes, attributeName), 77 "Unexpected attribute %s found in tag %s", attributeName, tagName); 78 } 79 } 80 81 /** 82 * Check given {@link XmlSerializedVibration} represents the expected {@code vibration} object 83 * when it's deserialized. 84 */ 85 @NonNull checkSerializedVibration( XmlSerializedVibration<T> serializedVibration, T expectedVibration)86 public static <T> void checkSerializedVibration( 87 XmlSerializedVibration<T> serializedVibration, T expectedVibration) 88 throws XmlSerializerException { 89 T deserializedVibration = requireNonNull(serializedVibration.deserialize()); 90 checkSerializerCondition(Objects.equals(expectedVibration, deserializedVibration), 91 "Unexpected serialized vibration %s: found deserialization %s, expected %s", 92 serializedVibration, deserializedVibration, expectedVibration); 93 } 94 95 /** 96 * Check generic serializer condition 97 * 98 * @throws XmlSerializerException if {@code expression} is false 99 */ checkSerializerCondition(boolean expression, String messageTemplate, Object... messageArgs)100 public static void checkSerializerCondition(boolean expression, 101 String messageTemplate, Object... messageArgs) throws XmlSerializerException { 102 if (!expression) { 103 throw new XmlSerializerException(TextUtils.formatSimple(messageTemplate, messageArgs)); 104 } 105 } 106 107 /** 108 * Check generic parser condition 109 * 110 * @throws XmlParserException if {@code expression} is false 111 */ checkParserCondition(boolean expression, String messageTemplate, Object... messageArgs)112 public static void checkParserCondition(boolean expression, 113 String messageTemplate, Object... messageArgs) throws XmlParserException { 114 if (!expression) { 115 throw new XmlParserException(TextUtils.formatSimple(messageTemplate, messageArgs)); 116 } 117 } 118 } 119