Beispiel #1
0
        public ConfigurationNode SelectNode(string path)
        {
            if (_rootNode == null)
            {
                _rootNode = new ConfigurationNode(null);
            }

            var node = _rootNode;

            if (path != null)
            {
                var nodeNames = path.Split(ConfigurationNode.Delimiter);

                for (var i = 0; i < nodeNames.Length; i++)
                {
                    var childNodeName = nodeNames[i];
                    var contains      = node.ChildNodes.TryGetValue(childNodeName, out var childNode);

                    if (!contains)
                    {
                        childNode = new ConfigurationNode(childNodeName);
                        node.AddChildNode(childNode);
                    }

                    node = childNode;
                }
            }

            return(node);
        }
Beispiel #2
0
        private void Load(out ConfigurationNode rootNode, out StringCollection fileNames)
        {
            var reader = new ConfigurationReader();

            fileNames = new StringCollection();
            rootNode  = reader.Read(ConfigFileName, SectionName, fileNames);
        }
Beispiel #3
0
        private void Initialize()
        {
            StringCollection fileNames = null;

            try
            {
                ConfigurationNode rootNode = null;
                Load(out rootNode, out fileNames);
                RootNode = rootNode;
            }
            catch (Exception e)
            {
                Log.Write(LogLevel.Error, e.ToString());
            }

            if (fileNames != null)
            {
                try
                {
                    foreach (var fileName in fileNames)
                    {
                        var watcher = new FileSystemWatcher(fileName);
                        watcher.NotifyFilter        = NotifyFilters.LastWrite | NotifyFilters.CreationTime;
                        watcher.Changed            += OnChanged;
                        watcher.EnableRaisingEvents = true;
                    }

                    IsFileSystemWatcherEnabled = fileNames.Count > 0;
                }
                catch (Exception e)
                {
                    Log.Write(LogLevel.Error, e.ToString());
                }
            }
        }
Beispiel #4
0
        public void LoadXml(string xml, string sectionName)
        {
            var reader     = new ConfigurationReader();
            var textReader = new StringReader(xml);
            var xmlReader  = new XmlTextReader(textReader);

            _rootNode = reader.Read(xmlReader, null, sectionName, null);
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="childNode"></param>
        public void RemoveChildNode(ConfigurationNode childNode)
        {
            Assert.IsNotNull(childNode);
            Assert.IsValidOperation(this == childNode.Parent);

            ChildNodes.Remove(childNode);
            childNode.Parent = null;
        }
        public void Load(XmlReader xmlReader)
        {
            var reader = new ConfigurationReader();

            RootNode = reader.Read(xmlReader, _sectionName, null, null);

            if (RootNode == null)
            {
                RootNode = new ConfigurationNode(null);
            }
        }
Beispiel #7
0
        public void InsertChildNode(int index, ConfigurationNode childNode)
        {
            Assert.IsTrue(childNode.Parent == null);

            if (childNode.Name == null)
            {
                childNode.Name = ConfigurationElementName.Node + "[" + index + ']';
                index++;
            }

            ChildNodes.Insert(index, childNode);
            childNode.Parent = this;
        }
Beispiel #8
0
        public void AddChildNode(ConfigurationNode childNode)
        {
            Assert.IsTrue(childNode.Parent == null);

            if (childNode.Name == null)
            {
                childNode.Name = ConfigurationElementName.Node + "[" + _index + ']';
                _index++;
            }

            ChildNodes.Add(childNode);
            childNode.Parent = this;
        }
Beispiel #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="childNode"></param>
        public void AddChildNode(ConfigurationNode childNode)
        {
            FoundationContract.Requires <ArgumentException>(childNode.Parent == null);

            if (childNode.Name == null)
            {
                childNode.Name = ConfigurationElementName.Node + "[" + _index + ']';
                _index++;
            }

            ChildNodes.Add(childNode);
            childNode.Parent = this;
        }
Beispiel #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="index"></param>
        /// <param name="childNode"></param>
        public void InsertChildNode(int index, ConfigurationNode childNode)
        {
            FoundationContract.Requires <ArgumentException>(childNode.Parent == null);

            if (childNode.Name == null)
            {
                childNode.Name = ConfigurationElementName.Node + "[" + index + ']';
                index++;
            }

            ChildNodes.Insert(index, childNode);
            childNode.Parent = this;
        }
 private void Check(string nodeName, ConfigurationNode node)
 {
     if (node == null)
     {
         if (!File.Exists(ConfigFileName))
         {
             throw new FileNotFoundException("Configuration file not found.", ConfigFileName);
         }
         else
         {
             throw new ArgumentException($"Configuration node not found.\r\nNodeName: {nodeName}\r\nConfigFileName: {ConfigFileName}");
         }
     }
 }
Beispiel #12
0
        /// <summary>
        /// Reads a config file into memory.
        /// </summary>
        /// <param name="configFileName">The name of the file to read</param>
        /// <param name="sectionName"></param>
        /// <param name="fileNames"></param>
        /// <returns></returns>
        public ConfigurationNode Read(string configFileName, string sectionName, StringCollection fileNames)
        {
            ConfigurationNode node = null;

            using (var stream = OpenStream(configFileName))
            {
                if (stream != null)
                {
                    var xmlTextReader = new XmlTextReader(stream);
                    node = Read(xmlTextReader, configFileName, sectionName, fileNames);
                }
            }

            return(node);
        }
        public void Load(string fileName, string sectionName)
        {
            _fileName    = fileName;
            _sectionName = sectionName;

            if (File.Exists(fileName))
            {
                var reader    = new ConfigurationReader();
                var fileNames = new StringCollection();
                RootNode = reader.Read(fileName, sectionName, fileNames);
            }
            else
            {
                RootNode = new ConfigurationNode(null);
            }
        }
Beispiel #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlReader"></param>
        /// <returns></returns>
        public ConfigurationNode Read(XmlReader xmlReader)
        {
            _xmlReader = xmlReader;
            var node      = new ConfigurationNode(null);
            var fileNames = new StringCollection();

            Read(node, fileNames);

            if (node.ChildNodes.Count == 1)
            {
                var childNode = node.ChildNodes[0];
                node.RemoveChildNode(childNode);
                node = childNode;
            }

            return(node);
        }
Beispiel #15
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public ConfigurationNode Clone()
        {
            var clone = new ConfigurationNode(Name);

            foreach (var attribute in Attributes)
            {
                var attributeClone = attribute.Clone();
                clone.Attributes.Add(attributeClone);
            }

            foreach (var childNode in ChildNodes)
            {
                var childNodeClone = childNode.Clone();
                clone.AddChildNode(childNodeClone);
            }

            return(clone);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlWriter"></param>
        /// <param name="node"></param>
        public static void WriteNode(XmlWriter xmlWriter, ConfigurationNode node)
        {
            string xmlElementName;
            string xmlAttributeValue;

            if (node.HasName)
            {
                var nodeName    = node.Name;
                var encodedName = XmlConvert.EncodeName(nodeName);

                if (nodeName == encodedName)
                {
                    xmlElementName    = nodeName;
                    xmlAttributeValue = null;
                }
                else
                {
                    xmlElementName    = ConfigurationElementName.Node;
                    xmlAttributeValue = nodeName;
                }
            }
            else
            {
                xmlElementName    = ConfigurationElementName.Node;
                xmlAttributeValue = null;
            }

            using (xmlWriter.WriteElement(xmlElementName))
            {
                if (xmlAttributeValue != null)
                {
                    xmlWriter.WriteAttributeString("name", xmlAttributeValue);
                }

                Write(xmlWriter, node.Attributes);

                foreach (var childNode in node.ChildNodes)
                {
                    WriteNode(xmlWriter, childNode);
                }
            }
        }
Beispiel #17
0
        public ConfigurationNode CreateNode(string nodeName)
        {
            Assert.IsNotNull(nodeName);

            var node      = this;
            var nodeNames = nodeName.Split(Delimiter);

            for (var i = 0; i < nodeNames.Length; i++)
            {
                var contains = node.ChildNodes.TryGetValue(nodeNames[i], out var childNode);
                if (!contains)
                {
                    childNode = new ConfigurationNode(nodeNames[i]);
                    node.AddChildNode(childNode);
                }

                node = childNode;
            }

            return(node);
        }
Beispiel #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static ConfigurationNode Read(TextReader reader)
        {
            var node        = new ConfigurationNode(null);
            var currentNode = node;

            while (reader.Peek() != -1)
            {
                var line = reader.ReadLine();

                if (!string.IsNullOrEmpty(line))
                {
                    if (line[0] == '[')
                    {
                        var index     = line.IndexOf(']');
                        var name      = line.Substring(1, index - 1);
                        var childNode = new ConfigurationNode(name);
                        node.AddChildNode(childNode);
                        currentNode = childNode;
                    }
                    else
                    {
                        var index = line.IndexOf('=');

                        if (index >= 0)
                        {
                            var name   = line.Substring(0, index);
                            var length = line.Length - index - 1;
                            var value  = line.Substring(index + 1, length);
                            currentNode.Attributes.Add(new ConfigurationAttribute(name, value, null));
                        }
                    }
                }
            }

            return(node);
        }
Beispiel #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlReader"></param>
        /// <param name="configFilename"></param>
        /// <param name="sectionName"></param>
        /// <param name="fileNames"></param>
        /// <returns></returns>
        public ConfigurationNode Read(
            XmlReader xmlReader,
            string configFilename,
            string sectionName,
            StringCollection fileNames)
        {
            Log.Trace("ConfigurationReader.Read({0},{1})...", configFilename, sectionName);
            var startTick = Stopwatch.GetTimestamp();

            _xmlReader   = xmlReader;
            _fileName    = configFilename;
            _sectionName = sectionName;
            ConfigurationNode node    = null;
            string            message = null;

            try
            {
                var found = FindSection(sectionName);

                if (found)
                {
                    var nodeType = xmlReader.MoveToContent();

                    if (nodeType == XmlNodeType.Element)
                    {
                        InitCultureInfo();
                        _enableFileSystemWatcher = StringHelper.ParseBoolean(xmlReader["enableFileSystemWatcher"], false);
                        node = new ConfigurationNode(null);
                        Read(node, fileNames);

                        if (_enableFileSystemWatcher && fileNames != null && !fileNames.Contains(configFilename))
                        {
                            fileNames.Add(configFilename);
                        }
                    }
                    else
                    {
                        message = $"RootNode not found. fileName: {_fileName}, sectionName: {sectionName}";
                        AddError(ErrorType.Error, message, null);
                    }
                }
                else
                {
                    message = $"RootNode not found. fileName: {_fileName}, sectionName: {sectionName}";
                    AddError(ErrorType.Information, message, null);
                }
            }
            catch (Exception e)
            {
                AddError(ErrorType.Error, null, e);
            }

            var ticks = Stopwatch.GetTimestamp() - startTick;

            message = $"{configFilename} loaded successfully in {StopwatchTimeSpan.ToString(ticks, 3)}.";
            LogLevel logLevel;
            var      source = _errors.Where(e => e.Type == ErrorType.Error);

            if (source.Any())
            {
                logLevel = LogLevel.Error;
            }
            else
            {
                var enumerable = _errors.Where(e => e.Type == ErrorType.Warning);
                logLevel = enumerable.Any() ? LogLevel.Warning : LogLevel.Trace;
            }

            Log.Write(logLevel, "ConfigurationReader.Read finished.\r\nthis.errors.Count: {0}\r\n{1}", _errors.Count, _errors.ToString());
            return(node);
        }
Beispiel #20
0
        private void Read(ConfigurationNode node, StringCollection fileNames)
        {
            var name      = node.Name;
            var endOfNode = _xmlReader.IsEmptyElement;

            if (name != null)
            {
                var hasNext = _xmlReader.MoveToFirstAttribute();

                while (hasNext)
                {
                    var attributeName  = _xmlReader.Name;
                    var attributeValue = _xmlReader.Value;

                    if (attributeName == "name")
                    {
                    }
                    else if (attributeName == "description")
                    {
                        node.Description = attributeValue;
                    }
                    else
                    {
                        var attribute = new ConfigurationAttribute(attributeName, attributeValue, null);
                        node.Attributes.Add(attribute);
                    }

                    hasNext = _xmlReader.MoveToNextAttribute();
                }
            }

            while (!endOfNode && _xmlReader.Read())
            {
                switch (_xmlReader.NodeType)
                {
                case XmlNodeType.Element:
                {
                    var elementName = _xmlReader.Name;

                    switch (elementName)
                    {
                    case ConfigurationElementName.Attribute:
                        ReadAttribute(node);
                        break;

                    case ConfigurationElementName.Node:
                    {
                        var nodeName  = _xmlReader.GetAttribute("name");
                        var childNode = new ConfigurationNode(nodeName);
                        node.AddChildNode(childNode);
                        Read(childNode, fileNames);
                    }

                    break;

                    case "include":
                    {
                        var fileName = _xmlReader.GetAttribute("fileName");
                        fileName = Environment.ExpandEnvironmentVariables(fileName);

                        var reader2     = new ConfigurationReader();
                        var includeNode = reader2.Read(fileName, _sectionName, fileNames);
                        node.Attributes.Add(includeNode.Attributes);

                        foreach (var childNode in includeNode.ChildNodes)
                        {
                            node.AddChildNode(childNode);
                        }

                        if (_enableFileSystemWatcher && !fileNames.Contains(fileName))
                        {
                            fileNames.Add(fileName);
                        }
                    }

                    break;

                    default:
                    {
                        var nodeName  = XmlConvert.DecodeName(elementName);
                        var childNode = new ConfigurationNode(nodeName);
                        node.AddChildNode(childNode);
                        Read(childNode, fileNames);
                    }

                    break;
                    }
                }

                break;

                case XmlNodeType.EndElement:
                    endOfNode = true;
                    break;

                default:
                    break;
                }
            }
        }
Beispiel #21
0
        private void ReadAttribute(ConfigurationNode node)
        {
            ConfigurationAttribute attribute = null;
            var    name  = _xmlReader["name"];
            object value = null;

            try
            {
                if (name == null)
                {
                    AddError(ErrorType.Warning, "name attribute not found", null);
                }

                var typeName = _xmlReader["type"];
                var type     = GetType(typeName);

                if (type != null)
                {
                    var isNullStr   = _xmlReader["isNull"];
                    var isNull      = false;
                    var description = _xmlReader["description"];

                    if (isNullStr != null)
                    {
                        try
                        {
                            isNull = bool.Parse(isNullStr);
                        }
                        catch (Exception e)
                        {
                            AddError(ErrorType.Error, "Error parsing isNull attribute.", e);
                        }
                    }

                    if (!isNull)
                    {
                        var valueStr = _xmlReader["value"];

                        try
                        {
                            if (valueStr != null)
                            {
                                value = Convert.ChangeType(valueStr, type, _formatProvider);
                            }
                            else
                            {
                                value = ReadAttributeValue(type);
                            }
                        }
                        catch (Exception e)
                        {
                            AddError(ErrorType.Error, "Reading attribute value failed.", e);
                        }
                    }

                    attribute = new ConfigurationAttribute(name, value, description);
                }
                else
                {
                    AddError(ErrorType.Warning, "Parsing attribute type failed.", null);
                }
            }
            catch (Exception e)
            {
                AddError(ErrorType.Error, "Reading attribute failed.", e);
            }

            if (attribute != null)
            {
                try
                {
                    node.Attributes.Add(attribute);
                }
                catch (Exception e)
                {
                    AddError(ErrorType.Error, "Adding attribute to node failed.", e);
                }
            }
        }