// TODO: Get rid of the HashSet param if we're not using it private Boolean TryPopulateGroup( IElement group, ModelElement modelGroup, IEnumerator<Segment> segments, HashSet<String> matchedSegments) { List<IElement> itemsInGroup = new List<IElement>(modelGroup.Items.Length); Int32 i = 0, bookmark = 0; Boolean modelElementMatchedOnce = false; while (i < modelGroup.Items.Length) { Boolean didMatch = false; ModelElement modelElement = (ModelElement)modelGroup.Items[i]; // If the current model item is a group... if (modelElement is ModelSegmentGroup) { // Create a container for the message tree SegmentGroup g = new SegmentGroup(modelElement.Name); // Then try to populate the new group if (TryPopulateGroup(g, modelElement, segments, matchedSegments)) { itemsInGroup.Add(g); didMatch = true; } } // Else the current model item is a segment else { // If the current segment matches the current model // segment by name... if (segments.Current.Name == modelElement.Name) { // TODO: Check for a grouping definition here // and call a method to group segments itemsInGroup.Add(segments.Current); segments.MoveNext(); didMatch = true; } } // If we did match... if (didMatch) { if (!modelElement.CanRepeat) { i++; bookmark = i; modelElementMatchedOnce = false; } else modelElementMatchedOnce = true; } // ...else we didn't match else { // If it's the first iteration, consider the possibility that the parent group is // a better place to try matching. if (i == 0 && modelGroup.CanRepeat) break; // If the model element is required and wasn't matched on a prior iteration... // We may also need to check here if the parent wasn't matched on a prior iteration if (modelElement.IsRequired && !modelElementMatchedOnce) { if (! segments.MoveNext()) throw new Exception("Required element could not be matched."); } // Else if we've reached the end of the model group else if (i == modelGroup.Items.Length) { // Since we didn't match by peeking ahead to the end of the group, reset i to bookmarked value if (bookmark < i) i = bookmark; } // Otherwise, just move to the next element in the model group else { i++; modelElementMatchedOnce = false; } } // Always check if current is null and exit the while if (segments.Current == null) break; } group.Items = itemsInGroup.ToArray(); return itemsInGroup.Any(); }
// TODO: Get rid of the HashSet param if we're not using it private Boolean TryPopulateGroup( IElement group, ModelElement modelGroup, IEnumerator <Segment> segments, HashSet <String> matchedSegments) { List <IElement> itemsInGroup = new List <IElement>(modelGroup.Items.Length); Int32 i = 0, bookmark = 0; Boolean modelElementMatchedOnce = false; while (i < modelGroup.Items.Length) { Boolean didMatch = false; ModelElement modelElement = (ModelElement)modelGroup.Items[i]; // If the current model item is a group... if (modelElement is ModelSegmentGroup) { // Create a container for the message tree SegmentGroup g = new SegmentGroup(modelElement.Name); // Then try to populate the new group if (TryPopulateGroup(g, modelElement, segments, matchedSegments)) { itemsInGroup.Add(g); didMatch = true; } } // Else the current model item is a segment else { // If the current segment matches the current model // segment by name... if (segments.Current.Name == modelElement.Name) { // TODO: Check for a grouping definition here // and call a method to group segments itemsInGroup.Add(segments.Current); segments.MoveNext(); didMatch = true; } } // If we did match... if (didMatch) { if (!modelElement.CanRepeat) { i++; bookmark = i; modelElementMatchedOnce = false; } else { modelElementMatchedOnce = true; } } // ...else we didn't match else { // If it's the first iteration, consider the possibility that the parent group is // a better place to try matching. if (i == 0 && modelGroup.CanRepeat) { break; } // If the model element is required and wasn't matched on a prior iteration... // We may also need to check here if the parent wasn't matched on a prior iteration if (modelElement.IsRequired && !modelElementMatchedOnce) { if (!segments.MoveNext()) { throw new Exception("Required element could not be matched."); } } // Else if we've reached the end of the model group else if (i == modelGroup.Items.Length) { // Since we didn't match by peeking ahead to the end of the group, reset i to bookmarked value if (bookmark < i) { i = bookmark; } } // Otherwise, just move to the next element in the model group else { i++; modelElementMatchedOnce = false; } } // Always check if current is null and exit the while if (segments.Current == null) { break; } } group.Items = itemsInGroup.ToArray(); return(itemsInGroup.Any()); }