Ejemplo n.º 1
0
        public override XElement Parse(ITagProcessor parentProcessor, XElement startElement)
        {
            this.ValidateStartTag(startElement, StartTagName);
            var startTag = startElement;

            if (string.IsNullOrEmpty(startTag.Value))
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, "If"));
            }

            var endTag = this.FindEndTag(startTag);

            var content    = TraverseUtils.ElementsBetween(startTag, endTag).ToList();
            var expression = startTag.GetExpression();

            var ifTag = new IfTag
            {
                Conidition = expression,
                EndIf      = endTag,
                IfContent  = content,
                StartIf    = startTag,
            };

            var ifProcessor = new IfProcessor {
                Tag = ifTag
            };

            if (content.Any())
            {
                this.GoDeeper(ifProcessor, content.First(), endTag);
            }
            parentProcessor.AddProcessor(ifProcessor);

            return(endTag);
        }
Ejemplo n.º 2
0
        private XElement FindEndTag(XElement startTag)
        {
            var ifTagsOpened = 1;
            var current      = startTag;

            while (ifTagsOpened > 0 && current != null)
            {
                var nextTagElements = TraverseUtils.NextTagElements(current, new Collection <string> {
                    "if", "endif"
                }).ToList();
                foreach (var nextTagElement in nextTagElements)
                {
                    if (nextTagElement.IsTag("if"))
                    {
                        ifTagsOpened++;
                    }
                    else
                    {
                        if (--ifTagsOpened == 0)
                        {
                            return(nextTagElement);
                        }
                    }
                }
            }
            if (current == null)
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, "EndIf"));
            }
            return(current);
        }
Ejemplo n.º 3
0
 private static void ParseStructuredElement(RepeaterElement repeaterElement, string startTag, string endTag)
 {
     repeaterElement.EndTag      = FindEndTag(repeaterElement.StartTag, startTag, endTag);
     repeaterElement.TagElements = TraverseUtils.SecondElementsBetween(repeaterElement.StartTag,
                                                                       repeaterElement.EndTag)
                                   .Select(MakeElementCallback);
 }
Ejemplo n.º 4
0
        private static XElement FindEndTag(XElement startTag, string startTagName, string endTagName)
        {
            var startTagsOpened = 1;
            var current         = startTag;

            while (startTagsOpened > 0 && current != null)
            {
                var nextTagElements = TraverseUtils.NextTagElements(current, new Collection <string> {
                    startTagName, endTagName
                }).ToList();
                var index = -1;
                while ((index < nextTagElements.Count) && (startTagsOpened != 0))
                {
                    index++;
                    if (nextTagElements[index].IsTag(startTagName))
                    {
                        startTagsOpened++;
                    }
                    else
                    {
                        startTagsOpened--;
                    }
                }
                current = nextTagElements[index];
            }
            if (current == null)
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, endTagName));
            }
            return(current);
        }
Ejemplo n.º 5
0
        public override XElement Parse(ITagProcessor parentProcessor, XElement startElement)
        {
            this.ValidateStartTag(startElement, TagName);
            var endRepeater = TryGetRequiredTag(startElement, EndTagName);
            var itemsSource = startElement.GetExpression();

            if (string.IsNullOrEmpty(itemsSource))
            {
                throw new Exception(MessageStrings.ItemsAreEmpty);
            }

            IEnumerable <XElement> elementsBetween = TraverseUtils.ElementsBetween(startElement, endRepeater).ToList();
            var repeaterTag = new RepeaterTag
            {
                Source              = itemsSource,
                StartRepeater       = startElement,
                EndRepeater         = endRepeater,
                MakeElementCallback = MakeElementCallback
            };

            var repeaterProcessor = new RepeaterProcessor
            {
                RepeaterTag = repeaterTag,
            };

            if (elementsBetween.Any())
            {
                this.GoDeeper(repeaterProcessor, elementsBetween.First());
            }

            parentProcessor.AddProcessor(repeaterProcessor);

            return(endRepeater);
        }
        protected ICollection <XElement> TryGetRequiredTags(XElement startElement, string tagName)
        {
            var tags = TraverseUtils.NextTagElements(startElement, tagName).ToList();

            if (!tags.Any())
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, tagName));
            }
            return(tags);
        }
        protected XElement TryGetRequiredTag(XElement startElement, XElement endElement, string tagName)
        {
            var tag = TraverseUtils.TagElementsBetween(startElement, endElement, tagName).FirstOrDefault();

            if (tag == null)
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, tagName));
            }
            return(tag);
        }
Ejemplo n.º 8
0
        public XElement Parse(ItemRepeaterTag tag, IList <DataReader> dataReaders, XElement current = null)
        {
            var startElement = tag.StartItemRepeater;
            var endElement   = tag.EndItemRepeater;
            List <ItemRepeaterElement> itemRepeaterElements;

            itemRepeaterElements =
                this.MarkLastElements(
                    TraverseUtils.ElementsBetween(startElement, endElement).Select(MakeElementCallback).ToList())
                .ToList();

            var nestedRepeaters = this.MarkNotSeparatedRepeaters(this.GetAllNestedRepeaters(tag), itemRepeaterElements);

            if (startElement.Parent.Name == WordMl.ParagraphName)
            {
                startElement = startElement.Parent;
            }
            var flgDeleteStartEnd = current == null;

            current = current ?? startElement;
            var      repeaterElements = this.GetSiblingElements(itemRepeaterElements.ToList(), nestedRepeaters.ToList()).Where(sel => !(sel.XElement.IsTag("enditemrepeater") || sel.XElement.IsTag("itemrepeater")));
            XElement currentNested;

            for (var index = 1; index <= dataReaders.Count(); index++)
            {
                this.SetRepeaterElementsVisible(repeaterElements.ToList());
                ICollection <XElement> bisectElements;
                var ifCount = 1;
                var flg     = false;
                repeaterElements = this.MarkInvisibleElements(repeaterElements, dataReaders[index - 1], ref ifCount, ref flg, index == dataReaders.Count);
                if (ifCount != 1)
                {
                    throw new Exception("ItemIf error. Check REndIf count.");
                }
                current       = this.ProcessElements(repeaterElements, dataReaders[index - 1], current, null, index, out bisectElements);
                currentNested = this.ProcessNestedRepeaters(tag, dataReaders[index - 1], (!bisectElements.Any()) ? new List <XElement>()
                {
                    current
                } : bisectElements);
                if ((currentNested != null) && repeaterElements.Last().IsBeforeNestedRepeater)
                {
                    current = currentNested;
                }
            }
            if (flgDeleteStartEnd)
            {
                foreach (var itemRepeaterElement in itemRepeaterElements)
                {
                    itemRepeaterElement.XElement.Remove();
                }
                startElement.Remove();
                endElement.Remove();
            }
            return(current);
        }
        public virtual XElement Parse(ITagProcessor parentProcessor, XElement startElement)
        {
            var sdtElement = startElement.Descendants(WordMl.SdtName).FirstOrDefault();

            while (sdtElement != null)
            {
                sdtElement = this.ParseSdt(parentProcessor, sdtElement);
                sdtElement = TraverseUtils.NextTagElements(sdtElement).FirstOrDefault();
            }
            return(startElement);
        }
Ejemplo n.º 10
0
 private bool CheckNestedElementForContinue(ItemRepeaterElement firstItemRepeaterElement,
                                            ItemRepeaterElement currentItemRepeaterElement,
                                            XElement nestedRepeaterEndElement)
 {
     if (nestedRepeaterEndElement != null)
     {
         return
             (TraverseUtils.SecondElementsBetween(firstItemRepeaterElement.XElement, nestedRepeaterEndElement)
              .Contains(currentItemRepeaterElement.XElement));
     }
     return(false);
 }
Ejemplo n.º 11
0
        public TableTag Parse(XElement startElement, XElement endTableTag)
        {
            var itemsSource = startElement.Value;

            if (string.IsNullOrEmpty(itemsSource))
            {
                throw new Exception(string.Format(MessageStrings.TagNotFoundOrEmpty, "Items"));
            }

            var tag = new TableTag
            {
                TagTable    = startElement,
                TagEndTable = endTableTag,
                ItemsSource = itemsSource,
            };

            tag.MakeTableElementCallback = MakeTableElementCallback;
            int?dynamicRow = null;
            var rowCount   = 1;
            //var between = TraverseUtils.SecondElementsBetween(startElement, endTableTag);
            var tableElement = startElement.NextSdt(WordMl.TableName).FirstOrDefault(element => element.IsBefore(endTableTag));
            var rows         = TraverseUtils.SecondElementsBetween(startElement, endTableTag).Descendants(WordMl.TableRowName).Where(tr => tr.Parent.Equals(tableElement));

            //var tableElement = startElement.NextSdt(WordMl.TableName).FirstOrDefault(element => element.IsBefore(endTableTag));
            // TODO Extend sdt names in loop
            foreach (var tableRow in rows)
            {
                if (
                    tableRow.Descendants()
                    .Any(
                        el =>
                        el.IsTag(ItemIndexTagName) || el.IsTag(ItemTextTagName) || el.IsTag(ItemIfTagName) ||
                        el.IsTag(ItemHtmlContentTagName) || el.IsTag(ItemRepeaterTagName)))
                {
                    if (dynamicRow != null)
                    {
                        throw new Exception("Invalid template! Found several dynamic rows. (Or none dynamic rows)");
                    }
                    dynamicRow = rowCount;
                }
                rowCount++;
            }

            if (tableElement != null)
            {
                tag.Table = tableElement;
            }

            tag.DynamicRow = dynamicRow;
            return(tag);
        }
Ejemplo n.º 12
0
        public XElement Generate(ItemRepeaterTag tag, IEnumerable <DataReader> dataReaders, XElement previous = null, XElement parent = null, bool forcedElementSave = false)
        {
            var startElement         = tag.StartItemRepeater;
            var endElement           = tag.EndItemRepeater;
            var inlineWrapping       = CheckInlineWrappingMode(startElement, endElement);
            var itemRepeaterElements =
                TraverseUtils.SecondElementsBetween(startElement, endElement).Select(MakeElementCallback).ToList();
            var      flgCleanUpElements = previous == null;
            XElement current;

            if (inlineWrapping && flgCleanUpElements)
            {
                current = startElement;
            }
            else
            {
                if (previous == null)
                {
                    current = startElement.Parent.Name.Equals(WordMl.ParagraphName) ? startElement.Parent : startElement;
                }
                else
                {
                    current = previous;
                }
            }
            for (var index = 1; index <= dataReaders.Count(); index++)
            {
                XElement nestedRepeaterEndElementTmp = null;
                XElement endIfElementTmp             = null;
                current = this.ProcessElements(
                    itemRepeaterElements,
                    dataReaders.ElementAt(index - 1),
                    current,
                    (inlineWrapping && (parent != null)) ? parent : null,
                    index,
                    ref nestedRepeaterEndElementTmp,
                    ref endIfElementTmp,
                    inlineWrapping && (parent != null)
                    );
            }
            if (flgCleanUpElements && !forcedElementSave)
            {
                foreach (var itemRepeaterElement in itemRepeaterElements)
                {
                    itemRepeaterElement.XElement.Remove();
                }
                startElement.Remove();
                endElement.Remove();
            }
            return(current);
        }
        public static XElement ProcessItemTableElement(XElement startTableElement, XElement endTableElement,
                                                       DataReader dataReader)
        {
            var tableElement = TraverseUtils.SecondElementsBetween(startTableElement,
                                                                   endTableElement)
                               .SingleOrDefault(re => re.Name.Equals(WordMl.TableName));
            var tableContainer = new XElement("TempContainerElement");

            tableContainer.Add(startTableElement);
            tableContainer.Add(tableElement);
            tableContainer.Add(endTableElement);
            var itemTableGenerator = new ItemTableGenerator();

            itemTableGenerator.Generate(tableContainer.Elements().First(), tableContainer.Elements().Last(), dataReader);
            return(new XElement(tableContainer.Elements().SingleOrDefault()));
        }
Ejemplo n.º 14
0
        private static IEnumerable <TableElement> MakeTableElement(XElement startElement, XElement endElement)
        {
            ICollection <TableElement> tableElements = new Collection <TableElement>();
            var currentTagElement = TraverseUtils.NextTagElements(startElement).FirstOrDefault();

            while (currentTagElement != null && currentTagElement.IsBefore(endElement))
            {
                var tableElement = ToTableElement(currentTagElement);

                if (tableElement.IsItem || tableElement.IsIndex || tableElement.IsItemIf || tableElement.IsItemRepeater || tableElement.IsItemHtmlContent || tableElement.IsCellColor)
                {
                    tableElements.Add(tableElement);
                }
                if (tableElement.IsItemIf || tableElement.IsItemRepeater)
                {
                    currentTagElement = tableElement.EndTag;
                }
                currentTagElement = TraverseUtils.NextTagElements(currentTagElement).FirstOrDefault();
            }
            return(tableElements);
        }
        /// <summary>
        /// Do parsing
        /// </summary>
        public override XElement Parse(ITagProcessor parentProcessor, XElement startElement)
        {
            this.ValidateStartTag(startElement, "Table");

            if (parentProcessor == null)
            {
                throw new ArgumentNullException();
            }

            var endTableTag = this.TryGetRequiredTags(startElement, "EndTable").First();
            var coreParser  = new CoreTableParser(false);
            var tag         = coreParser.Parse(startElement, endTableTag);
            var processor   = new TableProcessor {
                TableTag = tag
            };

            if (TraverseUtils.ElementsBetween(startElement, endTableTag).Any())
            {
                this.GoDeeper(processor, TraverseUtils.ElementsBetween(startElement, endTableTag).First());
            }
            parentProcessor.AddProcessor(processor);

            return(endTableTag);
        }
Ejemplo n.º 16
0
        private XElement RenderDataReaders(ItemRepeaterTag tag, DataReader dataReader, XElement current)
        {
            var elements = TraverseUtils.ElementsBetween(tag.StartItemRepeater,
                                                         ((tag.EndItemRepeater.Parent.Name == WordMl.ParagraphName) &&
                                                          (!tag.EndItemRepeater.Parent.Elements().Contains(tag.StartItemRepeater)) &&
                                                          (tag.StartItemRepeater.Parent.Elements().Any(el => el.IsSdt() && !el.IsTag("itemrepeater")))
                                                         ) ? tag.EndItemRepeater.Parent : tag.EndItemRepeater)
                           .Select(MakeElementCallback)
                           .ToList();
            var processedElements = new List <ItemRepeaterElement>();

            if (elements.Count > 1)
            {
                processedElements.AddRange(this.CreateParagraphForInlineElements(elements));
            }
            else
            {
                processedElements.Add(elements[0]);
            }
            processedElements = this.SetRepeaterElementsVisible(processedElements.ToList()).ToList();
            var flgWrapInline = false;

            if (processedElements.Count == 1)
            {
                var rootContainer = processedElements.FirstOrDefault();
                if ((rootContainer != null) && rootContainer.XElement.Name.Equals(WordMl.ParagraphName))
                {
                    var originalContainer = elements[0].XElement.Parent;
                    var endItemRepeater   = originalContainer.Elements().FirstOrDefault(el => el.IsTag("enditemrepeater"));
                    if (endItemRepeater != null)
                    {
                        flgWrapInline = elements.All(e => e.XElement.Parent.Elements().Contains(endItemRepeater));
                    }
                }
            }
            var dataReaders = dataReader.GetReaders(tag.Source).ToList();

            for (var index = 1; index <= dataReaders.Count; index++)
            {
                ICollection <XElement> bisectElements;
                var ifCount = 1;
                var flg     = false;
                processedElements = this.MarkInvisibleElements(processedElements, dataReaders[index - 1], ref ifCount, ref flg, index == dataReaders.Count).ToList();
                if (ifCount != 1)
                {
                    throw new Exception("RItemIf error. Check REndIf count.");
                }
                var inlineWrapping = (index > 1) && flgWrapInline;
                if (inlineWrapping)
                {
                    current = current.Elements().Last();
                }
                current = this.ProcessElements(
                    inlineWrapping ? processedElements.First().Elements.ToList() : processedElements,
                    dataReaders[index - 1],
                    inlineWrapping ? null : current,
                    inlineWrapping ? current.Parent : null,
                    index,
                    out bisectElements,
                    (index > 1) && flgWrapInline);
            }
            return(current);
        }
Ejemplo n.º 17
0
        private IEnumerable <ItemRepeaterElement> GetSiblingElements(
            IEnumerable <ItemRepeaterElement> itemRepeaterElements, IEnumerable <ItemRepeaterTag> nestedRepeaters)
        {
            var repeaterElements = new List <ItemRepeaterElement>();
            int i = 0;

            if (!nestedRepeaters.Any())
            {
                repeaterElements.AddRange(itemRepeaterElements);
            }
            else
            {
                foreach (var itemRepeaterElement in itemRepeaterElements)
                {
                    var flagAdd = nestedRepeaters.All(nr =>
                    {
                        var elementsBetween = TraverseUtils.ElementsBetween(nr.StartItemRepeater,
                                                                            nr.EndItemRepeater.Parent != null && (nr.EndItemRepeater.Parent.Name == WordMl.ParagraphName)
                                                                                    ? nr.EndItemRepeater.Parent : nr.EndItemRepeater);
                        return(!elementsBetween.Any(element => element.Equals(itemRepeaterElement.XElement)));
                    });
                    if (flagAdd)
                    {
                        if (itemRepeaterElement.HasElements &&
                            (itemRepeaterElement.Elements.Any(ire => ire.IsItemRepeater) || itemRepeaterElement.Elements.Any(ire => ire.IsEndItemRepeater)) &&
                            itemRepeaterElement.XElement.Name.Equals(WordMl.ParagraphName))
                        {
                            var nestedElements = itemRepeaterElement.Elements.ToList();
                            var element        = new XElement(itemRepeaterElement.XElement.Name);
                            var dicBisect      = new Dictionary <XElement, ItemRepeaterElement>();
                            if (!nestedElements.Any(ne => ne.IsEndItemRepeater))
                            {
                                foreach (var nestedElement in nestedElements)
                                {
                                    element.Add(nestedElement.XElement);
                                    if (nestedElement.IsBeforeNestedRepeater)
                                    {
                                        dicBisect.Add(nestedElement.XElement, new ItemRepeaterElement()
                                        {
                                            XElement = nestedElement.NextNestedRepeater.XElement
                                        });
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var end  = nestedElements.First(ne => ne.IsEndItemRepeater);
                                var next = end.XElement;
                                while ((next = next.NextElement()) != null)
                                {
                                    element.Add(next);
                                }
                            }
                            var newItemRepeaterElement = MakeElementCallback(element);
                            var elements = newItemRepeaterElement.Elements.ToList();

                            foreach (var kv in dicBisect)
                            {
                                foreach (var repeaterElement in elements.Where(repeaterElement => XNode.DeepEquals(repeaterElement.XElement, kv.Key)))
                                {
                                    var val = new ItemRepeaterElement {
                                        XElement = new XElement(kv.Value.XElement)
                                    };
                                    repeaterElement.NextNestedRepeater = val;
                                }
                            }
                            newItemRepeaterElement.Elements = elements;
                            if (element.HasElements)
                            {
                                repeaterElements.Add(newItemRepeaterElement);
                            }
                        }
                        else
                        {
                            repeaterElements.Add(itemRepeaterElement);
                        }
                    }
                }
            }
            return(repeaterElements);
        }