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.pidf;
18 
19 import android.annotation.StringDef;
20 import android.util.Log;
21 
22 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase;
23 import com.android.ims.rcs.uce.util.UceUtils;
24 
25 import org.xmlpull.v1.XmlPullParser;
26 import org.xmlpull.v1.XmlPullParserException;
27 import org.xmlpull.v1.XmlSerializer;
28 
29 import java.io.IOException;
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /**
34  * The "basic" element of the pidf.
35  */
36 public class Basic extends ElementBase {
37     private static final String LOG_TAG = UceUtils.getLogPrefix() + "Basic";
38 
39     /** The name of this element */
40     public static final String ELEMENT_NAME = "basic";
41 
42     /** The value "open" of the Basic element */
43     public static final String OPEN = "open";
44 
45     /** The value "closed" of the Basic element */
46     public static final String CLOSED = "closed";
47 
48     @StringDef(value = {
49             OPEN,
50             CLOSED})
51     @Retention(RetentionPolicy.SOURCE)
52     public @interface BasicValue {}
53 
54     private @BasicValue String mBasic;
55 
Basic()56     public Basic() {
57     }
58 
Basic(@asicValue String value)59     public Basic(@BasicValue String value) {
60         mBasic = value;
61     }
62 
63     @Override
initNamespace()64     protected String initNamespace() {
65         return PidfConstant.NAMESPACE;
66     }
67 
68     @Override
initElementName()69     protected String initElementName() {
70         return ELEMENT_NAME;
71     }
72 
getValue()73     public String getValue() {
74         return mBasic;
75     }
76 
77     @Override
serialize(XmlSerializer serializer)78     public void serialize(XmlSerializer serializer) throws IOException {
79         if (mBasic == null) {
80             return;
81         }
82         final String namespace = getNamespace();
83         final String element = getElementName();
84         serializer.startTag(namespace, element);
85         serializer.text(mBasic);
86         serializer.endTag(namespace, element);
87     }
88 
89     @Override
parse(XmlPullParser parser)90     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
91         String namespace = parser.getNamespace();
92         String name = parser.getName();
93 
94         if (!verifyParsingElement(namespace, name)) {
95             throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name);
96         }
97 
98         // Move to the next event to get the value.
99         int eventType = parser.next();
100 
101         // Get the value if the event type is text.
102         if (eventType == XmlPullParser.TEXT) {
103             String basicValue = parser.getText();
104             if (OPEN.equals(basicValue)) {
105                 mBasic = OPEN;
106             } else if (CLOSED.equals(basicValue)) {
107                 mBasic = CLOSED;
108             } else {
109                 mBasic = null;
110             }
111         } else {
112             Log.d(LOG_TAG, "The eventType is not TEXT=" + eventType);
113         }
114 
115         // Move to the end tag.
116         moveToElementEndTag(parser, eventType);
117     }
118 }
119