示例#1
0
 public void DocTree()
 {
     ServerUtils.ApplyArgs(args, reporter);
     if (Parse() && Resolve())
     {
         var docTree = new DocTree(dafnyProgram).GetDocTree();
         Console.WriteLine("DOCTREE_START " + ConvertToJson(docTree) + " DOCTREE_END");
     }
     else
     {
         Console.WriteLine("DOCTREE_START [] DOCTREE_END");
     }
 }
        private Inline CreateGrid(DocTree tree)
        {
            Grid g = new Grid();
            int  rowMax = 0; int colMax = 0;
            Dictionary <int, Dictionary <int, RichTextBox> > cells = new Dictionary <int, Dictionary <int, RichTextBox> >();

            foreach (var child in tree.Children)
            {
                Span span = new Span();
                DocTreeToTextElement(child, span.Inlines);

                int row = child.Row - 1;
                int col = child.Column - 1;
                if (!cells.ContainsKey(col))
                {
                    cells.Add(col, new Dictionary <int, RichTextBox>());
                }
                var column = cells[col];
                if (!column.ContainsKey(row))
                {
                    var spanel = new RichTextBox()
                    {
                        BorderThickness = new Thickness(0),
                        IsReadOnly      = true
                    };
                    spanel.SetValue(Grid.ColumnProperty, col);
                    spanel.SetValue(Grid.RowProperty, row);
                    column.Add(row, spanel);
                    g.Children.Add(spanel);
                }
                RichTextBox tb = column[row];
                tb.Selection.Insert(span);
                rowMax = (int)Math.Max(rowMax, child.Row);
                colMax = (int)Math.Max(colMax, child.Column);
            }
            for (int i = 0; i < rowMax; i++)
            {
                g.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < colMax; i++)
            {
                g.ColumnDefinitions.Add(new ColumnDefinition());
            }
            InlineUIContainer c = new InlineUIContainer()
            {
                Child = g
            };

            return(c);
        }
 private void DocTreeToTextElement(DocTree tree, InlineCollection coll)
 {
     if (tree.Element is Table)
     {
         coll.Add(new LineBreak());
         AddElementToCollection(CreateGrid(tree), coll);
     }
     else
     {
         AddElementToCollection(tree.Element, coll);
         if (!tree.HasChildren)
         {
             return;
         }
         foreach (var child in tree.Children)
         {
             DocTreeToTextElement(child, coll);
         }
     }
 }
 private void DocTreeToTextElement(DocTree tree, InlineCollection coll)
 {
     if (tree.Element is Table)
     {
         coll.Add(new LineBreak());
         AddElementToCollection(CreateGrid(tree), coll);
     }
     else
     {
         AddElementToCollection(tree.Element, coll);
         if (!tree.HasChildren)
             return;
         foreach (var child in tree.Children)
         {
             DocTreeToTextElement(child, coll);
         }
     }
 }
        private Inline CreateGrid(DocTree tree)
        {
            Grid g = new Grid();
            int rowMax = 0; int colMax = 0;
            Dictionary<int, Dictionary<int, RichTextBox>> cells = new Dictionary<int, Dictionary<int, RichTextBox>>();
            foreach (var child in tree.Children)
            {
                Span span = new Span();
                DocTreeToTextElement(child, span.Inlines);

                int row = child.Row - 1;
                int col = child.Column - 1;
                if (!cells.ContainsKey(col))
                    cells.Add(col, new Dictionary<int, RichTextBox>());
                var column = cells[col];
                if (!column.ContainsKey(row))
                {
                    var spanel = new RichTextBox()
                    {
                        BorderThickness = new Thickness(0),
                        IsReadOnly = true
                    };
                    spanel.SetValue(Grid.ColumnProperty, col);
                    spanel.SetValue(Grid.RowProperty, row);
                    column.Add(row, spanel);
                    g.Children.Add(spanel);
                }
                RichTextBox tb = column[row];
                tb.Selection.Insert(span);
                rowMax = (int)Math.Max(rowMax, child.Row);
                colMax = (int)Math.Max(colMax, child.Column);
            }
            for (int i = 0; i < rowMax; i++)
                g.RowDefinitions.Add(new RowDefinition());
            for (int i = 0; i < colMax; i++)
                g.ColumnDefinitions.Add(new ColumnDefinition());
            InlineUIContainer c = new InlineUIContainer()
            {
                Child = g
            };
            return c;
        }
        private void ParseAndSetSpecifiedText(string text)
        {
            // Clear the collection of Inlines
            SelectAll();
            this.Selection.Text = "";
            // Wrap the input in a <div> (so even plain text becomes valid XML)
            using (var stringReader = new StringReader(string.Concat("<div>", text, "</div>")))
            {
                DocTree RootElement = new DocTree(new Span(), 0, 0) { FontSize = 10 };
                DocTree currentElement = RootElement;
                // Read the input
                using (var xmlReader = XmlReader.Create(stringReader))
                {
                    // State variables
                    var bold = 0;
                    var italic = 0;
                    var underline = 0;
                    var td = 0;
                    var tr = 0;
                    Stack<double> fontSize = new Stack<double>();
                    fontSize.Push(10);
                    System.Collections.Generic.Stack<TextElement> elementTree = new System.Collections.Generic.Stack<TextElement>();
                    string link = null;
                    var lastElement = elementP;
                    // Read the entire XML DOM...
                    while (xmlReader.Read())
                    {
                        var nameUpper = xmlReader.Name.ToUpper();
                        switch (xmlReader.NodeType)
                        {
                            case XmlNodeType.Element:
                                // Handle the begin element
                                switch (nameUpper)
                                {
                                    case "H1":
                                        fontSize.Push(24);
                                        break;
                                    case "H2":
                                        fontSize.Push(18);
                                        break;
                                    case "H3":
                                        fontSize.Push(14);
                                        break;
                                    case "H4":
                                        fontSize.Push(12);
                                        break;
                                    case elementA:
                                        link = "";
                                        // Look for the HREF attribute (can't use .MoveToAttribute because it's case-sensitive)
                                        if (xmlReader.MoveToFirstAttribute())
                                        {
                                            do
                                            {
                                                if ("HREF" == xmlReader.Name.ToUpper())
                                                {
                                                    // Store the link target
                                                    link = xmlReader.Value;
                                                    break;
                                                }
                                            } while (xmlReader.MoveToNextAttribute());
                                        }
                                        break;
                                    case elementB:
                                    case elementSTRONG: bold++; break;
                                    case elementI:
                                    case elementEM: italic++; break;
                                    case elementU: underline++; break;
                                    case elementBR:
                                        currentElement.Children.Add(new DocTree(new LineBreak(), tr, td));
                                        break;
                                    //Selection.Insert(new LineBreak() ); break;
                                    case elementTR:
                                        tr++; td = 0; break;
                                    case elementTD:
                                        td++; break;
                                    case "TH":
                                        td++;
                                        bold++;
                                        break;
                                    case elementTABLE:
                                        DocTree p = new DocTree(new Table(), tr, td);
                                        currentElement.Children.Add(p);
                                        tr = 0; td = 0;
                                        currentElement = p;
                                        break;
                                    case elementP:
                                        DocTree p2 = new DocTree(new InlineParaGraph(), tr, td);
                                        currentElement.Children.Add(p2);
                                        currentElement = p2;
                                        break;
                                    case elementImage:
                                        var img = new InlineUIContainer()
                                        {
                                            Child = new Image()
                                            {
                                                Source = new System.Windows.Media.Imaging.BitmapImage(
                                                    new System.Uri(xmlReader.GetAttribute("src"), System.UriKind.Absolute)
                                                ),
                                                Stretch = Stretch.None //This should be improved to read image size if set
                                            }
                                        };
                                        currentElement.Children.Add(new DocTree(img, tr, td));
                                        break;
                                }
                                lastElement = nameUpper;
                                break;
                            case XmlNodeType.EndElement:
                                // Handle the end element
                                switch (nameUpper)
                                {
                                    case elementA: link = null; break;
                                    case elementB:
                                    case elementSTRONG: bold--; break;
                                    case elementI:
                                    case elementEM: italic--; break;
                                    case elementU: underline--; break;
                                    case "H1":
                                    case "H2":
                                    case "H3":
                                    case "H4":
                                        fontSize.Pop();
                                        break;
                                    case elementTR:
                                        td = 0; break;
                                    case elementTD:
                                        break;
                                    case "TH":
                                        bold--;
                                        break;
                                    case elementTABLE:
                                        tr = 0; td = 0;
                                        currentElement = currentElement.Parent ?? RootElement;
                                        break;
                                    case elementP:
                                        currentElement = currentElement.Parent ?? RootElement;
                                        //Selection.Insert(elementTree.Pop());
                                        //Selection.Insert(new LineBreak());
                                        //Selection.Insert(new LineBreak());
                                        break;
                                }
                                lastElement = nameUpper;
                                break;
                            case XmlNodeType.Text:
                            case XmlNodeType.Whitespace:
                                // Create a Run for the visible text
                                // Collapse contiguous whitespace per HTML behavior
                                StringBuilder builder = new StringBuilder(xmlReader.Value.Length);
                                var last = '\0';
                                foreach (char c in xmlReader.Value.Replace('\n', ' '))
                                {
                                    if ((' ' != last) || (' ' != c))
                                    {
                                        builder.Append(c);
                                    }
                                    last = c;
                                }
                                // Trim leading whitespace if following a <P> or <BR> element per HTML behavior
                                var builderString = builder.ToString();
                                if ((elementP == lastElement) || (elementBR == lastElement))
                                {
                                    builderString = builderString.TrimStart();
                                }
                                // If any text remains to display...
                                if (0 < builderString.Length)
                                {
                                    // Create a Run to display it
                                    Run run = new Run
                                    {
                                        Text = builderString,
                                        FontSize = currentElement.FontSize
                                    };

                                    // Style the Run appropriately
                                    if (0 < bold) run.FontWeight = FontWeights.Bold;
                                    if (0 < italic) run.FontStyle = FontStyles.Italic;
                                    if (0 < underline) run.TextDecorations = System.Windows.TextDecorations.Underline;
                                    if (fontSize.Count > 0) run.FontSize = fontSize.Peek();
                                    if (null != link)
                                    {
                                        // Links get styled and display their HREF since Run doesn't support MouseLeftButton* events
                                        run.TextDecorations = System.Windows.TextDecorations.Underline;
                                        run.Foreground = HyperlinkColor ?? new SolidColorBrush { Color = Color.FromArgb(255, 177, 211, 250) };
                                        Hyperlink hlink = new Hyperlink()
                                        {
                                            NavigateUri = new System.Uri(link, System.UriKind.RelativeOrAbsolute),
                                            TargetName = "_blank"
                                        };
                                        hlink.Inlines.Add(run);
                                        currentElement.Children.Add(new DocTree(hlink, tr, td));
                                    }
                                    else
                                        // Add the Run to the collection
                                        currentElement.Children.Add(new DocTree(run, tr, td));
                                    lastElement = null;
                                }
                                break;
                        }
                    }

                }
                Span s = new Span();
                DocTreeToTextElement(RootElement, s.Inlines);
                Selection.Insert(s);
            }
        }
        private void ParseAndSetSpecifiedText(string text)
        {
            // Clear the collection of Inlines
            SelectAll();
            this.Selection.Text = "";
            // Wrap the input in a <div> (so even plain text becomes valid XML)
            using (var stringReader = new StringReader(string.Concat("<div>", text, "</div>")))
            {
                DocTree RootElement = new DocTree(new Span(), 0, 0)
                {
                    FontSize = 10
                };
                DocTree currentElement = RootElement;
                // Read the input
                using (var xmlReader = XmlReader.Create(stringReader))
                {
                    // State variables
                    var            bold      = 0;
                    var            italic    = 0;
                    var            underline = 0;
                    var            td        = 0;
                    var            tr        = 0;
                    Stack <double> fontSize  = new Stack <double>();
                    fontSize.Push(10);
                    System.Collections.Generic.Stack <TextElement> elementTree = new System.Collections.Generic.Stack <TextElement>();
                    string link        = null;
                    var    lastElement = elementP;
                    // Read the entire XML DOM...
                    while (xmlReader.Read())
                    {
                        var nameUpper = xmlReader.Name.ToUpper();
                        switch (xmlReader.NodeType)
                        {
                        case XmlNodeType.Element:
                            // Handle the begin element
                            switch (nameUpper)
                            {
                            case "H1":
                                fontSize.Push(24);
                                break;

                            case "H2":
                                fontSize.Push(18);
                                break;

                            case "H3":
                                fontSize.Push(14);
                                break;

                            case "H4":
                                fontSize.Push(12);
                                break;

                            case elementA:
                                link = "";
                                // Look for the HREF attribute (can't use .MoveToAttribute because it's case-sensitive)
                                if (xmlReader.MoveToFirstAttribute())
                                {
                                    do
                                    {
                                        if ("HREF" == xmlReader.Name.ToUpper())
                                        {
                                            // Store the link target
                                            link = xmlReader.Value;
                                            break;
                                        }
                                    } while (xmlReader.MoveToNextAttribute());
                                }
                                break;

                            case elementB:
                            case elementSTRONG: bold++; break;

                            case elementI:
                            case elementEM: italic++; break;

                            case elementU: underline++; break;

                            case elementBR:
                                currentElement.Children.Add(new DocTree(new LineBreak(), tr, td));
                                break;

                            //Selection.Insert(new LineBreak() ); break;
                            case elementTR:
                                tr++; td = 0; break;

                            case elementTD:
                                td++; break;

                            case "TH":
                                td++;
                                bold++;
                                break;

                            case elementTABLE:
                                DocTree p = new DocTree(new Table(), tr, td);
                                currentElement.Children.Add(p);
                                tr             = 0; td = 0;
                                currentElement = p;
                                break;

                            case elementP:
                                DocTree p2 = new DocTree(new InlineParaGraph(), tr, td);
                                currentElement.Children.Add(p2);
                                currentElement = p2;
                                break;

                            case elementImage:
                                var img = new InlineUIContainer()
                                {
                                    Child = new Image()
                                    {
                                        Source = new System.Windows.Media.Imaging.BitmapImage(
                                            new System.Uri(xmlReader.GetAttribute("src"), System.UriKind.Absolute)
                                            ),
                                        Stretch = Stretch.None         //This should be improved to read image size if set
                                    }
                                };
                                currentElement.Children.Add(new DocTree(img, tr, td));
                                break;
                            }
                            lastElement = nameUpper;
                            break;

                        case XmlNodeType.EndElement:
                            // Handle the end element
                            switch (nameUpper)
                            {
                            case elementA: link = null; break;

                            case elementB:
                            case elementSTRONG: bold--; break;

                            case elementI:
                            case elementEM: italic--; break;

                            case elementU: underline--; break;

                            case "H1":
                            case "H2":
                            case "H3":
                            case "H4":
                                fontSize.Pop();
                                break;

                            case elementTR:
                                td = 0; break;

                            case elementTD:
                                break;

                            case "TH":
                                bold--;
                                break;

                            case elementTABLE:
                                tr             = 0; td = 0;
                                currentElement = currentElement.Parent ?? RootElement;
                                break;

                            case elementP:
                                currentElement = currentElement.Parent ?? RootElement;
                                //Selection.Insert(elementTree.Pop());
                                //Selection.Insert(new LineBreak());
                                //Selection.Insert(new LineBreak());
                                break;
                            }
                            lastElement = nameUpper;
                            break;

                        case XmlNodeType.Text:
                        case XmlNodeType.Whitespace:
                            // Create a Run for the visible text
                            // Collapse contiguous whitespace per HTML behavior
                            StringBuilder builder = new StringBuilder(xmlReader.Value.Length);
                            var           last    = '\0';
                            foreach (char c in xmlReader.Value.Replace('\n', ' '))
                            {
                                if ((' ' != last) || (' ' != c))
                                {
                                    builder.Append(c);
                                }
                                last = c;
                            }
                            // Trim leading whitespace if following a <P> or <BR> element per HTML behavior
                            var builderString = builder.ToString();
                            if ((elementP == lastElement) || (elementBR == lastElement))
                            {
                                builderString = builderString.TrimStart();
                            }
                            // If any text remains to display...
                            if (0 < builderString.Length)
                            {
                                // Create a Run to display it
                                Run run = new Run
                                {
                                    Text     = builderString,
                                    FontSize = currentElement.FontSize
                                };

                                // Style the Run appropriately
                                if (0 < bold)
                                {
                                    run.FontWeight = FontWeights.Bold;
                                }
                                if (0 < italic)
                                {
                                    run.FontStyle = FontStyles.Italic;
                                }
                                if (0 < underline)
                                {
                                    run.TextDecorations = System.Windows.TextDecorations.Underline;
                                }
                                if (fontSize.Count > 0)
                                {
                                    run.FontSize = fontSize.Peek();
                                }
                                if (null != link)
                                {
                                    // Links get styled and display their HREF since Run doesn't support MouseLeftButton* events
                                    run.TextDecorations = System.Windows.TextDecorations.Underline;
                                    run.Foreground      = HyperlinkColor ?? new SolidColorBrush {
                                        Color = Color.FromArgb(255, 177, 211, 250)
                                    };
                                    Hyperlink hlink = new Hyperlink()
                                    {
                                        NavigateUri = new System.Uri(link, System.UriKind.RelativeOrAbsolute),
                                        TargetName  = "_blank"
                                    };
                                    hlink.Inlines.Add(run);
                                    currentElement.Children.Add(new DocTree(hlink, tr, td));
                                }
                                else
                                {
                                    // Add the Run to the collection
                                    currentElement.Children.Add(new DocTree(run, tr, td));
                                }
                                lastElement = null;
                            }
                            break;
                        }
                    }
                }
                Span s = new Span();
                DocTreeToTextElement(RootElement, s.Inlines);
                Selection.Insert(s);
            }
        }