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.

MIFHandler.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.mif;
  52. // Java
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import org.apache.fop.apps.Document;
  56. import org.apache.fop.apps.FOPException;
  57. import org.apache.fop.fo.FOInputHandler;
  58. import org.apache.fop.fo.flow.Block;
  59. import org.apache.fop.fo.flow.ExternalGraphic;
  60. import org.apache.fop.fo.flow.InstreamForeignObject;
  61. import org.apache.fop.fo.flow.Inline;
  62. import org.apache.fop.fo.flow.Leader;
  63. import org.apache.fop.fo.flow.ListBlock;
  64. import org.apache.fop.fo.flow.ListItem;
  65. import org.apache.fop.fo.flow.PageNumber;
  66. import org.apache.fop.fo.flow.Table;
  67. import org.apache.fop.fo.flow.TableBody;
  68. import org.apache.fop.fo.flow.TableCell;
  69. import org.apache.fop.fo.flow.TableColumn;
  70. import org.apache.fop.fo.flow.TableRow;
  71. import org.apache.fop.fo.pagination.Flow;
  72. import org.apache.fop.fo.pagination.PageSequence;
  73. import org.apache.fop.fo.pagination.PageSequenceMaster;
  74. import org.apache.fop.fo.pagination.SimplePageMaster;
  75. import org.xml.sax.SAXException;
  76. // TODO: do we really want every method throwing a SAXException
  77. /**
  78. * The MIF Handler.
  79. * This generates MIF output using the structure events from
  80. * the FO Tree sent to this structure handler.
  81. * This builds an MIF file and writes it to the output.
  82. */
  83. public class MIFHandler extends FOInputHandler {
  84. /** the MIFFile instance */
  85. protected MIFFile mifFile;
  86. /** the OutputStream to write to */
  87. protected OutputStream outStream;
  88. // current state elements
  89. private MIFElement textFlow;
  90. private MIFElement para;
  91. /**
  92. * Creates a new MIF handler on a given OutputStream.
  93. * @param os OutputStream to write to
  94. */
  95. public MIFHandler(Document doc, OutputStream os) {
  96. super(doc);
  97. outStream = os;
  98. // use pdf fonts for now, this is only for resolving names
  99. org.apache.fop.render.pdf.FontSetup.setup(doc, null);
  100. }
  101. /**
  102. * @see org.apache.fop.fo.FOInputHandler#startDocument()
  103. */
  104. public void startDocument() throws SAXException {
  105. mifFile = new MIFFile();
  106. try {
  107. mifFile.output(outStream);
  108. } catch (IOException ioe) {
  109. throw new SAXException(ioe);
  110. }
  111. }
  112. /**
  113. * @see org.apache.fop.fo.FOInputHandler#endDocument()
  114. */
  115. public void endDocument() throws SAXException {
  116. // finish all open elements
  117. mifFile.finish(true);
  118. try {
  119. mifFile.output(outStream);
  120. outStream.flush();
  121. } catch (IOException ioe) {
  122. throw new SAXException(ioe);
  123. }
  124. }
  125. /**
  126. * Start the page sequence.
  127. * This creates the pages in the MIF document that will be used
  128. * by the following flows and static areas.
  129. * @see org.apache.fop.fo.FOInputHandler
  130. */
  131. public void startPageSequence(PageSequence pageSeq) {
  132. // get the layout master set
  133. // setup the pages for this sequence
  134. String name = pageSeq.getProperty("master-reference").getString();
  135. SimplePageMaster spm = pageSeq.getLayoutMasterSet().getSimplePageMaster(name);
  136. if (spm == null) {
  137. PageSequenceMaster psm = pageSeq.getLayoutMasterSet().getPageSequenceMaster(name);
  138. } else {
  139. // create simple master with regions
  140. MIFElement prop = new MIFElement("PageType");
  141. prop.setValue("BodyPage");
  142. MIFElement page = new MIFElement("Page");
  143. page.addElement(prop);
  144. prop = new MIFElement("PageBackground");
  145. prop.setValue("'Default'");
  146. page.addElement(prop);
  147. // build regions
  148. MIFElement textRect = new MIFElement("TextRect");
  149. prop = new MIFElement("ID");
  150. prop.setValue("1");
  151. textRect.addElement(prop);
  152. prop = new MIFElement("ShapeRect");
  153. prop.setValue("0.0 841.889 453.543 0.0");
  154. textRect.addElement(prop);
  155. page.addElement(textRect);
  156. textRect = new MIFElement("TextRect");
  157. prop = new MIFElement("ID");
  158. prop.setValue("2");
  159. textRect.addElement(prop);
  160. prop = new MIFElement("ShapeRect");
  161. prop.setValue("0.0 841.889 453.543 187.65");
  162. textRect.addElement(prop);
  163. page.addElement(textRect);
  164. mifFile.addPage(page);
  165. }
  166. }
  167. /**
  168. * @see org.apache.fop.fo.FOInputHandler#endPageSequence(PageSequence)
  169. */
  170. public void endPageSequence(PageSequence pageSeq) throws FOPException {
  171. }
  172. /**
  173. * @see org.apache.fop.fo.FOInputHandler#startFlow(Flow)
  174. */
  175. public void startFlow(Flow fl) {
  176. // start text flow in body region
  177. textFlow = new MIFElement("TextFlow");
  178. }
  179. /**
  180. * @see org.apache.fop.fo.FOInputHandler#endFlow(Flow)
  181. */
  182. public void endFlow(Flow fl) {
  183. textFlow.finish(true);
  184. mifFile.addElement(textFlow);
  185. textFlow = null;
  186. }
  187. /**
  188. * @see org.apache.fop.fo.FOInputHandler#startBlock(Block)
  189. */
  190. public void startBlock(Block bl) {
  191. para = new MIFElement("Para");
  192. // get font
  193. textFlow.addElement(para);
  194. }
  195. /**
  196. * @see org.apache.fop.fo.FOInputHandler#endBlock(Block)
  197. */
  198. public void endBlock(Block bl) {
  199. para.finish(true);
  200. para = null;
  201. }
  202. /**
  203. *
  204. * @param inl Inline that is starting.
  205. */
  206. public void startInline(Inline inl){
  207. }
  208. /**
  209. *
  210. * @param inl Inline that is ending.
  211. */
  212. public void endInline(Inline inl){
  213. }
  214. /**
  215. * @see org.apache.fop.fo.FOInputHandler#startTable(Table)
  216. */
  217. public void startTable(Table tbl) {
  218. }
  219. /**
  220. * @see org.apache.fop.fo.FOInputHandler#endTable(Table)
  221. */
  222. public void endTable(Table tbl) {
  223. }
  224. /**
  225. *
  226. * @param tc TableColumn that is starting;
  227. */
  228. public void startColumn(TableColumn tc) {
  229. }
  230. /**
  231. *
  232. * @param tc TableColumn that is ending;
  233. */
  234. public void endColumn(TableColumn tc) {
  235. }
  236. /**
  237. * @see org.apache.fop.fo.FOInputHandler#startHeader(TableBody)
  238. */
  239. public void startHeader(TableBody th) {
  240. }
  241. /**
  242. * @see org.apache.fop.fo.FOInputHandler#endHeader(TableBody)
  243. */
  244. public void endHeader(TableBody th) {
  245. }
  246. /**
  247. * @see org.apache.fop.fo.FOInputHandler#startFooter(TableBody)
  248. */
  249. public void startFooter(TableBody tf) {
  250. }
  251. /**
  252. * @see org.apache.fop.fo.FOInputHandler#endFooter(TableBody)
  253. */
  254. public void endFooter(TableBody tf) {
  255. }
  256. /**
  257. * @see org.apache.fop.fo.FOInputHandler#startBody(TableBody)
  258. */
  259. public void startBody(TableBody tb) {
  260. }
  261. /**
  262. * @see org.apache.fop.fo.FOInputHandler#endBody(TableBody)
  263. */
  264. public void endBody(TableBody tb) {
  265. }
  266. /**
  267. * @see org.apache.fop.fo.FOInputHandler#startRow(TableRow)
  268. */
  269. public void startRow(TableRow tr) {
  270. }
  271. /**
  272. * @see org.apache.fop.fo.FOInputHandler#endRow(TableRow)
  273. */
  274. public void endRow(TableRow tr) {
  275. }
  276. /**
  277. * @see org.apache.fop.fo.FOInputHandler#startCell(TableCell)
  278. */
  279. public void startCell(TableCell tc) {
  280. }
  281. /**
  282. * @see org.apache.fop.fo.FOInputHandler#endCell(TableCell)
  283. */
  284. public void endCell(TableCell tc) {
  285. }
  286. // Lists
  287. /**
  288. * @see org.apache.fop.fo.FOInputHandler#startList(ListBlock)
  289. */
  290. public void startList(ListBlock lb) {
  291. }
  292. /**
  293. * @see org.apache.fop.fo.FOInputHandler#endList(ListBlock)
  294. */
  295. public void endList(ListBlock lb) {
  296. }
  297. /**
  298. * @see org.apache.fop.fo.FOInputHandler#startListItem(ListItem)
  299. */
  300. public void startListItem(ListItem li) {
  301. }
  302. /**
  303. * @see org.apache.fop.fo.FOInputHandler#endListItem(ListItem)
  304. */
  305. public void endListItem(ListItem li) {
  306. }
  307. /**
  308. * @see org.apache.fop.fo.FOInputHandler#startListLabel()
  309. */
  310. public void startListLabel() {
  311. }
  312. /**
  313. * @see org.apache.fop.fo.FOInputHandler#endListLabel()
  314. */
  315. public void endListLabel() {
  316. }
  317. /**
  318. * @see org.apache.fop.fo.FOInputHandler#startListBody()
  319. */
  320. public void startListBody() {
  321. }
  322. /**
  323. * @see org.apache.fop.fo.FOInputHandler#endListBody()
  324. */
  325. public void endListBody() {
  326. }
  327. // Static Regions
  328. /**
  329. * @see org.apache.fop.fo.FOInputHandler#startStatic()
  330. */
  331. public void startStatic() {
  332. }
  333. /**
  334. * @see org.apache.fop.fo.FOInputHandler#endStatic()
  335. */
  336. public void endStatic() {
  337. }
  338. /**
  339. * @see org.apache.fop.fo.FOInputHandler#startMarkup()
  340. */
  341. public void startMarkup() {
  342. }
  343. /**
  344. * @see org.apache.fop.fo.FOInputHandler#endMarkup()
  345. */
  346. public void endMarkup() {
  347. }
  348. /**
  349. * @see org.apache.fop.fo.FOInputHandler#startLink()
  350. */
  351. public void startLink() {
  352. }
  353. /**
  354. * @see org.apache.fop.fo.FOInputHandler#endLink()
  355. */
  356. public void endLink() {
  357. }
  358. /**
  359. * @see org.apache.fop.fo.FOInputHandler#image(ExternalGraphic)
  360. */
  361. public void image(ExternalGraphic eg) {
  362. }
  363. /**
  364. * @see org.apache.fop.fo.FOInputHandler#pageRef()
  365. */
  366. public void pageRef() {
  367. }
  368. /**
  369. * @see org.apache.fop.fo.FOInputHandler#foreignObject(InstreamForeignObject)
  370. */
  371. public void foreignObject(InstreamForeignObject ifo) {
  372. }
  373. /**
  374. * @see org.apache.fop.fo.FOInputHandler#footnote()
  375. */
  376. public void footnote() {
  377. }
  378. /**
  379. * @see org.apache.fop.fo.FOInputHandler#leader(Leader)
  380. */
  381. public void leader(Leader l) {
  382. }
  383. /**
  384. * @see org.apache.fop.fo.FOInputHandler#characters(char[], int, int)
  385. */
  386. public void characters(char data[], int start, int length) {
  387. if (para != null) {
  388. String str = new String(data, start, length);
  389. str = str.trim();
  390. // break into nice length chunks
  391. if (str.length() == 0) {
  392. return;
  393. }
  394. MIFElement line = new MIFElement("ParaLine");
  395. MIFElement prop = new MIFElement("TextRectID");
  396. prop.setValue("2");
  397. line.addElement(prop);
  398. prop = new MIFElement("String");
  399. prop.setValue("\"" + str + "\"");
  400. line.addElement(prop);
  401. para.addElement(line);
  402. }
  403. }
  404. /**
  405. *
  406. * @param pagenum PageNumber that is starting.
  407. */
  408. public void startPageNumber(PageNumber pagenum) {
  409. }
  410. /**
  411. *
  412. * @param pagenum PageNumber that is ending.
  413. */
  414. public void endPageNumber(PageNumber pagenum) {
  415. }
  416. }