XmlNode.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package eu.tankernn.gameEngine.loader.xmlLoader;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. /**
  7. * Represents a node in an XML file. This contains the name of the node, a map
  8. * of the attributes and their values, any text data between the start and end
  9. * tag, and a list of all its children nodes.
  10. *
  11. * @author Karl
  12. *
  13. */
  14. public class XmlNode {
  15. private String name;
  16. private Map<String, String> attributes;
  17. private String data;
  18. private Map<String, List<XmlNode>> childNodes;
  19. protected XmlNode(String name) {
  20. this.name = name;
  21. }
  22. /**
  23. * @return The name of the XML node.
  24. */
  25. public String getName() {
  26. return name;
  27. }
  28. /**
  29. * @return Any text data contained between the start and end tag of the
  30. * node.
  31. */
  32. public String getData() {
  33. return data;
  34. }
  35. /**
  36. * Gets the value of a certain attribute of the node. Returns {@code null}
  37. * if the attribute doesn't exist.
  38. *
  39. * @param attr
  40. * - the name of the attribute.
  41. * @return The value of the attribute.
  42. */
  43. public String getAttribute(String attr) {
  44. if (attributes != null) {
  45. return attributes.get(attr);
  46. } else {
  47. return null;
  48. }
  49. }
  50. /**
  51. * Gets a certain child node of this node.
  52. *
  53. * @param childName
  54. * - the name of the child node.
  55. * @return The child XML node with the given name.
  56. */
  57. public XmlNode getChild(String childName) {
  58. if (childNodes != null) {
  59. List<XmlNode> nodes = childNodes.get(childName);
  60. if (nodes != null && !nodes.isEmpty()) {
  61. return nodes.get(0);
  62. }
  63. }
  64. return null;
  65. }
  66. /**
  67. * Gets a child node with a certain name, and with a given value of a given
  68. * attribute. Used to get a specific child when there are multiple child
  69. * nodes with the same node name.
  70. *
  71. * @param childName
  72. * - the name of the child node.
  73. * @param attr
  74. * - the attribute whose value is to be checked.
  75. * @param value
  76. * - the value that the attribute must have.
  77. * @return The child node which has the correct name and the correct value
  78. * for the chosen attribute.
  79. */
  80. public XmlNode getChildWithAttribute(String childName, String attr, String value) {
  81. List<XmlNode> children = getChildren(childName);
  82. if (children == null || children.isEmpty()) {
  83. return null;
  84. }
  85. for (XmlNode child : children) {
  86. String val = child.getAttribute(attr);
  87. if (value.equals(val)) {
  88. return child;
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * Get the child nodes of this node that have a given name.
  95. *
  96. * @param name
  97. * - the name of the child nodes.
  98. * @return A list of the child nodes with the given name. If none exist then
  99. * an empty list is returned.
  100. */
  101. public List<XmlNode> getChildren(String name) {
  102. if (childNodes != null) {
  103. List<XmlNode> children = childNodes.get(name);
  104. if (children != null) {
  105. return children;
  106. }
  107. }
  108. return new ArrayList<XmlNode>();
  109. }
  110. /**
  111. * Adds a new attribute to this node. An attribute has a name and a value.
  112. * Attributes are stored in a HashMap which is initialized in here if it was
  113. * previously null.
  114. *
  115. * @param attr
  116. * - the name of the attribute.
  117. * @param value
  118. * - the value of the attribute.
  119. */
  120. protected void addAttribute(String attr, String value) {
  121. if (attributes == null) {
  122. attributes = new HashMap<String, String>();
  123. }
  124. attributes.put(attr, value);
  125. }
  126. /**
  127. * Adds a child node to this node.
  128. *
  129. * @param child
  130. * - the child node to add.
  131. */
  132. protected void addChild(XmlNode child) {
  133. if (childNodes == null) {
  134. childNodes = new HashMap<String, List<XmlNode>>();
  135. }
  136. List<XmlNode> list = childNodes.get(child.name);
  137. if (list == null) {
  138. list = new ArrayList<XmlNode>();
  139. childNodes.put(child.name, list);
  140. }
  141. list.add(child);
  142. }
  143. /**
  144. * Sets some data for this node.
  145. *
  146. * @param data
  147. * - the data for this node (text that is found between the start
  148. * and end tags of this node).
  149. */
  150. protected void setData(String data) {
  151. this.data = data;
  152. }
  153. }