1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException; 30 import java.io.Writer ; 31 import java.io.IOException ; 32 import java.io.BufferedWriter ; 33 import java.io.FileWriter ; 34 import java.io.OutputStreamWriter ; 35 36 42 43 public class RtfFile 44 extends RtfContainer { 45 private RtfHeader header; 46 private RtfPageArea pageArea; 47 private RtfListTable listTable; 48 private RtfDocumentArea docArea; 49 private RtfContainer listTableContainer; 51 private int listNum = 0; 52 53 58 public RtfFile(Writer w) throws IOException { 59 super(null, w); 60 } 61 62 63 68 72 79 84 public RtfHeader startHeader() 85 throws IOException { 86 if (header != null) { 87 throw new RtfStructureException("startHeader called more than once"); 88 } 89 header = new RtfHeader(this, writer); 90 listTableContainer = new RtfContainer(this, writer); 91 return header; 92 } 93 94 100 public RtfListTable startListTable(RtfAttributes attr) 101 throws IOException { 102 listNum++; 103 if (listTable != null) { 104 return listTable; 105 } else { 106 listTable = new RtfListTable(this, writer, new Integer (listNum), attr); 107 listTableContainer.addChild(listTable); 108 } 109 110 return listTable; 111 } 112 113 117 public RtfListTable getListTable() { 118 return listTable; 119 } 120 121 130 public RtfPageArea startPageArea() 131 throws IOException { 132 if (pageArea != null) { 133 throw new RtfStructureException("startPageArea called more than once"); 134 } 135 if (header == null) { 137 startHeader(); 138 } 139 header.close(); 140 pageArea = new RtfPageArea(this, writer); 141 addChild(pageArea); 142 return pageArea; 143 } 144 145 151 public RtfPageArea getPageArea() 152 throws IOException { 153 if (pageArea == null) { 154 return startPageArea(); 155 } 156 return pageArea; 157 } 158 159 166 public RtfDocumentArea startDocumentArea() 167 throws IOException { 168 if (docArea != null) { 169 throw new RtfStructureException("startDocumentArea called more than once"); 170 } 171 if (header == null) { 173 startHeader(); 174 } 175 header.close(); 176 docArea = new RtfDocumentArea(this, writer); 177 addChild(docArea); 178 return docArea; 179 } 180 181 182 183 189 public RtfDocumentArea getDocumentArea() 190 throws IOException { 191 if (docArea == null) { 192 return startDocumentArea(); 193 } 194 return docArea; 195 } 196 197 201 protected void writeRtfPrefix() throws IOException { 202 writeGroupMark(true); 203 writeControlWord("rtf1"); 204 } 205 206 210 protected void writeRtfSuffix() throws IOException { 211 writeGroupMark(false); 212 } 213 214 218 public synchronized void flush() throws IOException { 219 writeRtf(); 220 writer.flush(); 221 } 222 223 228 public static void main(String [] args) 229 throws Exception { 230 Writer w = null; 231 if (args.length != 0) { 232 final String outFile = args[0]; 233 System.err.println("Outputting RTF to file '" + outFile + "'"); 234 w = new BufferedWriter (new FileWriter (outFile)); 235 } else { 236 System.err.println("Outputting RTF code to standard output"); 237 w = new BufferedWriter (new OutputStreamWriter (System.out)); 238 } 239 240 final RtfFile f = new RtfFile(w); 241 final RtfSection sect = f.startDocumentArea().newSection(); 242 243 final RtfParagraph p = sect.newParagraph(); 244 p.newText("Hello, RTF world.\n", null); 245 final RtfAttributes attr = new RtfAttributes(); 246 attr.set(RtfText.ATTR_BOLD); 247 attr.set(RtfText.ATTR_ITALIC); 248 attr.set(RtfText.ATTR_FONT_SIZE, 36); 249 p.newText("This is bold, italic, 36 points", attr); 250 251 f.flush(); 252 System.err.println("RtfFile test: all done."); 253 } 254 } 255 | Popular Tags |