private void ProcessTermSetGroup(XElement xmlNode, LocalTermStore termStore) { string name = this.GetRequiredAttributeValue(xmlNode, TaxmlSpec.NameToken); if (TaxmlSpec.IsReservedName(name)) { // TODO: Handle system groups here throw new NotImplementedException(); } Guid id = this.GetGuidAttributeValue(xmlNode, TaxmlSpec.IdToken) ?? Guid.Empty; LocalTermGroup termGroup = new LocalTermGroup(id, name, termStore.DefaultLanguageLcid); this.ReadTaxmlComments(xmlNode, termGroup); string description = this.GetAttributeValue(xmlNode, TaxmlSpec.DescriptionToken); if (description != null) { termGroup.Description = description; } // Add the group to the term store termStore.AddTermGroup(termGroup); bool processedDescription = false; foreach (XElement childNode in xmlNode.Elements()) { switch (childNode.Name.LocalName) { case TaxmlSpec.DescriptionToken: if (processedDescription) { throw new ParseException("The description cannot be specified more than once", childNode); } processedDescription = true; termGroup.Description = childNode.Value; break; case TaxmlSpec.TermSetToken: this.ProcessTermSet(childNode, termGroup); break; case TaxmlSpec.SyncActionToken: this.ProcessSyncAction(childNode, termGroup); break; default: throw new ParseException("Unimplemented XML tag \"" + childNode.Name.LocalName + "\"", childNode); } } }
public LocalTermStore LoadFromFile(string csvFileName) { LocalTermStore termStore = new LocalTermStore(Guid.Empty, "Term Store"); LocalTermGroup termGroup = termStore.AddTermGroup(Guid.Empty, Path.GetFileNameWithoutExtension(csvFileName)); using (CsvReader csvReader = new CsvReader(csvFileName)) { csvReader.WrapExceptions(() => { ProcessCsvLines(termGroup, csvReader); }); } return(termStore); }
private void ProcessTermSet(XElement xmlNode, LocalTermGroup termGroup) { string name = this.GetRequiredAttributeValue(xmlNode, TaxmlSpec.NameToken); Guid id = this.GetGuidAttributeValue(xmlNode, TaxmlSpec.IdToken) ?? Guid.Empty; if (TaxmlSpec.IsReservedName(name)) { if (id != Guid.Empty) { throw new ParseException( "The \"ID\" attribute may not be used with a reserved name such as \"" + name + "\"", xmlNode); } if (!termGroup.IsSystemGroup) { throw new ParseException( "The reserved TermSet \"" + name + "\" should be used inside the TermSetGroup with name \"" + TaxmlSpec.SystemGroupReservedName + "\".", xmlNode); } if (StringComparer.OrdinalIgnoreCase.Equals(name, TaxmlSpec.OrphanedTermsTermSetReservedName)) { // TODO: Handle system term sets throw new NotImplementedException(); } else if (StringComparer.OrdinalIgnoreCase.Equals(name, TaxmlSpec.KeywordsTermSetReservedName)) { // TODO: Handle system term sets throw new NotImplementedException(); } else { throw new InvalidOperationException("Unrecognized reserved name \"" + name + "\""); } } if (id == null) { id = Guid.NewGuid(); } LocalTermSet termSet = new LocalTermSet(id, name, termGroup.DefaultLanguageLcid); this.ReadTaxmlComments(xmlNode, termSet); bool?isAvailableForTaggning = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.IsAvailableForTaggingToken); if (isAvailableForTaggning.HasValue) { if (termGroup.IsSystemGroup) { throw new ParseException("The " + TaxmlSpec.IsAvailableForTaggingToken + " attribute is not supported for system term sets", xmlNode); } termSet.IsAvailableForTagging = isAvailableForTaggning.Value; } bool?isOpenForTermCreation = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.IsOpenForTermCreationToken); if (isOpenForTermCreation.HasValue) { if (termGroup.IsSystemGroup) { throw new ParseException("The " + TaxmlSpec.IsOpenForTermCreationToken + " attribute is not supported for system term sets", xmlNode); } termSet.IsOpenForTermCreation = isOpenForTermCreation.Value; } string owner = this.GetAttributeValue(xmlNode, TaxmlSpec.OwnerToken); if (owner != null) { termSet.Owner = owner; } string contact = this.GetAttributeValue(xmlNode, TaxmlSpec.ContactToken); if (contact != null) { termSet.Contact = contact; } // Add the term set to the group termGroup.AddTermSet(termSet); var inOrderList = new List <Guid>(); foreach (XElement childNode in xmlNode.Elements()) { switch (childNode.Name.LocalName) { case TaxmlSpec.LocalizedNameToken: int lcid = this.GetLcidFromLanguageAttribute(childNode, termSet); termSet.SetName(childNode.Value, lcid); break; case TaxmlSpec.DescriptionToken: // TermSet.Description is not localized termSet.Description = childNode.Value; break; case TaxmlSpec.CustomPropertyToken: this.ProcessCustomProperty(termSet.CustomProperties, childNode); break; case TaxmlSpec.CustomSortOrderToken: this.ProcessCustomSortOrder(termSet.CustomSortOrder, childNode); break; case TaxmlSpec.StakeHolderToken: termSet.AddStakeholder(childNode.Value); break; case TaxmlSpec.TermToken: this.ProcessTerm(childNode, termSet, inOrderList, isTermLink: false); break; case TaxmlSpec.TermLinkToken: this.ProcessTerm(childNode, termSet, inOrderList, isTermLink: true); break; case TaxmlSpec.SyncActionToken: this.ProcessSyncAction(childNode, termSet); break; default: throw new ParseException("Unimplemented XML tag \"" + childNode.Name.LocalName + "\"", childNode); } } this.ProcessInOrderList(termSet, inOrderList, xmlNode); }
protected override void SetParentItem(LocalTaxonomyItem value) { this.ParentItem = (LocalTermGroup)value; }
private static void ProcessCsvLines(LocalTermGroup termGroup, CsvReader csvReader) { int columnTermSetName = csvReader.GetColumnIndex("Term Set Name"); int columnTermSetDescription = csvReader.GetColumnIndex("Term Set Description"); int columnLcid = csvReader.GetColumnIndex("LCID"); int columnAvailableForTagging = csvReader.GetColumnIndex("Available for Tagging"); int columnTermDescription = csvReader.GetColumnIndex("Term Description"); int[] columnTermLabels = { csvReader.GetColumnIndex("Level 1 Term"), csvReader.GetColumnIndex("Level 2 Term"), csvReader.GetColumnIndex("Level 3 Term"), csvReader.GetColumnIndex("Level 4 Term"), csvReader.GetColumnIndex("Level 5 Term"), csvReader.GetColumnIndex("Level 6 Term"), csvReader.GetColumnIndex("Level 7 Term") }; LocalTermSet termSet = null; while (csvReader.ReadNextLine()) { // Is it a blank row? if (csvReader.Values.All(x => string.IsNullOrWhiteSpace(x))) { // Yes, skip it continue; } string termSetName = csvReader[columnTermSetName].Trim(); string termSetDescription = csvReader[columnTermSetDescription].Trim(); string lcidString = csvReader[columnLcid].Trim(); int currentLcid; if (!int.TryParse(lcidString, out currentLcid)) { currentLcid = termGroup.DefaultLanguageLcid; } bool availableForTagging = true; if (!string.IsNullOrWhiteSpace(csvReader[columnAvailableForTagging])) { availableForTagging = bool.Parse(csvReader[columnAvailableForTagging]); } string termDescription = csvReader[columnTermDescription].Trim(); var termLabels = new List <string>(); for (int i = 0; i < columnTermLabels.Length; ++i) { int columnIndex = columnTermLabels[i]; if (columnIndex < csvReader.Values.Count) { string value = csvReader[columnIndex].Trim(); if (string.IsNullOrEmpty(value)) { break; } termLabels.Add(value); } } if (!string.IsNullOrEmpty(termSetName)) { termSet = termGroup.AddTermSet(Guid.Empty, termSetName); termSet.SetName(termSetName, currentLcid); termSet.Description = termSetDescription; } if (termSet != null) { LocalTermContainer parent = termSet; if (termLabels.Count == 0) { throw new Exception("Missing term label"); } foreach (string parentTermLabel in termLabels.Take(termLabels.Count - 1)) { LocalTerm newParent; if (!parent.Terms.TryGetByName(parentTermLabel, currentLcid, out newParent)) { throw new Exception("No match found for parent term \"" + parentTermLabel + "\""); } parent = newParent; } string termLabel = termLabels.Last(); LocalTerm term = parent.AddTerm(Guid.Empty, termLabel); term.SetName(termLabel, currentLcid); term.SetDescription(termDescription, currentLcid); term.IsAvailableForTagging = availableForTagging; } } }