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 package com.android.ims.rcs.uce.presence.pidfparser.omapres; 18 19 import android.text.TextUtils; 20 21 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase; 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 import org.xmlpull.v1.XmlSerializer; 26 27 import java.io.IOException; 28 29 /** 30 * The "version" element of the pidf. 31 */ 32 public class Version extends ElementBase { 33 /** The name of this element */ 34 public static final String ELEMENT_NAME = "version"; 35 36 private int mMajorVersion; 37 private int mMinorVersion; 38 Version()39 public Version() { 40 } 41 Version(int majorVersion, int minorVersion)42 public Version(int majorVersion, int minorVersion) { 43 mMajorVersion = majorVersion; 44 mMinorVersion = minorVersion; 45 } 46 47 @Override initNamespace()48 protected String initNamespace() { 49 return OmaPresConstant.NAMESPACE; 50 } 51 52 @Override initElementName()53 protected String initElementName() { 54 return ELEMENT_NAME; 55 } 56 getValue()57 public String getValue() { 58 StringBuilder builder = new StringBuilder(); 59 builder.append(mMajorVersion).append(".").append(mMinorVersion); 60 return builder.toString(); 61 } 62 63 @Override serialize(XmlSerializer serializer)64 public void serialize(XmlSerializer serializer) throws IOException { 65 String namespace = getNamespace(); 66 String elementName = getElementName(); 67 serializer.startTag(namespace, elementName); 68 serializer.text(getValue()); 69 serializer.endTag(namespace, elementName); 70 } 71 72 @Override parse(XmlPullParser parser)73 public void parse(XmlPullParser parser) throws IOException, XmlPullParserException { 74 String namespace = parser.getNamespace(); 75 String name = parser.getName(); 76 77 if (!verifyParsingElement(namespace, name)) { 78 throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name); 79 } 80 81 // Move to the next event to get the value. 82 int eventType = parser.next(); 83 84 // Get the value if the event type is text. 85 if (eventType == XmlPullParser.TEXT) { 86 String version = parser.getText(); 87 handleParsedVersion(version); 88 } 89 90 // Move to the end tag. 91 moveToElementEndTag(parser, eventType); 92 } 93 handleParsedVersion(String version)94 private void handleParsedVersion(String version) { 95 if (TextUtils.isEmpty(version)) { 96 return; 97 } 98 99 String[] versionAry = version.split("\\."); 100 if (versionAry != null && versionAry.length == 2) { 101 mMajorVersion = Integer.parseInt(versionAry[0]); 102 mMinorVersion = Integer.parseInt(versionAry[1]); 103 } 104 } 105 } 106