Esempio n. 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);
        }
Esempio n. 2
0
        /// <summary>
        /// Process the annotated property and set the values from the configuration parameters.
        /// </summary>
        /// <typeparam name="T">Target Instance type</typeparam>
        /// <param name="node">Configuration Node</param>
        /// <param name="target">Target Type instance.</param>
        /// <param name="property">Property to update</param>
        /// <param name="param">Config param annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessProperty <T>(ConfigPathNode node, T target, PropertyInfo property, ConfigParam param, List <string> valuePath)
        {
            string pname = param.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = property.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(param.Path))
            {
                AbstractConfigNode nnode = node.Find(param.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }
            if (node != null)
            {
                ConfigParametersNode pnode = node.GetParameters();
                if (pnode != null)
                {
                    ConfigValueNode vn = pnode.GetValue(pname);
                    if (vn != null)
                    {
                        value = vn.GetValue();
                    }
                    if (valuePath != null)
                    {
                        valuePath.Add(pnode.GetSearchPath());
                    }
                }
            }
            if (!String.IsNullOrWhiteSpace(value))
            {
                object v = GetValue <T>(pname, value, param.Function, property.PropertyType, target, param.Required);
                if (v != null)
                {
                    property.SetValue(target, v);
                }
            }
            else if (param.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }
Esempio n. 3
0
        /// <summary>
        /// Write a Key/Value node (parameters/properties)
        /// </summary>
        /// <param name="writer">XML Writer</param>
        /// <param name="node">Key/Value node</param>
        /// <param name="name">Node name</param>
        private void WriteKeyValueNode(XmlWriter writer, ConfigKeyValueNode node, string name)
        {
            writer.WriteStartElement(name);
            Dictionary <string, ConfigValueNode> values = node.GetValues();

            if (values != null && values.Count > 0)
            {
                foreach (string key in values.Keys)
                {
                    ConfigValueNode vn = values[key];
                    if (vn != null)
                    {
                        writer.WriteStartElement(vn.Name);
                        writer.WriteString(vn.GetValue());
                        writer.WriteEndElement();
                    }
                }
            }
            writer.WriteEndElement();
        }
Esempio n. 4
0
        /// <summary>
        /// Find the parameter values for the method.
        /// </summary>
        /// <param name="pnode">Parameters Node</param>
        /// <param name="method">Method Name</param>
        /// <param name="parameters">Parameters</param>
        /// <returns>List of object values</returns>
        private static List <object> FindParameters(ConfigParametersNode pnode, string method, ParameterInfo[] parameters)
        {
            List <object> values = new List <object>();

            foreach (ParameterInfo pi in parameters)
            {
                ConfigParam param = (ConfigParam)Attribute.GetCustomAttribute(pi, typeof(ConfigParam));
                if (param != null)
                {
                    object          v  = null;
                    ConfigValueNode cv = pnode.GetValue(param.Name);
                    if (cv != null)
                    {
                        string value = cv.GetValue();
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            v = ReflectionUtils.ConvertFromString(pi.ParameterType, value);
                        }
                    }
                    if (v != null)
                    {
                        values.Add(v);
                    }
                    else
                    {
                        throw new AnnotationProcessorException(String.Format("Error Invoking Method: Value not found for parameter. [method={0}][parameter={1}]",
                                                                             method, pi.Name));
                    }
                }
                else
                {
                    throw new AnnotationProcessorException(String.Format("Error Invoking Method: Annotation not defined for parameter. [method={0}][parameter={1}]",
                                                                         method, pi.Name));
                }
            }
            if (values.Count > 0)
            {
                return(values);
            }
            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// Process the annotated field and set the values from the configuration value.
        /// </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="configValue">Config value annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigValue configValue, List <string> valuePaths)
        {
            string pname = configValue.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = field.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(configValue.Path))
            {
                AbstractConfigNode nnode = node.Find(configValue.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }

            if (node != null)
            {
                AbstractConfigNode cnode = node.GetChildNode(pname);
                if (cnode != null)
                {
                    if (valuePaths != null)
                    {
                        valuePaths.Add(cnode.GetSearchPath());
                    }
                    if (cnode.GetType() == typeof(ConfigValueNode))
                    {
                        ConfigValueNode vn = (ConfigValueNode)cnode;
                        if (vn != null)
                        {
                            value = vn.GetValue();
                        }
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            object v = GetValue <T>(pname, value, configValue.Function, field.FieldType, target, configValue.Required);
                            if (v != null)
                            {
                                TypeUtils.CallSetter(field, target, v);
                            }
                        }
                    }
                    else
                    {
                        if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(List <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertListFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                        else if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(HashSet <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertSetFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            object ov = TypeUtils.CallGetter(field, target);

            if (ov == null && configValue.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }
Esempio n. 6
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);
                        }
                    }
                }
            }
        }
Esempio n. 7
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);
     }
 }