PageRenderTime 79ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DocumentGenerator/WordDocuments/AODL/Document/Content/Text/FormatedText.cs

#
C# | 436 lines | 222 code | 46 blank | 168 comment | 29 complexity | 6274d69347de92ed95c5ace32444c608 MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using System;
  23. using System.Xml;
  24. using AODL.Document.Styles;
  25. using AODL.Document.Styles.Properties;
  26. using AODL.Document.Import.OpenDocument.NodeProcessors;
  27. using AODL.Document.Content;
  28. using AODL.Document;
  29. namespace AODL.Document.Content.Text
  30. {
  31. /// <summary>
  32. /// Represent a formated Text e.g bold, italic, underline etc.
  33. /// </summary>
  34. public class FormatedText : IHtml, IText, ITextContainer, ICloneable
  35. {
  36. /// <summary>
  37. /// Gets or sets the text style.
  38. /// </summary>
  39. /// <value>The text style.</value>
  40. public TextStyle TextStyle
  41. {
  42. get { return (TextStyle)this.Style; }
  43. set { this.Style = value; }
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="FormatedText"/> class.
  47. /// </summary>
  48. /// <param name="document">The document.</param>
  49. /// <param name="node">The node.</param>
  50. public FormatedText(IDocument document, XmlNode node)
  51. {
  52. this.Document = document;
  53. this.Node = node;
  54. this.InitStandards();
  55. }
  56. /// <summary>
  57. /// Overloaded constructor.
  58. /// </summary>
  59. /// <param name="document">The content object to which the formated text belongs to.</param>
  60. /// <param name="name">The stylename which should be referenced with this FormatedText object.</param>
  61. /// <param name="text">The Displaytext.</param>
  62. public FormatedText(IDocument document, string name, string text)
  63. {
  64. this.Document = document;
  65. this.NewXmlNode(name);
  66. this.InitStandards();
  67. this.Text = text;
  68. this.Style = (IStyle)new TextStyle(this.Document, name);
  69. this.Document.Styles.Add(this.Style);
  70. }
  71. // Phil Jollans 18-Feb-2008
  72. // Additional construcor
  73. /// <summary>
  74. /// Overloaded constructor.
  75. /// </summary>
  76. /// <param name="document">The content object to which the formated text belongs to.</param>
  77. /// <param name="textstyle">An existing TextStyle object.</param>
  78. /// <param name="text">The Displaytext.</param>
  79. public FormatedText(IDocument document, TextStyle textstyle, string text)
  80. {
  81. this.Document = document;
  82. this.NewXmlNode ( textstyle.StyleName ) ;
  83. this.InitStandards();
  84. this.Text = text;
  85. this.Style = textstyle ;
  86. }
  87. /// <summary>
  88. /// Inits the standards.
  89. /// </summary>
  90. private void InitStandards()
  91. {
  92. this.TextContent = new ITextCollection();
  93. this.TextContent.Inserted += TextContent_Inserted;
  94. this.TextContent.Removed += TextContent_Removed;
  95. }
  96. /// <summary>
  97. /// Create a new XmlNode.
  98. /// </summary>
  99. /// <param name="stylename">The stylename which should be referenced with this FormatedText.</param>
  100. private void NewXmlNode( string stylename)
  101. {
  102. this.Node = this.Document.CreateNode("span", "text");
  103. XmlAttribute xa = this.Document.CreateAttribute("style-name", "text");
  104. xa.Value = stylename;
  105. this.Node.Attributes.Append(xa);
  106. }
  107. /// <summary>
  108. /// Create a XmlAttribute for propertie XmlNode.
  109. /// </summary>
  110. /// <param name="name">The attribute name.</param>
  111. /// <param name="text">The attribute value.</param>
  112. /// <param name="prefix">The namespace prefix.</param>
  113. private void CreateAttribute(string name, string text, string prefix)
  114. {
  115. XmlAttribute xa = this.Document.CreateAttribute(name, prefix);
  116. xa.Value = text;
  117. this.Node.Attributes.Append(xa);
  118. }
  119. #region IHtml Member
  120. /// <summary>
  121. /// Return the content as Html string
  122. /// </summary>
  123. /// <returns>The html string</returns>
  124. public string GetHtml()
  125. {
  126. string style = ((TextStyle)this.Style).TextProperties.GetHtmlStyle();
  127. string html = "<span ";
  128. string text = this.GetTextWithHtmlControl();
  129. if (style.Length > 0)
  130. html = html + style + ">\n";
  131. else
  132. html += ">\n";
  133. if (text.Length > 0)
  134. html += text;
  135. html += "</span>\n";
  136. html = this.GetSubOrSupStartTag()+html+this.GetSubOrSupEndTag();
  137. return html;
  138. }
  139. /// <summary>
  140. /// Gets the text with HTML controls
  141. /// as Tab as &amp;nbsp; and line-break as br tag
  142. /// </summary>
  143. /// <returns>The string</returns>
  144. private string GetTextWithHtmlControl()
  145. {
  146. string text = "";
  147. foreach(XmlNode node in this.Node)
  148. {
  149. if (node.LocalName == "tab")
  150. text += "&nbsp;&nbsp;&nbsp;";
  151. else if (node.LocalName == "line-break")
  152. text += "<br>";
  153. // else if (node.LocalName == "s")
  154. // text += WhiteSpace.GetWhiteSpaceHtml(node.OuterXml);
  155. else if (node.InnerText.Length > 0)
  156. text += node.InnerText;
  157. }
  158. return text;
  159. }
  160. /// <summary>
  161. /// Gets the sub or sup start tag.
  162. /// </summary>
  163. /// <returns></returns>
  164. private string GetSubOrSupStartTag()
  165. {
  166. if (((TextStyle)this.Style).TextProperties.Position != null)
  167. if (((TextStyle)this.Style).TextProperties.Position.Length > 0)
  168. if (((TextStyle)this.Style).TextProperties.Position.ToLower().StartsWith("sub"))
  169. return "<sub>";
  170. else
  171. return "<sup>";
  172. return "";
  173. }
  174. /// <summary>
  175. /// Gets the sub or sup end tag.
  176. /// </summary>
  177. /// <returns></returns>
  178. private string GetSubOrSupEndTag()
  179. {
  180. if (((TextStyle)this.Style).TextProperties.Position != null)
  181. if (((TextStyle)this.Style).TextProperties.Position.Length > 0)
  182. if (((TextStyle)this.Style).TextProperties.Position.ToLower().StartsWith("sub"))
  183. return "</sub>";
  184. else
  185. return "</sup>";
  186. return "";
  187. }
  188. #endregion
  189. #region IText Member
  190. private XmlNode _node;
  191. /// <summary>
  192. /// The node that represent the text content.
  193. /// </summary>
  194. /// <value></value>
  195. public XmlNode Node
  196. {
  197. get
  198. {
  199. return this._node;
  200. }
  201. set
  202. {
  203. this._node = value;
  204. }
  205. }
  206. /// <summary>
  207. /// Use this if use text without control character,
  208. /// otherwise use the the TextColllection TextContent.
  209. /// </summary>
  210. /// <value></value>
  211. public string Text
  212. {
  213. get
  214. {
  215. return this.Node.InnerText;
  216. }
  217. set
  218. {
  219. this.Node.InnerText = value;
  220. }
  221. }
  222. private IDocument _document;
  223. /// <summary>
  224. /// The document to which this text content belongs to.
  225. /// </summary>
  226. /// <value></value>
  227. public IDocument Document
  228. {
  229. get
  230. {
  231. return this._document;
  232. }
  233. set
  234. {
  235. this._document = value;
  236. }
  237. }
  238. private IStyle _style;
  239. /// <summary>
  240. /// The style which is referenced with this text object.
  241. /// This is null if no style is available.
  242. /// </summary>
  243. /// <value></value>
  244. public IStyle Style
  245. {
  246. get
  247. {
  248. return this._style;
  249. }
  250. set
  251. {
  252. this.StyleName = value.StyleName;
  253. this._style = value;
  254. }
  255. }
  256. /// <summary>
  257. /// The style name which is used for the referenced style.
  258. /// This is null is no style is available.
  259. /// </summary>
  260. /// <value></value>
  261. public string StyleName
  262. {
  263. get
  264. {
  265. XmlNode xn = this._node.SelectSingleNode("@text:style-name",
  266. this.Document.NamespaceManager);
  267. if (xn != null)
  268. return xn.InnerText;
  269. return null;
  270. }
  271. set
  272. {
  273. XmlNode xn = this._node.SelectSingleNode("@text:style-name",
  274. this.Document.NamespaceManager);
  275. if (xn == null)
  276. this.CreateAttribute("style-name", value, "text");
  277. this._node.SelectSingleNode("@text:style-name",
  278. this.Document.NamespaceManager).InnerText = value;
  279. }
  280. }
  281. #endregion
  282. #region ITextContainer Member
  283. private ITextCollection _textContent;
  284. /// <summary>
  285. /// All Content objects have a Text container. Which represents
  286. /// his Text this could be SimpleText, FormatedText or mixed.
  287. /// </summary>
  288. /// <value></value>
  289. public ITextCollection TextContent
  290. {
  291. get
  292. {
  293. return this._textContent;
  294. }
  295. set
  296. {
  297. if (this._textContent != null)
  298. foreach(IText text in this._textContent)
  299. this.Node.RemoveChild(text.Node);
  300. this._textContent = value;
  301. if (this._textContent != null)
  302. foreach(IText text in this._textContent)
  303. this.Node.AppendChild(text.Node);
  304. }
  305. }
  306. #endregion
  307. /// <summary>
  308. /// Texts the content_ inserted.
  309. /// </summary>
  310. /// <param name="index">The index.</param>
  311. /// <param name="value">The value.</param>
  312. private void TextContent_Inserted(int index, object value)
  313. {
  314. this.Node.AppendChild(((IText)value).Node);
  315. }
  316. /// <summary>
  317. /// Texts the content_ removed.
  318. /// </summary>
  319. /// <param name="index">The index.</param>
  320. /// <param name="value">The value.</param>
  321. private void TextContent_Removed(int index, object value)
  322. {
  323. this.Node.RemoveChild(((IText)value).Node);
  324. }
  325. #region ICloneable Member
  326. /// <summary>
  327. /// Create a deep clone of this FormatedText object.
  328. /// </summary>
  329. /// <remarks>A possible Attached Style wouldn't be cloned!</remarks>
  330. /// <returns>
  331. /// A clone of this object.
  332. /// </returns>
  333. public object Clone()
  334. {
  335. FormatedText formatedTextClone = null;
  336. if (this.Document != null && this.Node != null)
  337. {
  338. TextContentProcessor tcp = new TextContentProcessor();
  339. formatedTextClone = tcp.CreateFormatedText(
  340. this.Document, this.Node.CloneNode(true));
  341. }
  342. return formatedTextClone;
  343. }
  344. #endregion
  345. }
  346. }
  347. /*
  348. * $Log: FormatedText.cs,v $
  349. * Revision 1.4 2008/04/29 15:39:46 mt
  350. * new copyright header
  351. *
  352. * Revision 1.3 2008/04/10 17:33:16 larsbehr
  353. * - Added several bug fixes mainly for the table handling which are submitted by Phil Jollans
  354. *
  355. * Revision 1.2 2007/04/08 16:51:23 larsbehr
  356. * - finished master pages and styles for text documents
  357. * - several bug fixes
  358. *
  359. * Revision 1.1 2007/02/25 08:58:38 larsbehr
  360. * initial checkin, import from Sourceforge.net to OpenOffice.org
  361. *
  362. * Revision 1.3 2006/02/02 21:55:59 larsbm
  363. * - Added Clone object support for many AODL object types
  364. * - New Importer implementation PlainTextImporter and CsvImporter
  365. * - New tests
  366. *
  367. * Revision 1.2 2006/01/29 18:52:14 larsbm
  368. * - Added support for common styles (style templates in OpenOffice)
  369. * - Draw TextBox import and export
  370. * - DrawTextBox html export
  371. *
  372. * Revision 1.1 2006/01/29 11:28:22 larsbm
  373. * - Changes for the new version. 1.2. see next changelog for details
  374. *
  375. * Revision 1.4 2005/12/12 19:39:17 larsbm
  376. * - Added Paragraph Header
  377. * - Added Table Row Header
  378. * - Fixed some bugs
  379. * - better whitespace handling
  380. * - Implmemenation of HTML Exporter
  381. *
  382. * Revision 1.3 2005/11/20 17:31:20 larsbm
  383. * - added suport for XLinks, TabStopStyles
  384. * - First experimental of loading dcuments
  385. * - load and save via importer and exporter interfaces
  386. *
  387. * Revision 1.2 2005/10/08 08:19:25 larsbm
  388. * - added cvs tags
  389. *
  390. */