public LevelPrototype(ProcessContext context, IEnumerable <XElement> prototypeItems, IEnumerable <string> fieldNames)
            {
                _context = context;
                var currentLevelPrototype = new List <XElement>();

                // Items for which no content control. Add this items to the prototype if there are
                // items after them.
                var maybeNeedToAdd          = new List <XElement>();
                var numberingElementReached = false;

                foreach (var prototypeItem in prototypeItems)
                {
                    //search for first item with numbering
                    if (!numberingElementReached)
                    {
                        var paragraph = prototypeItem.DescendantsAndSelf(W.P).FirstOrDefault();
                        if (paragraph != null &&
                            ListItemRetriever.RetrieveListItem(
                                context.Document.NumberingPart, context.Document.StylesPart, paragraph)
                            .IsListItem)
                        {
                            numberingElementReached = true;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if ((!prototypeItem.FirstLevelDescendantsAndSelf(W.Sdt).Any() && prototypeItem.Value != "") ||
                        (prototypeItem
                         .FirstLevelDescendantsAndSelf(W.Sdt)
                         .Any(sdt => fieldNames.Contains(sdt.SdtTagName()))))
                    {
                        currentLevelPrototype.AddRange(maybeNeedToAdd);
                        currentLevelPrototype.Add(prototypeItem);
                    }
                    else
                    {
                        maybeNeedToAdd.Add(prototypeItem);
                    }
                }
                if (!currentLevelPrototype.Any())
                {
                    return;
                }

                PrototypeItems = currentLevelPrototype;


                if (fieldNames.Any(fn => !SdtTags.Contains(fn)))
                {
                    IsValid = false;
                    return;
                }

                IsValid        = true;
                PrototypeItems = currentLevelPrototype;
            }
Exemple #2
0
 internal ContentProcessor(ProcessContext context)
 {
     _processors = new List <IProcessor>
     {
         new FieldsProcessor(),
         new TableProcessor(context),
         new ListProcessor(context),
         new ImagesProcessor(context),
         new SingleProcessor(context),
     };
 }
            /// <summary>
            /// Creates prototype from list content control and fieldNames.
            /// </summary>
            /// <param name="context">Process context.</param>
            /// <param name="listContentControl">List content control element.</param>
            /// <param name="fieldNames"></param>
            public Prototype(ProcessContext context, XElement listContentControl, IEnumerable <string> fieldNames)
            {
                if (listContentControl == null)
                {
                    throw new ArgumentNullException("listContentControl");
                }
                _context = context;
                if (listContentControl.Name != W.Sdt)
                {
                    throw new Exception("List content control is not a content control element");
                }

                // All elements inside list control content are included to the prototype.

                var xElement = listContentControl.Element(W.SdtContent);

                if (xElement == null)
                {
                    IsValid = false;
                    return;
                }
                var tagsInPrototype = xElement.Elements()
                                      .DescendantsAndSelf(W.Sdt)
                                      .Select(sdt => sdt.SdtTagName())
                                      .ToList();

                // If any field not found return empty list.
                if (fieldNames.Any(fn => !tagsInPrototype.Contains(fn)))
                {
                    IsValid = false;
                    return;
                }

                // All elements inside list control content are included to the prototype.
                var element = listContentControl.Element(W.SdtContent);

                if (element == null)
                {
                    return;
                }

                IsValid        = true;
                PrototypeItems = element.Elements().ToList();
            }
 public TableProcessor(ProcessContext context)
 {
     _context = context;
 }
 public SingleProcessor(ProcessContext context)
 {
     _context = context;
 }
 public ImagesProcessor(ProcessContext context)
 {
     _context = context;
 }
 private LevelPrototype(ProcessContext context, IEnumerable <XElement> prototypeItems)
 {
     _context       = context;
     PrototypeItems = prototypeItems.ToList();
 }
 public ListProcessor(ProcessContext context)
 {
     _context = context;
 }