/// <summary> /// Write a Element List node. /// </summary> /// <param name="writer">XML Writer</param> /// <param name="node">List Element Node.</param> /// <param name="settings">Configuration Settings.</param> private void WriteListElementNode(XmlWriter writer, ConfigElementListNode node, ConfigurationSettings settings) { writer.WriteStartElement(node.Name); { List <ConfigElementNode> values = node.GetValues(); if (values != null && values.Count > 0) { foreach (ConfigElementNode vn in values) { WriteNode(writer, vn, settings); } } } writer.WriteEndElement(); }
/// <summary> /// Load the defined processes for the pipeline. /// </summary> /// <param name="pipeline">Parent pipeline</param> /// <param name="node">Configuration node.</param> /// <param name="name">Pipeline name</param> private void LoadProcessors(object pipeline, ConfigPathNode node, string name) { AbstractConfigNode pnode = node.Find(CONFIG_NODE_PROCESSORS); Conditions.NotNull(pnode); if (pnode.GetType() == typeof(ConfigPathNode)) { LoadProcessor(pipeline, (ConfigPathNode)pnode, name); } else if (pnode.GetType() == typeof(ConfigElementListNode)) { ConfigElementListNode nodes = (ConfigElementListNode)pnode; foreach (ConfigElementNode elem in nodes.GetValues()) { if (elem.GetType() == typeof(ConfigPathNode) && elem.Name == CONFIG_NODE_PROCESSOR) { LoadProcessor(pipeline, (ConfigPathNode)elem, name); } } } }
/// <summary> /// Load defined pipelines from the configuration. /// </summary> /// <param name="config">Configuration Node</param> public void Load(AbstractConfigNode config) { Contract.Requires(config != null); AbstractConfigNode ps = null; if (config.Name == CONFIG_NODE_PIPELINES) { ps = config; } else { ps = config.Find(CONFIG_NODE_PIPELINES); } if (ps != null) { if (ps.GetType() == typeof(ConfigPathNode)) { ps = ps.Find(CONFIG_NODE_PIPELINE); if (ps != null && ps.GetType() == typeof(ConfigPathNode)) { LoadPipeline((ConfigPathNode)ps); } else { LogUtils.Warn(String.Format("No pipeline defined: [path={0}]", config.GetAbsolutePath())); } } else if (ps.GetType() == typeof(ConfigElementListNode)) { ConfigElementListNode nodes = (ConfigElementListNode)ps; foreach (ConfigElementNode elem in nodes.GetValues()) { if (elem.GetType() == typeof(ConfigPathNode) && elem.Name == CONFIG_NODE_PIPELINE) { LoadPipeline((ConfigPathNode)elem); } } } } }
/// <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); } } } } }
/// <summary> /// Parse a configuration node. /// </summary> /// <param name="name">Config node name</param> /// <param name="elem">XML Element</param> /// <param name="nodeStack">Current Node Stack</param> private void ParseBodyNode(string name, XmlElement elem, Stack <AbstractConfigNode> nodeStack) { bool popStack = false; bool processed = false; AbstractConfigNode parent = nodeStack.Peek(); if (IsTextNode(elem)) { bool encrypted = false; if (elem.HasAttributes) { string attr = elem.Attributes[XML_VALUE_ENCRYPTED].Value; if (!String.IsNullOrWhiteSpace(attr)) { if (attr.CompareTo("true") == 0) { encrypted = true; } } } AddValueNode(parent, elem.Name, elem.FirstChild.Value, encrypted); } else { XmlNodeType nt = IsListNode(elem); if (nt == XmlNodeType.Element) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; ConfigElementListNode nodeList = new ConfigElementListNode(parent.Configuration, parent); nodeList.Name = name; pnode.AddChildNode(nodeList); nodeStack.Push(nodeList); popStack = true; } else if (nt == XmlNodeType.Text) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; ConfigListValueNode nodeList = new ConfigListValueNode(parent.Configuration, parent); nodeList.Name = name; pnode.AddChildNode(nodeList); nodeStack.Push(nodeList); popStack = true; } else { if (elem.Name == ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; AddIncludeNode(pnode, elem); processed = true; } else if (elem.Name == ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; AddResourceNode(pnode, elem); processed = true; } else if (elem.Name == settings.ParametersNodeName) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; ConfigParametersNode paramNode = new ConfigParametersNode(parent.Configuration, parent); paramNode.Name = name; pnode.AddChildNode(paramNode); nodeStack.Push(paramNode); popStack = true; } else if (elem.Name == settings.PropertiesNodeName) { if (parent.GetType() != typeof(ConfigPathNode)) { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName)); } ConfigPathNode pnode = (ConfigPathNode)parent; ConfigPropertiesNode propNode = new ConfigPropertiesNode(parent.Configuration, parent); propNode.Name = name; pnode.AddChildNode(propNode); nodeStack.Push(propNode); popStack = true; } else { ConfigPathNode cnode = new ConfigPathNode(parent.Configuration, parent); cnode.Name = name; if (parent.GetType() == typeof(ConfigPathNode)) { ConfigPathNode pnode = (ConfigPathNode)parent; pnode.AddChildNode(cnode); } else if (parent.GetType() == typeof(ConfigElementListNode)) { ConfigElementListNode nodeList = (ConfigElementListNode)parent; nodeList.Add(cnode); } else { throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add path node to parent. [parent={0}]", parent.GetType().FullName)); } nodeStack.Push(cnode); popStack = true; } } if (!processed) { if (elem.HasAttributes) { AbstractConfigNode pp = nodeStack.Peek(); if (pp.GetType() == typeof(ConfigPathNode)) { ConfigPathNode cp = (ConfigPathNode)pp; ConfigAttributesNode attrs = new ConfigAttributesNode(cp.Configuration, cp); cp.AddChildNode(attrs); foreach (XmlAttribute attr in elem.Attributes) { ConfigValueNode vn = new ConfigValueNode(attrs.Configuration, attrs); vn.Name = attr.Name; vn.SetValue(attr.Value); attrs.Add(vn); } } } if (elem.HasChildNodes) { foreach (XmlNode cnode in elem.ChildNodes) { if (cnode.NodeType == XmlNodeType.Element) { ParseBodyNode(cnode.Name, (XmlElement)cnode, nodeStack); } } } } if (popStack) { nodeStack.Pop(); } } }