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.

RTFHandler.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. package org.apache.fop.render.rtf;
  52. // Java
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import java.io.OutputStreamWriter;
  56. import org.apache.avalon.framework.logger.ConsoleLogger;
  57. import org.apache.avalon.framework.logger.Logger;
  58. import org.apache.fop.apps.FOPException;
  59. import org.apache.fop.fo.EnumProperty;
  60. import org.apache.fop.fo.FOInputHandler;
  61. import org.apache.fop.datatypes.FixedLength;
  62. import org.apache.fop.fo.flow.Block;
  63. import org.apache.fop.fo.flow.ExternalGraphic;
  64. import org.apache.fop.fo.flow.Inline;
  65. import org.apache.fop.fo.flow.InstreamForeignObject;
  66. import org.apache.fop.fo.flow.Leader;
  67. import org.apache.fop.fo.flow.ListBlock;
  68. import org.apache.fop.fo.flow.ListItem;
  69. import org.apache.fop.fo.flow.PageNumber;
  70. import org.apache.fop.fo.flow.Table;
  71. import org.apache.fop.fo.flow.TableColumn;
  72. import org.apache.fop.fo.flow.TableBody;
  73. import org.apache.fop.fo.flow.TableCell;
  74. import org.apache.fop.fo.flow.TableHeader;
  75. import org.apache.fop.fo.flow.TableRow;
  76. import org.apache.fop.fo.pagination.Flow;
  77. import org.apache.fop.fo.pagination.PageSequence;
  78. import org.apache.fop.fo.pagination.SimplePageMaster;
  79. import org.apache.fop.fo.properties.Constants;
  80. import org.apache.fop.fo.Property;
  81. import org.apache.fop.fo.LengthProperty;
  82. import org.apache.fop.apps.Document;
  83. import org.apache.fop.render.rtf.rtflib.rtfdoc.ITableAttributes;
  84. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfAfterContainer;
  85. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfBeforeContainer;
  86. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfListContainer;
  87. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfTextrunContainer;
  88. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAfter;
  89. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
  90. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfBefore;
  91. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfDocumentArea;
  92. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfElement;
  93. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfExternalGraphic;
  94. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFile;
  95. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfList;
  96. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfListItem;
  97. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfListItem.RtfListItemLabel;
  98. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfSection;
  99. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTextrun;
  100. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTable;
  101. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableRow;
  102. import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableCell;
  103. import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfTableContainer;
  104. import org.apache.fop.fonts.FontSetup;
  105. import org.xml.sax.SAXException;
  106. /**
  107. * RTF Handler: generates RTF output using the structure events from
  108. * the FO Tree sent to this structure handler.
  109. *
  110. * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
  111. * @author Trembicki-Guy, Ed <GuyE@DNB.com>
  112. * @author Boris Poudérous <boris.pouderous@eads-telecom.com>
  113. * @author Peter Herweg <pherweg@web.de>
  114. * @author Andreas Putz <a.putz@skynamics.com>
  115. */
  116. public class RTFHandler extends FOInputHandler {
  117. private RtfFile rtfFile;
  118. private final OutputStream os;
  119. private final Logger log = new ConsoleLogger();
  120. private RtfSection sect;
  121. private RtfDocumentArea docArea;
  122. private boolean bPrevHeaderSpecified = false;//true, if there has been a
  123. //header in any page-sequence
  124. private boolean bPrevFooterSpecified = false;//true, if there has been a
  125. //footer in any page-sequence
  126. private boolean bHeaderSpecified = false; //true, if there is a header
  127. //in current page-sequence
  128. private boolean bFooterSpecified = false; //true, if there is a footer
  129. //in current page-sequence
  130. private BuilderContext builderContext = new BuilderContext(null);
  131. private static final String ALPHA_WARNING = "WARNING: RTF renderer is "
  132. + "veryveryalpha at this time, see class org.apache.fop.rtf.renderer.RTFHandler";
  133. /**
  134. * Creates a new RTF structure handler.
  135. * @param doc the Document for which this RTFHandler is processing
  136. * @param os OutputStream to write to
  137. */
  138. public RTFHandler(Document doc, OutputStream os) {
  139. super(doc);
  140. this.os = os;
  141. FontSetup.setup(doc, null);
  142. log.warn(ALPHA_WARNING);
  143. }
  144. /**
  145. * @see org.apache.fop.fo.FOInputHandler#startDocument()
  146. */
  147. public void startDocument() throws SAXException {
  148. // TODO sections should be created
  149. try {
  150. rtfFile = new RtfFile(new OutputStreamWriter(os));
  151. docArea = rtfFile.startDocumentArea();
  152. } catch (IOException ioe) {
  153. // TODO could we throw Exception in all FOInputHandler events?
  154. throw new SAXException(ioe);
  155. }
  156. }
  157. /**
  158. * @see org.apache.fop.fo.FOInputHandler#endDocument()
  159. */
  160. public void endDocument() throws SAXException {
  161. try {
  162. rtfFile.flush();
  163. } catch (IOException ioe) {
  164. // TODO could we throw Exception in all FOInputHandler events?
  165. throw new SAXException(ioe);
  166. }
  167. }
  168. /**
  169. * @see org.apache.fop.fo.FOInputHandler
  170. */
  171. public void startPageSequence(PageSequence pageSeq) {
  172. try {
  173. sect = docArea.newSection();
  174. //read page size and margins, if specified
  175. Property prop;
  176. if ((prop = pageSeq.properties.get("master-reference")) != null) {
  177. String reference = prop.getString();
  178. SimplePageMaster pagemaster
  179. = pageSeq.getLayoutMasterSet().getSimplePageMaster(reference);
  180. //only simple-page-master supported, so pagemaster may be null
  181. if (pagemaster != null) {
  182. sect.getRtfAttributes().set(
  183. PageAttributesConverter.convertPageAttributes(
  184. pagemaster.properties, null));
  185. }
  186. }
  187. builderContext.pushContainer(sect);
  188. bHeaderSpecified = false;
  189. bFooterSpecified = false;
  190. } catch (IOException ioe) {
  191. // TODO could we throw Exception in all FOInputHandler events?
  192. log.error("startPageSequence: " + ioe.getMessage());
  193. //TODO throw new FOPException(ioe);
  194. }
  195. }
  196. /**
  197. * @see org.apache.fop.fo.FOInputHandler#endPageSequence(PageSequence)
  198. */
  199. public void endPageSequence(PageSequence pageSeq) throws FOPException {
  200. builderContext.popContainer();
  201. }
  202. /**
  203. * @see org.apache.fop.fo.FOInputHandler#startFlow(Flow)
  204. */
  205. public void startFlow(Flow fl) {
  206. try {
  207. if (fl.getFlowName().equals("xsl-region-body")) {
  208. // if there is no header in current page-sequence but there has been
  209. // a header in a previous page-sequence, insert an empty header.
  210. if (bPrevHeaderSpecified && !bHeaderSpecified) {
  211. RtfAttributes attr = new RtfAttributes();
  212. attr.set(RtfBefore.HEADER);
  213. final IRtfBeforeContainer contBefore
  214. = (IRtfBeforeContainer)builderContext.getContainer
  215. (IRtfBeforeContainer.class, true, this);
  216. contBefore.newBefore(attr);
  217. }
  218. // if there is no footer in current page-sequence but there has been
  219. // a footer in a previous page-sequence, insert an empty footer.
  220. if (bPrevFooterSpecified && !bFooterSpecified) {
  221. RtfAttributes attr = new RtfAttributes();
  222. attr.set(RtfAfter.FOOTER);
  223. final IRtfAfterContainer contAfter
  224. = (IRtfAfterContainer)builderContext.getContainer
  225. (IRtfAfterContainer.class, true, this);
  226. contAfter.newAfter(attr);
  227. }
  228. } else if (fl.getFlowName().equals("xsl-region-before")) {
  229. bHeaderSpecified = true;
  230. bPrevHeaderSpecified = true;
  231. final IRtfBeforeContainer c
  232. = (IRtfBeforeContainer)builderContext.getContainer(
  233. IRtfBeforeContainer.class,
  234. true, this);
  235. RtfAttributes beforeAttributes = ((RtfElement)c).getRtfAttributes();
  236. if (beforeAttributes == null) {
  237. beforeAttributes = new RtfAttributes();
  238. }
  239. beforeAttributes.set(RtfBefore.HEADER);
  240. RtfBefore before = c.newBefore(beforeAttributes);
  241. builderContext.pushContainer(before);
  242. } else if (fl.getFlowName().equals("xsl-region-after")) {
  243. bFooterSpecified = true;
  244. bPrevFooterSpecified = true;
  245. final IRtfAfterContainer c
  246. = (IRtfAfterContainer)builderContext.getContainer(
  247. IRtfAfterContainer.class,
  248. true, this);
  249. RtfAttributes afterAttributes = ((RtfElement)c).getRtfAttributes();
  250. if (afterAttributes == null) {
  251. afterAttributes = new RtfAttributes();
  252. }
  253. afterAttributes.set(RtfAfter.FOOTER);
  254. RtfAfter after = c.newAfter(afterAttributes);
  255. builderContext.pushContainer(after);
  256. }
  257. } catch (IOException ioe) {
  258. log.error("startFlow: " + ioe.getMessage());
  259. throw new Error(ioe.getMessage());
  260. } catch (Exception e) {
  261. log.error("startFlow: " + e.getMessage());
  262. throw new Error(e.getMessage());
  263. }
  264. }
  265. /**
  266. * @see org.apache.fop.fo.FOInputHandler#endFlow(Flow)
  267. */
  268. public void endFlow(Flow fl) {
  269. try {
  270. if (fl.getFlowName().equals("xsl-region-body")) {
  271. //just do nothing
  272. } else if (fl.getFlowName().equals("xsl-region-before")) {
  273. builderContext.popContainer();
  274. } else if (fl.getFlowName().equals("xsl-region-after")) {
  275. builderContext.popContainer();
  276. }
  277. } catch (Exception e) {
  278. log.error("endFlow: " + e.getMessage());
  279. throw new Error(e.getMessage());
  280. }
  281. }
  282. /**
  283. * @see org.apache.fop.fo.FOInputHandler#startBlock(Block)
  284. */
  285. public void startBlock(Block bl) {
  286. try {
  287. RtfAttributes rtfAttr
  288. = TextAttributesConverter.convertAttributes(bl.properties, null);
  289. IRtfTextrunContainer container
  290. = (IRtfTextrunContainer)builderContext.getContainer(
  291. IRtfTextrunContainer.class,
  292. true, this);
  293. RtfTextrun textrun = container.getTextrun();
  294. textrun.addParagraphBreak();
  295. textrun.pushAttributes(rtfAttr);
  296. } catch (IOException ioe) {
  297. // TODO could we throw Exception in all FOInputHandler events?
  298. log.error("startBlock: " + ioe.getMessage());
  299. throw new Error("IOException: " + ioe);
  300. } catch (Exception e) {
  301. log.error("startBlock: " + e.getMessage());
  302. throw new Error("Exception: " + e);
  303. }
  304. }
  305. /**
  306. * @see org.apache.fop.fo.FOInputHandler#endBlock(Block)
  307. */
  308. public void endBlock(Block bl) {
  309. try {
  310. IRtfTextrunContainer container
  311. = (IRtfTextrunContainer)builderContext.getContainer(
  312. IRtfTextrunContainer.class,
  313. true, this);
  314. RtfTextrun textrun = container.getTextrun();
  315. textrun.addParagraphBreak();
  316. textrun.popAttributes();
  317. } catch (IOException ioe) {
  318. log.error("startBlock:" + ioe.getMessage());
  319. throw new Error(ioe.getMessage());
  320. } catch (Exception e) {
  321. log.error("startBlock:" + e.getMessage());
  322. throw new Error(e.getMessage());
  323. }
  324. }
  325. /**
  326. * @see org.apache.fop.fo.FOInputHandler#startTable(Table)
  327. */
  328. public void startTable(Table tbl) {
  329. // create an RtfTable in the current table container
  330. TableContext tableContext = new TableContext(builderContext);
  331. try {
  332. RtfAttributes atts
  333. = TableAttributesConverter.convertTableAttributes(tbl.properties);
  334. final IRtfTableContainer tc
  335. = (IRtfTableContainer)builderContext.getContainer(
  336. IRtfTableContainer.class, true, null);
  337. builderContext.pushContainer(tc.newTable(atts, tableContext));
  338. } catch (Exception e) {
  339. log.error("startTable:" + e.getMessage());
  340. throw new Error(e.getMessage());
  341. }
  342. builderContext.pushTableContext(tableContext);
  343. }
  344. /**
  345. * @see org.apache.fop.fo.FOInputHandler#endTable(Table)
  346. */
  347. public void endTable(Table tbl) {
  348. builderContext.popTableContext();
  349. builderContext.popContainer();
  350. }
  351. /**
  352. *
  353. * @param tc TableColumn that is starting;
  354. */
  355. public void startColumn(TableColumn tc) {
  356. try {
  357. Integer iWidth = new Integer(tc.getColumnWidth() / 1000);
  358. builderContext.getTableContext().setNextColumnWidth(iWidth.toString() + "pt");
  359. builderContext.getTableContext().setNextColumnRowSpanning(new Integer(0), null);
  360. } catch (Exception e) {
  361. log.error("startColumn: " + e.getMessage());
  362. throw new Error(e.getMessage());
  363. }
  364. }
  365. /**
  366. *
  367. * @param tc TableColumn that is ending;
  368. */
  369. public void endColumn(TableColumn tc) {
  370. }
  371. /**
  372. * @see org.apache.fop.fo.FOInputHandler#startHeader(TableBody)
  373. */
  374. public void startHeader(TableBody th) {
  375. }
  376. /**
  377. * @see org.apache.fop.fo.FOInputHandler#endHeader(TableBody)
  378. */
  379. public void endHeader(TableBody th) {
  380. }
  381. /**
  382. * @see org.apache.fop.fo.FOInputHandler#startFooter(TableBody)
  383. */
  384. public void startFooter(TableBody tf) {
  385. }
  386. /**
  387. * @see org.apache.fop.fo.FOInputHandler#endFooter(TableBody)
  388. */
  389. public void endFooter(TableBody tf) {
  390. }
  391. /**
  392. *
  393. * @param inl Inline that is starting.
  394. */
  395. public void startInline(Inline inl) {
  396. try {
  397. RtfAttributes rtfAttr
  398. = TextAttributesConverter.convertCharacterAttributes(inl.properties, null);
  399. IRtfTextrunContainer container
  400. = (IRtfTextrunContainer)builderContext.getContainer(
  401. IRtfTextrunContainer.class, true, this);
  402. RtfTextrun textrun = container.getTextrun();
  403. textrun.pushAttributes(rtfAttr);
  404. } catch (IOException ioe) {
  405. log.error("startInline:" + ioe.getMessage());
  406. throw new Error(ioe.getMessage());
  407. } catch (FOPException fe) {
  408. log.error("startInline:" + fe.getMessage());
  409. throw new Error(fe.getMessage());
  410. } catch (Exception e) {
  411. log.error("startInline:" + e.getMessage());
  412. throw new Error(e.getMessage());
  413. }
  414. }
  415. /**
  416. *
  417. * @param inl Inline that is ending.
  418. */
  419. public void endInline(Inline inl) {
  420. try {
  421. IRtfTextrunContainer container
  422. = (IRtfTextrunContainer)builderContext.getContainer(
  423. IRtfTextrunContainer.class, true, this);
  424. RtfTextrun textrun = container.getTextrun();
  425. textrun.popAttributes();
  426. } catch (IOException ioe) {
  427. log.error("startInline:" + ioe.getMessage());
  428. throw new Error(ioe.getMessage());
  429. } catch (Exception e) {
  430. log.error("startInline:" + e.getMessage());
  431. throw new Error(e.getMessage());
  432. }
  433. }
  434. /**
  435. * @see org.apache.fop.fo.FOInputHandler#startBody(TableBody)
  436. */
  437. public void startBody(TableBody tb) {
  438. try {
  439. RtfAttributes atts = TableAttributesConverter.convertRowAttributes (tb.properties,
  440. null);
  441. RtfTable tbl = (RtfTable)builderContext.getContainer(RtfTable.class, true, this);
  442. tbl.setHeaderAttribs(atts);
  443. } catch (Exception e) {
  444. log.error("startBody: " + e.getMessage());
  445. throw new Error(e.getMessage());
  446. }
  447. }
  448. /**
  449. * @see org.apache.fop.fo.FOInputHandler#endBody(TableBody)
  450. */
  451. public void endBody(TableBody tb) {
  452. try {
  453. RtfTable tbl = (RtfTable)builderContext.getContainer(RtfTable.class, true, this);
  454. tbl.setHeaderAttribs(null);
  455. } catch (Exception e) {
  456. log.error("endBody: " + e.getMessage());
  457. throw new Error(e.getMessage());
  458. }
  459. }
  460. /**
  461. * @see org.apache.fop.fo.FOInputHandler#startRow(TableRow)
  462. */
  463. public void startRow(TableRow tr) {
  464. try {
  465. // create an RtfTableRow in the current RtfTable
  466. final RtfTable tbl = (RtfTable)builderContext.getContainer(RtfTable.class,
  467. true, null);
  468. RtfAttributes atts = TableAttributesConverter.convertRowAttributes(tr.properties,
  469. tbl.getHeaderAttribs());
  470. if (tr.getParent() instanceof TableHeader) {
  471. atts.set(ITableAttributes.ATTR_HEADER);
  472. }
  473. builderContext.pushContainer(tbl.newTableRow(atts));
  474. // reset column iteration index to correctly access column widths
  475. builderContext.getTableContext().selectFirstColumn();
  476. } catch (Exception e) {
  477. log.error("startRow: " + e.getMessage());
  478. throw new Error(e.getMessage());
  479. }
  480. }
  481. /**
  482. * @see org.apache.fop.fo.FOInputHandler#endRow(TableRow)
  483. */
  484. public void endRow(TableRow tr) {
  485. builderContext.popContainer();
  486. builderContext.getTableContext().decreaseRowSpannings();
  487. }
  488. /**
  489. * @see org.apache.fop.fo.FOInputHandler#startCell(TableCell)
  490. */
  491. public void startCell(TableCell tc) {
  492. try {
  493. TableContext tctx = builderContext.getTableContext();
  494. final RtfTableRow row = (RtfTableRow)builderContext.getContainer(RtfTableRow.class,
  495. true, null);
  496. //while the current column is in row-spanning, act as if
  497. //a vertical merged cell would have been specified.
  498. while (tctx.getNumberOfColumns() > tctx.getColumnIndex()
  499. && tctx.getColumnRowSpanningNumber().intValue() > 0) {
  500. row.newTableCellMergedVertically((int)tctx.getColumnWidth(),
  501. tctx.getColumnRowSpanningAttrs());
  502. tctx.selectNextColumn();
  503. }
  504. //get the width of the currently started cell
  505. float width = tctx.getColumnWidth();
  506. // create an RtfTableCell in the current RtfTableRow
  507. RtfAttributes atts = TableAttributesConverter.convertCellAttributes(tc.properties);
  508. RtfTableCell cell = row.newTableCell((int)width, atts);
  509. //process number-rows-spanned attribute
  510. Property p = null;
  511. if ((p = tc.properties.get("number-rows-spanned")) != null) {
  512. // Start vertical merge
  513. cell.setVMerge(RtfTableCell.MERGE_START);
  514. // set the number of rows spanned
  515. tctx.setCurrentColumnRowSpanning(new Integer(p.getNumber().intValue()),
  516. cell.getRtfAttributes());
  517. } else {
  518. tctx.setCurrentColumnRowSpanning(new Integer(1), null);
  519. }
  520. builderContext.pushContainer(cell);
  521. } catch (Exception e) {
  522. log.error("startCell: " + e.getMessage());
  523. throw new Error(e.getMessage());
  524. }
  525. }
  526. /**
  527. * @see org.apache.fop.fo.FOInputHandler#endCell(TableCell)
  528. */
  529. public void endCell(TableCell tc) {
  530. builderContext.popContainer();
  531. builderContext.getTableContext().selectNextColumn();
  532. }
  533. // Lists
  534. /**
  535. * @see org.apache.fop.fo.FOInputHandler#startList(ListBlock)
  536. */
  537. public void startList(ListBlock lb) {
  538. try {
  539. // create an RtfList in the current list container
  540. final IRtfListContainer c
  541. = (IRtfListContainer)builderContext.getContainer(
  542. IRtfListContainer.class, true, this);
  543. final RtfList newList = c.newList(
  544. ListAttributesConverter.convertAttributes(lb.properties));
  545. builderContext.pushContainer(newList);
  546. } catch (IOException ioe) {
  547. log.error("startList: " + ioe.getMessage());
  548. throw new Error(ioe.getMessage());
  549. } catch (FOPException fe) {
  550. log.error("startList: " + fe.getMessage());
  551. throw new Error(fe.getMessage());
  552. } catch (Exception e) {
  553. log.error("startList: " + e.getMessage());
  554. throw new Error(e.getMessage());
  555. }
  556. }
  557. /**
  558. * @see org.apache.fop.fo.FOInputHandler#endList(ListBlock)
  559. */
  560. public void endList(ListBlock lb) {
  561. builderContext.popContainer();
  562. }
  563. /**
  564. * @see org.apache.fop.fo.FOInputHandler#startListItem(ListItem)
  565. */
  566. public void startListItem(ListItem li) {
  567. // create an RtfListItem in the current RtfList
  568. try {
  569. final RtfList list = (RtfList)builderContext.getContainer(
  570. RtfList.class, true, this);
  571. builderContext.pushContainer(list.newListItem());
  572. } catch (IOException ioe) {
  573. log.error("startList: " + ioe.getMessage());
  574. throw new Error(ioe.getMessage());
  575. } catch (FOPException fe) {
  576. log.error("startList: " + fe.getMessage());
  577. throw new Error(fe.getMessage());
  578. } catch (Exception e) {
  579. log.error("startList: " + e.getMessage());
  580. throw new Error(e.getMessage());
  581. }
  582. }
  583. /**
  584. * @see org.apache.fop.fo.FOInputHandler#endListItem(ListItem)
  585. */
  586. public void endListItem(ListItem li) {
  587. builderContext.popContainer();
  588. }
  589. /**
  590. * @see org.apache.fop.fo.FOInputHandler#startListLabel()
  591. */
  592. public void startListLabel() {
  593. try {
  594. RtfListItem item
  595. = (RtfListItem)builderContext.getContainer(RtfListItem.class, true, this);
  596. RtfListItemLabel label = item.new RtfListItemLabel(item);
  597. builderContext.pushContainer(label);
  598. } catch (IOException ioe) {
  599. log.error("startPageNumber:" + ioe.getMessage());
  600. throw new Error(ioe.getMessage());
  601. } catch (Exception e) {
  602. log.error("startPageNumber: " + e.getMessage());
  603. throw new Error(e.getMessage());
  604. }
  605. }
  606. /**
  607. * @see org.apache.fop.fo.FOInputHandler#endListLabel()
  608. */
  609. public void endListLabel() {
  610. builderContext.popContainer();
  611. }
  612. /**
  613. * @see org.apache.fop.fo.FOInputHandler#startListBody()
  614. */
  615. public void startListBody() {
  616. }
  617. /**
  618. * @see org.apache.fop.fo.FOInputHandler#endListBody()
  619. */
  620. public void endListBody() {
  621. }
  622. // Static Regions
  623. /**
  624. * @see org.apache.fop.fo.FOInputHandler#startStatic()
  625. */
  626. public void startStatic() {
  627. }
  628. /**
  629. * @see org.apache.fop.fo.FOInputHandler#endStatic()
  630. */
  631. public void endStatic() {
  632. }
  633. /**
  634. * @see org.apache.fop.fo.FOInputHandler#startMarkup()
  635. */
  636. public void startMarkup() {
  637. }
  638. /**
  639. * @see org.apache.fop.fo.FOInputHandler#endMarkup()
  640. */
  641. public void endMarkup() {
  642. }
  643. /**
  644. * @see org.apache.fop.fo.FOInputHandler#startLink()
  645. */
  646. public void startLink() {
  647. }
  648. /**
  649. * @see org.apache.fop.fo.FOInputHandler#endLink()
  650. */
  651. public void endLink() {
  652. }
  653. /**
  654. * @see org.apache.fop.fo.FOInputHandler#image(ExternalGraphic)
  655. */
  656. public void image(ExternalGraphic eg) {
  657. try {
  658. final IRtfTextrunContainer c
  659. = (IRtfTextrunContainer)builderContext.getContainer(
  660. IRtfTextrunContainer.class, true, this);
  661. final RtfExternalGraphic newGraphic = c.getTextrun().newImage();
  662. Property p = null;
  663. //get source file
  664. if ((p = eg.properties.get("src")) != null) {
  665. newGraphic.setURL (p.getString());
  666. } else {
  667. log.error("The attribute 'src' of <fo:external-graphic> is required.");
  668. return;
  669. }
  670. //get scaling
  671. if ((p = eg.properties.get("scaling")) != null) {
  672. EnumProperty e = (EnumProperty)p;
  673. if (p.getEnum() == Constants.UNIFORM) {
  674. newGraphic.setScaling ("uniform");
  675. }
  676. }
  677. //get width
  678. if ((p = eg.properties.get("width")) != null) {
  679. LengthProperty lengthProp = (LengthProperty)p;
  680. if (lengthProp.getLength() instanceof FixedLength) {
  681. Float f = new Float(lengthProp.getLength().getValue() / 1000f);
  682. String sValue = f.toString() + "pt";
  683. newGraphic.setWidth(sValue);
  684. }
  685. }
  686. //get height
  687. if ((p = eg.properties.get("height")) != null) {
  688. LengthProperty lengthProp = (LengthProperty)p;
  689. if (lengthProp.getLength() instanceof FixedLength) {
  690. Float f = new Float(lengthProp.getLength().getValue() / 1000f);
  691. String sValue = f.toString() + "pt";
  692. newGraphic.setHeight(sValue);
  693. }
  694. }
  695. //TODO: make this configurable:
  696. // int compression = m_context.m_options.getRtfExternalGraphicCompressionRate ();
  697. int compression = 0;
  698. if (compression != 0) {
  699. if (!newGraphic.setCompressionRate(compression)) {
  700. log.warn("The compression rate " + compression
  701. + " is invalid. The value has to be between 1 and 100 %.");
  702. }
  703. }
  704. } catch (Exception e) {
  705. log.error("image: " + e.getMessage());
  706. }
  707. }
  708. /**
  709. * @see org.apache.fop.fo.FOInputHandler#pageRef()
  710. */
  711. public void pageRef() {
  712. }
  713. /**
  714. * @see org.apache.fop.fo.FOInputHandler#foreignObject(InstreamForeignObject)
  715. */
  716. public void foreignObject(InstreamForeignObject ifo) {
  717. }
  718. /**
  719. * @see org.apache.fop.fo.FOInputHandler#footnote()
  720. */
  721. public void footnote() {
  722. }
  723. /**
  724. * @see org.apache.fop.fo.FOInputHandler#leader(Leader)
  725. */
  726. public void leader(Leader l) {
  727. }
  728. /**
  729. * @see org.apache.fop.fo.FOInputHandler#characters(char[], int, int)
  730. */
  731. public void characters(char[] data, int start, int length) {
  732. try {
  733. IRtfTextrunContainer container
  734. = (IRtfTextrunContainer)builderContext.getContainer(
  735. IRtfTextrunContainer.class, true, this);
  736. RtfTextrun textrun = container.getTextrun();
  737. textrun.addString(new String(data, start, length));
  738. } catch (IOException ioe) {
  739. // FIXME could we throw Exception in all FOInputHandler events?
  740. log.error("characters: " + ioe.getMessage());
  741. throw new Error(ioe.getMessage());
  742. } catch (Exception e) {
  743. log.error("characters:" + e.getMessage());
  744. throw new Error(e.getMessage());
  745. }
  746. }
  747. /**
  748. *
  749. * @param pagenum PageNumber that is starting.
  750. */
  751. public void startPageNumber(PageNumber pagenum) {
  752. try {
  753. RtfAttributes rtfAttr
  754. = TextAttributesConverter.convertCharacterAttributes(
  755. pagenum.properties, null);
  756. IRtfTextrunContainer container
  757. = (IRtfTextrunContainer)builderContext.getContainer(
  758. IRtfTextrunContainer.class, true, this);
  759. RtfTextrun textrun = container.getTextrun();
  760. textrun.addPageNumber(rtfAttr);
  761. } catch (IOException ioe) {
  762. log.error("startPageNumber:" + ioe.getMessage());
  763. throw new Error(ioe.getMessage());
  764. } catch (Exception e) {
  765. log.error("startPageNumber: " + e.getMessage());
  766. throw new Error(e.getMessage());
  767. }
  768. }
  769. /**
  770. *
  771. * @param pagenum PageNumber that is ending.
  772. */
  773. public void endPageNumber(PageNumber pagenum) {
  774. }
  775. }