IteraciĆ³n 3(VersiĆ³n sin idiomas)
[ISBets21MAUBRY] / eclipse-workspace / ISBets21MAUBRY / src / main / java / configuration / ConfigXML.java
1 package configuration;
2
3 import java.io.File;
4
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12
13 /**
14  * It provides the configuration data from the "resources/config.xml" XML file
15  */
16 public class ConfigXML {
17
18         private String configFile = "src/main/resources/config.xml";
19
20         private String businessLogicNode;
21
22         private String businessLogicPort;
23
24         private String businessLogicName;
25
26         private static String dbFilename;
27
28         // Two possible values: "open" or "initialize"
29         private String dataBaseOpenMode;
30
31         // Two possible values: true (no instance of RemoteServer needs to be launched)
32         // or false (RemoteServer needs to be run first)
33         private boolean businessLogicLocal;
34
35         // Two possible values: true (if the database is in same node as business logic
36         // ) or false (in other case)
37         private boolean databaseLocal;
38
39         private String databaseNode;
40
41         private int databasePort;
42
43         private String user;
44
45         private String password;
46
47         private String locale;
48
49         public String getLocale() {
50                 return locale;
51         }
52
53         public int getDatabasePort() {
54                 return databasePort;
55         }
56
57         public String getUser() {
58                 return user;
59         }
60
61         public String getPassword() {
62                 return password;
63         }
64
65         public boolean isDatabaseLocal() {
66                 return databaseLocal;
67         }
68
69         public boolean isBusinessLogicLocal() {
70                 return businessLogicLocal;
71         }
72
73         private static ConfigXML theInstance = new ConfigXML();
74
75         private ConfigXML() {
76
77                 try {
78                         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
79                         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
80                         Document doc = dBuilder.parse(new File(configFile));
81                         doc.getDocumentElement().normalize();
82
83                         NodeList list = doc.getElementsByTagName("config");
84                         Element config = (Element) list.item(0); // list.item(0) is a Node that is an Element
85
86                         // Two possible values: true (no instance of RemoteServer needs to be launched)
87                         // or false (RemoteServer needs to be run first)
88                         String value = ((Element) config.getElementsByTagName("businessLogic").item(0)).getAttribute("local");
89                         businessLogicLocal = value.equals("true");
90
91                         businessLogicNode = getTagValue("businessLogicNode", config);
92
93                         businessLogicPort = getTagValue("businessLogicPort", config);
94
95                         businessLogicName = getTagValue("businessLogicName", config);
96
97                         locale = getTagValue("locale", config);
98
99                         dbFilename = getTagValue("dbFilename", config);
100
101                         // Two possible values: true (no instance of RemoteServer needs to be launched)
102                         // or false (RemoteServer needs to be run first)
103                         value = ((Element) config.getElementsByTagName("database").item(0)).getAttribute("local");
104                         databaseLocal = value.equals("true");
105
106                         // Two possible values: "open" or "initialize"
107                         // dataBaseOpenMode= getTagValue("dataBaseOpenMode", config);
108                         dataBaseOpenMode = "open";
109
110                         databaseNode = getTagValue("databaseNode", config);
111
112                         databasePort = Integer.parseInt(getTagValue("databasePort", config));
113
114                         user = getTagValue("user", config);
115
116                         password = getTagValue("password", config);
117
118                         System.out.print("Read from config.xml: ");
119                         System.out.print("\t businessLogicLocal=" + businessLogicLocal);
120                         System.out.print("\t databaseLocal=" + databaseLocal);
121                         System.out.println("\t dataBaseOpenMode=" + dataBaseOpenMode);
122
123                 } catch (Exception e) {
124                         System.out.println("Error in ConfigXML.java: problems with " + configFile);
125                         e.printStackTrace();
126                 }
127
128         }
129
130         private static String getTagValue(String sTag, Element eElement) {
131                 NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
132                 Node nValue = nlList.item(0);
133
134                 return nValue.getNodeValue();
135
136         }
137
138         public static ConfigXML getInstance() {
139                 return theInstance;
140         }
141
142         public String getBusinessLogicNode() {
143                 return businessLogicNode;
144         }
145
146         public String getBusinessLogicPort() {
147                 return businessLogicPort;
148         }
149
150         public String getBusinessLogicName() {
151                 return businessLogicName;
152         }
153
154         public String getDbFilename() {
155                 return dbFilename;
156         }
157
158         public String getDataBaseOpenMode() {
159                 return dataBaseOpenMode;
160         }
161
162         public String getDatabaseNode() {
163                 return databaseNode;
164         }
165
166 }