/// <summary> /// Process the annotated field and set the values from the configuration attributes. /// </summary> /// <typeparam name="T">Target Instance type</typeparam> /// <param name="node">Configuration Node</param> /// <param name="target">Target Type instance.</param> /// <param name="field">Property to update</param> /// <param name="attr">Config attribute annotation</param> /// <returns>Updated Target Type instance.</returns> private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigAttribute attr, List <string> valuePaths) { string pname = attr.Name; if (String.IsNullOrWhiteSpace(pname)) { pname = field.Name; } string value = null; if (!String.IsNullOrWhiteSpace(attr.Path)) { AbstractConfigNode nnode = node.Find(attr.Path); if (nnode != null && nnode.GetType() == typeof(ConfigPathNode)) { node = (ConfigPathNode)nnode; } else { node = null; } } if (node != null) { ConfigAttributesNode pnode = node.GetAttributes(); if (pnode != null) { ConfigValueNode vn = pnode.GetValue(pname); if (vn != null) { value = vn.GetValue(); } if (valuePaths != null) { valuePaths.Add(pnode.GetSearchPath()); } } } if (!String.IsNullOrWhiteSpace(value)) { object v = GetValue <T>(pname, value, attr.Function, field.FieldType, target, attr.Required); if (v != null) { TypeUtils.CallSetter(field, target, v); } } else if (attr.Required) { throw AnnotationProcessorException.Throw(target.GetType(), pname); } return(target); }
/// <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); } }