changeset 3:568c206089c1

Refactor de la gestion des configurations
author defr@localhost.localdomain
date Sun, 13 May 2007 21:42:13 +0200
parents a2285ee1c687
children f9f6348eb8e9
files org/defr/bots/Configuration.java
diffstat 1 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/org/defr/bots/Configuration.java	Sat May 12 22:29:45 2007 +0200
+++ b/org/defr/bots/Configuration.java	Sun May 13 21:42:13 2007 +0200
@@ -8,22 +8,28 @@
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 public class Configuration {
+	
+	private Document document;
+	
 	protected String botName = "Foo";
 	protected String botPassword = null;
 	protected String serverName;
 	protected String services;
+	protected String encoding;
 	
 	public Configuration(File configFile) {
 		try {
 			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-			Document document = db.parse(configFile);
-			botName = document.getElementsByTagName("name").item(0).getTextContent();
-			botPassword = document.getElementsByTagName("password").item(0).getTextContent();
-			serverName = document.getElementsByTagName("server").item(0).getTextContent();
-			services = document.getElementsByTagName("services").item(0).getTextContent();
+			document = db.parse(configFile);
+			botName = document.getDocumentElement().getAttribute("name");
+			botPassword = getPref("password", new String());
+			serverName = getPref("server", "irc.freenode.net");
+			services = getPref("services", "services.freenode.net");
+			encoding = getPref("encoding", "iso-8859-1");
 		} catch (ParserConfigurationException e) {
 			System.err.println("Erreur de configuration du DocumentBuilderFactory");
 		} catch (SAXException e) {
@@ -41,15 +47,32 @@
 	public String getBotName() {
 		return botName;
 	}
+	
 	public String getBotPassword() {
 		return botPassword;
 	}
+	
 	public String getServerName() {
 		return serverName;
 	}
+	
 	public String getServices() {
 		return services;
 	}
 	
+	public String getEncoding() {
+		return encoding;
+	}
 	
+	private String getPref(String prefName, String prefDefault) {
+		String returnValue = null;
+		NodeList nl = document.getElementsByTagName(prefName);
+		if(nl.getLength() > 0) {
+			returnValue = nl.item(0).getTextContent();
+		}
+		else {
+			returnValue = prefDefault;
+		}
+		return returnValue;
+	}
 }