/// <summary> /// The TranslateElements method iterates over a list of objects /// from an entity in the XML representation of a dictionary and /// translates them into a collection of VFX dictionary types. /// </summary> /// <param name="elements"> /// The list of objects from an entity in the XML representation /// of a VersaFix dictionary. /// </param> /// <returns> /// An instance of FixDxCollection that has been populated with /// the VersaFix dictionary types that correspond to the objects /// in the list of XML elements. /// </returns> private static FixDxCollection TranslateElements(List <object> elements) { FixDxCollection result = new FixDxCollection(); foreach (object element in elements) { if (element is XmlFixDxFieldReference) { XmlFixDxFieldReference src = element as XmlFixDxFieldReference; result.Add(new FixDxFieldReference(src.Name, src.Required)); } else if (element is XmlFixDxGroupReference) { XmlFixDxGroupReference src = element as XmlFixDxGroupReference; FixDxGroupReference dst = new FixDxGroupReference(src.Name, src.Required); foreach (IFixDxElement member in TranslateElements(src.Elements)) { dst.Elements.Add(member); } result.Add(dst); } else if (element is XmlFixDxBlockReference) { XmlFixDxBlockReference src = element as XmlFixDxBlockReference; result.Add(new FixDxBlockReference(src.Name, src.Required)); } } return(result); }
/// <summary> /// The Expand method iterates over all of the elements in /// a collection of dictionary elements and expands any of /// them that reference collections of other elements. /// </summary> /// <param name="source"> /// The collection of elements to expand. /// </param> /// <returns> /// A new instance of a collection of dictionary elements /// with all expandable references in the source collection /// having been recursively expanded. /// </returns> public FixDxCollection Expand(FixDxCollection source) { FixDxCollection result = new FixDxCollection(); foreach (IFixDxElement dxEntry in source) { if (dxEntry is FixDxBlockReference) { FixDxBlockReference blockReference = dxEntry as FixDxBlockReference; if (blockReference != null) { if (_mapBlocksByName.ContainsKey(blockReference.Name)) { FixDxBlock blockEntry = _mapBlocksByName[blockReference.Name]; FixDxCollection blockElements = Expand(blockEntry.Elements); foreach (IFixDxElement blockElement in blockElements) { if (blockElement is FixDxFieldReference) { FixDxFieldReference reference = blockElement as FixDxFieldReference; result.Add(reference); } else if (blockElement is FixDxBlockReference) { FixDxBlockReference reference = blockElement as FixDxBlockReference; result.Add(reference); } } } } } else { if (dxEntry is FixDxGroupReference) { FixDxGroupReference srcReference = dxEntry as FixDxGroupReference; FixDxGroupReference dstReference = new FixDxGroupReference(srcReference.Name, srcReference.Required); foreach (IFixDxElement srcElement in Expand(srcReference.Elements)) { dstReference.Elements.Add(srcElement); } result.Add(dstReference); } else if (dxEntry is FixDxFieldReference) { FixDxFieldReference element = dxEntry as FixDxFieldReference; result.Add(element); } } } return(result); }
/// <summary> /// The TranslateElements method converts a collection of /// dictionary elements into their corresponding representation /// as instances of XML serializable classes. The method only /// translates field, group, and block references, since those /// are the only elements that should be found in collections /// of dictionary elements under normal usage. /// </summary> /// <param name="dxElements"> /// The collection of field, group, and block references that /// is to be converted to XML serializable classes. /// </param> /// <returns> /// The resulting collection of XML serializable classes. /// </returns> private static XmlFixDxElements TranslateElements(FixDxCollection dxElements) { XmlFixDxElements result = new XmlFixDxElements(); foreach (IFixDxElement dxElement in dxElements) { if (dxElement is FixDxFieldReference) { FixDxFieldReference dxReference = dxElement as FixDxFieldReference; XmlFixDxFieldReference xmlReference = new XmlFixDxFieldReference(); xmlReference.Name = dxReference.Name; xmlReference.Required = dxReference.Required; result.Elements.Add(xmlReference); } else if (dxElement is FixDxGroupReference) { FixDxGroupReference dxReference = dxElement as FixDxGroupReference; XmlFixDxGroupReference xmlReference = new XmlFixDxGroupReference(); xmlReference.Name = dxReference.Name; xmlReference.Required = dxReference.Required; XmlFixDxElements xmlElements = TranslateElements(dxReference.Elements); foreach (object xmlElement in xmlElements.Elements) { xmlReference.Elements.Add(xmlElement); } result.Elements.Add(xmlReference); } else if (dxElement is FixDxBlockReference) { FixDxBlockReference dxReference = dxElement as FixDxBlockReference; XmlFixDxBlockReference xmlReference = new XmlFixDxBlockReference(); xmlReference.Name = dxReference.Name; xmlReference.Required = dxReference.Required; result.Elements.Add(xmlReference); } } return(result); }
/// <summary> /// The Resolve method attempts to resolve all of the /// element references in a collection to the entries /// that correspond to them. /// </summary> /// <param name="elements"> /// The collection of dictionary elements to resolve. /// </param> /// <returns> /// The resulting collection of resolved elements. /// </returns> /// <exception cref="ArgumentException"> /// Thrown if any elements in the collection that is /// supplied to the method cannot be resolved. /// </exception> public FixDxCollection Resolve(FixDxCollection elements) { FixDxCollection result = new FixDxCollection(); // REC: Iterate over all of the elements in the collection // and determine how to resolve each of them: foreach (IFixDxElement dxElement in elements) { if (dxElement is FixDxFieldReference) { FixDxFieldReference fieldReference = dxElement as FixDxFieldReference; result.Add(ResolveFieldReference(fieldReference)); } else if (dxElement is FixDxGroupReference) { FixDxGroupReference groupReference = dxElement as FixDxGroupReference; result.Add(ResolveGroupReference(groupReference)); } else if (dxElement is FixDxBlockReference) { FixDxBlockReference blockReference = dxElement as FixDxBlockReference; // REC: Determine what type of block the reference // is referring to (component or repeating): if (string.IsNullOrEmpty(blockReference.Name)) { string error = "The supplied block reference's name is null or empty."; throw new ArgumentException(error); } else if (!_mapBlocksByName.ContainsKey(blockReference.Name)) { string error = string.Format("The block reference {0} couldn't be resolved.", blockReference.Name); throw new ArgumentException(error); } else { FixDxBlock dxBlock = _mapBlocksByName[blockReference.Name]; if (dxBlock.Type == FixDxBlockTypes.Component) { foreach (IFixDxElement element in Resolve(Expand(dxBlock.Elements))) { result.Add(element); } } else if (dxBlock.Type == FixDxBlockTypes.Repeating) { // REC: Attempt to resolve the field that the repeating // block references as the start field for the group: if (string.IsNullOrEmpty(dxBlock.Field)) { string error = string.Format("Repeating Block {0}'s start field is null or empty.", dxBlock.Field); throw new ArgumentException(error); } else if (!_mapFieldsByName.ContainsKey(dxBlock.Field)) { string error = string.Format("Repeating block {0}'s start field can't be resolved.", dxBlock.Field); throw new ArgumentException(error); } else { FixDxField dxField = _mapFieldsByName[dxBlock.Field]; FixDxResolvedGroup dxGroup = new FixDxResolvedGroup(dxField.Tag, dxField.Name, false); foreach (IFixDxElement element in Resolve(Expand(dxBlock.Elements))) { dxGroup.Elements.Add(element); } result.Add(dxGroup); } } } } } // REC: Patch from RC - sanity check all elements in the result // to ensure that there are no unresolved references. foreach (IFixDxElement e in result) { if (e is FixDxFieldReference || e is FixDxGroupReference || e is FixDxFieldReference) { throw new Exception("unresolved references exist in the resolved collection"); } } return(result); }