Пример #1
0
        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);
        }
Пример #2
0
        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;
                }
            }
        }