PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ResourceManagement.Client/RmFactory.cs

#
C# | 225 lines | 186 code | 11 blank | 28 comment | 38 complexity | 5c1f2702f0c193c3abb4578bafe5199d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Schema;
  4. using System.Xml;
  5. using System.Text;
  6. using Microsoft.ResourceManagement.ObjectModel;
  7. namespace Microsoft.ResourceManagement.Client
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public class RmFactory
  13. {
  14. public XmlSchemaSet RmSchema;
  15. protected const String RmNamespace = "http://schemas.microsoft.com/2006/11/ResourceManagement";
  16. protected Dictionary<RmAttributeName, RmAttributeInfo> RmAttributeCache;
  17. protected Dictionary<String, Dictionary<RmAttributeName, RmAttributeInfo>> RmObjectCache;
  18. protected XmlDocument RmDoc;
  19. protected XmlNamespaceManager RmNsManager;
  20. /// <summary>
  21. /// Creates an indexable cache of Resource Management attributes and objects based on the given schema set.
  22. /// </summary>
  23. /// <param name="rmSchema"></param>
  24. public RmFactory(XmlSchemaSet rmSchema)
  25. {
  26. if (rmSchema == null)
  27. {
  28. throw new ArgumentNullException("rmSchema");
  29. }
  30. lock (rmSchema)
  31. {
  32. this.RmSchema = rmSchema;
  33. if (this.RmSchema.IsCompiled == false)
  34. {
  35. this.RmSchema.Compile();
  36. }
  37. this.RmAttributeCache = new Dictionary<RmAttributeName, RmAttributeInfo>();
  38. this.RmObjectCache = new Dictionary<string, Dictionary<RmAttributeName, RmAttributeInfo> >();
  39. this.RmDoc = new XmlDocument();
  40. this.RmNsManager = new XmlNamespaceManager(this.RmDoc.NameTable);
  41. this.RmNsManager.AddNamespace("rm", RmNamespace);
  42. foreach (XmlSchemaObject schemaObj in this.RmSchema.GlobalTypes.Values)
  43. {
  44. XmlSchemaComplexType schemaObjComplexType = schemaObj as XmlSchemaComplexType;
  45. if (schemaObjComplexType != null)
  46. {
  47. if (schemaObjComplexType.Name == null || schemaObjComplexType.Particle == null)
  48. {
  49. continue;
  50. }
  51. RmObjectCache[schemaObjComplexType.Name] = new Dictionary<RmAttributeName, RmAttributeInfo>();
  52. XmlSchemaSequence schemaObjSequence = schemaObjComplexType.Particle as XmlSchemaSequence;
  53. if (schemaObjSequence != null)
  54. {
  55. foreach (XmlSchemaObject sequenceObj in schemaObjSequence.Items)
  56. {
  57. XmlSchemaElement sequenceElement = sequenceObj as XmlSchemaElement;
  58. if (sequenceElement != null)
  59. {
  60. RmAttributeInfo info = new RmAttributeInfo();
  61. if (sequenceElement.MaxOccurs > Decimal.One)
  62. {
  63. info.IsMultiValue = true;
  64. }
  65. if (sequenceElement.MinOccurs > Decimal.Zero)
  66. {
  67. info.IsRequired = true;
  68. }
  69. String attributeTypeName = sequenceElement.ElementSchemaType.QualifiedName.Name.ToUpperInvariant();
  70. if (attributeTypeName.Contains("COLLECTION"))
  71. {
  72. info.IsMultiValue = true;
  73. }
  74. if (attributeTypeName.Contains("REFERENCE"))
  75. {
  76. info.AttributeType = RmAttributeType.Reference;
  77. }
  78. else if (attributeTypeName.Contains("BOOLEAN"))
  79. {
  80. info.AttributeType = RmAttributeType.Boolean;
  81. }
  82. else if (attributeTypeName.Contains("INTEGER"))
  83. {
  84. info.AttributeType = RmAttributeType.Integer;
  85. }
  86. else if (attributeTypeName.Contains("DATETIME"))
  87. {
  88. info.AttributeType = RmAttributeType.DateTime;
  89. }
  90. else if (attributeTypeName.Contains("BINARY"))
  91. {
  92. info.AttributeType = RmAttributeType.Binary;
  93. }
  94. else
  95. {
  96. info.AttributeType = RmAttributeType.String;
  97. }
  98. RmAttributeName attributeName = new RmAttributeName(sequenceElement.Name);
  99. RmObjectCache[schemaObjComplexType.Name][attributeName] = info;
  100. RmAttributeCache[attributeName] = info;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// DO NOT USE THIS METHOD -- FOR TESTING ONLY!
  110. /// </summary>
  111. /// <param name="attributeName"></param>
  112. /// <returns></returns>
  113. public bool IsMultiValued(RmAttributeName attributeName)
  114. {
  115. RmAttributeInfo retValue = null;
  116. RmAttributeCache.TryGetValue(attributeName, out retValue);
  117. if (retValue == null)
  118. {
  119. return false;
  120. }
  121. else
  122. {
  123. return retValue.IsMultiValue;
  124. }
  125. }
  126. /// <summary>
  127. /// DO NOT USE THIS METHOD -- FOR TESTING ONLY!
  128. /// </summary>
  129. /// <param name="attributeName"></param>
  130. /// <returns></returns>
  131. public bool IsReference(RmAttributeName attributeName)
  132. {
  133. RmAttributeInfo retValue = null;
  134. RmAttributeCache.TryGetValue(attributeName, out retValue);
  135. if (retValue == null)
  136. {
  137. return false;
  138. }
  139. else
  140. {
  141. return retValue.AttributeType == RmAttributeType.Reference;
  142. }
  143. }
  144. /// <summary>
  145. /// DO NOT USE THIS METHOD -- FOR TESTING ONLY!
  146. /// </summary>
  147. /// <param name="objectType"></param>
  148. /// <param name="attributeName"></param>
  149. /// <returns></returns>
  150. public bool IsRequired(String objectType, RmAttributeName attributeName)
  151. {
  152. Dictionary<RmAttributeName, RmAttributeInfo> attributeValue = null;
  153. RmObjectCache.TryGetValue(objectType, out attributeValue);
  154. if (attributeValue == null)
  155. {
  156. return false;
  157. }
  158. else
  159. {
  160. RmAttributeInfo attributeInfo = null;
  161. attributeValue.TryGetValue(attributeName, out attributeInfo);
  162. if (attributeInfo == null)
  163. {
  164. return false;
  165. }
  166. else
  167. {
  168. return attributeInfo.IsRequired;
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// DO NOT USE THIS METHOD! IT IS FOR TESTING ONLY!
  174. /// </summary>
  175. /// <param name="objectType"></param>
  176. /// <returns></returns>
  177. public List<RmAttributeName> RequiredAttributes(String objectType)
  178. {
  179. List<RmAttributeName> retList = new List<RmAttributeName>();
  180. Dictionary<RmAttributeName, RmAttributeInfo> attributeValue = null;
  181. RmObjectCache.TryGetValue(objectType, out attributeValue);
  182. if (attributeValue == null)
  183. {
  184. return retList;
  185. }
  186. else
  187. {
  188. foreach(KeyValuePair<RmAttributeName, RmAttributeInfo> pair in attributeValue)
  189. {
  190. if (pair.Value.IsRequired)
  191. {
  192. retList.Add(pair.Key);
  193. }
  194. }
  195. return retList;
  196. }
  197. }
  198. protected class RmAttributeInfo
  199. {
  200. public bool IsMultiValue;
  201. public bool IsRequired;
  202. public RmAttributeType AttributeType;
  203. }
  204. protected enum RmAttributeType
  205. {
  206. String,
  207. Reference,
  208. DateTime,
  209. Integer,
  210. Binary,
  211. Boolean
  212. }
  213. }
  214. }