Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. import net.swordie.ms.ServerConstants;
  2. import org.w3c.dom.Document;
  3. import org.w3c.dom.NamedNodeMap;
  4. import org.w3c.dom.Node;
  5. import org.xml.sax.SAXException;
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16.  
  17. public class XMLApi {
  18.  
  19. private Node root;
  20.  
  21. public XMLApi(Node root) {
  22. this.root = root;
  23. }
  24.  
  25. public Node getRoot() {
  26. return root;
  27. }
  28.  
  29. /**
  30. * Returns a list of Nodes containing all children of a given Node. Filters out all text elements.
  31. *
  32. * @param node The Node of which the children are requested.
  33. * @return The list of children nodes of <code>node</code>. An empty list if there are none.
  34. */
  35. public static List<Node> getAllChildren(Node node) {
  36. List<Node> result = new ArrayList<>();
  37.  
  38. Node childNode = node.getFirstChild();
  39. while (childNode != null) {
  40. if (!childNode.getNodeName().contains("#text")) {
  41. result.add(childNode);
  42. }
  43. childNode = childNode.getNextSibling();
  44. }
  45.  
  46. return result == null ? new ArrayList<>() : result;
  47. }
  48.  
  49. /**
  50. * Gets all attributes in a String, String map.
  51. *
  52. * @param node The Node the attributes are requested for.
  53. * @return The attributes corresponding to <code>node</code>.
  54. */
  55. public static Map<String, String> getAttributes(Node node) {
  56. Map<String, String> result = new HashMap<>();
  57. NamedNodeMap namedNodeMap = node.getAttributes();
  58. for (int i = 0; i < namedNodeMap.getLength(); i++) {
  59. Node n = namedNodeMap.item(i);
  60. result.put(n.getNodeName(), n.getNodeValue());
  61. }
  62. return result;
  63. }
  64.  
  65. /**
  66. * Generates a parsed Document, given an XML file.
  67. *
  68. * @param file The file to parse.
  69. * @return The parsed Document.
  70. */
  71. public static Document getRoot(File file) {
  72.  
  73. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  74. DocumentBuilder dBuilder = null;
  75. try {
  76. dBuilder = dbFactory.newDocumentBuilder();
  77. } catch (ParserConfigurationException e) {
  78. e.printStackTrace();
  79. }
  80. Document doc = null;
  81. try {
  82. doc = dBuilder.parse(file);
  83. } catch (SAXException | IOException e) {
  84. e.printStackTrace();
  85. }
  86. return doc;
  87. }
  88.  
  89. /**
  90. * Breadth first search for a child node of a given Node.
  91. *
  92. * @param node The node to search in.
  93. * @param name The name of the child.
  94. * @return The first occurrence of the name in the children of node, or null if there is none.
  95. */
  96. public static Node getFirstChildByNameBF(Node node, String name) {
  97. List<Node> nodes = getAllChildren(node);
  98. for (Node n : nodes) {
  99. Map<String, String> attrs = getAttributes(n);
  100. String nodeName = attrs.get("name");
  101. if (name.equals(nodeName)) {
  102. return n;
  103. }
  104. }
  105. for (Node n : nodes) {
  106. Node child = getFirstChildByNameBF(n, name);
  107. if (child != null) {
  108. return child;
  109. }
  110. }
  111. return null;
  112. }
  113.  
  114. /**
  115. * Depth first search for a child node of a given Node.
  116. *
  117. * @param node The node to search in.
  118. * @param name The name of the child.
  119. * @return The first occurrence of the name in the children of node, or null if there is none.
  120. */
  121. public static Node getFirstChildByNameDF(Node node, String name) {
  122. List<Node> nodes = getAllChildren(node);
  123. for (Node n : nodes) {
  124. Map<String, String> attrs = getAttributes(n);
  125. String nodeName = attrs.get("name");
  126. if (name.equals(nodeName)) {
  127. return n;
  128. }
  129. for (Node n2 : getAllChildren(n)) {
  130. Node child = getFirstChildByNameDF(n2, name);
  131. if (child != null) {
  132. return child;
  133. }
  134. }
  135. }
  136. return null;
  137. }
  138.  
  139. /**
  140. * Grabs the Node from a specified path.
  141. *
  142. * @param xmlPath The path to the xml file.
  143. * @param nodeName The name of the node to be returned.
  144. * @return The first occurrence of the given nodeName in the given xmlPath, or null if there is none.
  145. */
  146. public static Node getNodeByPath(String xmlPath, String nodeName) {
  147. File file = new File(ServerConstants.WZ_DIR + "/" + xmlPath + ".xml");
  148. Document doc = getRoot(file);
  149. Node node = getAllChildren(doc).get(0);
  150.  
  151. return getFirstChildByNameBF(node, nodeName);
  152. }
  153.  
  154. public static String getNamedAttribute(Node node, String name) {
  155. return getAttributes(node).get(name);
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement