/LocServerDBConnectorLib/src/com/localization/util/ServerConfiguration.java

https://github.com/leobin/Localization · Java · 148 lines · 94 code · 25 blank · 29 comment · 2 complexity · 4564409e370f5e269d0d0849d3ff26c7 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.localization.util;
  6. import com.sun.org.apache.xml.internal.serialize.OutputFormat;
  7. import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import javax.xml.parsers.DocumentBuilder;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import org.w3c.dom.Document;
  17. import org.w3c.dom.Element;
  18. import org.w3c.dom.Node;
  19. /**
  20. *
  21. * @author leobin
  22. */
  23. public class ServerConfiguration {
  24. private static final String TAG_MYSQL_URL = "url";
  25. private static final String TAG_MYSQL_USERNAME = "username";
  26. private static final String TAG_MYSQL_PASSWORD = "password";
  27. private static final String TAG_MYSQL_DRIVER = "driver";
  28. private static final String TAG_PREFERENCES = "preferences";
  29. private static ServerConfiguration singleton;
  30. public String url;
  31. public String username;
  32. public String password;
  33. public String driver;
  34. private File configurationFile = new File("preferences.xml");
  35. private Document dom;
  36. private ServerConfiguration() {
  37. try {
  38. //ErrorLog.writeToLog(file.getAbsolutePath().toString());
  39. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  40. DocumentBuilder db = dbf.newDocumentBuilder();
  41. Document doc = (Document) db.parse(configurationFile);
  42. Node node = doc.getElementsByTagName(TAG_MYSQL_URL).item(0).getChildNodes().item(0);
  43. this.url = node.getNodeValue().trim();
  44. node = doc.getElementsByTagName(TAG_MYSQL_USERNAME).item(0).getChildNodes().item(0);
  45. this.username = node.getNodeValue().trim();
  46. node = doc.getElementsByTagName(TAG_MYSQL_PASSWORD).item(0).getChildNodes().item(0);
  47. this.password = node.getNodeValue().trim();
  48. node = doc.getElementsByTagName(TAG_MYSQL_DRIVER).item(0).getChildNodes().item(0);
  49. this.driver = node.getNodeValue().trim();
  50. } catch (Exception e) {
  51. this.url ="jdbc:mysql://localhost:3306/localization_database_v4";
  52. this.username = "root";
  53. this.password = "";
  54. this.driver = "com.mysql.jdbc.Driver";
  55. }
  56. }
  57. /**
  58. * singleton implementation for this class
  59. * @return
  60. */
  61. public static ServerConfiguration load() {
  62. if (singleton == null) {
  63. singleton = new ServerConfiguration();
  64. }
  65. return singleton;
  66. }
  67. /**
  68. * save new configuration to file
  69. */
  70. public void saveConfiguration() {
  71. createDocument();
  72. createDomTree();
  73. printToFile();
  74. }
  75. /**
  76. * create document for new configuration
  77. */
  78. private void createDocument() {
  79. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  80. try {
  81. DocumentBuilder db = dbf.newDocumentBuilder();
  82. dom = db.newDocument();
  83. } catch (ParserConfigurationException ex) {
  84. System.out.println("Error when create document builder");
  85. }
  86. }
  87. /**
  88. * create dom tree for configuration
  89. */
  90. private void createDomTree() {
  91. Element mapEle = dom.createElement(TAG_PREFERENCES);
  92. Element mysqlURLEle = dom.createElement(TAG_MYSQL_URL);
  93. mysqlURLEle.setTextContent(url);
  94. Element mysqlUsernameEle = dom.createElement(TAG_MYSQL_USERNAME);
  95. mysqlUsernameEle.setTextContent(url);
  96. Element mysqlPasswordEle = dom.createElement(TAG_MYSQL_PASSWORD);
  97. mysqlPasswordEle.setTextContent(url);
  98. Element mysqlDriverEle = dom.createElement(TAG_MYSQL_DRIVER);
  99. mysqlPasswordEle.setTextContent(url);
  100. mapEle.appendChild(mysqlURLEle);
  101. mapEle.appendChild(mysqlUsernameEle);
  102. mapEle.appendChild(mysqlPasswordEle);
  103. mapEle.appendChild(mysqlDriverEle);
  104. dom.appendChild(mapEle);
  105. }
  106. /**
  107. * write new configuration to file, finish saving
  108. */
  109. private void printToFile() {
  110. try {
  111. //print
  112. OutputFormat format = new OutputFormat(dom);
  113. format.setIndenting(true);
  114. //to generate output to console use this serializer
  115. //XMLSerializer serializer = new XMLSerializer(System.out, format);
  116. //to generate a file output use fileoutputstream instead of system.out
  117. XMLSerializer serializer = new XMLSerializer(
  118. new FileOutputStream(configurationFile), format);
  119. serializer.serialize(dom);
  120. } catch (IOException ie) {
  121. }
  122. }
  123. }