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