# HG changeset patch # User defr@localhost.localdomain # Date 1179085333 -7200 # Node ID 568c206089c164cb5a9323541b55a5a8eff7c35f # Parent a2285ee1c687bcd31afc5c238e4259fd2e10cd07 Refactor de la gestion des configurations diff -r a2285ee1c687 -r 568c206089c1 org/defr/bots/Configuration.java --- 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; + } }