// Config.cs // // Copyright (C) 2008,2009 Bartek Jasicki // // This file is part of Grubng. // // Grubng is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // /**@file Config.cs * Provide functions for config files.*/ using System; using System.Xml; namespace grubupload { /**Provide functions for config files.*/ internal static class Config { /**Function create grub.xml file and write default configuration.*/ public static void CreateConfig() { XmlTextWriter xmlw = new XmlTextWriter("grub.xml", null); xmlw.Formatting = Formatting.Indented; xmlw.WriteStartDocument(false); xmlw.WriteStartElement("configuration"); xmlw.WriteElementString("workdir", "/home/thindil/tmp"); xmlw.WriteElementString("tempdir", "/home/thindil/tmp"); xmlw.WriteElementString("address", "*"); xmlw.WriteElementString("port", "8080"); xmlw.WriteElementString("hashpass", "HASHPASS"); xmlw.WriteElementString("enablelog", "Y"); xmlw.WriteElementString("logpath", "/home/thindil/tmp/grubupload.log"); xmlw.WriteElementString("enableapi", "Y"); xmlw.WriteElementString("enabledb", "Y"); xmlw.WriteElementString("dbpath", "/home/thindil/tmp/grupupload.db"); xmlw.WriteElementString("minfreespace", "1024"); xmlw.WriteElementString("checkevery", "30"); xmlw.WriteElementString("enableupload", "N"); xmlw.WriteElementString("hadooppath", "/path/to/hadoop"); xmlw.WriteElementString("uploadpath", "/path/to/upload"); xmlw.WriteElementString("hadoopfreespace", "10240"); xmlw.WriteEndElement(); xmlw.Flush(); xmlw.Close(); } /**Function read data from grub.xml file. * @param node XML node which been read*/ public static string ReadConfig(string node) { XmlDocument doc = new XmlDocument(); try { doc.Load("grub.xml"); } catch (XmlException) { return "Invalid XML"; } string answer = null; try { XmlNode noder = doc.SelectSingleNode(node); answer = noder.InnerText; doc = null; } catch (System.NullReferenceException) { answer = String.Empty; } return answer; } /**Function insert new data to grub.xml file. * @param data Value which been inserted to file * @param name Name of XML Element which been inserted to file*/ private static void InsertConfig(string data, string name) { XmlDocument doc = new XmlDocument(); doc.Load("grub.xml"); XmlNode nodew = doc.SelectSingleNode("/configuration"); XmlNode childNode = doc.CreateNode(XmlNodeType.Element, name, String.Empty); childNode.InnerText = data; childNode = nodew.AppendChild(childNode); doc.Save("grub.xml"); doc = null; } /**Function update config file.*/ public static void UpdateConfig() { XmlDocument doc = new XmlDocument(); doc.Load("grub.xml"); string[] nodes = new string[16] {"workdir", "tempdir", "address", "port", "hashpass", "enablelog", "logpath", "enableapi", "enabledb", "dbpath", "minfreespace", "checkevery", "enableupload", "hadooppath", "uploadpath", "hadoopfreespace"}; string[] values = new string[16] {"/home/thindil/tmp", "/home/thindil/tmp", "*", "8080", "HASHPASS", "Y", "/home/thindil/tmp/grubupload.log", "Y", "Y", "/home/thindil/tmp/grupupload.db", "1024", "30", "N", "/path/to/hadoop", "/path/to/upload", "10240"}; XmlNode noder; foreach (string node in nodes) { noder = doc.SelectSingleNode("/configuration/" + node); if (noder == null) { int index = Array.IndexOf(nodes, node); Config.InsertConfig(values[index], node); } } doc = null; } } }