private static void ScanEvents(ElementDef elementDef) { foreach (var eventInfo in elementDef.Type.GetEvents().Where(ei => ei.IsDefined(typeof(XmlEventAttribute), false))) { elementDef.AddAttribute(eventInfo.Name, eventInfo); } }
private static void ScanProperties(ElementDef elementDef) { foreach (var propertyInfo in elementDef.Type.GetProperties().Where(pi => pi.IsDefined(typeof(XmlPropertyAttribute), false))) { elementDef.AddAttribute(propertyInfo.Name, propertyInfo); } }
/// <summary> /// Parse element and call it's handler. /// </summary> /// <param name="parent">Parent control.</param> /// <returns>Control.</returns> public Gwen.Control.ControlBase ParseElement(Gwen.Control.ControlBase parent) { ElementDef elementDef; if (m_ElementHandlers.TryGetValue(m_Reader.Name, out elementDef)) { m_CurrentElement = elementDef; return(elementDef.Handler(this, elementDef.Type, parent)); } return(null); }
private static void ScanProperties(ElementDef elementDef) { foreach (var propertyInfo in elementDef.Type.GetProperties().Where(pi => pi.IsDefined(typeof(XmlPropertyAttribute), false))) { if (m_AttributeValueConverters.ContainsKey(propertyInfo.PropertyType)) { elementDef.AddAttribute(propertyInfo.Name, propertyInfo); } else { throw new XmlException(String.Format("No converter found for an attribute '{0}' value type '{1}'.", propertyInfo.Name, propertyInfo.PropertyType.Name)); } } }
internal void ParseComponentAttributes(Component component) { Type componentType = component.GetType(); Type viewType = component.View.GetType(); ElementDef componentDef = null; ElementDef viewDef = null; foreach (ElementDef elementDef in m_ElementHandlers.Values) { if (elementDef.Type == componentType) { componentDef = elementDef; } else if (elementDef.Type == viewType) { viewDef = elementDef; } } if (componentDef == null) { throw new XmlException("Component is not registered."); } if (viewDef == null) { throw new XmlException("Component view is not registered."); } if (m_Reader.HasAttributes) { while (m_Reader.MoveToNextAttribute()) { if (!SetAttributeValue(componentDef, component, m_Reader.Name, m_Reader.Value)) { if (!SetAttributeValue(viewDef, component.View, m_Reader.Name, m_Reader.Value)) { if (!SetComponentAttribute(component, m_Reader.Name, m_Reader.Value)) { throw new XmlException(String.Format("Attribute '{0}' not found.", m_Reader.Name)); } } } } m_Reader.MoveToElement(); } }
/// <summary> /// Register a XML element. All XML elements must be registered before usage. /// </summary> /// <param name="name">Name of the element.</param> /// <param name="type">Type of the control or component.</param> /// <param name="handler">Handler function for creating the control or component.</param> /// <returns>True if registered successfully or false is already registered.</returns> public static bool RegisterElement(string name, Type type, ElementHandler handler) { if (!m_ElementHandlers.ContainsKey(name)) { ElementDef elementDef = new ElementDef(type, handler); m_ElementHandlers[name] = elementDef; ScanProperties(elementDef); ScanEvents(elementDef); return(true); } return(false); }
private bool SetAttributeValue(ElementDef elementDef, object element, string attribute, string value) { MemberInfo memberInfo = elementDef.GetAttribute(attribute); if (memberInfo != null) { if (memberInfo is PropertyInfo) { return(SetPropertyValue(element, memberInfo as PropertyInfo, value)); } else if (memberInfo is EventInfo) { return(SetEventValue(element, memberInfo as EventInfo, value)); } } return(false); }
/// <summary> /// Parse typed element and call it's handler. /// </summary> /// <typeparam name="T">Control type to be created.</typeparam> /// <param name="parent">Parent control.</param> /// <returns>Control.</returns> public T ParseElement <T>(Gwen.Control.ControlBase parent) where T : Gwen.Control.ControlBase { Type type = typeof(T); XmlControlAttribute attrib = null; object[] attribs = type.GetCustomAttributes(typeof(XmlControlAttribute), false); if (attribs.Length > 0) { attrib = attribs[0] as XmlControlAttribute; } ElementDef elementDef; if (m_ElementHandlers.TryGetValue(attrib != null && attrib.ElementName != null ? attrib.ElementName : type.Name, out elementDef)) { if (elementDef.Type == type) { m_CurrentElement = elementDef; return(elementDef.Handler(this, elementDef.Type, parent) as T); } } return(null); }
private static XElement CreateXElementForType(ElementDef definition) { XNamespace xs = "http://www.w3.org/2001/XMLSchema"; XElement xele = new XElement(xs + "element", new XAttribute("name", definition.Type.Name)); string fqan = definition.Type.FullName; XElement membersNode = m_comments.Root.Element("members"); List <XElement> comments = membersNode.Elements("member").Where <XElement>(x => x.Attribute("name").Value.Contains(fqan)).ToList <XElement>(); if (comments.Count != 0) { XElement comment = comments.SingleOrDefault <XElement>(x => x.Attribute("name").Value == $"T:{fqan}"); if (comment != null) { xele.Add(new XElement(xs + "annotation", new XElement(xs + "documentation", comment.Element("summary").Value))); } } XElement content; if (definition.Type.Name == "ControlBase") { content = new XElement(xs + "complextType", new XAttribute("mixed", "true")); xele.Add(content); content.Add( new XElement(xs + "choice", new XAttribute("minOccurs", "0"), new XAttribute("maxOccurs", "unbounded"), new XElement(xs + "group", new XAttribute("ref", "controls")), new XElement(xs + "any", new XAttribute("maxOccurs", "unbounded"), new XAttribute("processContents", "lax")) )); } else { content = new XElement(xs + "extension", new XAttribute("base", "ControlBase")); xele.Add(new XElement(xs + "complextType", new XAttribute("mixed", "true"), new XElement(xs + "complexContent", content))); } foreach (KeyValuePair <string, MemberInfo> info in definition.Attributes) { if (definition.Type.Name != "ControlBase" && info.Value.DeclaringType == typeof(Gwen.Control.ControlBase)) { continue; } XElement xmi = new XElement(xs + "attribute", new XAttribute("name", info.Key), new XAttribute("type", "xs:text")); //assuming we only register events and properties string typeHint = info.Value.MemberType == MemberTypes.Event ? "E" : "P"; XElement miComment = comments.SingleOrDefault <XElement>(x => x.Attribute("name").Value == $"{typeHint}:{fqan}.{info.Key}"); if (miComment != null) { string mt = info.Value.MemberType.ToString(); string xmlComment = miComment.Element("summary").Value; string typeName = info.Value.ToString().Split(' ')[0]; List <XElement> ctorComments = membersNode.Elements("member").Where <XElement>(x => x.Attribute("name").Value.Contains($"M:{typeName}.#ctor")).ToList <XElement>(); string ctorDesc = ""; foreach (XElement ctorComment in ctorComments) { ctorDesc += ctorComment.Value + ","; } xmi.Add(new XElement(xs + "annotation", new XElement(xs + "documentation", $"{mt} {typeName}, Description: {xmlComment} ,{ctorDesc} "))); } content.Add(xmi); } return(xele); }