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 "service-id" element of the pidf.
31  */
32 public class ServiceId extends ElementBase {
33     /** The name of this element */
34     public static final String ELEMENT_NAME = "service-id";
35 
36     private String mServiceId;
37 
ServiceId()38     public ServiceId() {
39     }
40 
ServiceId(String serviceId)41     public ServiceId(String serviceId) {
42         mServiceId = serviceId;
43     }
44 
45     @Override
initNamespace()46     protected String initNamespace() {
47         return OmaPresConstant.NAMESPACE;
48     }
49 
50     @Override
initElementName()51     protected String initElementName() {
52         return ELEMENT_NAME;
53     }
54 
getValue()55     public String getValue() {
56         return mServiceId;
57     }
58 
59     @Override
serialize(XmlSerializer serializer)60     public void serialize(XmlSerializer serializer) throws IOException {
61         if (mServiceId == null) {
62             return;
63         }
64         String namespace = getNamespace();
65         String elementName = getElementName();
66         serializer.startTag(namespace, elementName);
67         serializer.text(mServiceId);
68         serializer.endTag(namespace, elementName);
69     }
70 
71     @Override
parse(XmlPullParser parser)72     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
73         String namespace = parser.getNamespace();
74         String name = parser.getName();
75 
76         if (!verifyParsingElement(namespace, name)) {
77             throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name);
78         }
79 
80         // Move to the next event to get the value.
81         int eventType = parser.next();
82 
83         // Get the value if the event type is text.
84         if (eventType == XmlPullParser.TEXT) {
85             String serviceId = parser.getText();
86             if (!TextUtils.isEmpty(serviceId)) {
87                 mServiceId = serviceId;
88             }
89         }
90 
91         // Move to the end tag.
92         moveToElementEndTag(parser, eventType);
93     }
94 }
95