Exemple #1
0
        /// <summary>Creates new instance of the X2O config, adding the default elements processors and type resolvers</summary>
        public static X2OConfig                                                         DefaultConfig()
        {
            var cfg = new X2OConfig(GetDefaultElementsProcessors());

            cfg.m_TypeResolvers.AddRange(cfg.GetDefaultTypeResolvers());
            return(cfg);
        }
Exemple #2
0
        /// <summary>
        /// Will read the XML file and returns instance of the read object completely initialized with values from XML file.
        /// The Type generic argument is used as the declared type of the root node.
        /// </summary>
        /// <param name="_filename">The filename of the XML file to read into the object</param>
        /// <returns>The instance of the object according to the type read from XML file</returns>
        public static T                                                                         ReadFromFile <T>(string _filename)
        {
            var cfg    = X2OConfig.DefaultConfig().UsingExternalConfigSearchPath(new System.IO.FileInfo(_filename).DirectoryName);
            var result = ReadFromFile <T>(_filename, cfg);

            return((T)result);
        }
Exemple #3
0
        /// <summary>Creates new instance of the X2O config, with adding the default type resolvers</summary>
        public static X2OConfig                                                         WithDefaultTypeResolvers()
        {
            var cfg = new X2OConfig(null);

            cfg.m_TypeResolvers.AddRange(cfg.GetDefaultTypeResolvers());
            return(cfg);
        }
Exemple #4
0
        /// <summary>
        /// Will analyse the passed element (which is root) and returns the type which can be used as _declared_type
        /// when calling the ProcessElements function for the root node of Xml.
        /// </summary>
        public virtual Type GetTypeOfRoot(XmlElement _element, X2OConfig _config)
        {
            var type_attr = _element.Attributes["type"];
            Type tgt_type = null;
            if (type_attr != null)
            {
                tgt_type = GetTypeByName(type_attr.Value, _config);
            }

            if (tgt_type == null)
            {
                tgt_type = GetTypeByName(_element.Name, _config);
            }

            return tgt_type;
        }
Exemple #5
0
        /// <summary>
        /// This function based on the name of type returns the type.
        /// This helps to consider the type even with type shortcuts (e.g. not using namespaces and so on).
        /// It goes through the full list of types in all loaded assemblies and tries to find the best match - only single is allowed.
        /// </summary>
        /// <exception cref="InvalidOperationException">In case, there is more than one suitable type.</exception>
        public virtual Type GetTypeByName(string _type_name, X2OConfig _config)
        {
            // first try by type attribute
            var tgt_types = from a in AppDomain.CurrentDomain.GetAssemblies()
                            from t in a.GetTypes()
                            where t.FullName == _type_name || t.Name == _type_name || t.AssemblyQualifiedName == _type_name
                            select t;

            if (tgt_types.Count() == 1)
                return tgt_types.First();
            if (tgt_types.Count() > 1)
                throw new InvalidOperationException($"Multiple types has been found for resolving the type name \"{_type_name}\"");
            // no type found, so look within type resolvers
            foreach(var type_resolver in _config.TypeResolvers)
            {
                var resolved_type = type_resolver(_type_name);
                if (resolved_type != null)
                    return resolved_type;
            }

            // no type found
            return null;
        }
Exemple #6
0
        /// <summary>
        /// Will process elements under the _parent element by finding the best suitable 
        /// elements processor (the one which returns non-null value)
        /// </summary>
        /// <returns>
        /// The object which has evolved as the result of elements processor run, 
        /// or null, if the current element with its child nodes could not be read by any elements processor.
        /// </returns>
        public virtual object ProcessElements(XmlElement _parent_element, Type _declared_type, X2OConfig _config)
        {
            Type tgt_type = _declared_type;

            // consider target type
            // firstly we will look for the type atribute
            var type_attr = _parent_element.Attributes["type"];
            if (type_attr != null)
            {
                tgt_type = GetTypeByName(type_attr.Value, _config);
                if (tgt_type == null)
                    throw new ArgumentException($"Type name specified in type attribute ({type_attr.Value}) couldn't been resolved.");
                if (!_declared_type.IsAssignableFrom(tgt_type))
                    throw new ArgumentException($"Type name specified in type attribute ({type_attr.Value}, resolved as {tgt_type.FullName}) cannot be converted into declared type ({_declared_type.FullName}).");
            }

            foreach(var processor in ElementsProcessors.Reverse())
            {
                var result = processor.ProcessElements(_parent_element, tgt_type, _config);
                if (result != null)
                    return result;
            }

            return null;
        }
Exemple #7
0
        /// <summary>Creates new instance of the X2O config with adding the default elements processors</summary>
        public static X2OConfig                                                         WithDefaultElementsProcessors()
        {
            var cfg = new X2OConfig(GetDefaultElementsProcessors());

            return(cfg);
        }
Exemple #8
0
        /// <summary>Create new instance of the X2O config without any custom settings made</summary>
        public static X2OConfig                                                         EmptyConfig()
        {
            var cfg = new X2OConfig();

            return(cfg);
        }
Exemple #9
0
        /// <summary>
        /// Will read the XML file and returns instance of the read object completely initialized with values from XML file.
        /// </summary>
        /// <param name="_element">Usually the root node of the XML document to be deserialized and returned as object instance.</param>
        /// <param name="_target_type">The type which is the suppossed the be the result (or convertible into this type)</param>
        /// <param name="_config">Configurtion object of X2OReader</param>
        /// <returns>The instance of the object according to the type read from XML file</returns>
        public static object                                                            ReadFromXmlElement(XmlElement _element, Type _target_type, X2OConfig _config)
        {
            var result = _config.Processor.ProcessElements(_element, _target_type, _config);

            return(result);
        }
Exemple #10
0
        /// <summary>
        /// Will read the XML file and returns instance of the read object completely initialized with values from XML file.
        /// The Type generic argument is used as the declared type of the root node.
        /// </summary>
        /// <param name="_filename">The filename of the XML file to read into the object</param>
        /// <param name="_config">The configuration of X2O reader</param>
        /// <returns>The instance of the object according to the type read from XML file</returns>
        public static T                                                                         ReadFromFile <T>(string _filename, X2OConfig _config)
        {
            var root_node = GetXmlRootElementFromFile(_filename);
            var root_type = typeof(T);
            var result    = ReadFromXmlElement(root_node, root_type, _config);

            return((T)result);
        }
Exemple #11
0
        /// <summary>Will read the XML file and returns instance of the read object completely initialized with values from XML file</summary>
        /// <param name="_filename">The filename of the XML file to read into the object</param>
        /// <param name="_config">The configuration of X2O reader</param>
        /// <returns>The instance of the object according to the type read from XML file</returns>
        public static object                                                            ReadFromFile(string _filename, X2OConfig _config)
        {
            var root_node = GetXmlRootElementFromFile(_filename);
            var root_type = _config.Processor.GetTypeOfRoot(root_node, _config);
            var result    = ReadFromXmlElement(root_node, root_type, _config);

            return(result);
        }