Exemplo n.º 1
0
        public void Reload()
        {
            string          filePath = "Reload.xml";
            XmlConfigSource source   = new XmlConfigSource();

            IConfig petConfig = source.AddConfig("Pets");

            petConfig.Set("cat", "Muffy");
            petConfig.Set("dog", "Rover");
            IConfig weatherConfig = source.AddConfig("Weather");

            weatherConfig.Set("skies", "cloudy");
            weatherConfig.Set("precipitation", "rain");
            source.Save(filePath);

            Assert.AreEqual(2, petConfig.GetKeys().Length);
            Assert.AreEqual("Muffy", petConfig.Get("cat"));
            Assert.AreEqual(2, source.Configs.Count);

            XmlConfigSource newSource = new XmlConfigSource(filePath);

            IConfig compareConfig = newSource.Configs["Pets"];

            Assert.AreEqual(2, compareConfig.GetKeys().Length);
            Assert.AreEqual("Muffy", compareConfig.Get("cat"));
            Assert.IsTrue(compareConfig == newSource.Configs["Pets"],
                          "References before are not equal");

            // Set the new values to source
            source.Configs["Pets"].Set("cat", "Misha");
            source.Configs["Pets"].Set("lizard", "Lizzy");
            source.Configs["Pets"].Set("hampster", "Surly");
            source.Configs["Pets"].Remove("dog");
            source.Configs.Remove(weatherConfig);
            source.Save();              // saves new value

            // Reload the new source and check for changes
            newSource.Reload();
            Assert.IsTrue(compareConfig == newSource.Configs["Pets"],
                          "References after are not equal");
            Assert.AreEqual(1, newSource.Configs.Count);
            Assert.AreEqual(3, newSource.Configs["Pets"].GetKeys().Length);
            Assert.AreEqual("Lizzy", newSource.Configs["Pets"].Get("lizard"));
            Assert.AreEqual("Misha", newSource.Configs["Pets"].Get("cat"));
            Assert.IsNull(newSource.Configs["Pets"].Get("dog"));

            File.Delete(filePath);
        }
Exemplo n.º 2
0
        public void SaveToStream()
        {
            string     filePath = "SaveToStream.ini";
            FileStream stream   = new FileStream(filePath, FileMode.Create);

            // Create a new document and save to stream
            XmlConfigSource source = new XmlConfigSource();
            IConfig         config = source.AddConfig("Pets");

            config.Set("dog", "rover");
            config.Set("cat", "muffy");
            source.Save(stream);
            stream.Close();

            XmlConfigSource newSource = new XmlConfigSource(filePath);

            config = newSource.Configs["Pets"];
            Assert.IsNotNull(config);
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("rover", config.GetString("dog"));
            Assert.AreEqual("muffy", config.GetString("cat"));

            stream.Close();

            File.Delete(filePath);
        }
Exemplo n.º 3
0
        public void RemoveConfigAndKeyFromFile()
        {
            string filePath = "Test.xml";

            StreamWriter  xmlWriter = new StreamWriter(filePath);
            XmlTextWriter writer    = NiniWriter(xmlWriter);

            WriteSection(writer, "test 1");
            WriteKey(writer, "dog", "Rover");
            writer.WriteEndElement();
            WriteSection(writer, "test 2");
            WriteKey(writer, "cat", "Muffy");
            WriteKey(writer, "lizard", "Lizzy");
            writer.WriteEndDocument();
            xmlWriter.Close();

            XmlConfigSource source = new XmlConfigSource(filePath);

            Assert.IsNotNull(source.Configs["test 1"]);
            Assert.IsNotNull(source.Configs["test 2"]);
            Assert.IsNotNull(source.Configs["test 2"].Get("cat"));

            source.Configs.Remove(source.Configs["test 1"]);
            source.Configs["test 2"].Remove("cat");
            source.AddConfig("cause error");
            source.Save();

            source = new XmlConfigSource(filePath);
            Assert.IsNull(source.Configs["test 1"]);
            Assert.IsNotNull(source.Configs["test 2"]);
            Assert.IsNull(source.Configs["test 2"].Get("cat"));

            File.Delete(filePath);
        }
Exemplo n.º 4
0
        public void EmptyConstructor()
        {
            string          filePath = "EmptyConstructor.xml";
            XmlConfigSource source   = new XmlConfigSource();

            IConfig config = source.AddConfig("Pets");

            config.Set("cat", "Muffy");
            config.Set("dog", "Rover");
            config.Set("bird", "Tweety");
            source.Save(filePath);

            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Tweety", config.Get("bird"));

            source = new XmlConfigSource(filePath);
            config = source.Configs["Pets"];

            Assert.AreEqual(3, config.GetKeys().Length);
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Tweety", config.Get("bird"));

            File.Delete(filePath);
        }
Exemplo n.º 5
0
        public void SaveToWriter()
        {
            string newPath = "TestNew.xml";

            StringWriter  writer    = new StringWriter();
            XmlTextWriter xmlWriter = NiniWriter(writer);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "Muffy");
            WriteKey(xmlWriter, "dog", "Rover");
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();

            StringReader    reader    = new StringReader(writer.ToString());
            XmlTextReader   xmlReader = new XmlTextReader(reader);
            XmlConfigSource source    = new XmlConfigSource(xmlReader);

            IConfig config = source.Configs["Pets"];

            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            StreamWriter textWriter = new StreamWriter(newPath);

            source.Save(textWriter);
            textWriter.Close();              // save to disk

            source = new XmlConfigSource(newPath);
            config = source.Configs["Pets"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            File.Delete(newPath);
        }
Exemplo n.º 6
0
        public void SaveNewSection()
        {
            string filePath = "Test.xml";

            StringWriter  textWriter = new StringWriter();
            XmlTextWriter writer     = NiniWriter(textWriter);

            WriteSection(writer, "new section");
            WriteKey(writer, "dog", "Rover");
            WriteKey(writer, "cat", "Muffy");
            writer.WriteEndDocument();

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(textWriter.ToString());
            doc.Save(filePath);

            XmlConfigSource source = new XmlConfigSource(filePath);
            IConfig         config = source.AddConfig("test");

            Assert.IsNotNull(source.Configs["test"]);
            source.Save();

            source = new XmlConfigSource(filePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));
            Assert.IsNotNull(source.Configs["test"]);

            File.Delete(filePath);
        }
Exemplo n.º 7
0
Arquivo: Editor.cs Projeto: psla/nini
        /// <summary>
        /// Creates a new config file.
        /// </summary>
        private void CreateNewFile()
        {
            string type = null;;

            if (IsArg("set-type"))
            {
                type = GetArg("set-type").ToLower();
            }
            else
            {
                ThrowError("You must supply a type (--set-type)");
            }

            switch (type)
            {
            case "ini":
                IniConfigSource iniSource = new IniConfigSource();
                iniSource.Save(configPath);
                break;

            case "xml":
                XmlConfigSource xmlSource = new XmlConfigSource();
                xmlSource.Save(configPath);
                break;

            case "config":
                DotNetConfigSource dotnetSource = new DotNetConfigSource();
                dotnetSource.Save(configPath);
                break;

            default:
                ThrowError("Unknown type");
                break;
            }
        }
Exemplo n.º 8
0
        public void SaveToNewPath()
        {
            string filePath = "Test.xml";
            string newPath  = "TestNew.xml";

            StreamWriter  textWriter = new StreamWriter(filePath);
            XmlTextWriter xmlWriter  = NiniWriter(textWriter);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "Muffy");
            WriteKey(xmlWriter, "dog", "Rover");
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();

            XmlConfigSource source = new XmlConfigSource(filePath);
            IConfig         config = source.Configs["Pets"];

            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            source.Save(newPath);

            source = new XmlConfigSource(newPath);
            config = source.Configs["Pets"];
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            File.Delete(filePath);
            File.Delete(newPath);
        }
Exemplo n.º 9
0
 private void CheckSection(string section)
 {
     if (Source.Configs[section] == null)
     {
         Source.Configs.Add(section);
         Source.Save();
     }
 }
Exemplo n.º 10
0
 private void WriteDefaultConfig()
 {
     _configSource = new XmlConfigSource();
     _configSource.AddConfig("Main");
     try
     {
         _configSource.Save(Path.Combine(StartupPath, "Config.xml"));
         _configSource.AutoSave = true;
     }
     catch { }
 }
Exemplo n.º 11
0
 public void Save(string path)
 {
     if (Source is IniConfigSource)
     {
         IniConfigSource iniCon = (IniConfigSource)Source;
         iniCon.Save(path);
     }
     else if (Source is XmlConfigSource)
     {
         XmlConfigSource xmlCon = (XmlConfigSource)Source;
         xmlCon.Save(path);
     }
 }
Exemplo n.º 12
0
        // Nini (config) related Methods
        public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
        {
            if (!File.Exists(fileName))
            {
                //create new file
            }

            XmlConfigSource config = new XmlConfigSource(fileName);

            AddDataRowToConfig(config, row);
            config.Save();

            return(config);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Saves the peer configuration.
 /// </summary>
 /// <param name="fileName">The name of the file where to save it.</param>
 private void SaveTo(String fileName)
 {
     // Save the configuration as input for future reconfiguration
     try
     {
         using (FileStream oStream = new FileStream(fileName, FileMode.Create))
         {
             XmlConfigSource source2 = new XmlConfigSource();
             source2.Merge(source);
             source2.Save(oStream);
         }
     }
     catch (Exception e)
     {
         if (log.IsWarnEnabled)
         {
             log.Warn(e);
             log.Warn("Could not save to " + fileName);
         }
     }
 }
Exemplo n.º 14
0
        public void SetAndSave()
        {
            string filePath = "Test.xml";

            StreamWriter  textWriter = File.CreateText(filePath);
            XmlTextWriter writer     = NiniWriter(textWriter);

            WriteSection(writer, "new section");
            WriteKey(writer, "dog", "Rover");
            WriteKey(writer, "cat", "Muffy");
            writer.WriteEndDocument();
            textWriter.Close();

            XmlConfigSource source = new XmlConfigSource(filePath);

            IConfig config = source.Configs["new section"];

            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Muffy", config.Get("cat"));

            config.Set("dog", "Spots");
            config.Set("cat", "Misha");
            config.Set("DoesNotExist", "SomeValue");

            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));
            source.Save();

            source = new XmlConfigSource(filePath);
            config = source.Configs["new section"];
            Assert.AreEqual("Spots", config.Get("dog"));
            Assert.AreEqual("Misha", config.Get("cat"));
            Assert.AreEqual("SomeValue", config.Get("DoesNotExist"));

            File.Delete(filePath);
        }
Exemplo n.º 15
0
        public void MergeAndSave()
        {
            string xmlFileName = "NiniConfig.xml";

            StreamWriter  textWriter = new StreamWriter(xmlFileName);
            XmlTextWriter xmlWriter  = NiniWriter(textWriter);

            WriteSection(xmlWriter, "Pets");
            WriteKey(xmlWriter, "cat", "Muffy");
            WriteKey(xmlWriter, "dog", "Rover");
            WriteKey(xmlWriter, "bird", "Tweety");
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();

            StringWriter writer = new StringWriter();

            writer.WriteLine("[Pets]");
            writer.WriteLine("cat = Becky");              // overwrite
            writer.WriteLine("lizard = Saurus");          // new
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource = new IniConfigSource
                                            (new StringReader(writer.ToString()));

            XmlConfigSource xmlSource = new XmlConfigSource(xmlFileName);

            xmlSource.Merge(iniSource);

            IConfig config = xmlSource.Configs["Pets"];

            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));

            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            xmlSource.Save();

            xmlSource = new XmlConfigSource(xmlFileName);

            config = xmlSource.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));

            File.Delete(xmlFileName);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Change and load configuration file data.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmd"></param>
        private void HandleConfig(string module, string[] cmd)
        {
            List <string> args = new List <string>(cmd);

            args.RemoveAt(0);
            string[] cmdparams = args.ToArray();

            if (cmdparams.Length > 0)
            {
                string firstParam = cmdparams[0].ToLower();

                switch (firstParam)
                {
                case "set":
                    if (cmdparams.Length < 4)
                    {
                        Notice("Syntax: config set <section> <key> <value>");
                        Notice("Example: config set ScriptEngine.DotNetEngine NumberOfScriptThreads 5");
                    }
                    else
                    {
                        IConfig       c;
                        IConfigSource source = new IniConfigSource();
                        c = source.AddConfig(cmdparams[1]);
                        if (c != null)
                        {
                            string _value = String.Join(" ", cmdparams, 3, cmdparams.Length - 3);
                            c.Set(cmdparams[2], _value);
                            Config.Merge(source);

                            Notice("In section [{0}], set {1} = {2}", c.Name, cmdparams[2], _value);
                        }
                    }
                    break;

                case "get":
                case "show":
                    if (cmdparams.Length == 1)
                    {
                        foreach (IConfig config in Config.Configs)
                        {
                            Notice("[{0}]", config.Name);
                            string[] keys = config.GetKeys();
                            foreach (string key in keys)
                            {
                                Notice("  {0} = {1}", key, config.GetString(key));
                            }
                        }
                    }
                    else if (cmdparams.Length == 2 || cmdparams.Length == 3)
                    {
                        IConfig config = Config.Configs[cmdparams[1]];
                        if (config == null)
                        {
                            Notice("Section \"{0}\" does not exist.", cmdparams[1]);
                            break;
                        }
                        else
                        {
                            if (cmdparams.Length == 2)
                            {
                                Notice("[{0}]", config.Name);
                                foreach (string key in config.GetKeys())
                                {
                                    Notice("  {0} = {1}", key, config.GetString(key));
                                }
                            }
                            else
                            {
                                Notice(
                                    "config get {0} {1} : {2}",
                                    cmdparams[1], cmdparams[2], config.GetString(cmdparams[2]));
                            }
                        }
                    }
                    else
                    {
                        Notice("Syntax: config {0} [<section>] [<key>]", firstParam);
                        Notice("Example: config {0} ScriptEngine.DotNetEngine NumberOfScriptThreads", firstParam);
                    }

                    break;

                case "save":
                    if (cmdparams.Length < 2)
                    {
                        Notice("Syntax: config save <path>");
                        return;
                    }

                    string path = cmdparams[1];
                    Notice("Saving configuration file: {0}", path);

                    if (Config is IniConfigSource)
                    {
                        IniConfigSource iniCon = (IniConfigSource)Config;
                        iniCon.Save(path);
                    }
                    else if (Config is XmlConfigSource)
                    {
                        XmlConfigSource xmlCon = (XmlConfigSource)Config;
                        xmlCon.Save(path);
                    }

                    break;
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
        /// </summary>
        /// <param name="iniPath">Full path to the ini</param>
        /// <returns></returns>
        private bool ReadConfig(OpenSimConfigSource configSource, string iniPath)
        {
            bool   success = false;
            string cfn     = Environment.GetEnvironmentVariable("DUMP_CONFIG_FILE");

            if (!IsUri(iniPath))
            {
                m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath));

                IniConfigSource cs = new IniConfigSource(iniPath);

                if ((cfn != null) && (cfn != ""))
                {
                    StringWriter sw = new StringWriter();
                    cs.Save(sw);
                    string[]     lines = sw.ToString().Split(new char[] { '\n' });
                    StreamWriter cfw   = new StreamWriter(cfn, true);
                    try {
                        cfw.WriteLine("## " + Path.GetFullPath(iniPath));
                        StringBuilder sb = new StringBuilder();
                        foreach (string line in lines)
                        {
                            string lin = line.Trim();
                            if ((lin != "") && !lin.StartsWith(";"))
                            {
                                if (!lin.StartsWith("["))
                                {
                                    cfw.Write("  ");
                                }
                                cfw.WriteLine(lin);
                            }
                        }
                    } finally {
                        cfw.Close();
                    }
                }

                configSource.Source.Merge(cs);
                success = true;
            }
            else
            {
                m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath);

                // The ini file path is a http URI
                // Try to read it
                try
                {
                    XmlReader       r  = XmlReader.Create(iniPath);
                    XmlConfigSource cs = new XmlConfigSource(r);

                    if ((cfn != null) && (cfn != ""))
                    {
                        StreamWriter cfw = new StreamWriter(cfn, true);
                        try {
                            cfw.WriteLine("## " + iniPath);
                            cs.Save(cfw);
                            cfw.WriteLine("");
                        } finally {
                            cfw.Close();
                        }
                    }

                    configSource.Source.Merge(cs);
                    success = true;
                }
                catch (Exception e)
                {
                    m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath);
                    Environment.Exit(1);
                }
            }
            return(success);
        }