private static void WriteFromPosNodes(XmlWriter writer, ILcmReferenceCollection <IPartOfSpeech> fromPoSes, string sElementName) { if (fromPoSes == null || fromPoSes.Count < 1) { return; } foreach (IPartOfSpeech pos in fromPoSes) { writer.WriteStartElement(sElementName); writer.WriteAttributeString("fromCat", pos.Hvo.ToString(CultureInfo.InvariantCulture)); writer.WriteAttributeString("fromCatAbbr", pos.Abbreviation.BestAnalysisAlternative.Text); writer.WriteEndElement(); //sElementName } }
private static void WriteProductivityRestrictionNodes(XmlWriter writer, ILcmReferenceCollection <ICmPossibility> prodRests, string sElementName) { if (prodRests == null || prodRests.Count < 1) { return; } foreach (ICmPossibility pr in prodRests) { writer.WriteStartElement(sElementName); writer.WriteAttributeString("id", pr.Hvo.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString("name", pr.Name.BestAnalysisAlternative.Text); writer.WriteEndElement(); //sElementName } }
// This function is generic because some lists, like ILexSense.AnthroCodesRC, are lists of interfaces *derived* // from ICmPossibility (e.g., ICmAnthroItem). This results in type errors at compile time: parameter // types like ILcmReferenceCollection<ICmPossibility> don't match ILcmReferenceCollection<ICmAnthroCode>. // Generics solve the problem, and can be automatically inferred by the compiler to boot. public void SetPossibilitiesCollection <T>(ILcmReferenceCollection <T> dest, IEnumerable <T> newItems) where T : class, ICmPossibility { // If we know of NO valid possibility keys, don't make any changes. That's because knowing of NO valid possibility keys // is FAR more likely to happen because of a bug than because we really removed an entire possibility list, and if there's // a bug, we shouldn't drop all the Lcm data for this possibility list. if (PossibilitiesByKey.Count == 0 && _canonicalSource == null) { return; } // We have to calculate the update (which items to remove and which to add) here; ILcmReferenceCollection won't do it for us. List <T> itemsToAdd = newItems.ToList(); HashSet <Guid> guidsToAdd = new HashSet <Guid>(itemsToAdd.Select(poss => poss.Guid)); List <T> itemsToRemove = new List <T>(); foreach (T poss in dest) { if (!guidsToAdd.Contains(poss.Guid)) { itemsToRemove.Add(poss); } } dest.Replace(itemsToRemove, itemsToAdd); }
// Assumption: "source" contains valid keys. CAUTION: No error checking is done to ensure that this is true. public void UpdatePossibilitiesFromStringArray <T>(ILcmReferenceCollection <T> dest, LfStringArrayField source) where T : class, ICmPossibility { SetPossibilitiesCollection(dest, FromStringArrayField <T>(source)); }