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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.io.Writer;
  59. import java.io.IOException;
  60. import org.apache.fop.render.rtf.rtflib.interfaces.ITableColumnsInfo;
  61. /** Models a section in an RTF document
  62. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  63. */
  64. public class RtfSection
  65. extends RtfContainer
  66. implements
  67. IRtfParagraphContainer,
  68. IRtfTableContainer,
  69. IRtfListContainer,
  70. IRtfExternalGraphicContainer,
  71. IRtfBeforeContainer,
  72. IRtfParagraphKeepTogetherContainer,
  73. IRtfAfterContainer,
  74. IRtfJforCmdContainer,
  75. IRtfTextrunContainer {
  76. private RtfParagraph paragraph;
  77. private RtfTable table;
  78. private RtfList list;
  79. private RtfExternalGraphic externalGraphic;
  80. private RtfBefore before;
  81. private RtfAfter after;
  82. private RtfJforCmd jforCmd;
  83. /** Create an RTF container as a child of given container */
  84. RtfSection(RtfDocumentArea parent, Writer w) throws IOException {
  85. super(parent, w);
  86. }
  87. /**
  88. * Start a new external graphic after closing current paragraph, list and table
  89. * @return new RtfExternalGraphic object
  90. * @throws IOException for I/O problems
  91. */
  92. public RtfExternalGraphic newImage() throws IOException {
  93. closeAll();
  94. externalGraphic = new RtfExternalGraphic(this, writer);
  95. return externalGraphic;
  96. }
  97. /**
  98. * Start a new paragraph after closing current paragraph, list and table
  99. * @param attrs attributes for new RtfParagraph
  100. * @return new RtfParagraph object
  101. * @throws IOException for I/O problems
  102. */
  103. public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException {
  104. closeAll();
  105. paragraph = new RtfParagraph(this, writer, attrs);
  106. return paragraph;
  107. }
  108. /**
  109. * Close current paragraph if any and start a new one with default attributes
  110. * @return new RtfParagraph
  111. * @throws IOException for I/O problems
  112. */
  113. public RtfParagraph newParagraph() throws IOException {
  114. return newParagraph(null);
  115. }
  116. /**
  117. * Close current paragraph if any and start a new one
  118. * @return new RtfParagraphKeepTogether
  119. * @throws IOException for I/O problems
  120. */
  121. public RtfParagraphKeepTogether newParagraphKeepTogether() throws IOException {
  122. return new RtfParagraphKeepTogether(this, writer);
  123. }
  124. /**
  125. * Start a new table after closing current paragraph, list and table
  126. * @param tc Table context used for number-columns-spanned attribute (added by
  127. * Boris Poudérous on july 2002)
  128. * @return new RtfTable object
  129. * @throws IOException for I/O problems
  130. */
  131. public RtfTable newTable(ITableColumnsInfo tc) throws IOException {
  132. closeAll();
  133. table = new RtfTable(this, writer, tc);
  134. return table;
  135. }
  136. /**
  137. * Start a new table after closing current paragraph, list and table
  138. * @param attrs attributes of new RtfTable
  139. * @param tc Table context used for number-columns-spanned attribute (added by
  140. * Boris Poudérous on july 2002)
  141. * @return new RtfTable object
  142. * @throws IOException for I/O problems
  143. */
  144. public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc) throws IOException {
  145. closeAll();
  146. table = new RtfTable(this, writer, attrs, tc);
  147. return table;
  148. }
  149. /**
  150. * Start a new list after closing current paragraph, list and table
  151. * @param attrs attributes of new RftList object
  152. * @return new RtfList
  153. * @throws IOException for I/O problems
  154. */
  155. public RtfList newList(RtfAttributes attrs) throws IOException {
  156. closeAll();
  157. list = new RtfList(this, writer, attrs);
  158. return list;
  159. }
  160. /**
  161. * IRtfBeforeContainer
  162. * @param attrs attributes of new RtfBefore object
  163. * @return new RtfBefore object
  164. * @throws IOException for I/O problems
  165. */
  166. public RtfBefore newBefore(RtfAttributes attrs) throws IOException {
  167. closeAll();
  168. before = new RtfBefore(this, writer, attrs);
  169. return before;
  170. }
  171. /**
  172. * IRtfAfterContainer
  173. * @param attrs attributes of new RtfAfter object
  174. * @return new RtfAfter object
  175. * @throws IOException for I/O problems
  176. */
  177. public RtfAfter newAfter(RtfAttributes attrs) throws IOException {
  178. closeAll();
  179. after = new RtfAfter(this, writer, attrs);
  180. return after;
  181. }
  182. /**
  183. *
  184. * @param attrs attributes of new RtfJforCmd
  185. * @return the new RtfJforCmd
  186. * @throws IOException for I/O problems
  187. */
  188. public RtfJforCmd newJforCmd(RtfAttributes attrs) throws IOException {
  189. jforCmd = new RtfJforCmd(this, writer, attrs);
  190. return jforCmd;
  191. }
  192. /**
  193. * Can be overridden to write RTF prefix code, what comes before our children
  194. * @throws IOException for I/O problems
  195. */
  196. protected void writeRtfPrefix() throws IOException {
  197. writeControlWord("sectd");
  198. writeAttributes(attrib, RtfPage.PAGE_ATTR);
  199. }
  200. /**
  201. * Can be overridden to write RTF suffix code, what comes after our children
  202. * @throws IOException for I/O problems
  203. */
  204. protected void writeRtfSuffix() throws IOException {
  205. writeControlWord("sect");
  206. }
  207. private void closeCurrentTable() throws IOException {
  208. if (table != null) {
  209. table.close();
  210. }
  211. }
  212. private void closeCurrentParagraph() throws IOException {
  213. if (paragraph != null) {
  214. paragraph.close();
  215. }
  216. }
  217. private void closeCurrentList() throws IOException {
  218. if (list != null) {
  219. list.close();
  220. }
  221. }
  222. private void closeCurrentExternalGraphic() throws IOException {
  223. if (externalGraphic != null) {
  224. externalGraphic.close();
  225. }
  226. }
  227. private void closeCurrentBefore() throws IOException {
  228. if (before != null) {
  229. before.close();
  230. }
  231. }
  232. private void closeAll()
  233. throws IOException {
  234. closeCurrentTable();
  235. closeCurrentParagraph();
  236. closeCurrentList();
  237. closeCurrentExternalGraphic();
  238. closeCurrentBefore();
  239. }
  240. public RtfTextrun getTextrun()
  241. throws IOException {
  242. return RtfTextrun.getTextrun(this, writer, null);
  243. }
  244. }