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

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