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 com.android.ims.rcs.uce.presence.pidfparser.ElementBase;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 import org.xmlpull.v1.XmlSerializer;
24 
25 import java.io.IOException;
26 
27 /**
28  * The "service-description" element of the pidf.
29  */
30 public class ServiceDescription extends ElementBase {
31     /** The name of this element */
32     public static final String ELEMENT_NAME = "service-description";
33 
34     private ServiceId mServiceId;
35     private Version mVersion;
36     private Description mDescription;
37 
ServiceDescription()38     public ServiceDescription() {
39     }
40 
41     @Override
initNamespace()42     protected String initNamespace() {
43         return OmaPresConstant.NAMESPACE;
44     }
45 
46     @Override
initElementName()47     protected String initElementName() {
48         return ELEMENT_NAME;
49     }
50 
setServiceId(ServiceId serviceId)51     public void setServiceId(ServiceId serviceId) {
52         mServiceId = serviceId;
53     }
54 
getServiceId()55     public ServiceId getServiceId() {
56         return mServiceId;
57     }
58 
setVersion(Version version)59     public void setVersion(Version version) {
60         mVersion = version;
61     }
62 
getVersion()63     public Version getVersion() {
64         return mVersion;
65     }
66 
setDescription(Description description)67     public void setDescription(Description description) {
68         mDescription = description;
69     }
70 
getDescription()71     public Description getDescription() {
72         return mDescription;
73     }
74 
75     @Override
serialize(XmlSerializer serializer)76     public void serialize(XmlSerializer serializer) throws IOException {
77         if(mServiceId == null && mVersion == null && mDescription == null) {
78             return;
79         }
80         final String namespace = getNamespace();
81         final String element = getElementName();
82         serializer.startTag(namespace, element);
83         if (mServiceId != null) {
84             mServiceId.serialize(serializer);
85         }
86         if (mVersion != null) {
87             mVersion.serialize(serializer);
88         }
89         if (mDescription != null) {
90             mDescription.serialize(serializer);
91         }
92         serializer.endTag(namespace, element);
93     }
94 
95     @Override
parse(XmlPullParser parser)96     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
97         String namespace = parser.getNamespace();
98         String name = parser.getName();
99 
100         if (!verifyParsingElement(namespace, name)) {
101             throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name);
102         }
103 
104         // Move to the next event.
105         int eventType = parser.next();
106 
107         while(!(eventType == XmlPullParser.END_TAG
108                 && getNamespace().equals(parser.getNamespace())
109                 && getElementName().equals(parser.getName()))) {
110 
111             if (eventType == XmlPullParser.START_TAG) {
112                 String tagName = parser.getName();
113 
114                 if (ServiceId.ELEMENT_NAME.equals(tagName)) {
115                     ServiceId serviceId = new ServiceId();
116                     serviceId.parse(parser);
117                     mServiceId = serviceId;
118                 } else if (Version.ELEMENT_NAME.equals(tagName)) {
119                     Version version = new Version();
120                     version.parse(parser);
121                     mVersion = version;
122                 } else if (Description.ELEMENT_NAME.equals(tagName)) {
123                     Description description = new Description();
124                     description.parse(parser);
125                     mDescription = description;
126                 }
127             }
128 
129             eventType = parser.next();
130 
131             // Leave directly if the event type is the end of the document.
132             if (eventType == XmlPullParser.END_DOCUMENT) {
133                 return;
134             }
135         }
136     }
137 }
138