/// <summary> /// Gets the fragment seeder for the specified fragment type ID, /// which is located in <c>seed:fragmentSeeders</c>. /// </summary> /// <param name="typeId">The fragment seeder type ID.</param> /// <returns>Seeder, or null if not found.</returns> /// <exception cref="ArgumentNullException">typeId</exception> public IFragmentSeeder GetFragmentSeeder(string typeId) { if (typeId == null) { throw new ArgumentNullException(nameof(typeId)); } var entry = ComponentFactoryConfigEntry.ReadComponentEntry( Configuration, "seed:fragmentSeeders", typeId); if (entry == null) { return(null); } SeedOptions options = GetSeedOptions(); IFragmentSeeder seeder = GetComponent <IFragmentSeeder>( typeId, entry.OptionsPath, false); if (seeder == null) { return(null); } seeder.SetSeedOptions(options); return(seeder); }
public void GetFragmentSeeder_Comment_Ok() { IFragmentSeeder seeder = _factory.GetFragmentSeeder( "seed.fr.net.fusisoft.comment"); Assert.NotNull(seeder); }
/// <summary> /// Creates and seeds a new part with its fragments. The fragment /// type comes from the part's role ID. /// </summary> /// <param name="item">The item this part should belong to.</param> /// <param name="roleId">The optional part role ID.</param> /// <param name="factory">The part seeder factory. This is used /// for layer parts, which need to seed a set of fragments.</param> /// <returns>A new part.</returns> /// <exception cref="ArgumentNullException">item or factory</exception> public override IPart GetPart(IItem item, string roleId, PartSeederFactory factory) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (_options == null || _options.MaxFragmentCount < 1) { return(null); } // get the base text part; nothing to do if none TokenTextPart textPart = item.Parts .OfType <TokenTextPart>() .FirstOrDefault(); if (textPart == null) { return(null); } // get the seeder; nothing to do if none string frTypeId = StripColonSuffix(roleId); IFragmentSeeder seeder = factory.GetFragmentSeeder("seed." + frTypeId); if (seeder == null) { return(null); } // get the layer part for the specified fragment type Type constructedType = typeof(TokenTextLayerPart <>) .MakeGenericType(seeder.GetFragmentType()); IPart part = (IPart)Activator.CreateInstance(constructedType); // seed metadata SetPartMetadata(part, frTypeId, item); // seed by adding fragments int count = Randomizer.Seed.Next(1, _options.MaxFragmentCount); IList <Tuple <string, string> > locAndTexts = PickLocAndTexts(textPart, count); // must invoke AddFragment via reflection, as the closed type // is known only at runtime foreach (var lt in locAndTexts) { ITextLayerFragment fr = seeder.GetFragment( item, lt.Item1, lt.Item2); if (fr != null) { constructedType.InvokeMember("AddFragment", BindingFlags.InvokeMethod, null, part, new[] { fr }); } } return(part); }