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;
18 
19 import android.util.Log;
20 import org.xmlpull.v1.XmlPullParser;
21 import org.xmlpull.v1.XmlPullParserException;
22 import org.xmlpull.v1.XmlSerializer;
23 
24 import java.io.IOException;
25 
26 /**
27  * The base class of the pidf element.
28  */
29 public abstract class ElementBase {
30     private String mNamespace;
31     private String mElementName;
32 
ElementBase()33     public ElementBase() {
34         mNamespace = initNamespace();
35         mElementName = initElementName();
36     }
37 
initNamespace()38     protected abstract String initNamespace();
initElementName()39     protected abstract String initElementName();
40 
41     /**
42      * @return The namespace of this element
43      */
getNamespace()44     public String getNamespace() {
45         return mNamespace;
46     }
47 
48     /**
49      * @return The name of this element.
50      */
getElementName()51     public String getElementName() {
52         return mElementName;
53     }
54 
serialize(XmlSerializer serializer)55     public abstract void serialize(XmlSerializer serializer) throws IOException;
56 
parse(XmlPullParser parser)57     public abstract void parse(XmlPullParser parser) throws IOException, XmlPullParserException;
58 
verifyParsingElement(String namespace, String elementName)59     protected boolean verifyParsingElement(String namespace, String elementName) {
60         if (!getNamespace().equals(namespace) || !getElementName().equals(elementName)) {
61             return false;
62         } else {
63             return true;
64         }
65     }
66 
67     // Move to the end tag of this element
moveToElementEndTag(XmlPullParser parser, int type)68     protected void moveToElementEndTag(XmlPullParser parser, int type)
69             throws IOException, XmlPullParserException {
70         int eventType = type;
71 
72         // Move to the end tag of this element.
73         while(!(eventType == XmlPullParser.END_TAG
74                 && getNamespace().equals(parser.getNamespace())
75                 && getElementName().equals(parser.getName()))) {
76             eventType = parser.next();
77 
78             // Leave directly if the event type is the end of the document.
79             if (eventType == XmlPullParser.END_DOCUMENT) {
80                 return;
81             }
82         }
83     }
84 }
85