Exemple #1
0
        /// <summary>
        /// Sets the cache.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tarObject">The tar object.</param>
        protected void SetCache <T>(object tarObject) where T : class
        {
            var type = typeof(T);
            var xmlEntityCacheAttribute = XmlEntityUtility.GetXmlEntityCacheAttribute(type);

            if (null == xmlEntityCacheAttribute)
            {
                return;
            }
            //var id = typeof(T);
            var lifeCycle = xmlEntityCacheAttribute.LifeCycle;

            XmlEntityCacheWorker.Instance.Set(type, tarObject, lifeCycle);
        }
Exemple #2
0
        /// <summary>
        /// Parses the data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlContent">Content of the XML.</param>
        /// <returns>List{``0}.</returns>
        protected List <T> ParseData <T>(string xmlContent) where T : class
        {
            var res  = new List <T>();
            var type = typeof(T);

#if DEBUG
            Console.WriteLine("Real-time processing {0}, thread: {1}", type, Thread.CurrentThread.Name);
#endif
            var attribute = XmlEntityUtility.GetXmlEntityHandlerAttribute(type);
            var result    = attribute.EntityParser.Invoke(xmlContent, type);
            var flag      = XmlEntityUtility.GetXmlEntityAttribute(type).XmlEntityFlag;
            var isSingle  = false;
            switch (flag)
            {
            case XmlEntityFlags.Base:
            case XmlEntityFlags.Nested:
            case XmlEntityFlags.Single | XmlEntityFlags.Base:
            case XmlEntityFlags.Single | XmlEntityFlags.Nested:
            case XmlEntityFlags.Nested | XmlEntityFlags.Base | XmlEntityFlags.Single:
                isSingle = true;
                break;
            }
            //判断是否实体集
            if (!isSingle)
            {
                var items = result as object[];
                if (items != null)
                {
                    foreach (var item in items)
                    {
                        res.Add(item as T);
                    }
                }
                //尝试设置缓存
                SetCache <T>(res);
                return(res);
            }
            res.Add(result as T);
            //尝试设置缓存
            SetCache <T>(res);
            return(res);
        }
Exemple #3
0
        /// <summary>
        /// Parses the specified XML content.
        /// </summary>
        /// <param name="xmlContent">Content of the XML.</param>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns>List{``0}.</returns>
        /// <exception cref="System.Xml.XmlException">无效的Xml格式, + ex.Message</exception>
        public virtual object Parse(string xmlContent, Type entityType)
        {
            var xmlDoc = new XmlDocument();

            try
            {
                //移除命名空间
                xmlContent = Regex.Replace(xmlContent, @"(xmlns:?[^=]*=[""][^""]*[""])", string.Empty,
                                           RegexOptions.IgnoreCase | RegexOptions.Multiline);
                xmlDoc.LoadXml(xmlContent);
            }
            catch (Exception ex)
            {
                throw new XmlException("无效的Xml格式," + ex.Message);
            }
            var xmlEntityAttribute = XmlEntityUtility.GetXmlEntityAttribute(entityType);

            switch (xmlEntityAttribute.XmlEntityFlag)
            {
            case XmlEntityFlags.Base | XmlEntityFlags.Multiple:
            case XmlEntityFlags.Base | XmlEntityFlags.Nested | XmlEntityFlags.Multiple:
            case XmlEntityFlags.Nested | XmlEntityFlags.Multiple:
                //提取节点列表
                return(Parse(xmlDoc, xmlEntityAttribute.XPath, entityType));

            case XmlEntityFlags.Base:
            case XmlEntityFlags.Base | XmlEntityFlags.Single:
            case XmlEntityFlags.Nested:
            case XmlEntityFlags.Nested | XmlEntityFlags.Single:
            case XmlEntityFlags.Base | XmlEntityFlags.Nested | XmlEntityFlags.Single:
                //提取单个节点
                var xmlNode = xmlDoc.SelectSingleNode(xmlEntityAttribute.XPath);
                return(xmlNode == null ? null : Parse(xmlNode, entityType));
            }
            return(null);
        }
Exemple #4
0
        /// <summary>
        /// Extracts the item.
        /// </summary>
        /// <param name="xmlNode">The XML node.</param>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns>System.Object.</returns>
        public virtual object Parse(XmlNode xmlNode, Type entityType)
        {
            if (xmlNode == null)
            {
                return(null);
            }
            //创建实体
            var entity = Activator.CreateInstance(entityType);
            var pros   = entityType.GetProperties();

            foreach (var propertyInfo in pros)
            {
                //获取属性的特性
                var proAttribute =
                    (XmlEntityNodeAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlEntityNodeAttribute));
                if (proAttribute == null)
                {
                    //忽略未标注特性的属性
                    continue;
                }
                string value = null;
                //需要搜索根节点的某个属性值
                if (proAttribute.NodeAttributeType == XmlEntityNodeFlags.RootNodeAttribute)
                {
                    if (xmlNode.Attributes == null)
                    {
                        continue;
                    }
                    var arrRootConllection = xmlNode.Attributes[proAttribute.Name];
                    if (arrRootConllection != null)
                    {
                        value = arrRootConllection.Value;
                    }
                    XmlEntityUtility.SetEntityValue(entity, value, propertyInfo);
                    continue;
                }
                //需要搜索子节点的某个属性值
                if (proAttribute.NodeAttributeType == XmlEntityNodeFlags.SubNodeAttribute)
                {
                    //通过XPath搜索节点
                    var selectSingleNode = xmlNode.SelectSingleNode(proAttribute.XPath);
                    if (selectSingleNode == null)
                    {
                        continue;
                    }
                    if (selectSingleNode.Attributes == null)
                    {
                        continue;
                    }
                    var arrSubConllection = selectSingleNode.Attributes[proAttribute.Name];
                    if (arrSubConllection != null)
                    {
                        value = arrSubConllection.Value;
                    }
                    XmlEntityUtility.SetEntityValue(entity, value, propertyInfo);
                    continue;
                }
                //if (proAttribute.NodeAttributeType != XmlEntityNodeFlags.NormalNode)
                //{
                //    throw new Exception("未知的XmlEntity的节点,无法处理。数据:" + proAttribute.NodeAttributeType);
                //}
                if (proAttribute.NodeAttributeType != XmlEntityNodeFlags.NormalNode)
                {
                    continue;
                }
                //正常搜索节点
                //判断节点的特性
                var proType = propertyInfo.PropertyType;
                //判断是否是嵌套节点
                if (!XmlEntityUtility.IsNestedNode(proType))
                {
                    //通过XPath搜索节点
                    var selectSingleNode = xmlNode.SelectSingleNode(proAttribute.XPath);
                    if (selectSingleNode == null)
                    {
                        continue;
                    }
                    value = selectSingleNode.InnerText;
                    XmlEntityUtility.SetEntityValue(entity, value, propertyInfo);
                    continue;
                }
                //尝试获取嵌套节点的元素的实体类型
                var kwnAttribute = (XmlEntityKnownTypeAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlEntityKnownTypeAttribute));
                if (kwnAttribute == null)
                {
                    //忽略未标注XmlEntityKnownTypeAttribute特性的属性
                    continue;
                }
                //检查是否有嵌套列表
                if (XmlEntityUtility.IsNestedSingleNode(proType))
                {
                    //嵌套的单节点处理
                    var node = xmlNode.SelectSingleNode(proAttribute.XPath);
                    //循环到嵌套的实体子节点
                    var valueObject = Parse(node, kwnAttribute.KnownType);
                    XmlEntityUtility.SetEntityValue(entity, valueObject, propertyInfo, kwnAttribute.KnownType);
                    continue;
                }
                //嵌套的多节点处理
                var nodes = xmlNode.SelectNodes(proAttribute.XPath);
                if (nodes == null)
                {
                    continue;
                }
                var valueObjectList = new List <Object>();
                foreach (XmlNode node in nodes)
                {
                    //当前映射节点名称与待处理的节点名称相同时则开始处理该节点属性
                    if (node.Name == proAttribute.Name)
                    {
                        var subEntity = Parse(node, kwnAttribute.KnownType);
                        //添加实体到列表
                        if (subEntity != null)
                        {
                            valueObjectList.Add(subEntity);
                        }
                        continue;
                    }
                    //有子节点处理
                    var valueObject = Parse(node, proAttribute.Name, kwnAttribute.KnownType);
                    foreach (var item in valueObject)
                    {
                        valueObjectList.Add(item);
                    }
                }
                var valueObjects = valueObjectList.Count <= 0 ? null : valueObjectList.ToArray();
                XmlEntityUtility.SetEntityValue(entity, valueObjects, propertyInfo, kwnAttribute.KnownType);
            }
            return(entity);
        }
Exemple #5
0
        /// <summary>
        /// Determines whether the specified key has cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns><c>true</c> if the specified key has cache; otherwise, <c>false</c>.</returns>
        protected bool HasCache(Type key)
        {
            var xmlEntityCacheAttribute = XmlEntityUtility.GetXmlEntityCacheAttribute(key);

            return(null != xmlEntityCacheAttribute);
        }