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.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 "note" element of the pidf. This element is usually used for a human readable comment.
31  * It may appear as a child element of "presence" or as a child element of the "tuple" element.
32  */
33 public class Note extends ElementBase {
34     /** The name of this element */
35     public static final String ELEMENT_NAME = "note";
36 
37     private String mNote;
38 
Note()39     public Note() {
40     }
41 
Note(String note)42     public Note(String note) {
43         mNote = note;
44     }
45 
46     @Override
initNamespace()47     protected String initNamespace() {
48         return PidfConstant.NAMESPACE;
49     }
50 
51     @Override
initElementName()52     protected String initElementName() {
53         return ELEMENT_NAME;
54     }
55 
getNote()56     public String getNote() {
57         return mNote;
58     }
59 
60     @Override
serialize(XmlSerializer serializer)61     public void serialize(XmlSerializer serializer) throws IOException {
62         if (mNote == null) {
63             return;
64         }
65         final String namespace = getNamespace();
66         final String element = getElementName();
67         serializer.startTag(namespace, element);
68         serializer.text(mNote);
69         serializer.endTag(namespace, element);
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 note = parser.getText();
87             if (!TextUtils.isEmpty(note)) {
88                 mNote = note;
89             }
90         }
91 
92         // Move to the end tag.
93         moveToElementEndTag(parser, eventType);
94     }
95 }
96