Ejemplo n.º 1
0
        /// <summary>
        ///     Loads the namespaces and properties from the file into
        ///     Namespace and Property objects.
        /// </summary>
        /// 
        /// <param name="filename">The name of the file.</param>
        /// <returns>The populated Namespace objects</returns>
        /// 
        /// <exception cref="NamespaceClashException">
        ///     If a namespace clash occurs.
        /// </exception>
        /// 
        /// <exception cref="PropertyClashException">
        ///     If a property clash occurs.
        /// </exception>
        /// 
        /// <exception cref="InvalidConfigFileException">
        ///     If the configuration file is invalid.
        /// </exception>
        /// 
        /// <exception cref="UnknownFormatException">
        ///     If the format of the configuration file is unkown.
        /// </exception>
        /// 
        /// <exception cref="IOException">
        ///     If an I/O error occurs.
        /// </exception>
        public List<Namespace> LoadFromFile(string filename)
        {
            try
            {
                var namespaces = new List<Namespace>();

                using (var reader = new XmlTextReader(filename))
                {
                    while (reader.Read())
                    {
                        if (reader.Name.ToLower() == "namespace" && reader.IsStartElement())
                        {
                            var namespaceName = reader.GetAttribute("name");
                            var currentNamespace = new Namespace(namespaceName);
                            namespaces.Add(currentNamespace);
                            while (reader.Read())
                            {
                                if (reader.Name.ToLower() == "property" && reader.IsStartElement())
                                {
                                    var propertyName = reader.GetAttribute("name");
                                    var valueList = new List<string>();

                                    while (reader.Read())
                                    {
                                        if (reader.Name.ToLower() == "value" && reader.IsStartElement())
                                        {
                                            reader.Read();
                                            valueList.Add(reader.Value);
                                        }
                                        else if (reader.Name.ToLower() == "property" && reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    currentNamespace.AddProperty(new Property(propertyName, valueList));
                                }
                                else if (reader.Name.ToLower() == "namespace" && reader.NodeType == XmlNodeType.EndElement)
                                {
                                    break;
                                }
                            }
                        }
                        else if (reader.Name.ToLower() == "ConfigManager" && reader.NodeType == XmlNodeType.EndElement)
                        {
                            break;
                        }
                    }
                }

                return namespaces;
            }
            catch (XmlException e)
            {
                throw new InvalidConfigFileException("'" + filename + "' contains errors.", e);
            }
        }
Ejemplo n.º 2
0
        private static void LoadFile(TextReader reader, ICollection<Namespace> namespaces)
        {
            Namespace currentNamespace = null;
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith("#") || line.StartsWith(";"))
                {
                    // Comment line
                }
                else if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    currentNamespace = new Namespace(line.Substring(1, line.Length - 2));
                    namespaces.Add(currentNamespace);
                }
                else if (line.IndexOf("=", StringComparison.Ordinal) != -1)
                {
                    if (currentNamespace != null)
                    {
                        var propertyName = line.Substring(0, line.IndexOf("=", StringComparison.Ordinal)).Trim();
                        var valuesString = line.Substring(line.IndexOf("=", StringComparison.Ordinal) + 1).Trim();

                        var values = valuesString.Split(new[] {';'});
                        
                        for (var i = 0; i < values.Length; i++)
                            values[i] = values[i].Trim();

                        currentNamespace.AddProperty(new Property(propertyName, values.ToList()));
                    }
                    else
                    {
                        throw new InvalidConfigFileException("We've got values, but they're not inside a namespace");
                    }
                }
                else if (line.Length > 0)
                {
                    throw new InvalidConfigFileException("Unsupported line: " + line);
                }
            }
        }