aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/config/ConfigReader.java
diff options
context:
space:
mode:
authorMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-20 23:49:42 +0000
committerMatt Windsor <mbw500@student.cs.york.ac.uk>2011-02-20 23:49:42 +0000
commitcd972012ab8c40b66bd65f78bbf9e3422413b1aa (patch)
tree726aa9cefa80fc4a2f3922ed6e05cf0074421a6d /src/uk/org/ury/config/ConfigReader.java
parente2fc92f5c42dde942e8b71d38b9745b6f3c98053 (diff)
parentf89ef9ed36e8185f53c7d5f22f91935e2c4ccaa0 (diff)
Merge. DatabaseLogin over. Auth = Very Yes.
Diffstat (limited to 'src/uk/org/ury/config/ConfigReader.java')
-rw-r--r--src/uk/org/ury/config/ConfigReader.java106
1 files changed, 81 insertions, 25 deletions
diff --git a/src/uk/org/ury/config/ConfigReader.java b/src/uk/org/ury/config/ConfigReader.java
index 6b66e24..04deb49 100644
--- a/src/uk/org/ury/config/ConfigReader.java
+++ b/src/uk/org/ury/config/ConfigReader.java
@@ -1,49 +1,98 @@
package uk.org.ury.config;
import java.io.File;
-import org.w3c.dom.*;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
-import uk.org.ury.database.UserClass;
+import uk.org.ury.database.exceptions.MissingCredentialsException;
+/**
+ * Reads in an XML config file and creates config objects
+ *
+ * @author Nathan Lasseter
+ */
public class ConfigReader {
- private Database database;
- private Auth auth;
+ private Database database = null;
+ private Auth roAuth = null;
+ private Auth rwAuth = null;
+ /**
+ * Get the database configuration
+ *
+ * @return Database database
+ */
public Database getDatabase() { return database; }
- public Auth getAuth() { return auth; }
+ /**
+ * Get the read only login auth configuration
+ *
+ * @return Auth roAauth
+ */
+ public Auth getRoAuth() { return roAuth; }
+ /**
+ * Get the read write login auth configuration
+ *
+ * @return Auth rwAauth
+ */
+ public Auth getRwAuth() { return rwAuth; }
- public ConfigReader() {
+ /**
+ * Read in the config file and create the Database and Auth configuration objects.
+ * Specify a config file.
+ * @throws MissingCredentialsException
+ */
+ public ConfigReader(String configFile) throws MissingCredentialsException {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- Document doc = docBuilder.parse (new File("res/conf.xml"));
+ Document doc = docBuilder.parse (new File(configFile));
doc.getDocumentElement().normalize();
-
- System.out.println(doc.getDocumentElement().getNodeName());
-
- String user = doc.getElementsByTagName("user").item(0).getTextContent();
- String pass = doc.getElementsByTagName("pass").item(0).getTextContent();
- UserClass type;
- if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_only")) {
- type = UserClass.READ_ONLY;
- } else if(doc.getElementsByTagName("type").item(0).getTextContent().toLowerCase().equals("read_write")) {
- type = UserClass.READ_WRITE;
- } else {
- throw new IllegalArgumentException("Unused user class.");
+
+ NodeList nList = doc.getElementsByTagName("auth");
+ for(int i = 0; i < nList.getLength(); i++) {
+ Node nNode = nList.item(i);
+ if(nNode.getNodeType() == Node.ELEMENT_NODE) {
+ Element eElement = (Element) nNode;
+ String user = getTagValue("user", eElement);
+ String pass = getTagValue("pass", eElement);
+ String sType = getTagValue("type", eElement);
+ if(sType.equalsIgnoreCase("read_only")) {
+ if(roAuth != null) continue;
+ roAuth = new Auth(user, pass);
+ } else if(sType.equalsIgnoreCase("read_write")) {
+ if(rwAuth != null) continue;
+ rwAuth = new Auth(user, pass);
+ } else {
+ throw new IllegalArgumentException("Unused user class.");
+ }
+ }
}
- auth = new Auth(user, pass, type);
- String host = doc.getElementsByTagName("host").item(0).getTextContent();
- int port = Integer.parseInt(doc.getElementsByTagName("port").item(0).getTextContent().trim());
- String db = doc.getElementsByTagName("db").item(0).getTextContent();
- database = new Database(host, port, db);
+ nList = doc.getElementsByTagName("database");
+ for(int i = 0; i < nList.getLength(); i++) {
+ if(database != null) break;
+ Node nNode = nList.item(i);
+ if(nNode.getNodeType() == Node.ELEMENT_NODE) {
+ Element eElement = (Element) nNode;
+ String host = getTagValue("host", eElement);
+ String port = getTagValue("port", eElement);
+ String db = getTagValue("db", eElement);
+ database = new Database(host, Integer.parseInt(port.trim()), db);
+ }
+ }
+ }
+
+ catch(NullPointerException n) {
+ throw new MissingCredentialsException("An element node is empty.");
}
catch (SAXParseException err) {
@@ -58,4 +107,11 @@ public class ConfigReader {
t.printStackTrace ();
}
}
+
+ private static String getTagValue(String sTag, Element eElement){
+ NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
+ Node nValue = (Node) nlList.item(0);
+
+ return nValue.getNodeValue();
+ }
}