Click here to Skip to main content
15,893,923 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79.4K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
// <copyright file="ToodledoToXamlConverter.cs" company="GoalBook"> 
//    Copyright © 2009 Mark Brownsword. All rights reserved.
//    This source code and supporting files are licensed under The Code Project  
//    Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx. 
// </copyright>
namespace GoalBook.Infrastructure.Converters
{
    #region Using Statements
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Xml;
    using GoalBook.Public.HtmlConverter;
    using Parser = GoalBook.Public.HtmlParser;
    #endregion

    /// <summary>
    /// Value Converter for object model item foreign keys.
    /// </summary>
    public class ToodledoToXamlConverter : IValueConverter
    {
        #region Constants and Enums 
        #endregion

        #region IValueConverter Members
        /// <summary>
        /// Convert Toodledo text to Xaml.
        /// </summary>
        /// <param name="value">value parameter</param>
        /// <param name="targetType">targetType parameter</param>
        /// <param name="parameter">object parameter</param>
        /// <param name="culture">culture parameter</param>
        /// <returns>Xaml string from Toodledo text</returns>      
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            XamlToFlowDocumentConverter converter = new XamlToFlowDocumentConverter();

            // Convert Html to Xaml string.             
            string xaml = HtmlToXamlConverter.ConvertHtmlToXaml(value.ToString(), true);

            // Initialise FlowDocument from xaml.            
            FlowDocument flowDocument = converter.Convert(xaml, null, null, null) as FlowDocument;

            // Add Margin Styling to FlowDocument. This ensures that when the document is
            // edited in RichTextBox, new paragraphs have no margin.
            Style style = new Style(typeof(Paragraph));
            style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
            flowDocument.Resources.Add(typeof(Paragraph), style);
            
            // Convert back to xaml string for result.
            return converter.ConvertBack(flowDocument, null, null, null).ToString();
        }

        /// <summary>
        /// Convert Xaml to Toodledo text.
        /// </summary>
        /// <param name="value">value parameter</param>
        /// <param name="targetType">targetType parameter</param>
        /// <param name="parameter">object parameter</param>
        /// <param name="culture">culture parameter</param>
        /// <returns>Toodledo text from Html string</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Parser.ParseHTML parser = new Parser.ParseHTML();
            Parser.AttributeList htmlTag = null;
            Parser.Attribute attribute = null;
            StringBuilder result = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(result));
            ushort listItemCounter = 0;
            ushort formatTagCounter = 0;

            parser.Source = HtmlFromXamlConverter.ConvertXamlToHtml(value.ToString());
            
            while (!parser.Eof())
            {
                char ch = parser.Parse();
                if (ch == 0)
                {
                    // Html Tag
                    htmlTag = parser.GetTag();
                    
                    if (htmlTag.Name == "SPAN")
                    {
                        attribute = htmlTag[0];
                        if (attribute != null)
                        {
                            if (attribute.Name == "STYLE")
                            {
                                if (attribute.Value.Contains("font-weight:bold;"))
                                {
                                    writer.WriteStartElement("B");
                                    formatTagCounter++;
                                }

                                if (attribute.Value.Contains("font-style:italic;"))
                                {
                                    writer.WriteStartElement("I");
                                    formatTagCounter++;
                                }
                            }
                        }
                    }
                    else if (htmlTag.Name == "UL" || htmlTag.Name == "OL" || htmlTag.Name == "LI")
                    {
                        writer.WriteStartElement(htmlTag.Name);
                        listItemCounter++;
                    }
                    else if (htmlTag.Name == "A")
                    {
                        writer.WriteStartElement(htmlTag.Name);
                        for (int i = 0; i < htmlTag.Count; i++)
                        {
                            attribute = htmlTag[i];
                            writer.WriteAttributeString(attribute.Name, attribute.Value);
                        }                                                                                                                                                        
                    }                    
                    else if (htmlTag.Name == "/UL" || htmlTag.Name == "/OL" || htmlTag.Name == "/LI")
                    {
                        writer.WriteEndElement();
                        listItemCounter--;
                    }
                    else if (htmlTag.Name == "/A")
                    {
                        writer.WriteEndElement();
                    }
                    else if (htmlTag.Name == "/SPAN")
                    {
                        while (formatTagCounter > 0)
                        {
                            writer.WriteEndElement();
                            formatTagCounter--;
                        }
                    }
                    else if (IsEmptyParagraph(htmlTag) || htmlTag.Name == "/P" || htmlTag.Name == "BR")
                    {
                        if (listItemCounter == 0 && this.NewLineRequired(parser))
                        {
                            writer.WriteString(Environment.NewLine);
                        }
                    }
                }
                else
                {
                    // Text
                    writer.WriteString(ch.ToString());
                }
            }            
                                    
            return result.ToString();
        }
       
        /// <summary>
        /// Determine if tag Is Empty Paragraph.
        /// </summary>
        /// <returns></returns>
        private bool IsEmptyParagraph(Parser.AttributeList htmlTag)
        {
            if (htmlTag.Name == "P")
            {
                for (int i = 0; i < htmlTag.List.Count; i++)
                {
                    if ((htmlTag.List[i] as Parser.Attribute).Name == "/")
                    {
                        return true;
                    }
                }
            }

            return false;            
        }

        /// <summary>
        /// Determine if NewLine Required.
        /// </summary>
        /// <param name="parser">ParseHTML parameter</param>
        /// <returns>True if NewLine Required</returns>
        private bool NewLineRequired(Parser.ParseHTML parser)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 4; i++)
            {
                if (!parser.Eof())
                {
                    builder.Append(parser.GetCurrentChar(i));
                }
            }

            if (builder.ToString() == "<UL>" || builder.ToString() == "<OL>" || builder.ToString() == "<LI>" || builder.ToString() == "</BO")
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

Comments and Discussions