public void TestReadXml_OnlyApiKey()
        {
            string filepath = this.WriteTextToTempFile(Consts.SampleXmlOnlyDefaultApiKey);
            ILocanSettings settings = LocanSettings.Load(filepath);
            ILocanSettings defaultSettings = new LocanSettings();

            string expectedApiKey = @"5A2172FE-ED63-4708-A462-9041707454FF";
            Assert.AreEqual(expectedApiKey, settings.DefaultApiKey);

            Assert.AreEqual(defaultSettings.SupportedLanguages.Count, settings.SupportedLanguages.Count);
            Assert.AreEqual(defaultSettings.PreserveUpdates, settings.PreserveUpdates);
        }
Beispiel #2
0
        public static ILocanSettings Load(string uri)
        {
            XDocument  document    = XDocument.Load(uri);
            XNamespace ns          = Consts.XmlNamespace;
            XElement   rootElement = document.Element(ns + Consts.Locan);

            var resultList = from settingsElement in rootElement.Elements(ns + Consts.Settings)
                             from languagesElement in rootElement.Elements(ns + Consts.Languages)
                             select new {
                DefaultApiKey   = settingsElement.SafeGetAttributeValue(Consts.DefaultApiKey),
                PreserveUpdates = settingsElement.SafeGetAttributeValue(Consts.PreserveUpdates),
                Languages       = from l in languagesElement
                                  .Descendants(ns + Consts.Language)
                                  select l.SafeGetAttributeValue(Consts.Name)
            };
            var result = resultList.FirstOrDefault();

            if (result == null)
            {
                string message = string.Format("Unable to read settings (no settings found) from uri [{0}].", uri);
                throw new UnableToReadSettingsException(message);
            }


            ILocanSettings settings = new LocanSettings();

            if (!string.IsNullOrEmpty(result.DefaultApiKey))
            {
                settings.DefaultApiKey = result.DefaultApiKey;
            }
            if (!string.IsNullOrEmpty(result.PreserveUpdates))
            {
                settings.PreserveUpdates = bool.Parse(result.PreserveUpdates);
            }
            if (result.Languages != null && result.Languages.Count() > 0)
            {
                foreach (var lang in result.Languages)
                {
                    if (!string.IsNullOrEmpty(lang))
                    {
                        settings.SupportedLanguages.Add(new BaseLanguage(lang));
                    }
                }
            }

            return(settings);
        }
        public void TestWriteXml01_DefaultSupportedLanguages()
        {
            // write the settings out to a file and then read it back in
            string filepath = this.GetTempFilename(true);

            LocanSettings expectedSettings = new LocanSettings() {
                DefaultApiKey = Guid.NewGuid().ToString(),
                PreserveUpdates = false,
            };

            // write the file out
            expectedSettings.SaveAs(filepath);

            // read the file back in
            ILocanSettings writtenSettings = LocanSettings.Load(filepath);

            Assert.AreEqual(expectedSettings.DefaultApiKey, writtenSettings.DefaultApiKey);
            Assert.AreEqual(expectedSettings.PreserveUpdates, writtenSettings.PreserveUpdates);
            CustomAssert.AreEqual<ILanguage>(expectedSettings.SupportedLanguages, writtenSettings.SupportedLanguages,CustomAssert.AreEqual);
        }
        internal ILocanSettings GetProjectSettings(Project project)
        {
            if (project == null) { throw new ArgumentNullException("project"); }
            string filepath = this.GetPathToLocanSettingsFile(project);

            ILocanSettings settings = new LocanSettings();

            if (File.Exists(filepath)) {
                settings = LocanSettings.Load(filepath);
            }

            return settings;
        }
Beispiel #5
0
        public static ILocanSettings Load(string uri)
        {
            XDocument document = XDocument.Load(uri);
            XNamespace ns = Consts.XmlNamespace;
            XElement rootElement = document.Element(ns + Consts.Locan);

            var resultList = from settingsElement in rootElement.Elements(ns+Consts.Settings)
                             from languagesElement in rootElement.Elements(ns+Consts.Languages)
                             select new {
                                 DefaultApiKey = settingsElement.SafeGetAttributeValue(Consts.DefaultApiKey),
                                 PreserveUpdates = settingsElement.SafeGetAttributeValue(Consts.PreserveUpdates),
                                 Languages = from l in languagesElement
                                                 .Descendants(ns + Consts.Language)
                                             select l.SafeGetAttributeValue(Consts.Name)
                             };
            var result = resultList.FirstOrDefault();

            if (result == null) {
                string message = string.Format("Unable to read settings (no settings found) from uri [{0}].",uri);
                throw new UnableToReadSettingsException(message);
            }

            ILocanSettings settings = new LocanSettings();
            if (!string.IsNullOrEmpty(result.DefaultApiKey)) {
                settings.DefaultApiKey = result.DefaultApiKey;
            }
            if (!string.IsNullOrEmpty(result.PreserveUpdates)) {
                settings.PreserveUpdates = bool.Parse(result.PreserveUpdates);
            }
            if (result.Languages != null && result.Languages.Count() > 0) {
                foreach (var lang in result.Languages) {
                    if (!string.IsNullOrEmpty(lang)) {
                        settings.SupportedLanguages.Add(new BaseLanguage(lang));

                    }
                }
            }

            return settings;
        }