/// Determine if the specified object is empty and should not be saved if <see cref="SkipIfEmpty"/> is set public bool IsEmpty(object value) { if (value == null) { return(true); } IEnumerable e = (value as IEnumerable); if (e != null) { IEnumerator en = e.GetEnumerator(); if (en != null) { if (!AtLeastOneChildForNotEmpty || en.MoveNext()) { return(false); } } } // element is not empty if it has a not empty sub-element var props = value.GetType().GetProperties(); foreach (PropertyInfo c in props) { var ee = CustomAttributeHelper.First <XsElementAttribute>(c); if (ee != null && !ee.IsEmpty(c.GetValue(value, null))) { return(false); } } // Or any non-empty attributes object defValue = null; foreach (PropertyInfo c in props) { var n = XsAttributeAttribute.GetNames(c, false); if (n == null) { continue; } object v = c.GetValue(value, null); if (v != null) { if (defValue == null) { defValue = Utils.CreateInstance(value.GetType()); } object vdef = c.GetValue(defValue, null); if (!v.Equals(vdef)) { return(false); } } } return(true); }
/// <summary> /// Get property value. An exception is thrown if property with this name is not found /// </summary> /// <param name="name">Property name</param> /// <returns>Property value</returns> /// <exception cref="KeyNotFoundException">Thrown if property with the specified name is not found</exception> public object GetAttr(string name) { name = name ?? string.Empty; var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); foreach (PropertyInfo c in props) { var n = XsAttributeAttribute.GetNames(c, true); if (n != null) { foreach (var s in n) { if (string.Compare(s, name, StringComparison.OrdinalIgnoreCase) == 0) { return(c.GetValue(this, null)); } } } } throw new KeyNotFoundException("Property '" + name + "' not found"); }
/// <summary> /// Write attributes to the XmlWriter /// </summary> /// <param name="writer">XmlWriter where the attributes must be written</param> protected virtual void WriteAttributes(XmlWriter writer) { object defValue = null; foreach (PropertyInfo c in GetType().GetProperties()) { var n = XsAttributeAttribute.GetNames(c, false); if (hasText(c) || n == null) { continue; } if (defValue == null) { defValue = Utils.CreateInstance(GetType()); } object v = c.GetValue(this, null); if (v != null) { object vdef = c.GetValue(defValue, null); if (!v.Equals(vdef)) { writer.WriteStartAttribute(n[0]); if (v.GetType().IsEnum) { string[] se = v.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); writer.WriteValue(string.Join(" ", Array.ConvertAll <string, string>(se, Utils.LowercaseFirstLetter))); } else { writer.WriteValue(v); } writer.WriteEndAttribute(); } } } }
/// <summary> /// Called when XML Reader reads an attribute or a text field /// </summary> /// <param name="context">Context</param> /// <param name="attribute">Attribute name, or an empty string for element text</param> /// <param name="value">Attribute value</param> /// <param name="previouslyProcessed">List of previously processed attributes, to detect duplicate attributes. May be null if duplicate attributes are allowed.</param> /// <returns>true, if the attribute if correctly processed and false otherwise</returns> protected virtual bool ProcessAttribute(IXsContext context, string attribute, string value, IDictionary <string, bool> previouslyProcessed) { if (string.IsNullOrEmpty(attribute)) { return(processText(value)); } bool proc = false; foreach (PropertyInfo c in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty)) { var n = XsAttributeAttribute.GetNames(c, true); if (n == null) { continue; } foreach (var s in n) { if (string.Compare(s, attribute, StringComparison.OrdinalIgnoreCase) == 0) { SetValue(c, value); proc = true; } // Check that we aren't setting the same value twice if (previouslyProcessed != null && proc && !string.IsNullOrEmpty(attribute)) { previouslyProcessed.Add(s, true); } } if (proc) { break; } } return(proc); }
/// <summary> /// Returns a <see cref="System.String"/> that represents this instance. /// </summary> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> public override string ToString() { StringBuilder sb = new StringBuilder(); XsTypeAttribute v = CustomAttributeHelper.First <XsTypeAttribute>(GetType()); if (_elementName != null) { sb.Append(_elementName); } else { sb.Append((v == null) ? GetType().FullName : v.Name); } sb.Append("("); bool first = true; if (!string.IsNullOrEmpty(Id)) { sb.AppendFormat("id=\"{0}\"", Id); first = false; } object def = null; foreach (PropertyInfo pi in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty)) { var n = XsAttributeAttribute.GetNames(pi, false); if (n == null || n[0] == "id" || n[0] == "password") { continue; } object o = pi.GetValue(this, null); if (def == null) { def = Utils.CreateInstance(GetType()); } if (o == null || o.Equals(pi.GetValue(def, null)) || (n[0] == string.Empty && n.Length == 1)) { continue; } if (!first) { sb.Append(", "); } first = false; sb.Append(n[0]); sb.Append("="); sb.Append("\""); string s = o.ToString().Trim(); s = s.Replace("\r", "\\r"); s = s.Replace("\n", "\\n"); s = s.Replace("\t", "\\t"); int maxW = (n[0] == "from" || n[0] == "location") ? 50 : 30; var fw = (n[0] == "from" || n[0] == "location") ? FitWidthOption.EllipsisStart : FitWidthOption.EllipsisEnd; sb.Append(Utils.FitWidth(s, maxW, fw)); sb.Append("\""); } sb.Append(")"); return(sb.ToString()); }
/// <summary> /// Initialize action /// </summary> public virtual void Initialize() { foreach (PropertyInfo c in GetType().GetProperties()) { bool valSet = false; object val = null; foreach (XsElementAttribute e in CustomAttributeHelper.All <XsElementAttribute>(c)) { if (!valSet) { val = c.GetValue(this, null); valSet = true; } ScriptActionBase bl = val as ScriptActionBase; if (bl != null && bl._elementName == null) { bl._elementName = e.Name; } continue; } if (CustomAttributeHelper.Has <XsRequiredAttribute>(c)) { if (!valSet) { if (c.GetIndexParameters().Length > 0) { continue; } val = c.GetValue(this, null); } if (val == null) { throw new ParsingException(string.Format("Required attribute '{0}' is not set", XsAttributeAttribute.GetNames(c, false)[0])); } } if ((c.PropertyType == typeof(object) || c.PropertyType == typeof(string)) && !CustomAttributeHelper.Has <XsNotTransformed>(c) && !CustomAttributeHelper.Has <XmlIgnoreAttribute>(c) && c.GetSetMethod() != null) { if (!valSet) { if (c.GetIndexParameters().Length > 0) { continue; } val = c.GetValue(this, null); } if (val != null) { if (val is string) { Context.AssertGoodTransform((string)val, Transform); } else { Context.AssertGoodTransform(val.ToString(), Transform); } } } } }
private static List <XmlSchemaAttribute> generateAttributes(XmlSchema xmlSchema, Type type, Dictionary <Type, XmlSchemaType> xmlTypes, string ns) { var def = Utils.CreateInstance(type); var ret = new List <XmlSchemaAttribute>(); foreach (var pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty)) { var attnames = XsAttributeAttribute.GetNames(pi, false); if (attnames == null) { continue; } foreach (var nameAttr in attnames) { if (string.IsNullOrEmpty(nameAttr)) { continue; } XmlSchemaAttribute xsa = new XmlSchemaAttribute(); xsa.Name = nameAttr; if (pi.PropertyType == typeof(bool)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean).QualifiedName; } else if (pi.PropertyType == typeof(int)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Integer).QualifiedName; } else if (pi.PropertyType == typeof(float)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Float).QualifiedName; } else if (pi.PropertyType == typeof(double)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Double).QualifiedName; } else if (pi.PropertyType == typeof(uint)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.UnsignedInt).QualifiedName; } else if (pi.PropertyType == typeof(ulong)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.UnsignedLong).QualifiedName; } else if (pi.PropertyType == typeof(long)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Long).QualifiedName; } else if (pi.PropertyType == typeof(decimal)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Decimal).QualifiedName; } else if (pi.PropertyType.IsEnum) { if (!xmlTypes.ContainsKey(pi.PropertyType)) { createEnum(pi, xmlTypes, xmlSchema); } xsa.SchemaTypeName = new XmlQualifiedName(xmlTypes[pi.PropertyType].Name, ns); } else if (pi.PropertyType == typeof(string)) { xsa.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; } var req = CustomAttributeHelper.First <XsRequiredAttribute>(pi); if (req != null && (string.IsNullOrEmpty(req.Name) || req.Name == nameAttr)) { xsa.Use = XmlSchemaUse.Required; } else { object v = pi.GetValue(def, null); if (v != null) { string value = v.ToString(); if ((pi.PropertyType.IsPrimitive || pi.PropertyType.IsEnum)) { value = (value.Substring(0, 1).ToLower() + value.Substring(1)); } xsa.DefaultValue = value; } } ret.Add(xsa); } } return(ret); }