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