PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 288 lines | 155 code | 29 blank | 104 comment | 15 complexity | c812d6847804040ab9738e5a4942a3ab 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;
  25. using AODL.Document.Styles;
  26. using AODL.Document.Content;
  27. namespace AODL.Document.Content.Text
  28. {
  29. /// <summary>
  30. /// Represent a list which could be a numbered or bullet style list.
  31. /// </summary>
  32. public class List : IContent, IContentContainer, IHtml
  33. {
  34. private ParagraphStyle _paragraphstyle;
  35. /// <summary>
  36. /// The ParagraphStyle to which this List belongs.
  37. /// There is only one ParagraphStyle per List and
  38. /// its ListItems.
  39. /// </summary>
  40. public ParagraphStyle ParagraphStyle
  41. {
  42. get { return this._paragraphstyle; }
  43. set { this._paragraphstyle = value; }
  44. }
  45. /// <summary>
  46. /// Gets or sets the list style.
  47. /// </summary>
  48. /// <value>The list style.</value>
  49. public ListStyle ListStyle
  50. {
  51. get { return (ListStyle)this.Style; }
  52. set { this.Style = (IStyle)value; }
  53. }
  54. private ContentCollection _content;
  55. /// <summary>
  56. /// The ContentCollection of access
  57. /// to their list items.
  58. /// </summary>
  59. public ContentCollection Content
  60. {
  61. get { return this._content; }
  62. set { this._content = value; }
  63. }
  64. private ListStyles _type;
  65. /// <summary>
  66. /// Gets the type of the list.
  67. /// </summary>
  68. /// <value>The type of the list.</value>
  69. public ListStyles ListType
  70. {
  71. get { return this._type; }
  72. }
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref="List"/> class.
  75. /// </summary>
  76. /// <param name="document">The document.</param>
  77. /// <param name="node">The node.</param>
  78. public List(IDocument document, XmlNode node)
  79. {
  80. this.Document = document;
  81. this.Node = node;
  82. this.InitStandards();
  83. }
  84. /// <summary>
  85. /// Create a new List object
  86. /// </summary>
  87. /// <param name="document">The IDocument</param>
  88. /// <param name="styleName">The style name</param>
  89. /// <param name="typ">The list typ bullet, ..</param>
  90. /// <param name="paragraphStyleName">The style name for the ParagraphStyle.</param>
  91. public List(IDocument document, string styleName, ListStyles typ, string paragraphStyleName)
  92. {
  93. this.Document = document;
  94. this.NewXmlNode();
  95. this.InitStandards();
  96. this.Style = (IStyle)new ListStyle(this.Document, styleName);
  97. this.ParagraphStyle = new ParagraphStyle(this.Document, paragraphStyleName);
  98. this.Document.Styles.Add(this.Style);
  99. this.Document.Styles.Add(this.ParagraphStyle);
  100. this.ParagraphStyle.ListStyleName = styleName;
  101. this._type = typ;
  102. ((ListStyle)this.Style).AutomaticAddListLevelStyles(typ);
  103. }
  104. /// <summary>
  105. /// Create a new List which is used to represent a inner list.
  106. /// </summary>
  107. /// <param name="document">The IDocument</param>
  108. /// <param name="outerlist">The List to which this List belongs.</param>
  109. public List(IDocument document, List outerlist)
  110. {
  111. this.Document = document;
  112. this.ParagraphStyle = outerlist.ParagraphStyle;
  113. this.InitStandards();
  114. this._type = outerlist.ListType;
  115. //Create an inner list node, don't need a style
  116. //use the parents list style
  117. this.NewXmlNode();
  118. }
  119. /// <summary>
  120. /// Inits the standards.
  121. /// </summary>
  122. private void InitStandards()
  123. {
  124. this.Content = new ContentCollection();
  125. this.Content.Inserted += Content_Inserted;
  126. this.Content.Removed += Content_Removed;
  127. }
  128. /// <summary>
  129. /// Create a new XmlNode.
  130. /// </summary>
  131. private void NewXmlNode()
  132. {
  133. this.Node = this.Document.CreateNode("list", "text");
  134. }
  135. /// <summary>
  136. /// Create a XmlAttribute for propertie XmlNode.
  137. /// </summary>
  138. /// <param name="name">The attribute name.</param>
  139. /// <param name="text">The attribute value.</param>
  140. /// <param name="prefix">The namespace prefix.</param>
  141. private void CreateAttribute(string name, string text, string prefix)
  142. {
  143. XmlAttribute xa = this.Document.CreateAttribute(name, prefix);
  144. xa.Value = text;
  145. this.Node.Attributes.Append(xa);
  146. }
  147. #region IContent Member
  148. /// <summary>
  149. /// Gets or sets the name of the style.
  150. /// </summary>
  151. /// <value>The name of the style.</value>
  152. public string StyleName
  153. {
  154. get
  155. {
  156. XmlNode xn = this._node.SelectSingleNode("@text:style-name",
  157. this.Document.NamespaceManager);
  158. if (xn != null)
  159. return xn.InnerText;
  160. return null;
  161. }
  162. set
  163. {
  164. XmlNode xn = this._node.SelectSingleNode("@text:style-name",
  165. this.Document.NamespaceManager);
  166. if (xn == null)
  167. this.CreateAttribute("style-name", value, "text");
  168. this._node.SelectSingleNode("@text:style-name",
  169. this.Document.NamespaceManager).InnerText = value;
  170. }
  171. }
  172. private IDocument _document;
  173. /// <summary>
  174. /// Every object (typeof(IContent)) have to know his document.
  175. /// </summary>
  176. /// <value></value>
  177. public IDocument Document
  178. {
  179. get
  180. {
  181. return this._document;
  182. }
  183. set
  184. {
  185. this._document = value;
  186. }
  187. }
  188. private IStyle _style;
  189. /// <summary>
  190. /// A Style class wich is referenced with the content object.
  191. /// If no style is available this is null.
  192. /// </summary>
  193. /// <value></value>
  194. public IStyle Style
  195. {
  196. get
  197. {
  198. return this._style;
  199. }
  200. set
  201. {
  202. this.StyleName = value.StyleName;
  203. this._style = value;
  204. }
  205. }
  206. private XmlNode _node;
  207. /// <summary>
  208. /// Gets or sets the node.
  209. /// </summary>
  210. /// <value>The node.</value>
  211. public XmlNode Node
  212. {
  213. get { return this._node; }
  214. set { this._node = value; }
  215. }
  216. #endregion
  217. /// <summary>
  218. /// Content_s the inserted.
  219. /// </summary>
  220. /// <param name="index">The index.</param>
  221. /// <param name="value">The value.</param>
  222. private void Content_Inserted(int index, object value)
  223. {
  224. this.Node.AppendChild(((IContent)value).Node);
  225. }
  226. /// <summary>
  227. /// Content_s the removed.
  228. /// </summary>
  229. /// <param name="index">The index.</param>
  230. /// <param name="value">The value.</param>
  231. private void Content_Removed(int index, object value)
  232. {
  233. this.Node.RemoveChild(((IContent)value).Node);
  234. }
  235. #region IHtml Member
  236. /// <summary>
  237. /// Return the content as Html string
  238. /// </summary>
  239. /// <returns>The html string</returns>
  240. public string GetHtml()
  241. {
  242. string html = null;
  243. if (this.ListType == ListStyles.Bullet)
  244. html = "<ul>\n";
  245. else if (this.ListType == ListStyles.Number)
  246. html = "<ol>\n";
  247. foreach(IContent content in this.Content)
  248. if (content is IHtml)
  249. html += ((IHtml)content).GetHtml();
  250. if (this.ListType == ListStyles.Bullet)
  251. html += "</ul>\n";
  252. else if (this.ListType == ListStyles.Number)
  253. html += "</ol>\n";
  254. //html += "</ul>\n";
  255. return html;
  256. }
  257. #endregion
  258. }
  259. }