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.

RtfParagraph.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 java.util.List;
  61. /** Model of an RTF paragraph, which can contain RTF text elements.
  62. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  63. * @author Andreas Putz a.putz@skynamics.com
  64. * @author Boris Poudérous, boris.pouderous@free.fr
  65. */
  66. public class RtfParagraph extends RtfBookmarkContainerImpl
  67. implements IRtfTextContainer, IRtfPageBreakContainer, IRtfHyperLinkContainer,
  68. IRtfExternalGraphicContainer, IRtfPageNumberContainer,
  69. IRtfPageNumberCitationContainer {
  70. private RtfText text;
  71. private RtfHyperLink hyperlink;
  72. private RtfExternalGraphic externalGraphic;
  73. private RtfPageNumber pageNumber;
  74. private RtfPageNumberCitation pageNumberCitation;
  75. // Above line added by Boris POUDEROUS on 2002/07/09
  76. private boolean keepn = false;
  77. private boolean resetProperties = false;
  78. /* needed for importing Rtf into FrameMaker
  79. FrameMaker is not as forgiving as word in rtf
  80. thus /pard/par must be written in a page break directly
  81. after a table. /pard is probably needed in other places
  82. also, this is just a hack to make FrameMaker import Jfor rtf
  83. correctly */
  84. private boolean writeForBreak = false;
  85. /** Set of attributes that must be copied at the start of a paragraph */
  86. private static final String [] PARA_ATTRIBUTES = { "intbl" };
  87. /** Create an RTF paragraph as a child of given container with default attributes */
  88. RtfParagraph(IRtfParagraphContainer parent, Writer w) throws IOException {
  89. super((RtfContainer)parent, w);
  90. }
  91. /** Create an RTF paragraph as a child of given container with given attributes */
  92. RtfParagraph(IRtfParagraphContainer parent, Writer w, RtfAttributes attr) throws IOException {
  93. super((RtfContainer)parent, w, attr);
  94. }
  95. /**
  96. * Accessor for the paragraph text
  97. * @return the paragraph text
  98. */
  99. public String getText() {
  100. return (text.getText());
  101. }
  102. /** Set the keepn attribute for this paragraph */
  103. public void setKeepn() {
  104. this.keepn = true;
  105. }
  106. /** Force reset properties */
  107. public void setResetProperties() {
  108. this.resetProperties = true;
  109. }
  110. /**
  111. * IRtfTextContainer requirement: return a copy of our attributes
  112. * @return a copy of this paragraphs attributes
  113. */
  114. public RtfAttributes getTextContainerAttributes() {
  115. if (attrib == null) {
  116. return null;
  117. }
  118. return (RtfAttributes)this.attrib.clone();
  119. }
  120. /**
  121. * Overridden to write our attributes before our content
  122. * @throws IOException for I/O problems
  123. */
  124. protected void writeRtfPrefix() throws IOException {
  125. // collapse whitespace before writing out
  126. // TODO could be made configurable
  127. if (attrib != null && attrib.isSet("WhiteSpaceFalse")) {
  128. attrib.unset("WhiteSpaceFalse");
  129. } else {
  130. new WhitespaceCollapser(this);
  131. }
  132. //Reset paragraph properties if needed
  133. if (resetProperties) {
  134. writeControlWord("pard");
  135. }
  136. /*
  137. * Original comment said "do not write text attributes here, they are
  138. * handled by RtfText." However, the text attributes appear to be
  139. * relevant to paragraphs as well.
  140. */
  141. writeAttributes(attrib, RtfText.ATTR_NAMES);
  142. writeAttributes(attrib, PARA_ATTRIBUTES);
  143. // Added by Normand Masse
  144. // Write alignment attributes after \intbl for cells
  145. if (attrib.isSet("intbl") && mustWriteAttributes()) {
  146. writeAttributes(attrib, RtfText.ALIGNMENT);
  147. }
  148. //Set keepn if needed (Keep paragraph with the next paragraph)
  149. if (keepn) {
  150. writeControlWord("keepn");
  151. }
  152. // start a group for this paragraph and write our own attributes if needed
  153. if (mustWriteGroupMark()) {
  154. writeGroupMark(true);
  155. }
  156. if (mustWriteAttributes()) {
  157. // writeAttributes(m_attrib, new String [] {"cs"});
  158. // Added by Normand Masse
  159. // If \intbl then attributes have already been written (see higher in method)
  160. if (!attrib.isSet("intbl")) {
  161. writeAttributes(attrib, RtfText.ALIGNMENT);
  162. }
  163. //this line added by Chris Scott, Westinghouse
  164. writeAttributes(attrib, RtfText.BORDER);
  165. writeAttributes(attrib, RtfText.INDENT);
  166. writeAttributes(attrib, RtfText.TABS);
  167. if (writeForBreak) {
  168. writeControlWord("pard\\par");
  169. }
  170. }
  171. }
  172. /**
  173. * Overridden to close paragraph
  174. * @throws IOException for I/O problems
  175. */
  176. protected void writeRtfSuffix() throws IOException {
  177. // sometimes the end of paragraph mark must be suppressed in table cells
  178. boolean writeMark = true;
  179. if (parent instanceof RtfTableCell) {
  180. writeMark = ((RtfTableCell)parent).paragraphNeedsPar(this);
  181. }
  182. if (writeMark) {
  183. writeControlWord("par");
  184. }
  185. if (mustWriteGroupMark()) {
  186. writeGroupMark(false);
  187. }
  188. }
  189. /**
  190. * Close current text run if any and start a new one with default attributes
  191. * @param str if not null, added to the RtfText created
  192. * @return the new RtfText object
  193. * @throws IOException for I/O problems
  194. */
  195. public RtfText newText(String str) throws IOException {
  196. return newText(str, null);
  197. }
  198. /**
  199. * Close current text run if any and start a new one
  200. * @param str if not null, added to the RtfText created
  201. * @param attr attributes of the text
  202. * @return the new RtfText object
  203. * @throws IOException for I/O problems
  204. */
  205. public RtfText newText(String str, RtfAttributes attr) throws IOException {
  206. closeAll();
  207. text = new RtfText(this, writer, str, attr);
  208. return text;
  209. }
  210. /**
  211. * add a page break
  212. * @throws IOException for I/O problems
  213. */
  214. public void newPageBreak() throws IOException {
  215. writeForBreak = true;
  216. new RtfPageBreak(this, writer);
  217. }
  218. /**
  219. * add a line break
  220. * @throws IOException for I/O problems
  221. */
  222. public void newLineBreak() throws IOException {
  223. new RtfLineBreak(this, writer);
  224. }
  225. /**
  226. * Add a page number
  227. * @return new RtfPageNumber object
  228. * @throws IOException for I/O problems
  229. */
  230. public RtfPageNumber newPageNumber()throws IOException {
  231. pageNumber = new RtfPageNumber(this, writer);
  232. return pageNumber;
  233. }
  234. /**
  235. * Added by Boris POUDEROUS on 2002/07/09
  236. * @param id string containing the citation text
  237. * @return the new RtfPageNumberCitation object
  238. * @throws IOException for I/O problems
  239. */
  240. public RtfPageNumberCitation newPageNumberCitation(String id) throws IOException {
  241. pageNumberCitation = new RtfPageNumberCitation(this, writer, id);
  242. return pageNumberCitation;
  243. }
  244. /**
  245. * Creates a new hyperlink.
  246. * @param str string containing the hyperlink text
  247. * @param attr attributes of new hyperlink
  248. * @return the new RtfHyperLink object
  249. * @throws IOException for I/O problems
  250. */
  251. public RtfHyperLink newHyperLink(String str, RtfAttributes attr) throws IOException {
  252. hyperlink = new RtfHyperLink(this, writer, str, attr);
  253. return hyperlink;
  254. }
  255. /**
  256. * Start a new external graphic after closing all other elements
  257. * @return the new RtfExternalGraphic
  258. * @throws IOException for I/O problems
  259. */
  260. public RtfExternalGraphic newImage() throws IOException {
  261. closeAll();
  262. externalGraphic = new RtfExternalGraphic(this, writer);
  263. return externalGraphic;
  264. }
  265. private void closeCurrentText() throws IOException {
  266. if (text != null) {
  267. text.close();
  268. }
  269. }
  270. private void closeCurrentHyperLink() throws IOException {
  271. if (hyperlink != null) {
  272. hyperlink.close();
  273. }
  274. }
  275. private void closeAll() throws IOException {
  276. closeCurrentText();
  277. closeCurrentHyperLink();
  278. }
  279. /**
  280. * Depending on RtfOptions, do not emit any RTF for empty paragraphs
  281. * @return true if RTF should be written
  282. */
  283. protected boolean okToWriteRtf() {
  284. boolean result = super.okToWriteRtf();
  285. if (parent.getOptions().ignoreEmptyParagraphs() && getChildCount() == 0) {
  286. // TODO should test that this is the last RtfParagraph in the cell instead
  287. // of simply testing for last child??
  288. result = false;
  289. }
  290. return result;
  291. }
  292. /** true if we must write our own (non-text) attributes in the RTF */
  293. private boolean mustWriteAttributes() {
  294. boolean writeAttributes = false;
  295. final int children = getChildCount();
  296. if (children > 0) {
  297. final List childList = getChildren();
  298. for (int i = 0; i < children; i++) {
  299. final RtfElement el = (RtfElement) childList.get(i);
  300. if (!el.isEmpty()) {
  301. if (el.getClass() == RtfText.class) {
  302. boolean tmp = ((RtfText) el).isNbsp();
  303. if (!tmp) {
  304. writeAttributes = true;
  305. break;
  306. }
  307. } else {
  308. writeAttributes = true;
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. return writeAttributes;
  315. }
  316. /** true if we must write a group mark around this paragraph
  317. * TODO is this correct, study interaction with mustWriteAttributes()
  318. * <-- On implementation i have noticed if the groupmark set, the
  319. * format attributes are only for this content, i think this
  320. * implementation is ok
  321. */
  322. private boolean mustWriteGroupMark() {
  323. return getChildCount() > 0;
  324. }
  325. /**
  326. * accessor for text attributes
  327. * @return attributes of the text
  328. */
  329. public RtfAttributes getTextAttributes() {
  330. if (text == null) {
  331. return null;
  332. }
  333. return text.getTextAttributes();
  334. }
  335. }