Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. package model;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.net.URISyntaxException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9.  
  10. import javax.xml.parsers.DocumentBuilder;
  11. import javax.xml.parsers.DocumentBuilderFactory;
  12. import javax.xml.parsers.ParserConfigurationException;
  13. import javax.xml.transform.Transformer;
  14. import javax.xml.transform.TransformerConfigurationException;
  15. import javax.xml.transform.TransformerException;
  16. import javax.xml.transform.TransformerFactory;
  17. import javax.xml.transform.dom.DOMSource;
  18. import javax.xml.transform.stream.StreamResult;
  19.  
  20. import com.sun.org.apache.bcel.internal.util.ClassPath;
  21. import javafx.scene.control.Alert;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.Element;
  24. import org.w3c.dom.Node;
  25. import org.w3c.dom.NodeList;
  26. import org.xml.sax.SAXException;
  27.  
  28. /**
  29. * The XMLManager class is used to do operations on the XML file
  30. *
  31. * @author 2, 3
  32. */
  33. public class XMLManager
  34. {
  35. /**
  36. * the location of the XML file
  37. */
  38. private static File userfile = new File("Server/Resources/userlist.xml");
  39.  
  40.  
  41.  
  42. //gets all the user nodes
  43.  
  44. /**
  45. *
  46. * @return get a nodelist containing all the users in the XML file
  47. */
  48. public static NodeList getUserNodeList(){
  49. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  50. DocumentBuilder documentBuilder;
  51. Document document = null;
  52. try {
  53. documentBuilder = documentBuilderFactory.newDocumentBuilder();
  54. document = documentBuilder.parse(userfile);
  55. document.getDocumentElement().normalize();
  56. }
  57. catch (ParserConfigurationException e) {
  58. e.printStackTrace();
  59. }
  60. catch (IOException ioe) {
  61. ioe.printStackTrace();
  62. }
  63. catch (SAXException sae) {
  64. sae.printStackTrace();
  65. }
  66.  
  67. return document.getElementsByTagName("user");
  68. }
  69.  
  70. //Generates the userlist from the xml file
  71.  
  72. /**
  73. * This is used when the server starts. It reads the XML file and then returns an arraylist containing user objects.
  74. * Which is then used to set the observableUserList
  75. *
  76. * @return an arraylist containg the users from the XML file
  77. */
  78. public static ArrayList<User> xmlToList(){
  79. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  80. DocumentBuilder documentBuilder;
  81. Document document = null;
  82. NodeList nodeList;
  83. ArrayList<User> userlist = new ArrayList<User>();
  84.  
  85. try {
  86. documentBuilder = documentBuilderFactory.newDocumentBuilder();
  87. document = documentBuilder.parse(userfile);
  88. document.getDocumentElement().normalize();
  89.  
  90. } catch(NullPointerException ne){
  91. Alert alert = new Alert(Alert.AlertType.ERROR);
  92. alert.setTitle("Error");
  93. alert.setResizable(true);
  94. alert.getDialogPane().setPrefSize(320, 200);
  95. alert.setHeaderText("Can not find the file containing the userlist!");
  96. alert.setContentText("userlist.xml is supposed to be located in Server/Resources folder, with a root element <Users></Users>");
  97.  
  98. alert.showAndWait();
  99. System.exit(1);
  100. } catch (ParserConfigurationException e) {
  101. e.printStackTrace();
  102. } catch (IOException ioe) {
  103. ioe.printStackTrace();
  104. } catch (SAXException sae) {
  105. sae.printStackTrace();
  106. }
  107.  
  108. nodeList = document.getElementsByTagName("user");
  109.  
  110. for (int i = 0; i < nodeList.getLength(); i++) {
  111. Node userNode = nodeList.item(i);
  112. Element element = (Element) userNode;
  113. String username = element.getElementsByTagName("username").item(0).getTextContent();
  114. String password = element.getElementsByTagName("password").item(0).getTextContent();
  115. int userId = Integer.parseInt(element.getAttribute("id"));
  116. User user = new User(userId, username, password, UserStatus.OFFLINE);
  117. userlist.add(user);
  118. }
  119. return userlist;
  120.  
  121.  
  122.  
  123. }
  124. //writes a new user to the xml file
  125.  
  126. /**
  127. * This is used to write to the XML file when a new user is created
  128. * @param userId The userID that the user is to have
  129. * @param username The username that was chosen
  130. * @param password The password that was chosen
  131. */
  132. public static void writeToXml(int userId, String username, String password)
  133. {
  134.  
  135. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  136. DocumentBuilder documentBuilder;
  137. Document document = null;
  138. NodeList nodeList;
  139. Element rootElement = null;
  140.  
  141.  
  142. try {
  143. documentBuilder = documentBuilderFactory.newDocumentBuilder();
  144. document = documentBuilder.parse(userfile);
  145. document.getDocumentElement().normalize();
  146. rootElement = document.getDocumentElement();
  147.  
  148. }
  149. catch (ParserConfigurationException e) {
  150. e.printStackTrace();
  151. }
  152. catch (IOException ioe) {
  153. ioe.printStackTrace();
  154. }
  155. catch (SAXException sae) {
  156. sae.printStackTrace();
  157. }
  158.  
  159. Element newUser = document.createElement("user");
  160. Element xmlUsername = document.createElement("username");
  161. Element xmlPassword = document.createElement("password");
  162.  
  163.  
  164. newUser.setAttribute("id",Integer.toString(userId));
  165. xmlUsername.appendChild(document.createTextNode(username));
  166. xmlPassword.appendChild(document.createTextNode(password));
  167.  
  168.  
  169. newUser.appendChild(xmlUsername);
  170. newUser.appendChild(xmlPassword);
  171. rootElement.appendChild(newUser);
  172.  
  173.  
  174.  
  175.  
  176. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  177. try {
  178. Transformer transformer = transformerFactory.newTransformer();
  179. DOMSource domSource = new DOMSource(document);
  180. StreamResult streamResult = new StreamResult(userfile);
  181. transformer.transform(domSource, streamResult);
  182.  
  183. DOMSource source = new DOMSource(document);
  184. } catch (TransformerConfigurationException e) {
  185. e.printStackTrace();
  186. } catch (TransformerException e) {
  187. e.printStackTrace();
  188. }
  189.  
  190. }
  191. //Checks if username already exists in xml file
  192.  
  193. /**
  194. * Checks if the username is already taken in the XML file
  195. * @param username the username to check
  196. * @return true or false depending on whether the username is taken or not
  197. */
  198. public static boolean checkUsername(String username) {
  199.  
  200. NodeList nodeList = getUserNodeList();
  201. for (int i = 0; i < nodeList.getLength(); i++) {
  202. Node userNode = nodeList.item(i);
  203. Element element = (Element) userNode;
  204.  
  205. if (username.equals(element.getElementsByTagName("username").item(0).getTextContent())) {
  206. return false;
  207. }
  208. }
  209.  
  210. return true;
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement