PageRenderTime 131ms CodeModel.GetById 26ms RepoModel.GetById 4ms app.codeStats 0ms

/AODL/Document/Import/OpenDocument/NodeProcessors/MainContentProcessor.cs

https://bitbucket.org/chrisc/aodl
C# | 1409 lines | 843 code | 158 blank | 408 comment | 151 complexity | 6f6b4c5cfdf367bb5cb14813f6edf3bf MD5 | raw file

Large files files are truncated, but you can click here to view the full 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.Collections.Generic;
  24. using System.Linq;
  25. using System.Xml;
  26. using System.Xml.Linq;
  27. using AODL.Document.Content;
  28. using AODL.Document.Content.Draw;
  29. using AODL.Document.Content.EmbedObjects;
  30. using AODL.Document.Content.OfficeEvents;
  31. using AODL.Document.Content.Tables;
  32. using AODL.Document.Content.Text;
  33. using AODL.Document.Content.Text.Indexes;
  34. using AODL.Document.Exceptions;
  35. using AODL.Document.Forms.Controls;
  36. using AODL.Document.SpreadsheetDocuments;
  37. using AODL.Document.Styles;
  38. using AODL.Document.TextDocuments;
  39. namespace AODL.Document.Import.OpenDocument.NodeProcessors
  40. {
  41. public class MainContentProcessor
  42. {
  43. #region Delegates
  44. /// <summary>
  45. /// Warning delegate
  46. /// </summary>
  47. public delegate void WarningHandler(AODLWarning warning);
  48. #endregion
  49. /// <summary>
  50. /// The textdocument
  51. /// </summary>
  52. private readonly IDocument _document;
  53. /// <summary>
  54. /// If set to true all node content would be directed
  55. /// to Console.Out
  56. /// </summary>
  57. private bool _debugMode;
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="MainContentProcessor"/> class.
  60. /// </summary>
  61. /// <param name="document">The document.</param>
  62. public MainContentProcessor(IDocument document)
  63. {
  64. _document = document;
  65. }
  66. /// <summary>
  67. /// Warning event fired if something unexpected
  68. /// occour.
  69. /// </summary>
  70. public event WarningHandler Warning;
  71. private void OnWarning(AODLWarning warning)
  72. {
  73. if (Warning != null)
  74. {
  75. Warning(warning);
  76. }
  77. }
  78. private void AddToCollection(IContent content, ContentCollection coll)
  79. {
  80. coll.Add(content);
  81. if (content is ODFControlRef)
  82. {
  83. ODFControlRef ctrlRef = content as ODFControlRef;
  84. if (_document is TextDocument)
  85. {
  86. TextDocument td = _document as TextDocument;
  87. ODFFormControl fc = td.FindControlById(ctrlRef.DrawControl);
  88. if (fc != null)
  89. {
  90. fc.ContentCollection = coll;
  91. fc.ControlRef = ctrlRef;
  92. }
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Reads the content nodes.
  98. /// </summary>
  99. public void ReadContentNodes()
  100. {
  101. try
  102. {
  103. // this._document.XmlDoc = new XDocument();
  104. // this._document.XmlDoc.Load(contentFile);
  105. XElement node = null;
  106. if (_document is TextDocument)
  107. node = _document.XmlDoc.Elements(Ns.Office + "document-content")
  108. .Elements(Ns.Office + "body")
  109. .Elements(Ns.Office + "text").FirstOrDefault();
  110. else if (_document is SpreadsheetDocument)
  111. node = _document.XmlDoc.Elements(Ns.Office + "document-content")
  112. .Elements(Ns.Office + "body")
  113. .Elements(Ns.Office + "spreadsheet").FirstOrDefault();
  114. if (node != null)
  115. {
  116. CreateMainContent(node);
  117. }
  118. else
  119. {
  120. throw new AODLException("Unknow content type.");
  121. }
  122. //Remove all existing content will be created new
  123. node.RemoveAll();
  124. }
  125. catch (Exception ex)
  126. {
  127. throw new AODLException("Error while trying to load the content file!", ex);
  128. }
  129. }
  130. /// <summary>
  131. /// Creates the content.
  132. /// </summary>
  133. /// <param name="node">The node.</param>
  134. public void CreateMainContent(XElement node)
  135. {
  136. try
  137. {
  138. foreach (XElement nodeChild in node.Elements())
  139. {
  140. IContent iContent = CreateContent(new XElement(nodeChild));
  141. if (iContent != null)
  142. AddToCollection(iContent, _document.Content);
  143. //this._document.Content.Add(iContent);
  144. else
  145. {
  146. OnWarning(new AODLWarning("A couldn't create any content from an an first level node!.",
  147. nodeChild));
  148. }
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. throw new AODLException("Exception while processing a content node.", ex);
  154. }
  155. }
  156. /// <summary>
  157. /// Gets the content.
  158. /// </summary>
  159. /// <param name="node">The node to clone and create content from.</param>
  160. /// <returns></returns>
  161. public IContent CreateContent(XNode node)
  162. {
  163. try
  164. {
  165. if (node is XElement)
  166. {
  167. XElement element = (XElement) node;
  168. if (element.Name == Ns.Text + "p")
  169. return CreateParagraph(new XElement(element));
  170. if (element.Name == Ns.Text + "list")
  171. return CreateList(new XElement(element));
  172. if (element.Name == Ns.Text + "list-item")
  173. return CreateListItem(new XElement(element));
  174. if (element.Name == Ns.Table + "table")
  175. return CreateTable(new XElement(element));
  176. if (element.Name == Ns.Table + "table-column")
  177. return CreateTableColumn(new XElement(element));
  178. if (element.Name == Ns.Table + "table-row")
  179. return CreateTableRow(new XElement(element));
  180. if (element.Name == Ns.Table + "table-header-rows")
  181. return CreateTableHeaderRow(new XElement(element));
  182. if (element.Name == Ns.Table + "table-cell")
  183. return CreateTableCell(new XElement(element));
  184. if (element.Name == Ns.Table + "covered-table-cell")
  185. return CreateTableCellSpan(new XElement(element));
  186. if (element.Name == Ns.Text + "h")
  187. return CreateHeader(new XElement(element));
  188. if (element.Name == Ns.Text + "table-of-content")
  189. //Possible?
  190. return CreateTableOfContents(new XElement(element));
  191. if (element.Name == Ns.Draw + "frame")
  192. return CreateFrame(new XElement(element));
  193. if (element.Name == Ns.Draw + "object")
  194. return CreateEmbedObject(new XElement(element));
  195. if (element.Name == Ns.Draw + "text-box")
  196. return CreateDrawTextBox(new XElement(element));
  197. if (element.Name == Ns.Draw + "image")
  198. return CreateGraphic(new XElement(element));
  199. //@Liu Yuhua: What's that??? This is of course a image and not unknown!! Lars
  200. //return new UnknownContent(this._document, new XElement(element));
  201. if (element.Name == Ns.Draw + "area-rectangle")
  202. return CreateDrawAreaRectangle(new XElement(element));
  203. if (element.Name == Ns.Draw + "area-circle")
  204. return CreateDrawAreaCircle(new XElement(element));
  205. if (element.Name == Ns.Draw + "image-map")
  206. return CreateImageMap(new XElement(element));
  207. if (element.Name == Ns.Office + "event-listeners")
  208. return CreateEventListeners(new XElement(element));
  209. if (element.Name == Ns.Script + "event-listener")
  210. return CreateEventListeners(new XElement(element));
  211. if (element.Name == Ns.Draw + "control")
  212. return CreateControlRef(new XElement(element));
  213. return new UnknownContent(_document, new XElement(element));
  214. }
  215. return new SimpleText(_document, ((XText) node).Value);
  216. }
  217. catch (Exception ex)
  218. {
  219. throw new AODLException("Exception while processing a content node.", ex);
  220. }
  221. }
  222. /// <summary>
  223. /// Creates the table of contents.
  224. /// </summary>
  225. /// <param name="tocNode">The toc node.</param>
  226. /// <returns></returns>
  227. private TableOfContents CreateTableOfContents(XElement tocNode)
  228. {
  229. try
  230. {
  231. if (_document is TextDocument)
  232. {
  233. //Create the TableOfContents object
  234. TableOfContents tableOfContents = new TableOfContents(
  235. (_document), tocNode);
  236. //Recieve the Section style
  237. IStyle sectionStyle = _document.Styles.GetStyleByName(tableOfContents.StyleName);
  238. if (sectionStyle != null)
  239. tableOfContents.Style = sectionStyle;
  240. else
  241. {
  242. OnWarning(new AODLWarning("A SectionStyle for the TableOfContents object wasn't found.", tocNode));
  243. }
  244. //Create the text entries
  245. IEnumerable<XElement> paragraphNodeList =
  246. tocNode.Elements(Ns.Text + "index-body").Elements(Ns.Text + "p");
  247. XElement indexBodyNode = tocNode.Element(Ns.Text + "index-body");
  248. tableOfContents.IndexBodyNode = indexBodyNode;
  249. ContentCollection pCollection = new ContentCollection();
  250. foreach (XElement paragraphnode in paragraphNodeList)
  251. {
  252. Paragraph paragraph = CreateParagraph(paragraphnode);
  253. if (indexBodyNode != null)
  254. paragraphnode.Remove();
  255. //pCollection.Add(paragraph);
  256. AddToCollection(paragraph, pCollection);
  257. }
  258. foreach (IContent content in pCollection)
  259. AddToCollection(content, tableOfContents.Content);
  260. //tableOfContents.Content.Add(content);
  261. return tableOfContents;
  262. }
  263. return null;
  264. }
  265. catch (Exception ex)
  266. {
  267. throw new AODLException("Exception while trying to create a TableOfContents.", ex);
  268. }
  269. }
  270. /// <summary>
  271. /// Creates the paragraph.
  272. /// </summary>
  273. /// <param name="paragraphNode">The paragraph node.</param>
  274. public Paragraph CreateParagraph(XElement paragraphNode)
  275. {
  276. try
  277. {
  278. //Create a new Paragraph
  279. Paragraph paragraph = new Paragraph(paragraphNode, _document);
  280. //Recieve the ParagraphStyle
  281. IStyle paragraphStyle = _document.Styles.GetStyleByName(paragraph.StyleName);
  282. if (paragraphStyle != null)
  283. {
  284. paragraph.Style = paragraphStyle;
  285. }
  286. else if (paragraph.StyleName != "Standard"
  287. && paragraph.StyleName != "Table_20_Contents"
  288. && paragraph.StyleName != "Text_20_body"
  289. && _document is TextDocument)
  290. {
  291. //Check if it's a user defined style
  292. IStyle commonStyle = _document.CommonStyles.GetStyleByName(paragraph.StyleName);
  293. if (commonStyle == null)
  294. {
  295. OnWarning(new AODLWarning(string.Format(
  296. "A ParagraphStyle '{0}' wasn't found.", paragraph.StyleName),
  297. paragraph.Node));
  298. }
  299. }
  300. return ReadParagraphTextContent(paragraph);
  301. }
  302. catch (Exception ex)
  303. {
  304. throw new AODLException("Exception while trying to create a Paragraph.", ex);
  305. }
  306. }
  307. /// <summary>
  308. /// Reads the content of the paragraph text.
  309. /// </summary>
  310. /// <param name="paragraph">The paragraph.</param>
  311. /// <returns></returns>
  312. private Paragraph ReadParagraphTextContent(Paragraph paragraph)
  313. {
  314. try
  315. {
  316. if (_debugMode)
  317. LogNode(paragraph.Node, "Log Paragraph node before");
  318. IList<IContent> mixedContent = new List<IContent>();
  319. foreach (XNode nodeChild in paragraph.Node.Nodes())
  320. {
  321. //Check for IText content first
  322. TextContentProcessor tcp = new TextContentProcessor();
  323. IText iText = tcp.CreateTextObject(_document, nodeChild);
  324. if (iText != null)
  325. mixedContent.Add(iText);
  326. else
  327. {
  328. //Check against IContent
  329. IContent iContent = CreateContent(nodeChild);
  330. if (iContent != null)
  331. mixedContent.Add(iContent);
  332. }
  333. }
  334. //Remove all
  335. paragraph.Node.Value = "";
  336. foreach (IContent ob in mixedContent)
  337. {
  338. if (ob is IText)
  339. {
  340. if (_debugMode)
  341. LogNode(ob.Node, "Log IText node read");
  342. paragraph.TextContent.Add(ob as IText);
  343. }
  344. else
  345. {
  346. if (_debugMode)
  347. LogNode(ob.Node, "Log IContent node read");
  348. //paragraph.Content.Add(ob as IContent);
  349. AddToCollection(ob, paragraph.Content);
  350. }
  351. }
  352. if (_debugMode)
  353. LogNode(paragraph.Node, "Log Paragraph node after");
  354. return paragraph;
  355. }
  356. catch (Exception ex)
  357. {
  358. throw new AODLException("Exception while trying to create the Paragraph content.", ex);
  359. }
  360. }
  361. /// <summary>
  362. /// Creates the header.
  363. /// </summary>
  364. /// <param name="headernode">The headernode.</param>
  365. /// <returns></returns>
  366. public Header CreateHeader(XElement headernode)
  367. {
  368. try
  369. {
  370. if (_debugMode)
  371. LogNode(headernode, "Log header node before");
  372. //Create a new Header
  373. Header header = new Header(headernode, _document);
  374. //Create a ITextCollection
  375. ITextCollection textColl = new ITextCollection();
  376. //Recieve the HeaderStyle
  377. IStyle headerStyle = _document.Styles.GetStyleByName(header.StyleName);
  378. if (headerStyle != null)
  379. header.Style = headerStyle;
  380. //Create the IText content
  381. foreach (XNode nodeChild in header.Node.Nodes())
  382. {
  383. TextContentProcessor tcp = new TextContentProcessor();
  384. IText iText = tcp.CreateTextObject(_document, nodeChild);
  385. if (iText != null)
  386. textColl.Add(iText);
  387. else
  388. {
  389. OnWarning(new AODLWarning("Couldn't create IText object from header child node!.", nodeChild));
  390. }
  391. }
  392. //Remove all
  393. header.Node.Value = "";
  394. foreach (IText iText in textColl)
  395. {
  396. if (_debugMode)
  397. LogNode(iText.Node, "Log IText node read from header");
  398. header.TextContent.Add(iText);
  399. }
  400. return header;
  401. }
  402. catch (Exception ex)
  403. {
  404. throw new AODLException("Exception while trying to create a Header.", ex);
  405. }
  406. }
  407. /// <summary>
  408. /// Creates the graphic.
  409. /// </summary>
  410. /// <param name="graphicnode">The graphicnode.</param>
  411. /// <returns>The Graphic object</returns>
  412. private Graphic CreateGraphic(XElement graphicnode)
  413. {
  414. try
  415. {
  416. Graphic graphic = new Graphic(_document, null, null) {Node = graphicnode};
  417. graphic.GraphicFile = _document.Importer.GetFile(graphic.HRef);
  418. return graphic;
  419. }
  420. catch (Exception ex)
  421. {
  422. throw new AODLException("Exception while trying to create a Graphic.", ex);
  423. }
  424. }
  425. /// <summary>
  426. /// Creates the draw text box.
  427. /// </summary>
  428. /// <param name="drawTextBoxNode">The draw text box node.</param>
  429. /// <returns></returns>
  430. private DrawTextBox CreateDrawTextBox(XElement drawTextBoxNode)
  431. {
  432. try
  433. {
  434. DrawTextBox drawTextBox = new DrawTextBox(_document, drawTextBoxNode);
  435. ContentCollection iColl = new ContentCollection();
  436. foreach (XNode nodeChild in drawTextBox.Node.Nodes())
  437. {
  438. IContent iContent = CreateContent(nodeChild);
  439. if (iContent != null)
  440. //iColl.Add(iContent);
  441. AddToCollection(iContent, iColl);
  442. else
  443. {
  444. OnWarning(new AODLWarning("Couldn't create a IContent object for a DrawTextBox.", nodeChild));
  445. }
  446. }
  447. drawTextBox.Node.Value = "";
  448. foreach (IContent iContent in iColl)
  449. AddToCollection(iContent, drawTextBox.Content);
  450. //drawTextBox.Content.Add(iContent);
  451. return drawTextBox;
  452. }
  453. catch (Exception ex)
  454. {
  455. throw new AODLException("Exception while trying to create a Graphic.", ex);
  456. }
  457. }
  458. /// <summary>
  459. /// Creates the draw area rectangle.
  460. /// </summary>
  461. /// <param name="drawAreaRectangleNode">The draw area rectangle node.</param>
  462. /// <returns></returns>
  463. private DrawAreaRectangle CreateDrawAreaRectangle(XElement drawAreaRectangleNode)
  464. {
  465. try
  466. {
  467. DrawAreaRectangle dAreaRec = new DrawAreaRectangle(_document, drawAreaRectangleNode);
  468. ContentCollection iCol = new ContentCollection();
  469. if (dAreaRec.Node != null)
  470. foreach (XNode nodeChild in dAreaRec.Node.Nodes())
  471. {
  472. IContent iContent = CreateContent(nodeChild);
  473. if (iContent != null)
  474. AddToCollection(iContent, iCol);
  475. //iCol.Add(iContent);
  476. }
  477. dAreaRec.Node.Value = "";
  478. foreach (IContent iContent in iCol)
  479. AddToCollection(iContent, dAreaRec.Content);
  480. //dAreaRec.Content.Add(iContent);
  481. return dAreaRec;
  482. }
  483. catch (Exception ex)
  484. {
  485. throw new AODLException("Exception while trying to create a DrawAreaRectangle.", ex);
  486. }
  487. }
  488. /// <summary>
  489. /// Creates the draw area circle.
  490. /// </summary>
  491. /// <param name="drawAreaCircleNode">The draw area circle node.</param>
  492. /// <returns></returns>
  493. private DrawAreaCircle CreateDrawAreaCircle(XElement drawAreaCircleNode)
  494. {
  495. try
  496. {
  497. DrawAreaCircle dAreaCirc = new DrawAreaCircle(_document, drawAreaCircleNode);
  498. ContentCollection iCol = new ContentCollection();
  499. if (dAreaCirc.Node != null)
  500. foreach (XNode nodeChild in dAreaCirc.Node.Nodes())
  501. {
  502. IContent iContent = CreateContent(nodeChild);
  503. if (iContent != null)
  504. AddToCollection(iContent, iCol);
  505. //iCol.Add(iContent);
  506. }
  507. dAreaCirc.Node.Value = "";
  508. foreach (IContent iContent in iCol)
  509. AddToCollection(iContent, dAreaCirc.Content);
  510. //dAreaCirc.Content.Add(iContent);
  511. return dAreaCirc;
  512. }
  513. catch (Exception ex)
  514. {
  515. throw new AODLException("Exception while trying to create a DrawAreaCircle.", ex);
  516. }
  517. }
  518. /// <summary>
  519. /// Creates the image map.
  520. /// </summary>
  521. /// <param name="imageMapNode">The image map node.</param>
  522. /// <returns></returns>
  523. private ImageMap CreateImageMap(XElement imageMapNode)
  524. {
  525. try
  526. {
  527. ImageMap imageMap = new ImageMap(_document, imageMapNode);
  528. ContentCollection iCol = new ContentCollection();
  529. if (imageMap.Node != null)
  530. foreach (XNode nodeChild in imageMap.Node.Nodes())
  531. {
  532. IContent iContent = CreateContent(nodeChild);
  533. if (iContent != null)
  534. AddToCollection(iContent, iCol);
  535. //iCol.Add(iContent);
  536. }
  537. imageMap.Node.Value = "";
  538. foreach (IContent iContent in iCol)
  539. AddToCollection(iContent, imageMap.Content);
  540. //imageMap.Content.Add(iContent);
  541. return imageMap;
  542. }
  543. catch (Exception ex)
  544. {
  545. throw new AODLException("Exception while trying to create a ImageMap.", ex);
  546. }
  547. }
  548. /// <summary>
  549. /// Creates the event listener.
  550. /// </summary>
  551. /// <param name="eventListenerNode">The event listener node.</param>
  552. /// <returns></returns>
  553. public EventListener CreateEventListener(XElement eventListenerNode)
  554. {
  555. try
  556. {
  557. EventListener eventListener = new EventListener(_document, eventListenerNode);
  558. return eventListener;
  559. }
  560. catch (Exception ex)
  561. {
  562. throw new AODLException("Exception while trying to create a EventListener.", ex);
  563. }
  564. }
  565. /// <summary>
  566. /// Creates the event listeners.
  567. /// </summary>
  568. /// <param name="eventListenersNode">The event listeners node.</param>
  569. /// <returns></returns>
  570. public EventListeners CreateEventListeners(XElement eventListenersNode)
  571. {
  572. try
  573. {
  574. EventListeners eventList = new EventListeners(_document, eventListenersNode);
  575. ContentCollection iCol = new ContentCollection();
  576. if (eventList.Node != null)
  577. foreach (XNode nodeChild in eventList.Node.Nodes())
  578. {
  579. IContent iContent = CreateContent(nodeChild);
  580. if (iContent != null)
  581. AddToCollection(iContent, iCol);
  582. //iCol.Add(iContent);
  583. }
  584. eventList.Node.Value = "";
  585. foreach (IContent iContent in iCol)
  586. AddToCollection(iContent, eventList.Content);
  587. //eventList.Content.Add(iContent);
  588. return eventList;
  589. }
  590. catch (Exception ex)
  591. {
  592. throw new AODLException("Exception while trying to create a ImageMap.", ex);
  593. }
  594. }
  595. /// <summary>
  596. /// Creates the frame.
  597. /// </summary>
  598. /// <param name="refNode">The framenode.</param>
  599. /// <returns>The Frame object.</returns>
  600. public ODFControlRef CreateControlRef(XElement refNode)
  601. {
  602. try
  603. {
  604. ODFControlRef controlRef = new ODFControlRef(_document, refNode);
  605. return controlRef;
  606. }
  607. catch (Exception ex)
  608. {
  609. throw new AODLException("Exception while trying to create a Control Reference.", ex);
  610. }
  611. }
  612. /// <summary>
  613. /// Creates the frame.
  614. /// </summary>
  615. /// <param name="frameNode">The framenode.</param>
  616. /// <returns>The Frame object.</returns>
  617. public Frame CreateFrame(XElement frameNode)
  618. {
  619. try
  620. {
  621. #region Old code Todo: delete
  622. // Frame frame = null;
  623. // XElement graphicnode = null;
  624. // XElement graphicproperties = null;
  625. // string realgraphicname = "";
  626. // string stylename = "";
  627. // stylename = this.GetStyleName(framenode.OuterXml);
  628. // XElement stylenode = this.GetAStyleNode("style:style", stylename);
  629. // realgraphicname = this.GetAValueFromAnAttribute(framenode, "@draw:name");
  630. //
  631. // //Console.WriteLine("frame: {0}", framenode.OuterXml);
  632. //
  633. // //Up to now, the only sopported, inner content of a frame is a graphic
  634. // if (framenode.Elements().Count > 0)
  635. // if (framenode.Elements().Item(0).OuterXml.StartsWith("<draw:image"))
  636. // graphicnode = framenode.Elements().Item(0).CloneNode(true);
  637. //
  638. // //If not graphic, it could be text-box, ole or something else
  639. // //try to find graphic frame inside
  640. // if (graphicnode == null)
  641. // {
  642. // XElement child = framenode.SelectSingleNode("//draw:frame", this._document.NamespaceManager);
  643. // if (child != null)
  644. // frame = this.CreateFrame(child);
  645. // return frame;
  646. // }
  647. //
  648. // string graphicpath = this.GetAValueFromAnAttribute(graphicnode, "@xlink:href");
  649. //
  650. // if (stylenode != null)
  651. // if (stylenode.Elements().Count > 0)
  652. // if (stylenode.Elements().Item(0).OuterXml.StartsWith("<style:graphic-properties"))
  653. // graphicproperties = stylenode.Elements().Item(0).CloneNode(true);
  654. //
  655. // if (stylename.Length > 0 && stylenode != null && realgraphicname.Length > 0
  656. // && graphicnode != null && graphicpath.Length > 0 && graphicproperties != null)
  657. // {
  658. // graphicpath = graphicpath.Replace("Pictures", "");
  659. // graphicpath = OpenDocumentTextImporter.dirpics+graphicpath.Replace("/", @"\");
  660. //
  661. // frame = new Frame(this._document, stylename,
  662. // realgraphicname, graphicpath);
  663. //
  664. // frame.Style.Node = stylenode;
  665. // frame.Graphic.Node = graphicnode;
  666. // ((FrameStyle)frame.Style).GraphicProperties.Node = graphicproperties;
  667. //
  668. // XElement nodeSize = framenode.SelectSingleNode("@svg:height",
  669. // this._document.NamespaceManager);
  670. //
  671. // if (nodeSize != null)
  672. // if (nodeSize.InnerText != null)
  673. // frame.GraphicHeight = nodeSize.InnerText;
  674. //
  675. // nodeSize = framenode.SelectSingleNode("@svg:width",
  676. // this._document.NamespaceManager);
  677. //
  678. // if (nodeSize != null)
  679. // if (nodeSize.InnerText != null)
  680. // frame.GraphicWidth = nodeSize.InnerText;
  681. // }
  682. #endregion
  683. //Create a new Frame
  684. Frame frame = new Frame(_document, null) {Node = frameNode};
  685. ContentCollection iColl = new ContentCollection();
  686. //Revieve the FrameStyle
  687. IStyle frameStyle = _document.Styles.GetStyleByName(frame.StyleName);
  688. if (frameStyle != null)
  689. frame.Style = frameStyle;
  690. else
  691. {
  692. OnWarning(new AODLWarning("Couldn't recieve a FrameStyle.", frameNode));
  693. }
  694. //Create the frame content
  695. foreach (XNode nodeChild in frame.Node.Nodes())
  696. {
  697. IContent iContent = CreateContent(nodeChild);
  698. if (iContent != null)
  699. AddToCollection(iContent, iColl);
  700. //iColl.Add(iContent);
  701. else
  702. {
  703. OnWarning(new AODLWarning("Couldn't create a IContent object for a frame.", nodeChild));
  704. }
  705. }
  706. frame.Node.Value = "";
  707. foreach (IContent iContent in iColl)
  708. {
  709. AddToCollection(iContent, frame.Content);
  710. //frame.Content.Add(iContent);
  711. if (iContent is Graphic)
  712. {
  713. LoadFrameGraphic(frame, iContent as Graphic);
  714. }
  715. if (iContent is EmbedObject)
  716. {
  717. //(EmbedObject(iContent)).Frame =frame;
  718. (iContent as EmbedObject).Frame = frame;
  719. }
  720. }
  721. return frame;
  722. }
  723. catch (Exception ex)
  724. {
  725. throw new AODLException("Exception while trying to create a Frame.", ex);
  726. }
  727. }
  728. private void LoadFrameGraphic(Frame frame, Graphic content)
  729. {
  730. try
  731. {
  732. frame.LoadImageFromFile(content.GraphicFile);
  733. }
  734. catch (AODLGraphicException e)
  735. {
  736. OnWarning(
  737. new AODLWarning("A couldn't create any content from an an first level node!.", content.Node, e));
  738. }
  739. }
  740. private EmbedObject CreateEmbedObject(XElement objNode)
  741. {
  742. try
  743. {
  744. XElement objectNode = new XElement(objNode);
  745. string href = (string) objectNode.Attribute(Ns.XLink + "href");
  746. string objectFullPath = href.Substring(2) + "/";
  747. string objectRealPath = href.Substring(2);
  748. string objectName = href.Substring(2);
  749. //ObjectRealPath = ObjectRealPath.Replace ("/","\\");
  750. string mediaType = GetMediaType(objectFullPath);
  751. EmbedObjectHandler embedobjhandler = new EmbedObjectHandler(_document);
  752. return embedobjhandler.CreateEmbedObject(objectNode, mediaType, objectRealPath, objectName);
  753. }
  754. catch (Exception ex)
  755. {
  756. throw new AODLException("Exception while trying to create a Graphic.", ex);
  757. }
  758. }
  759. public string GetMediaType(string objectFullPath)
  760. {
  761. XDocument doc = ((SpreadsheetDocument) _document).DocumentManifest.Manifest;
  762. XElement node = doc.Element(Ns.Manifest + "manifest");
  763. foreach (XElement nodeChild in node.Elements())
  764. {
  765. //XElement Entry = nodeChild.SelectSingleNode ("@manifest:file-entry",this._document.NamespaceManager);
  766. string fullPath = (string) nodeChild.Attribute(Ns.Manifest + "full-path");
  767. if (fullPath == objectFullPath)
  768. {
  769. string mediaType = (string) nodeChild.Attribute(Ns.Manifest + "media-type");
  770. if (!string.IsNullOrEmpty(mediaType))
  771. return mediaType;
  772. }
  773. }
  774. return null;
  775. }
  776. /// <summary>
  777. /// Creates the list.
  778. /// </summary>
  779. /// <param name="listNode">The list node.</param>
  780. /// <returns>The List object</returns>
  781. private List CreateList(XElement listNode)
  782. {
  783. try
  784. {
  785. #region Old code Todo: delete
  786. // string stylename = null;
  787. // XElement stylenode = null;
  788. // ListStyles liststyles = ListStyles.Bullet; //as default
  789. // string paragraphstylename = null;
  790. //
  791. // if (outerlist == null)
  792. // {
  793. // stylename = this.GetStyleName(listNode.OuterXml);
  794. // stylenode = this.GetAStyleNode("text:list-style", stylename);
  795. // liststyles = this.GetListStyle(listNode);
  796. // }
  797. // List list = null;
  798. //
  799. // if (listNode.Elements().Count > 0)
  800. // {
  801. // try
  802. // {
  803. // paragraphstylename = this.GetAValueFromAnAttribute(listNode.Elements().Item(0).Elements().Item(0), "@style:style-name");
  804. // }
  805. // catch(Exception ex)
  806. // {
  807. // paragraphstylename = "P1";
  808. // }
  809. // }
  810. #endregion
  811. //Create a new List
  812. List list = new List(_document, listNode);
  813. ContentCollection iColl = new ContentCollection();
  814. //Revieve the ListStyle
  815. IStyle listStyle = _document.Styles.GetStyleByName(list.StyleName);
  816. if (listStyle != null)
  817. list.Style = listStyle;
  818. foreach (XNode nodeChild in list.Node.Nodes())
  819. {
  820. IContent iContent = CreateContent(nodeChild);
  821. if (iContent != null)
  822. AddToCollection(iContent, iColl);
  823. //iColl.Add(iContent);
  824. }
  825. list.Node.Value = "";
  826. foreach (IContent iContent in iColl)
  827. AddToCollection(iContent, list.Content);
  828. //list.Content.Add(iContent);
  829. return list;
  830. }
  831. catch (Exception ex)
  832. {
  833. throw new AODLException("Exception while trying to create a List.", ex);
  834. }
  835. }
  836. /// <summary>
  837. /// Creates the list item.
  838. /// </summary>
  839. /// <param name="node">The node.</param>
  840. /// <returns></returns>
  841. private ListItem CreateListItem(XElement node)
  842. {
  843. try
  844. {
  845. ListItem listItem = new ListItem(_document);
  846. ContentCollection iColl = new ContentCollection();
  847. listItem.Node = node;
  848. foreach (XNode nodeChild in listItem.Node.Nodes())
  849. {
  850. IContent iContent = CreateContent(nodeChild);
  851. if (iContent != null)
  852. AddToCollection(iContent, iColl);
  853. //iColl.Add(iContent);
  854. else
  855. {
  856. OnWarning(new AODLWarning("Couldn't create a IContent object for a ListItem.", nodeChild));
  857. }
  858. }
  859. listItem.Node.Value = "";
  860. foreach (IContent iContent in iColl)
  861. //listItem.Content.Add(iContent);
  862. AddToCollection(iContent, listItem.Content);
  863. return listItem;
  864. }
  865. catch (Exception ex)
  866. {
  867. throw new AODLException("Exception while trying to create a ListItem.", ex);
  868. }
  869. }
  870. /// <summary>
  871. /// Creates the table.
  872. /// </summary>
  873. /// <param name="tableNode">The tablenode.</param>
  874. /// <returns></returns>
  875. private Table CreateTable(XElement tableNode)
  876. {
  877. try
  878. {
  879. //Create a new table
  880. Table table = new Table(_document, tableNode);
  881. ContentCollection iColl = new ContentCollection();
  882. //Recieve the table style
  883. IStyle tableStyle = _document.Styles.GetStyleByName(table.StyleName);
  884. if (tableStyle != null)
  885. table.Style = tableStyle;
  886. else
  887. {
  888. OnWarning(new AODLWarning("Couldn't recieve a TableStyle.", tableNode));
  889. }
  890. //Create the table content
  891. foreach (XNode nodeChild in table.Node.Nodes())
  892. {
  893. IContent iContent = CreateContent(nodeChild);
  894. if (iContent != null)
  895. {
  896. //iColl.Add(iContent);
  897. AddToCollection(iContent, iColl);
  898. }
  899. else
  900. {
  901. OnWarning(
  902. new AODLWarning(
  903. "Couldn't create IContent from a table node. Content is unknown table content!",
  904. iContent.Node));
  905. }
  906. }
  907. table.Node.Value = "";
  908. foreach (IContent iContent in iColl)
  909. {
  910. if (iContent is Column)
  911. {
  912. ((Column) iContent).Table = table;
  913. table.ColumnCollection.Add(iContent as Column);
  914. }
  915. else if (iContent is Row)
  916. {
  917. ((Row) iContent).Table = table;
  918. table.Rows.Add(iContent as Row);
  919. }
  920. else if (iContent is RowHeader)
  921. {
  922. ((RowHeader) iContent).Table = table;
  923. table.RowHeader = iContent as RowHeader;
  924. }
  925. else
  926. {
  927. table.Node.Add(iContent.Node);
  928. OnWarning(new AODLWarning("Couldn't create IContent from a table node.", tableNode));
  929. }
  930. }
  931. return table;
  932. }
  933. catch (Exception ex)
  934. {
  935. throw new AODLException("Exception while trying to create a Table.", ex);
  936. }
  937. }
  938. /// <summary>
  939. /// Creates the table row.
  940. /// </summary>
  941. /// <param name="node">The node.</param>
  942. /// <returns></returns>
  943. private Row CreateTableRow(XElement node)
  944. {
  945. try
  946. {
  947. //Create a new Row
  948. Row row = new Row(_document, node);
  949. ContentCollection iColl = new ContentCollection();
  950. //Recieve RowStyle
  951. IStyle rowStyle = _document.Styles.GetStyleByName(row.StyleName);
  952. if (rowStyle != null)
  953. row.Style = rowStyle;
  954. //No need for a warning
  955. //Create the cells
  956. foreach (XElement nodeChild in row.Node.Elements())
  957. {
  958. // Phil Jollans 24-March-2008
  959. // Handle the attribute table:number-columns-repeated on cell nodes,
  960. // by inserting multiple nodes. CreateContent clones the nodes so this
  961. // seems fairly safe.
  962. int iRepeatCount = 1;
  963. XAttribute xn = nodeChild.Attribute(Ns.Table + "number-columns-repeated");
  964. if (xn != null)
  965. {
  966. iRepeatCount = int.Parse(xn.Value);
  967. // Inetrnally, the node is no longer repeated, so it seems correct
  968. // to remove the the attribute table:number-columns-repeated.
  969. xn.Remove();
  970. }
  971. for (int i = 0; i < iRepeatCount; i++)
  972. {
  973. IContent iContent = CreateContent(nodeChild);
  974. if (iContent != null)
  975. {
  976. //iColl.Add(iContent);
  977. AddToCollection(iContent, iColl);
  978. }
  979. else
  980. {
  981. OnWarning(new AODLWarning("Couldn't create IContent from a table row.", nodeChild));
  982. }
  983. }
  984. }
  985. row.Node.Value = "";
  986. foreach (IContent iContent in iColl)
  987. {
  988. if (iContent is Cell)
  989. {
  990. ((Cell) iContent).Row = row;
  991. row.Cells.Add(iContent as Cell);
  992. }
  993. else if (iContent is CellSpan)
  994. {
  995. ((CellSpan) iContent).Row = row;
  996. row.CellSpanCollection.Add(iContent as CellSpan);
  997. }
  998. else
  999. {
  1000. OnWarning(
  1001. new AODLWarning(
  1002. "Couldn't create IContent from a row node. Content is unknown table row content!",
  1003. iContent.Node));
  1004. }
  1005. }
  1006. return row;
  1007. }
  1008. catch (Exception ex)
  1009. {
  1010. throw new AODLException("Exception while trying to create a Table Row.", ex);
  1011. }
  1012. }
  1013. /// <summary>
  1014. /// Creates the table header row.
  1015. /// </summary>
  1016. /// <param name="node">The node.</param>
  1017. /// <returns></returns>
  1018. private RowHeader CreateTableHeaderRow(XElement node)
  1019. {
  1020. try
  1021. {
  1022. //Create a new Row
  1023. RowHeader rowHeader = new RowHeader(_document, node);
  1024. ContentCollection iColl = new ContentCollection();
  1025. //Recieve RowStyle
  1026. IStyle rowStyle = _document.Styles.GetStyleByName(rowHeader.StyleName);
  1027. if (rowStyle != null)
  1028. rowHeader.Style = rowStyle;
  1029. //No need for a warning
  1030. //Create the cells
  1031. foreach (XNode nodeChild in rowHeader.Node.Nodes())
  1032. {
  1033. IContent iContent = CreateContent(nodeChild);
  1034. if (iContent != null)
  1035. {
  1036. //iColl.Add(iContent);
  1037. AddToCollection(iContent, iColl);
  1038. }
  1039. else
  1040. {
  1041. OnWarning(new AODLWarning("Couldn't create IContent from a table row.", nodeChild));
  1042. }
  1043. }
  1044. rowHeader.Node.Value = "";
  1045. foreach (IContent iContent in iColl)
  1046. {
  1047. if (iContent is Row)
  1048. {
  1049. rowHeader.RowCollection.Add(iContent as Row);
  1050. }
  1051. else
  1052. {
  1053. OnWarning(
  1054. new AODLWarning(
  1055. "Couldn't create IContent from a row header node. Content is unknown table row header content!",
  1056. iContent.Node));
  1057. }
  1058. }
  1059. return rowHeader;
  1060. }
  1061. catch (Exception ex)
  1062. {
  1063. throw new AODLException("Exception while trying to create a Table Row.", ex);
  1064. }
  1065. }
  1066. /// <summary>
  1067. /// Creates the table column.
  1068. /// </summary>
  1069. /// <param name="node">The node.</param>
  1070. /// <returns></returns>
  1071. private Column CreateTableColumn(XElement node)
  1072. {
  1073. try
  1074. {
  1075. //Create a new Row
  1076. Column column = new Column(_document, node);
  1077. //Recieve RowStyle
  1078. IStyle columnStyle = _document.Styles.GetStyleByName(column.StyleName);
  1079. if (columnStyle != null)
  1080. column.Style = columnStyle;
  1081. //No need for a warning
  1082. return column;
  1083. }
  1084. catch (Exception ex)
  1085. {
  1086. throw new AODLException("Exception while trying to create a Table Column.", ex);
  1087. }
  1088. }
  1089. /// <summary>
  1090. /// Creates the table cell span.
  1091. /// </summary>
  1092. /// <param name="node">The node.</param>
  1093. /// <returns></returns>
  1094. private CellSpan CreateTableCellSpan(XElement node)
  1095. {
  1096. try
  1097. {
  1098. //Create a new CellSpan
  1099. CellSpan cellSpan = new CellSpan(_document, node);
  1100. //No need for a warnings or styles
  1101. return cellSpan;
  1102. }
  1103. catch (Exception ex)
  1104. {
  1105. throw new AODLException("Exception while trying to create a Table CellSpan.", ex);
  1106. }
  1107. }
  1108. /// <summary>
  1109. /// Creates the table cell.
  1110. /// </summary>
  1111. /// <param name="node">The node.</param>
  1112. /// <returns></returns>
  1113. private Cell CreateTableCell(XElement node)
  1114. {
  1115. try
  1116. {
  1117. //Create a new Cel
  1118. Cell cell = new Cell(_document, node);
  1119. ContentCollection iColl = new ContentCollection();
  1120. //Recieve CellStyle
  1121. IStyle cellStyle = _document.Styles.GetStyleByName(cell.StyleName);
  1122. if (cellStyle != null)
  1123. {
  1124. cell.Style = cellStyle;
  1125. }
  1126. //No need for a warning
  1127. //Create the cells content
  1128. foreach (XNode nodeChild in cell.Node.Nodes())
  1129. {
  1130. IContent iContent = CreateContent(nodeChild);
  1131. if (iContent != null)
  1132. {
  1133. //iColl.Add(iContent);
  1134. AddToCollection(iContent, iColl);
  1135. }
  1136. else
  1137. {
  1138. OnWarning(new AODLWarning("Couldn't create IContent from a table cell.", nodeChild));

Large files files are truncated, but you can click here to view the full file