コード例 #1
0
        private string generateCookieKey(XmlNode node)
        {
            string key = String.Empty;

            if (node.Name.ToLower() == "control")
            {
                key = node.FirstChild.Value + "_" + generateCookieKey(node.ParentNode);
            }
            else if (node.Name.ToLower() == "tab")
            {
                key = node.Attributes.GetNamedItem("caption").Value;
            }

            return(Casing.SafeAlias(key.ToLower()));
        }
コード例 #2
0
        /// <summary>
        /// Creates a collection of nodes with the type specified from the XML
        /// </summary>
        /// <param name="elements">The elements.</param>
        /// <returns>Collecton of .NET types from the XML</returns>
        internal IEnumerable <DocTypeBase> DynamicNodeCreation(IEnumerable <XElement> elements)
        {
            // TODO: investigate this method for performance bottlenecks
            // TODO: dataContext knows the types, maybe can load from there?
            List <DocTypeBase> ancestors = new List <DocTypeBase>();

            foreach (var ancestor in elements)
            {
                var alias      = Casing.SafeAlias(ancestor.Name.LocalName);
                var t          = KnownTypes[alias];
                var instaceOfT = (DocTypeBase)Activator.CreateInstance(t); //create an instance of the type and down-cast so we can use it
                this.LoadFromXml(ancestor, instaceOfT);
                instaceOfT.Provider = this;
                ancestors.Add(instaceOfT);
                yield return(instaceOfT);
            }
        }
コード例 #3
0
        private static string GenerateTypeName(string alias)
        {
            string s = Casing.SafeAlias(alias);

            return(s[0].ToString().ToUpper() + s.Substring(1, s.Length - 1));
        }
コード例 #4
0
        /// <summary>
        /// Loads from XML.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml">The XML.</param>
        /// <param name="node">The node.</param>
        public void LoadFromXml <T>(XElement xml, T node) where T : DocTypeBase
        {
            if (!ReflectionAssistance.CompareByAlias(node.GetType(), xml))
            {
                throw new DocTypeMissMatchException(xml.Name.LocalName, ReflectionAssistance.GetUmbracoInfoAttribute(node.GetType()).Alias);
            }

            node.Id           = (int)xml.Attribute("id");
            node.ParentNodeId = (int)xml.Attribute("parentID");
            node.NodeName     = (string)xml.Attribute("nodeName");
            node.Version      = (string)xml.Attribute("version");
            node.CreateDate   = (DateTime)xml.Attribute("createDate");
            node.SortOrder    = (int)xml.Attribute("sortOrder");
            node.UpdateDate   = (DateTime)xml.Attribute("updateDate");
            node.CreatorID    = (int)xml.Attribute("creatorID");
            node.CreatorName  = (string)xml.Attribute("creatorName");
            node.WriterID     = (int)xml.Attribute("writerID");
            node.WriterName   = (string)xml.Attribute("writerName");
            node.Level        = (int)xml.Attribute("level");
            node.TemplateId   = (int)xml.Attribute("template");

            var properties = node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttributes(typeof(PropertyAttribute), true).Count() > 0);

            foreach (var p in properties)
            {
                var attr = ReflectionAssistance.GetUmbracoInfoAttribute(p);

                XElement propertyXml = xml.Element(Casing.SafeAlias(attr.Alias));
                string   data        = null;
                //if the XML doesn't contain the property it means that the node hasn't been re-published with the property
                //so then we'll leave the data at null, otherwise let's grab it
                //Check if the propertyXml and propertyXml.FirstNode aren't null. If they are we don't need to set the value.
                if (propertyXml != null && propertyXml.FirstNode != null)
                {
                    //If the FirstNode is an XElement it means the property contains inner xml and we should return it. Otherwise just return the normal value.
                    if (propertyXml.FirstNode is XElement)
                    {
                        var reader = propertyXml.CreateReader();
                        reader.MoveToContent();
                        data = reader.ReadInnerXml();
                    }
                    else
                    {
                        data = propertyXml.Value;
                    }
                }

                if (p.PropertyType.IsValueType && p.PropertyType.GetGenericArguments().Length > 0 && typeof(Nullable <>).IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition()))
                {
                    if (string.IsNullOrEmpty(data))
                    {
                        //non-mandatory structs which have no value will be null
                        try
                        {
                            p.SetValue(node, null, null);
                        }
                        catch (FormatException ex)
                        {
                            throw new FormatException(
                                      string.Format("Unable to cast '{0}' to the appropriate type ({1}) for node `{2}`. The alias of the property being parsed is {3}. Refer to inner exception for more details", data, p.PropertyType.FullName, node.Id, attr.Alias), ex);
                        }
                    }
                    else
                    {
                        //non-mandatory structs which do have a value have to be cast based on the type of their Nullable<T>, found from the first (well, only) GenericArgument
                        try
                        {
                            p.SetValue(node, Convert.ChangeType(data, p.PropertyType.GetGenericArguments()[0]), null);
                        }
                        catch (FormatException ex)
                        {
                            throw new FormatException(
                                      string.Format("Unable to cast '{0}' to the appropriate type ({1}) for node `{2}`. The alias of the property being parsed is {3}. Refer to inner exception for more details", data, p.PropertyType.FullName, node.Id, attr.Alias), ex);
                        }
                    }
                }
                else
                {
                    // TODO: Address how Convert.ChangeType works in globalisation
                    try
                    {
                        p.SetValue(node, Convert.ChangeType(data, p.PropertyType), null);
                    }
                    catch (FormatException ex)
                    {
                        throw new FormatException(
                                  string.Format("Unable to cast '{0}' to the appropriate type ({1}) for node `{2}`. The alias of the property being parsed is {3}. Refer to inner exception for more details", data, p.PropertyType.FullName, node.Id, attr.Alias), ex);
                    }
                }
            }
        }