/src/Otis/MemberMappingDescriptor.cs

http://otis-lib.googlecode.com/ · C# · 205 lines · 133 code · 23 blank · 49 comment · 7 complexity · e5f809ddaada7cc162d6175533245b47 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using Otis.Parsing;
  5. namespace Otis
  6. {
  7. /// <summary>
  8. /// Describes a transformation for one field/property within a type transformation
  9. /// </summary>
  10. public class MemberMappingDescriptor
  11. {
  12. private string m_member;
  13. private string m_expression;
  14. private string m_format;
  15. private string m_nullValue;
  16. private Type m_type;
  17. private Type m_ownerType;
  18. private bool m_isArray;
  19. private bool m_isList;
  20. private AggregateMappingDescription m_aggregateDescriptor = null;
  21. ProjectionInfo m_projections = new ProjectionInfo();
  22. List<string> m_nullableParts = new List<string>();
  23. public MemberMappingDescriptor()
  24. {
  25. }
  26. public MemberMappingDescriptor(MemberMappingDescriptor copyFrom)
  27. {
  28. m_member = copyFrom.m_member;
  29. m_expression = copyFrom.m_expression;
  30. m_format = copyFrom.m_format;
  31. m_nullValue = copyFrom.m_nullValue;
  32. m_type = copyFrom.m_type;
  33. m_ownerType = copyFrom.m_ownerType;
  34. m_isArray = copyFrom.m_isArray;
  35. m_isList = copyFrom.m_isList;
  36. m_projections = copyFrom.m_projections;
  37. m_nullableParts = copyFrom.m_nullableParts;
  38. }
  39. /// <summary>
  40. /// Gets/sets name of the mapped field or property on the target class of transformation.
  41. /// </summary>
  42. public string Member
  43. {
  44. get { return m_member; }
  45. set { m_member = value; }
  46. }
  47. /// <summary>
  48. /// Gets/sets name of the expression to which <see cref="Member"/> is mapped.
  49. /// </summary>
  50. public string Expression
  51. {
  52. get { return m_expression; }
  53. set
  54. {
  55. m_expression = ExpressionParser.NormalizeExpression(value.Trim());
  56. UpdateNullableParts(m_expression);
  57. }
  58. }
  59. /// <summary>
  60. /// Gets/sets the value which will be assigned to target <see cref="Member"/> if the
  61. /// <see cref="Expression"/> is null
  62. /// </summary>
  63. public string NullValue
  64. {
  65. get { return m_nullValue; }
  66. set
  67. {
  68. if (value == null)
  69. m_nullValue = null;
  70. else
  71. m_nullValue = ExpressionParser.NormalizeExpression(value.Trim());
  72. }
  73. }
  74. /// <summary>
  75. /// Gets/sets the formatting string for dtring members
  76. /// </summary>
  77. public string Format
  78. {
  79. get { return m_format; }
  80. set { m_format = value; }
  81. }
  82. /// <summary>
  83. /// Gets/sets the type of the target member
  84. /// </summary>
  85. public Type Type
  86. {
  87. get { return m_type; }
  88. set { m_type = value; }
  89. }
  90. /// <summary>
  91. /// Gets/sets the type where the memeber is defined
  92. /// </summary>
  93. public Type OwnerType
  94. {
  95. get { return m_ownerType; }
  96. set { m_ownerType = value; }
  97. }
  98. /// <summary>
  99. /// Gets/sets whether the target member is an array
  100. /// </summary>
  101. public bool IsArray
  102. {
  103. get { return m_isArray; }
  104. set { m_isArray = value; }
  105. }
  106. /// <summary>
  107. /// Gets/sets whether the target member is an implementation of <see cref="ICollection{T}"/>
  108. /// </summary>
  109. public bool IsList
  110. {
  111. get { return m_isList; }
  112. set { m_isList = value; }
  113. }
  114. /// <summary>
  115. /// returns whether the mapping has the formatting string attached
  116. /// </summary>
  117. public bool HasFormatting
  118. {
  119. get { return !string.IsNullOrEmpty(m_format); }
  120. }
  121. /// <summary>
  122. /// returns whether the mapped expression is a aggregate expression
  123. /// </summary>
  124. public bool IsAggregateExpression
  125. {
  126. get { return ExpressionParser.IsAggregateExpression(Expression); }
  127. }
  128. /// <summary>
  129. /// Returns the descriptor for aggregate expression
  130. /// </summary>
  131. public AggregateMappingDescription AggregateMappingDescription
  132. {
  133. get
  134. {
  135. if (m_aggregateDescriptor == null)
  136. m_aggregateDescriptor = new AggregateMappingDescription(Expression, Type);
  137. return m_aggregateDescriptor;
  138. }
  139. }
  140. /// <summary>
  141. /// Returns the projections
  142. /// </summary>
  143. public ProjectionInfo Projections
  144. {
  145. get { return m_projections; }
  146. set { m_projections = value; }
  147. }
  148. /// <summary>
  149. /// Returns the list of nullable parts in the mapped expression
  150. /// </summary>
  151. public List<string> NullableParts
  152. {
  153. get { return m_nullableParts; }
  154. }
  155. /// <summary>
  156. /// returns whether there are parts in mapped expression which can have null value
  157. /// </summary>
  158. public bool HasNullableParts
  159. {
  160. get { return NullableParts.Count > 0; }
  161. }
  162. /// <summary>
  163. /// returns whether the mapping has a custom value if mapped expression is null
  164. /// </summary>
  165. public bool HasNullValue
  166. {
  167. get { return NullValue != null; }
  168. }
  169. private void UpdateNullableParts(string expression)
  170. {
  171. m_nullableParts.Clear();
  172. Regex regex = new Regex(@"(\$[A-Za-z]\w*(\(.*?\))?)");
  173. MatchCollection matches = regex.Matches(expression);
  174. if (matches.Count == 0)
  175. return;
  176. foreach (Match match in matches)
  177. {
  178. m_nullableParts.Add(match.Value);
  179. }
  180. }
  181. }
  182. }