From b6bd78e14426092121243b4573e03913ea74dbbf Mon Sep 17 00:00:00 2001 From: alcexhim Date: Wed, 1 Oct 2014 09:52:05 -0400 Subject: [PATCH] Begin to implement FormattedTextObjectModel --- .../Text/Formatted/FormattedTextItem.cs | 27 +++ .../Formatted/FormattedTextObjectModel.cs | 39 +++ .../Text/Formatted/FormattedTextStyle.cs | 22 ++ .../Text/Formatted/FormattedTextStyleGroup.cs | 16 ++ .../Text/Formatted/Items/Container.cs | 23 ++ .../Text/Formatted/Items/Literal.cs | 20 ++ .../UniversalEditor.Essential.csproj | 1 + .../Text/Formatted/XPS/XPSDataFormat.cs | 7 +- .../DataFormats/Text/HTML/HTMLDataFormat.cs | 224 +++++++++++------- .../DataFormats/Text/MHTML/MHTMLDataFormat.cs | 3 + 10 files changed, 293 insertions(+), 89 deletions(-) create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextItem.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextObjectModel.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyle.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyleGroup.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Container.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Literal.cs diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextItem.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextItem.cs new file mode 100644 index 00000000..5257f573 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextItem.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted +{ + public abstract class FormattedTextItem : ICloneable + { + public class FormattedTextItemCollection + : System.Collections.ObjectModel.Collection + { + + } + + private FormattedTextStyleGroup mvarBaseStyleGroup = null; + public FormattedTextStyleGroup BaseStyleGroup { get { return mvarBaseStyleGroup; } set { mvarBaseStyleGroup = value; } } + + private FormattedTextStyle.TextStyleCollection mvarStyles = new FormattedTextStyle.TextStyleCollection(); + public FormattedTextStyle.TextStyleCollection Styles { get { return mvarStyles; } } + + private string mvarText = String.Empty; + public string Text { get { return mvarText; } set { mvarText = value; } } + + public abstract object Clone(); + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextObjectModel.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextObjectModel.cs new file mode 100644 index 00000000..19a00235 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextObjectModel.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted +{ + public class FormattedTextObjectModel : ObjectModel + { + private ObjectModelReference _omr = null; + public override ObjectModelReference MakeReference() + { + if (_omr == null) + { + _omr = base.MakeReference(); + _omr.Title = "Formatted Text Document"; + _omr.Path = new string[] { "General", "Text", "Formatted" }; + } + return _omr; + } + + private FormattedTextItem.FormattedTextItemCollection mvarSegments = new FormattedTextItem.FormattedTextItemCollection(); + public FormattedTextItem.FormattedTextItemCollection Items { get { return mvarSegments; } } + + public override void Clear() + { + mvarSegments.Clear(); + } + + public override void CopyTo(ObjectModel where) + { + FormattedTextObjectModel clone = (where as FormattedTextObjectModel); + foreach (FormattedTextItem segment in mvarSegments) + { + clone.Items.Add(segment.Clone() as FormattedTextItem); + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyle.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyle.cs new file mode 100644 index 00000000..7b157e9e --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyle.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted +{ + public class FormattedTextStyle + { + public class TextStyleCollection + : System.Collections.ObjectModel.Collection + { + + } + + public object Clone() + { + FormattedTextStyle clone = new FormattedTextStyle(); + return clone; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyleGroup.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyleGroup.cs new file mode 100644 index 00000000..606efa1c --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/FormattedTextStyleGroup.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted +{ + public class FormattedTextStyleGroup + { + private string mvarName = String.Empty; + public string Name { get { return mvarName; } set { mvarName = value; } } + + private FormattedTextStyle.TextStyleCollection mvarStyles = new FormattedTextStyle.TextStyleCollection(); + public FormattedTextStyle.TextStyleCollection Styles { get { return mvarStyles; } } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Container.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Container.cs new file mode 100644 index 00000000..9ee1267f --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Container.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted.Items +{ + public class Container : FormattedTextItem + { + private FormattedTextItem.FormattedTextItemCollection mvarItems = new FormattedTextItemCollection(); + public FormattedTextItem.FormattedTextItemCollection Items { get { return mvarItems; } } + + public override object Clone() + { + Container clone = new Container(); + foreach (FormattedTextItem item in mvarItems) + { + clone.Items.Add(item.Clone() as FormattedTextItem); + } + return clone; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Literal.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Literal.cs new file mode 100644 index 00000000..2e590124 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Text/Formatted/Items/Literal.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Text.Formatted.Items +{ + public class Literal : FormattedTextItem + { + private string mvarText = String.Empty; + public string Text { get { return mvarText; } set { mvarText = value; } } + + public override object Clone() + { + Literal clone = new Literal(); + clone.Text = (mvarText.Clone() as string); + return clone; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj index 7d50c729..4f4cb9f0 100644 --- a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -96,6 +96,7 @@ + diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs index a58d165d..19816b60 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs @@ -23,6 +23,8 @@ using System; using UniversalEditor.DataFormats.FileSystem.ZIP; using UniversalEditor.ObjectModels.FileSystem; +using UniversalEditor.ObjectModels.Text.Formatted; + namespace UniversalEditor { public class XPSDataFormat : ZIPDataFormat @@ -33,7 +35,7 @@ namespace UniversalEditor if (_dfr == null) { _dfr = new DataFormatReference(GetType()); - // _dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All); + _dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All); _dfr.Filters.Add("Microsoft XPS document", new string[] { "*.xps", "*.oxps" }); } return _dfr; @@ -51,7 +53,7 @@ namespace UniversalEditor protected override void BeforeSaveInternal(System.Collections.Generic.Stack objectModels) { - // FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel); + FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel); FileSystemObjectModel fsom = new FileSystemObjectModel(); @@ -95,4 +97,3 @@ namespace UniversalEditor } } } - diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/HTML/HTMLDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/HTML/HTMLDataFormat.cs index 31deb245..f7475936 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/HTML/HTMLDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/HTML/HTMLDataFormat.cs @@ -7,106 +7,158 @@ using UniversalEditor.ObjectModels.Markup; using UniversalEditor.DataFormats.Markup.XML; using UniversalEditor.ObjectModels.Text.Plain; +using UniversalEditor.ObjectModels.Text.Formatted; +using UniversalEditor.ObjectModels.Text.Formatted.Items; namespace UniversalEditor.DataFormats.Text.HTML { - public class HTMLDataFormat : XMLDataFormat - { - private static DataFormatReference _dfr = null; - public override DataFormatReference MakeReference() - { - if (_dfr == null) - { - _dfr = new DataFormatReference(GetType()); - _dfr.Capabilities.Add(typeof(MarkupObjectModel), DataFormatCapabilities.Bootstrap); - _dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All); - _dfr.Filters.Add("HyperText Markup Language", new string[] { "*.htm", "*.html" }); + public class HTMLDataFormat : XMLDataFormat + { + private static DataFormatReference _dfr = null; + public override DataFormatReference MakeReference() + { + if (_dfr == null) + { + _dfr = new DataFormatReference(GetType()); + _dfr.Capabilities.Add(typeof(MarkupObjectModel), DataFormatCapabilities.Bootstrap); + _dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All); + _dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All); + _dfr.Filters.Add("HyperText Markup Language", new string[] { "*.htm", "*.html" }); - _dfr.ExportOptions.Add(new CustomOptionText("Title", "&Title: ")); - } - return _dfr; - } - protected override void BeforeLoadInternal(Stack objectModels) - { - base.BeforeLoadInternal(objectModels); - objectModels.Push(new MarkupObjectModel()); - } - protected override void AfterLoadInternal(Stack objectModels) - { - base.AfterLoadInternal(objectModels); - - MarkupObjectModel html = (objectModels.Pop() as MarkupObjectModel); - ObjectModel objectModel = objectModels.Pop(); + _dfr.ExportOptions.Add(new CustomOptionText("Title", "&Title: ")); + } + return _dfr; + } + protected override void BeforeLoadInternal(Stack objectModels) + { + base.BeforeLoadInternal(objectModels); + objectModels.Push(new MarkupObjectModel()); + } + protected override void AfterLoadInternal(Stack objectModels) + { + base.AfterLoadInternal(objectModels); + + MarkupObjectModel html = (objectModels.Pop() as MarkupObjectModel); + ObjectModel objectModel = objectModels.Pop(); - if (objectModel is PlainTextObjectModel) - { - PlainTextObjectModel text = (objectModel as PlainTextObjectModel); + if (objectModel is PlainTextObjectModel) + { + PlainTextObjectModel text = (objectModel as PlainTextObjectModel); - MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement); - if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag"); + MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement); + if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag"); - MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement); - if (tagHEAD != null) - { - MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement); - if (tagTITLE != null) mvarTitle = tagTITLE.Value; - } + MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement); + if (tagHEAD != null) + { + MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement); + if (tagTITLE != null) mvarTitle = tagTITLE.Value; + } - MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement); - if (tagBODY == null) throw new InvalidDataFormatException("Cannot find BODY tag"); + MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement); + if (tagBODY != null) + { + text.Text = tagBODY.Value; + } + } + else if (objectModel is FormattedTextObjectModel) + { + FormattedTextObjectModel text = (objectModel as FormattedTextObjectModel); - text.Text = tagBODY.Value; - } - } + MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement); + if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag"); - protected override void BeforeSaveInternal(Stack objectModels) - { - base.BeforeSaveInternal(objectModels); - ObjectModel objectModel = objectModels.Pop(); + MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement); + if (tagHEAD != null) + { + MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement); + if (tagTITLE != null) mvarTitle = tagTITLE.Value; + } + MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement); + if (tagBODY != null) + { - MarkupObjectModel html = new MarkupObjectModel(); - - #region Html - { - MarkupTagElement tagHTML = new MarkupTagElement(); - tagHTML.FullName = "html"; - #region Head - { - MarkupTagElement tagHEAD = new MarkupTagElement(); - tagHEAD.FullName = "head"; + } + } + } - if (!String.IsNullOrEmpty(mvarTitle)) - { - MarkupTagElement tagTITLE = new MarkupTagElement(); - tagTITLE.FullName = "title"; - tagTITLE.Value = mvarTitle; - tagHEAD.Elements.Add(tagTITLE); - } + protected override void BeforeSaveInternal(Stack objectModels) + { + base.BeforeSaveInternal(objectModels); + ObjectModel objectModel = objectModels.Pop(); - tagHTML.Elements.Add(tagHEAD); - } - #endregion - #region Body - { - MarkupTagElement tagBODY = new MarkupTagElement(); - tagBODY.FullName = "body"; + MarkupObjectModel html = new MarkupObjectModel(); + + #region Html + { + MarkupTagElement tagHTML = new MarkupTagElement(); + tagHTML.FullName = "html"; + #region Head + { + MarkupTagElement tagHEAD = new MarkupTagElement(); + tagHEAD.FullName = "head"; - if (objectModel is PlainTextObjectModel) - { - PlainTextObjectModel text = (objectModel as PlainTextObjectModel); - tagBODY.Value = text.Text; - } + if (!String.IsNullOrEmpty(mvarTitle)) + { + MarkupTagElement tagTITLE = new MarkupTagElement(); + tagTITLE.FullName = "title"; + tagTITLE.Value = mvarTitle; + tagHEAD.Elements.Add(tagTITLE); + } - tagHTML.Elements.Add(tagBODY); - } - #endregion - html.Elements.Add(tagHTML); - } - #endregion - objectModels.Push(html); - } + tagHTML.Elements.Add(tagHEAD); + } + #endregion + #region Body + { + MarkupTagElement tagBODY = new MarkupTagElement(); + tagBODY.FullName = "body"; - private string mvarTitle = String.Empty; - public string Title { get { return mvarTitle; } set { mvarTitle = value; } } - } + if (objectModel is PlainTextObjectModel) + { + PlainTextObjectModel text = (objectModel as PlainTextObjectModel); + tagBODY.Value = text.Text; + } + else if (objectModel is FormattedTextObjectModel) + { + FormattedTextObjectModel text = (objectModel as FormattedTextObjectModel); + foreach (FormattedTextItem segment in text.Items) + { + MarkupElement el = RenderFormattedTextItemToHTML(segment); + if (el != null) + { + tagBODY.Elements.Add(el); + } + else + { + Console.WriteLine("HTML: no implementation for segment '" + segment.GetType().Name + "'"); + } + } + } + + tagHTML.Elements.Add(tagBODY); + } + #endregion + html.Elements.Add(tagHTML); + } + #endregion + objectModels.Push(html); + } + + private MarkupElement RenderFormattedTextItemToHTML(FormattedTextItem segment) + { + if (segment is Literal) + { + Literal item = (segment as Literal); + MarkupTagElement tagSpan = new MarkupTagElement(); + tagSpan.FullName = "span"; + tagSpan.Value = item.Text; + return tagSpan; + } + return null; + } + + private string mvarTitle = String.Empty; + public string Title { get { return mvarTitle; } set { mvarTitle = value; } } + } } diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/MHTML/MHTMLDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/MHTML/MHTMLDataFormat.cs index 5840e28f..9bb20c3e 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/MHTML/MHTMLDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.Web/DataFormats/Text/MHTML/MHTMLDataFormat.cs @@ -4,7 +4,9 @@ using System.Linq; using System.Text; using UniversalEditor.Accessors; using UniversalEditor.IO; + using UniversalEditor.ObjectModels.Text.Plain; +using UniversalEditor.ObjectModels.Text.Formatted; namespace UniversalEditor.DataFormats.Text.MHTML { @@ -18,6 +20,7 @@ namespace UniversalEditor.DataFormats.Text.MHTML _dfr = base.MakeReference(); _dfr.Clear(); _dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All); + _dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All); _dfr.Filters.Add("MIME-encoded HyperText Markup Language", new string[] { "*.mht", "*.mhtml" }); } return _dfr;