コード例 #1
0
        /// <summary>
        /// Recursively replace defined variables with properties in the configuration nodes.
        ///
        /// </summary>
        /// <param name="node">Configuration Node.</param>
        /// <param name="inProps">Scoped properties map</param>
        /// <param name="replace">Replace variables?</param>
        private void NodePostLoad(AbstractConfigNode node, Dictionary <string, ConfigValueNode> inProps, bool replace)
        {
            Dictionary <string, ConfigValueNode> properties = General.Clone <string, ConfigValueNode>(inProps);

            if (node.GetType() == typeof(ConfigPathNode))
            {
                ConfigPathNode       pnode = (ConfigPathNode)node;
                ConfigPropertiesNode props = pnode.GetProperties();
                if (props != null && !props.IsEmpty())
                {
                    Dictionary <string, ConfigValueNode> pd = props.GetValues();
                    foreach (string key in pd.Keys)
                    {
                        if (!properties.ContainsKey(key))
                        {
                            properties.Add(key, pd[key]);
                        }
                        else
                        {
                            properties[key] = pd[key];
                        }
                    }
                }
                if (!pnode.IsEmpty())
                {
                    foreach (string key in pnode.GetChildren().Keys)
                    {
                        NodePostLoad(pnode.GetChildren()[key], properties, replace);
                    }
                }
            }
            else
            {
                if (node.GetType() == typeof(ConfigParametersNode))
                {
                    ConfigParametersNode pnode = (ConfigParametersNode)node;
                    if (!pnode.IsEmpty() && replace)
                    {
                        foreach (string key in pnode.GetValues().Keys)
                        {
                            ConfigValueNode vn    = pnode.GetValue(key);
                            string          value = vn.GetValue();
                            if (!String.IsNullOrWhiteSpace(value))
                            {
                                string nv = ReplaceVariable(value, properties);
                                vn.SetValue(nv);
                            }
                        }
                    }
                }
                else if (node.GetType() == typeof(ConfigAttributesNode))
                {
                    ConfigAttributesNode pnode = (ConfigAttributesNode)node;
                    if (!pnode.IsEmpty() && replace)
                    {
                        foreach (string key in pnode.GetValues().Keys)
                        {
                            ConfigValueNode vn    = pnode.GetValue(key);
                            string          value = vn.GetValue();
                            if (!String.IsNullOrWhiteSpace(value))
                            {
                                string nv = ReplaceVariable(value, properties);
                                vn.SetValue(nv);
                            }
                        }
                    }
                }
                else if (node.GetType() == typeof(ConfigListValueNode))
                {
                    ConfigListValueNode pnode = (ConfigListValueNode)node;
                    if (!pnode.IsEmpty() && replace)
                    {
                        foreach (ConfigValueNode vn in pnode.GetValues())
                        {
                            string value = vn.GetValue();
                            if (!String.IsNullOrWhiteSpace(value))
                            {
                                string nv = ReplaceVariable(value, properties);
                                vn.SetValue(nv);
                            }
                        }
                    }
                }
                else if (node.GetType() == typeof(ConfigElementListNode))
                {
                    ConfigElementListNode pnode = (ConfigElementListNode)node;
                    if (!pnode.IsEmpty())
                    {
                        foreach (ConfigElementNode vn in pnode.GetValues())
                        {
                            NodePostLoad(vn, properties, replace);
                        }
                    }
                }
                else if (node.GetType() == typeof(ConfigValueNode))
                {
                    if (replace)
                    {
                        ConfigValueNode vn    = (ConfigValueNode)node;
                        string          value = vn.GetValue();
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            string nv = ReplaceVariable(value, properties);
                            vn.SetValue(nv);
                        }
                    }
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Write a configuration node.
 /// </summary>
 /// <param name="writer">XML Writer</param>
 /// <param name="node">Node To write</param>
 /// <param name="settings">Configuration Settings.</param>
 private void WriteNode(XmlWriter writer, AbstractConfigNode node, ConfigurationSettings settings)
 {
     if (node.GetType() == typeof(ConfigPathNode))
     {
         ConfigPathNode pnode = (ConfigPathNode)node;
         writer.WriteStartElement(node.Name);
         {
             ConfigAttributesNode attrs = pnode.GetAttributes();
             if (attrs != null)
             {
                 Dictionary <string, ConfigValueNode> values = attrs.GetValues();
                 if (values != null && values.Count > 0)
                 {
                     foreach (string key in values.Keys)
                     {
                         ConfigValueNode vn = values[key];
                         if (vn != null)
                         {
                             writer.WriteAttributeString(vn.Name, vn.GetValue());
                         }
                     }
                 }
             }
             Dictionary <string, AbstractConfigNode> nodes = pnode.GetChildren();
             foreach (string key in nodes.Keys)
             {
                 AbstractConfigNode cnode = nodes[key];
                 if (cnode.Name == settings.AttributesNodeName)
                 {
                     continue;
                 }
                 WriteNode(writer, cnode, settings);
             }
         }
         writer.WriteEndElement();
     }
     else if (node.GetType() == typeof(ConfigValueNode))
     {
         ConfigValueNode vn = (ConfigValueNode)node;
         writer.WriteStartElement(vn.Name);
         writer.WriteString(vn.GetValue());
         writer.WriteEndElement();
     }
     else if (node.GetType() == typeof(ConfigParametersNode) || node.GetType() == typeof(ConfigPropertiesNode))
     {
         string name = null;
         if (node.GetType() == typeof(ConfigParametersNode))
         {
             name = settings.ParametersNodeName;
         }
         else
         {
             name = settings.PropertiesNodeName;
         }
         ConfigKeyValueNode kvnode = (ConfigKeyValueNode)node;
         WriteKeyValueNode(writer, kvnode, name);
     }
     else if (node.GetType() == typeof(ConfigListValueNode))
     {
         WriteListValueNode(writer, (ConfigListValueNode)node);
     }
     else if (node.GetType() == typeof(ConfigElementListNode))
     {
         WriteListElementNode(writer, (ConfigElementListNode)node, settings);
     }
     else if (node.GetType() == typeof(ConfigIncludeNode))
     {
         ConfigIncludeNode inode = (ConfigIncludeNode)node;
         WriteNode(writer, inode.Node, settings);
     }
     else if (typeof(ConfigResourceNode).IsAssignableFrom(node.GetType()))
     {
         ConfigResourceNode rnode = (ConfigResourceNode)node;
         WriteResourceNode(writer, rnode);
     }
 }