1 /** 2 * Copyright (c) 2016, 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 android.net.wifi.hotspot2.omadm; 18 19 import android.text.TextUtils; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 import java.util.Objects; 24 25 /** 26 * A class represent a node in an XML tree. Each node is an XML element. 27 * Used by {@link XMLParser} for parsing/converting each XML element to XMLNode. 28 * 29 * @hide 30 */ 31 public class XMLNode { 32 private final String mTag; 33 private final List<XMLNode> mChildren; 34 private final XMLNode mParent; 35 private StringBuilder mTextBuilder; 36 private String mText; 37 XMLNode(XMLNode parent, String tag)38 public XMLNode(XMLNode parent, String tag) { 39 mTag = tag; 40 mParent = parent; 41 mChildren = new ArrayList<>(); 42 mTextBuilder = new StringBuilder(); 43 mText = null; 44 } 45 46 /** 47 * Adding a text to this node. Invoked by {@link XMLParser#characters}. 48 * 49 * @param text String to be added 50 */ addText(String text)51 public void addText(String text) { 52 mTextBuilder.append(text); 53 } 54 55 /** 56 * Adding a child node to this node. Invoked by {@link XMLParser#startElement}. 57 * 58 * @param child XMLNode to be added 59 */ addChild(XMLNode child)60 public void addChild(XMLNode child) { 61 mChildren.add(child); 62 } 63 64 /** 65 * Invoked when the end of the XML element is detected. Used for further processing 66 * of the text enclosed within this XML element. Invoked by {@link XMLParser#endElement}. 67 */ close()68 public void close() { 69 // Remove the leading and the trailing whitespaces. 70 mText = mTextBuilder.toString().trim(); 71 mTextBuilder = null; 72 } 73 getTag()74 public String getTag() { 75 return mTag; 76 } 77 getParent()78 public XMLNode getParent() { 79 return mParent; 80 } 81 getText()82 public String getText() { 83 return mText; 84 } 85 getChildren()86 public List<XMLNode> getChildren() { 87 return mChildren; 88 } 89 90 @Override equals(Object thatObject)91 public boolean equals(Object thatObject) { 92 if (this == thatObject) { 93 return true; 94 } 95 if (!(thatObject instanceof XMLNode)) { 96 return false; 97 } 98 XMLNode that = (XMLNode) thatObject; 99 100 return TextUtils.equals(mTag, that.mTag) && 101 TextUtils.equals(mText, that.mText) && 102 mChildren.equals(that.mChildren); 103 } 104 105 @Override hashCode()106 public int hashCode() { 107 return Objects.hash(mTag, mText, mChildren); 108 } 109 } 110