private static InlineDescription GetInlineDescription(XmlText text)
        {
            var value = text.Value;

            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            var inlineDescription = new InlineDescription
            {
                Type = InlineType.Run, Text = value
            };

            return(inlineDescription);
        }
        private static Inline GetInline(FrameworkElement element, InlineDescription description)
        {
            Style style = null;
            if (!string.IsNullOrEmpty(description.StyleName))
            {
                style = element.FindResource(description.StyleName) as Style;
                if (style == null)
                    throw new InvalidOperationException("The style '" + description.StyleName + "' cannot be found");
            }

            Inline inline = null;
            switch (description.Type)
            {
                case InlineType.Run:
                    var run = new Run(description.Text);
                    inline = run;
                    break;

                case InlineType.LineBreak:
                    var lineBreak = new LineBreak();
                    inline = lineBreak;
                    break;

                case InlineType.Span:
                    var span = new Span();
                    inline = span;
                    break;

                case InlineType.Bold:
                    var bold = new Bold();
                    inline = bold;
                    break;

                case InlineType.Italic:
                    var italic = new Italic();
                    inline = italic;
                    break;

                case InlineType.Hyperlink:
                    var hyperlink = new Hyperlink();
                    inline = hyperlink;
                    break;

                case InlineType.Underline:
                    var underline = new Underline();
                    inline = underline;
                    break;
            }

            if (inline != null)
            {
                var span = inline as Span;
                if (span != null)
                {
                    var childInlines =
                        description.Inlines.Select(inlineDescription => GetInline(element, inlineDescription))
                            .Where(childInline => childInline != null)
                            .ToList();

                    span.Inlines.AddRange(childInlines);
                }

                if (style != null)
                    inline.Style = style;
            }

            return inline;
        }
        private static InlineDescription GetInlineDescription(XmlText text)
        {
            var value = text.Value;
            if (string.IsNullOrEmpty(value))
                return null;

            var inlineDescription = new InlineDescription
            {
                Type = InlineType.Run,
                Text = value
            };
            return inlineDescription;
        }
        private static InlineDescription GetInlineDescription(XmlElement element)
        {
            InlineType type;
            var elementName = element.Name.ToLower();
            switch (elementName)
            {
                case "run":
                    type = InlineType.Run;
                    break;

                case "linebreak":
                    type = InlineType.LineBreak;
                    break;

                case "span":
                    type = InlineType.Span;
                    break;

                case "bold":
                    type = InlineType.Bold;
                    break;

                case "italic":
                    type = InlineType.Italic;
                    break;

                case "hyperlink":
                    type = InlineType.Hyperlink;
                    break;

                case "underline":
                    type = InlineType.Underline;
                    break;

                default:
                    return null;
            }

            string styleName = null;
            var attribute = element.GetAttributeNode("style");
            if (attribute != null)
                styleName = attribute.Value;

            string text = null;
            var childDescriptions = new List<InlineDescription>();

            if (type == InlineType.Run || type == InlineType.LineBreak)
            {
                text = element.InnerText;
            }
            else
            {
                childDescriptions.AddRange(
                    element.ChildNodes.Cast<XmlNode>()
                        .Select(GetInlineDescription)
                        .Where(childDescription => childDescription != null));
            }

            var inlineDescription = new InlineDescription
            {
                Type = type,
                StyleName = styleName,
                Text = text,
                Inlines = childDescriptions.ToArray()
            };

            return inlineDescription;
        }
Beispiel #5
0
        /// <summary>
        /// Gets the inline description from an element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The inline description.</returns>
        private static InlineDescription GetInlineDescription(XmlElement element)
        {
            InlineType type;
            var        elementName = element.Name.ToLower();

            switch (elementName)
            {
            case "run":
                type = InlineType.Run;
                break;

            case "linebreak":
                type = InlineType.LineBreak;
                break;

            case "span":
                type = InlineType.Span;
                break;

            case "bold":
                type = InlineType.Bold;
                break;

            case "italic":
                type = InlineType.Italic;
                break;

            case "hyperlink":
                type = InlineType.Hyperlink;
                break;

            case "underline":
                type = InlineType.Underline;
                break;

            default:
                return(null);
            }

            string styleName = null;
            var    attribute = element.GetAttributeNode("style");

            if (attribute != null)
            {
                styleName = attribute.Value;
            }

            string text = null;
            var    childDescriptions = new List <InlineDescription>();

            if (type == InlineType.Run || type == InlineType.LineBreak)
            {
                text = element.InnerText;
            }
            else
            {
                foreach (XmlNode childNode in element.ChildNodes)
                {
                    var childDescription = GetInlineDescription(childNode);
                    if (childDescription == null)
                    {
                        continue;
                    }

                    childDescriptions.Add(childDescription);
                }
            }

            var inlineDescription = new InlineDescription
            {
                Type      = type,
                StyleName = styleName,
                Text      = text,
                Inlines   = childDescriptions.ToArray()
            };

            return(inlineDescription);
        }
Beispiel #6
0
        /// <summary>
        /// Gets the inline.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="description">The description.</param>
        /// <returns>The inline.</returns>
        /// <exception cref="InvalidOperationException">The style '" + description.StyleName + "' cannot be found</exception>
        private static Inline GetInline(FrameworkElement element, InlineDescription description)
        {
            Style style = null;

            if (!string.IsNullOrEmpty(description.StyleName))
            {
                style = element.FindResource(description.StyleName) as Style;
                if (style == null)
                {
                    throw new InvalidOperationException("The style '" + description.StyleName + "' cannot be found");
                }
            }

            Inline inline = null;

            switch (description.Type)
            {
            case InlineType.Run:
                var run = new Run(description.Text);
                inline = run;
                break;

            case InlineType.LineBreak:
                var lineBreak = new LineBreak();
                inline = lineBreak;
                break;

            case InlineType.Span:
                var span = new Span();
                inline = span;
                break;

            case InlineType.Bold:
                var bold = new Bold();
                inline = bold;
                break;

            case InlineType.Italic:
                var italic = new Italic();
                inline = italic;
                break;

            case InlineType.Hyperlink:
                var hyperlink = new Hyperlink();
                inline = hyperlink;
                break;

            case InlineType.Underline:
                var underline = new Underline();
                inline = underline;
                break;
            }

            if (inline != null)
            {
                var span = inline as Span;
                if (span != null)
                {
                    var childInlines = new List <Inline>();
                    foreach (var inlineDescription in description.Inlines)
                    {
                        var childInline = GetInline(element, inlineDescription);
                        if (childInline == null)
                        {
                            continue;
                        }

                        childInlines.Add(childInline);
                    }

                    span.Inlines.AddRange(childInlines);
                }

                if (style != null)
                {
                    inline.Style = style;
                }
            }

            return(inline);
        }
Beispiel #7
0
        private static Inline GetInline(FrameworkElement element, InlineDescription description)
        {
            Style style = null;

            if (!string.IsNullOrEmpty(description.StyleName))
            {
                style = element.FindResource(description.StyleName) as Style;
                if (style == null)
                {
                    throw new InvalidOperationException("The style '" + description.StyleName + "' cannot be found");
                }
            }

            Inline inline = null;

            switch (description.Type)
            {
            case InlineType.Run:
                Run run = new(description.Text);
                inline = run;
                break;

            case InlineType.LineBreak:
                LineBreak lineBreak = new();
                inline = lineBreak;
                break;

            case InlineType.Span:
                Span span = new();
                inline = span;
                break;

            case InlineType.Bold:
                Bold bold = new();
                inline = bold;
                break;

            case InlineType.Italic:
                Italic italic = new();
                inline = italic;
                break;

            case InlineType.Hyperlink:
                Hyperlink hyperlink = new();
                inline = hyperlink;
                break;

            case InlineType.Underline:
                Underline underline = new();
                inline = underline;
                break;
            }

            if (inline != null)
            {
                if (inline is Span span)
                {
                    List <Inline> childInlines = description.Inlines
                                                 .Select(inlineDescription =>
                                                         GetInline(element, inlineDescription))
                                                 .Where(childInline => childInline != null).ToList();

                    span.Inlines.AddRange(childInlines);
                }

                if (style != null)
                {
                    inline.Style = style;
                }
            }

            return(inline);
        }