예제 #1
0
        /// <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);
        }
예제 #2
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);
                        }
                    }
                }
            }
        }