You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfSection.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. import java.util.List;
  28. /**
  29. * <p>Models a section in an RTF document</p>
  30. *
  31. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  32. */
  33. public class RtfSection
  34. extends RtfContainer
  35. implements
  36. IRtfParagraphContainer,
  37. IRtfTableContainer,
  38. IRtfListContainer,
  39. IRtfExternalGraphicContainer,
  40. IRtfBeforeContainer,
  41. IRtfParagraphKeepTogetherContainer,
  42. IRtfAfterContainer,
  43. IRtfJforCmdContainer,
  44. IRtfTextrunContainer {
  45. private RtfParagraph paragraph;
  46. private RtfTable table;
  47. private RtfList list;
  48. private RtfExternalGraphic externalGraphic;
  49. private RtfBefore before;
  50. private RtfAfter after;
  51. private RtfJforCmd jforCmd;
  52. /** Create an RTF container as a child of given container */
  53. RtfSection(RtfDocumentArea parent, Writer w) throws IOException {
  54. super(parent, w);
  55. }
  56. /**
  57. * Start a new external graphic after closing current paragraph, list and table
  58. * @return new RtfExternalGraphic object
  59. * @throws IOException for I/O problems
  60. */
  61. public RtfExternalGraphic newImage() throws IOException {
  62. closeAll();
  63. externalGraphic = new RtfExternalGraphic(this, writer);
  64. return externalGraphic;
  65. }
  66. /**
  67. * Start a new paragraph after closing current paragraph, list and table
  68. * @param attrs attributes for new RtfParagraph
  69. * @return new RtfParagraph object
  70. * @throws IOException for I/O problems
  71. */
  72. public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException {
  73. closeAll();
  74. paragraph = new RtfParagraph(this, writer, attrs);
  75. return paragraph;
  76. }
  77. /**
  78. * Close current paragraph if any and start a new one with default attributes
  79. * @return new RtfParagraph
  80. * @throws IOException for I/O problems
  81. */
  82. public RtfParagraph newParagraph() throws IOException {
  83. return newParagraph(null);
  84. }
  85. /**
  86. * Close current paragraph if any and start a new one
  87. * @return new RtfParagraphKeepTogether
  88. * @throws IOException for I/O problems
  89. */
  90. public RtfParagraphKeepTogether newParagraphKeepTogether() throws IOException {
  91. return new RtfParagraphKeepTogether(this, writer);
  92. }
  93. /**
  94. * Start a new table after closing current paragraph, list and table
  95. * @param tc Table context used for number-columns-spanned attribute (added by
  96. * Boris Poudérous on july 2002)
  97. * @return new RtfTable object
  98. * @throws IOException for I/O problems
  99. */
  100. public RtfTable newTable(ITableColumnsInfo tc) throws IOException {
  101. closeAll();
  102. table = new RtfTable(this, writer, tc);
  103. return table;
  104. }
  105. /**
  106. * Start a new table after closing current paragraph, list and table
  107. * @param attrs attributes of new RtfTable
  108. * @param tc Table context used for number-columns-spanned attribute (added by
  109. * Boris Poudérous on july 2002)
  110. * @return new RtfTable object
  111. * @throws IOException for I/O problems
  112. */
  113. public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc) throws IOException {
  114. closeAll();
  115. table = new RtfTable(this, writer, attrs, tc);
  116. return table;
  117. }
  118. /**
  119. * Start a new list after closing current paragraph, list and table
  120. * @param attrs attributes of new RftList object
  121. * @return new RtfList
  122. * @throws IOException for I/O problems
  123. */
  124. public RtfList newList(RtfAttributes attrs) throws IOException {
  125. closeAll();
  126. list = new RtfList(this, writer, attrs);
  127. return list;
  128. }
  129. /**
  130. * IRtfBeforeContainer
  131. * @param attrs attributes of new RtfBefore object
  132. * @return new RtfBefore object
  133. * @throws IOException for I/O problems
  134. */
  135. public RtfBefore newBefore(RtfAttributes attrs) throws IOException {
  136. closeAll();
  137. before = new RtfBefore(this, writer, attrs);
  138. return before;
  139. }
  140. /**
  141. * IRtfAfterContainer
  142. * @param attrs attributes of new RtfAfter object
  143. * @return new RtfAfter object
  144. * @throws IOException for I/O problems
  145. */
  146. public RtfAfter newAfter(RtfAttributes attrs) throws IOException {
  147. closeAll();
  148. after = new RtfAfter(this, writer, attrs);
  149. return after;
  150. }
  151. /**
  152. *
  153. * @param attrs attributes of new RtfJforCmd
  154. * @return the new RtfJforCmd
  155. * @throws IOException for I/O problems
  156. */
  157. public RtfJforCmd newJforCmd(RtfAttributes attrs) throws IOException {
  158. jforCmd = new RtfJforCmd(this, writer, attrs);
  159. return jforCmd;
  160. }
  161. /**
  162. * Can be overridden to write RTF prefix code, what comes before our children
  163. * @throws IOException for I/O problems
  164. */
  165. protected void writeRtfPrefix() throws IOException {
  166. writeAttributes(attrib, RtfPage.PAGE_ATTR);
  167. newLine();
  168. writeControlWord("sectd");
  169. }
  170. /**
  171. * Can be overridden to write RTF suffix code, what comes after our children
  172. * @throws IOException for I/O problems
  173. */
  174. protected void writeRtfSuffix() throws IOException {
  175. // write suffix /sect only if this section is not last section (see bug #51484)
  176. List siblings = parent.getChildren();
  177. if ( ( siblings.indexOf ( this ) + 1 ) < siblings.size() ) {
  178. writeControlWord("sect");
  179. }
  180. }
  181. private void closeCurrentTable() throws IOException {
  182. if (table != null) {
  183. table.close();
  184. }
  185. }
  186. private void closeCurrentParagraph() throws IOException {
  187. if (paragraph != null) {
  188. paragraph.close();
  189. }
  190. }
  191. private void closeCurrentList() throws IOException {
  192. if (list != null) {
  193. list.close();
  194. }
  195. }
  196. private void closeCurrentExternalGraphic() throws IOException {
  197. if (externalGraphic != null) {
  198. externalGraphic.close();
  199. }
  200. }
  201. private void closeCurrentBefore() throws IOException {
  202. if (before != null) {
  203. before.close();
  204. }
  205. }
  206. private void closeAll()
  207. throws IOException {
  208. closeCurrentTable();
  209. closeCurrentParagraph();
  210. closeCurrentList();
  211. closeCurrentExternalGraphic();
  212. closeCurrentBefore();
  213. }
  214. /**
  215. * Returns the current RtfTextrun.
  216. * @return Current RtfTextrun
  217. * @throws IOException Thrown when an IO-problem occurs.
  218. */
  219. public RtfTextrun getTextrun()
  220. throws IOException {
  221. return RtfTextrun.getTextrun(this, writer, null);
  222. }
  223. }