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.

RtfFile.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
  59. import java.io.Writer;
  60. import java.io.IOException;
  61. import java.io.BufferedWriter;
  62. import java.io.FileWriter;
  63. import java.io.OutputStreamWriter;
  64. /**
  65. * Models the top-level structure of an RTF file.
  66. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  67. * @author Andreas Putz a.putz@skynamics.com
  68. * @author Christopher Scott scottc@westinghouse.com
  69. */
  70. public class RtfFile
  71. extends RtfContainer {
  72. private RtfHeader header;
  73. private RtfPageArea pageArea;
  74. private RtfListTable listTable;
  75. private RtfDocumentArea docArea;
  76. // private ConverterLogChannel m_log;
  77. private RtfContainer listTableContainer;
  78. private int listNum = 0;
  79. /**
  80. * Create an RTF file that outputs to the given Writer
  81. * @param w the Writer to write to
  82. * @throws IOException for I/O problems
  83. */
  84. public RtfFile(Writer w) throws IOException {
  85. super(null, w);
  86. }
  87. /** optional log channel */
  88. // public void setLogChannel(ConverterLogChannel log)
  89. // {
  90. // m_log = log;
  91. // }
  92. /**
  93. * Gets the log channel.
  94. * If logchannel not set, it will return a empty log channel.
  95. * @return our log channel, it is never null */
  96. // ConverterLogChannel getLog()
  97. // {
  98. // if (m_log == null)
  99. // m_log = new ConverterLogChannel (null);
  100. // return m_log;
  101. // }
  102. /**
  103. * If called, must be called before startDocumentArea
  104. * @return the new RtfHeader
  105. * @throws IOException for I/O problems
  106. */
  107. public RtfHeader startHeader()
  108. throws IOException {
  109. if (header != null) {
  110. throw new RtfStructureException("startHeader called more than once");
  111. }
  112. header = new RtfHeader(this, writer);
  113. listTableContainer = new RtfContainer(this, writer);
  114. return header;
  115. }
  116. /**
  117. * Creates the list table.
  118. * @param attr attributes for the RtfListTable
  119. * @return the new RtfListTable
  120. * @throws IOException for I/O problems
  121. */
  122. public RtfListTable startListTable(RtfAttributes attr)
  123. throws IOException {
  124. listNum++;
  125. listTable = new RtfListTable(this, writer, new Integer(listNum), attr);
  126. listTableContainer.addChild(listTable);
  127. return listTable;
  128. }
  129. /**
  130. * Closes the RtfHeader if not done yet, and starts the docment area.
  131. * Like startDocumentArea, is only called once. This is not optimal,
  132. * must be able to have multiple page definition, and corresponding
  133. * Document areas
  134. * @return the RtfPageArea
  135. * @throws IOException for I/O problems
  136. * @throws RtfStructureException for illegal RTF structure
  137. */
  138. public RtfPageArea startPageArea()
  139. throws IOException, RtfStructureException {
  140. if (pageArea != null) {
  141. throw new RtfStructureException("startPageArea called more than once");
  142. }
  143. // create an empty header if there was none
  144. if (header == null) {
  145. startHeader();
  146. }
  147. header.close();
  148. pageArea = new RtfPageArea(this, writer);
  149. addChild(pageArea);
  150. return pageArea;
  151. }
  152. /**
  153. * Call startPageArea if needed and return the page area object.
  154. * @return the RtfPageArea
  155. * @throws IOException for I/O problems
  156. * @throws RtfStructureException for illegal RTF structure
  157. */
  158. public RtfPageArea getPageArea()
  159. throws IOException, RtfStructureException {
  160. if (pageArea == null) {
  161. return startPageArea();
  162. }
  163. return pageArea;
  164. }
  165. /**
  166. * Closes the RtfHeader if not done yet, and starts the document area.
  167. * Must be called once only.
  168. * @return the RtfDocumentArea
  169. * @throws IOException for I/O problems
  170. * @throws RtfStructureException for illegal RTF structure
  171. */
  172. public RtfDocumentArea startDocumentArea()
  173. throws IOException, RtfStructureException {
  174. if (docArea != null) {
  175. throw new RtfStructureException("startDocumentArea called more than once");
  176. }
  177. // create an empty header if there was none
  178. if (header == null) {
  179. startHeader();
  180. }
  181. header.close();
  182. docArea = new RtfDocumentArea(this, writer);
  183. addChild(docArea);
  184. return docArea;
  185. }
  186. /**
  187. * Call startDocumentArea if needed and return the document area object.
  188. * @return the RtfDocumentArea
  189. * @throws IOException for I/O problems
  190. * @throws RtfStructureException for illegal RTF structure
  191. */
  192. public RtfDocumentArea getDocumentArea()
  193. throws IOException, RtfStructureException {
  194. if (docArea == null) {
  195. return startDocumentArea();
  196. }
  197. return docArea;
  198. }
  199. /**
  200. * overridden to write RTF prefix code, what comes before our children
  201. * @throws IOException for I/O problems
  202. */
  203. protected void writeRtfPrefix() throws IOException {
  204. writeGroupMark(true);
  205. writeControlWord("rtf1");
  206. }
  207. /**
  208. * overridden to write RTF suffix code, what comes after our children
  209. * @throws IOException for I/O problems
  210. */
  211. protected void writeRtfSuffix() throws IOException {
  212. writeGroupMark(false);
  213. }
  214. /**
  215. * must be called when done creating the document
  216. * @throws IOException for I/O problems
  217. */
  218. public synchronized void flush() throws IOException {
  219. writeRtf();
  220. writer.flush();
  221. }
  222. /**
  223. * minimal test and usage example
  224. * @param args command-line arguments
  225. * @throws Exception for problems
  226. */
  227. public static void main(String args[])
  228. throws Exception {
  229. Writer w = null;
  230. if (args.length != 0) {
  231. final String outFile = args[0];
  232. System.err.println("Outputting RTF to file '" + outFile + "'");
  233. w = new BufferedWriter(new FileWriter(outFile));
  234. } else {
  235. System.err.println("Outputting RTF code to standard output");
  236. w = new BufferedWriter(new OutputStreamWriter(System.out));
  237. }
  238. final RtfFile f = new RtfFile(w);
  239. final RtfSection sect = f.startDocumentArea().newSection();
  240. final RtfParagraph p = sect.newParagraph();
  241. p.newText("Hello, RTF world.\n", null);
  242. final RtfAttributes attr = new RtfAttributes();
  243. attr.set(RtfText.ATTR_BOLD);
  244. attr.set(RtfText.ATTR_ITALIC);
  245. attr.set(RtfText.ATTR_FONT_SIZE, 36);
  246. p.newText("This is bold, italic, 36 points", attr);
  247. f.flush();
  248. System.err.println("RtfFile test: all done.");
  249. }
  250. }