/// <summary> /// Initialize a new instance of the TerrestrialProvider class. /// </summary> /// <param name="name"></param> public TerrestrialProvider(string name) : base(name) { if (!name.Contains(".")) { country = new Country(name, null); area = null; return; } string[] parts = name.Split(new char[] { '.' }); country = new Country(parts[0], null); area = new Area(parts[1], 0); }
/// <summary> /// Load the country collection from the configuration file. /// </summary> public static Collection<Country> Load() { Collection<Country> countries = new Collection<Country>(); Country country = null; XmlReader reader = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; try { reader = XmlReader.Create(Path.Combine(RunParameters.ConfigDirectory, "Countries.cfg"), settings); } catch (IOException) { Logger.Instance.Write("Failed to open " + Path.Combine(RunParameters.ConfigDirectory, "Countries.cfg")); return(countries); } try { while (!reader.EOF) { reader.Read(); if (reader.IsStartElement()) { switch (reader.Name) { case "Country": if (country != null) addCountry(country, countries); country = new Country(reader.GetAttribute("name"), reader.GetAttribute("code")); break; default: if (country != null) country.load(reader); break; } } } if (country != null) addCountry(country, countries); } catch (XmlException e) { Logger.Instance.Write("Failed to load file " + Path.Combine(RunParameters.ConfigDirectory, "Countries.cfg")); Logger.Instance.Write("Data exception: " + e.Message); } catch (IOException e) { Logger.Instance.Write("Failed to load file " + Path.Combine(RunParameters.ConfigDirectory, "Countries.cfg")); Logger.Instance.Write("I/O exception: " + e.Message); } if (reader != null) reader.Close(); return (countries); }
/// <summary> /// Add a country to a collection. /// </summary> /// <param name="newCountry">The country to be added.</param> /// <param name="countries">The collection of countries to be added to.</param> public static void addCountry(Country newCountry, Collection<Country> countries) { if (countries.Count == 0) { Country undefinedCountry = new Country("-- Undefined --", ""); Area undefinedArea = new Area("-- Undefined --", 0); undefinedArea.Regions.Add(new Region("-- Undefined --", 0)); undefinedCountry.Areas.Add(undefinedArea); countries.Add(undefinedCountry); } foreach (Country oldCountry in countries) { if (oldCountry.Code == newCountry.Code) return; if (oldCountry.Code.CompareTo(newCountry.Code) > 0) { countries.Insert(countries.IndexOf(oldCountry), newCountry); return; } } countries.Add(newCountry); }