Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MIFHandler.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.mif;
  18. // Java
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.apps.FOUserAgent;
  24. import org.apache.fop.fo.FOEventHandler;
  25. import org.apache.fop.fo.flow.BasicLink;
  26. import org.apache.fop.fo.flow.Block;
  27. import org.apache.fop.fo.flow.ExternalGraphic;
  28. import org.apache.fop.fo.flow.Footnote;
  29. import org.apache.fop.fo.flow.FootnoteBody;
  30. import org.apache.fop.fo.flow.InstreamForeignObject;
  31. import org.apache.fop.fo.flow.Inline;
  32. import org.apache.fop.fo.flow.Leader;
  33. import org.apache.fop.fo.flow.ListBlock;
  34. import org.apache.fop.fo.flow.ListItem;
  35. import org.apache.fop.fo.flow.PageNumber;
  36. import org.apache.fop.fo.flow.Table;
  37. import org.apache.fop.fo.flow.TableBody;
  38. import org.apache.fop.fo.flow.TableCell;
  39. import org.apache.fop.fo.flow.TableColumn;
  40. import org.apache.fop.fo.flow.TableRow;
  41. import org.apache.fop.fo.pagination.Flow;
  42. import org.apache.fop.fo.pagination.PageSequence;
  43. import org.apache.fop.fo.pagination.PageSequenceMaster;
  44. import org.apache.fop.fo.pagination.SimplePageMaster;
  45. import org.apache.fop.fonts.FontSetup;
  46. import org.xml.sax.SAXException;
  47. // TODO: do we really want every method throwing a SAXException
  48. /**
  49. * The MIF Handler.
  50. * This generates MIF output using the structure events from
  51. * the FO Tree sent to this structure handler.
  52. * This builds an MIF file and writes it to the output.
  53. */
  54. public class MIFHandler extends FOEventHandler {
  55. /** Logger */
  56. private static Log log = LogFactory.getLog(MIFHandler.class);
  57. /** the MIFFile instance */
  58. protected MIFFile mifFile;
  59. /** the OutputStream to write to */
  60. protected OutputStream outStream;
  61. // current state elements
  62. private MIFElement textFlow;
  63. private MIFElement para;
  64. /**
  65. * Creates a new MIF handler on a given OutputStream.
  66. * @param ua FOUserAgent instance for this process
  67. * @param os OutputStream to write to
  68. */
  69. public MIFHandler(FOUserAgent ua, OutputStream os) {
  70. super(ua);
  71. outStream = os;
  72. FontSetup.setup(fontInfo, null);
  73. }
  74. /**
  75. * @see org.apache.fop.fo.FOEventHandler#startDocument()
  76. */
  77. public void startDocument() throws SAXException {
  78. log.fatal("The MIF Handler is non-functional at this time. Please help resurrect it!");
  79. mifFile = new MIFFile();
  80. try {
  81. mifFile.output(outStream);
  82. } catch (IOException ioe) {
  83. throw new SAXException(ioe);
  84. }
  85. }
  86. /**
  87. * @see org.apache.fop.fo.FOEventHandler#endDocument()
  88. */
  89. public void endDocument() throws SAXException {
  90. // finish all open elements
  91. mifFile.finish(true);
  92. try {
  93. mifFile.output(outStream);
  94. outStream.flush();
  95. } catch (IOException ioe) {
  96. throw new SAXException(ioe);
  97. }
  98. }
  99. /**
  100. * Start the page sequence.
  101. * This creates the pages in the MIF document that will be used
  102. * by the following flows and static areas.
  103. * @see org.apache.fop.fo.FOEventHandler
  104. */
  105. public void startPageSequence(PageSequence pageSeq) {
  106. // get the layout master set
  107. // setup the pages for this sequence
  108. String name = pageSeq.getMasterReference();
  109. SimplePageMaster spm = pageSeq.getRoot().getLayoutMasterSet().getSimplePageMaster(name);
  110. if (spm == null) {
  111. PageSequenceMaster psm = pageSeq.getRoot().getLayoutMasterSet().getPageSequenceMaster(name);
  112. } else {
  113. // create simple master with regions
  114. MIFElement prop = new MIFElement("PageType");
  115. prop.setValue("BodyPage");
  116. MIFElement page = new MIFElement("Page");
  117. page.addElement(prop);
  118. prop = new MIFElement("PageBackground");
  119. prop.setValue("'Default'");
  120. page.addElement(prop);
  121. // build regions
  122. MIFElement textRect = new MIFElement("TextRect");
  123. prop = new MIFElement("ID");
  124. prop.setValue("1");
  125. textRect.addElement(prop);
  126. prop = new MIFElement("ShapeRect");
  127. prop.setValue("0.0 841.889 453.543 0.0");
  128. textRect.addElement(prop);
  129. page.addElement(textRect);
  130. textRect = new MIFElement("TextRect");
  131. prop = new MIFElement("ID");
  132. prop.setValue("2");
  133. textRect.addElement(prop);
  134. prop = new MIFElement("ShapeRect");
  135. prop.setValue("0.0 841.889 453.543 187.65");
  136. textRect.addElement(prop);
  137. page.addElement(textRect);
  138. mifFile.addPage(page);
  139. }
  140. }
  141. /**
  142. * @see org.apache.fop.fo.FOEventHandler#endPageSequence(PageSequence)
  143. */
  144. public void endPageSequence(PageSequence pageSeq) {
  145. }
  146. /**
  147. * @see org.apache.fop.fo.FOEventHandler#startFlow(Flow)
  148. */
  149. public void startFlow(Flow fl) {
  150. // start text flow in body region
  151. textFlow = new MIFElement("TextFlow");
  152. }
  153. /**
  154. * @see org.apache.fop.fo.FOEventHandler#endFlow(Flow)
  155. */
  156. public void endFlow(Flow fl) {
  157. textFlow.finish(true);
  158. mifFile.addElement(textFlow);
  159. textFlow = null;
  160. }
  161. /**
  162. * @see org.apache.fop.fo.FOEventHandler#startBlock(Block)
  163. */
  164. public void startBlock(Block bl) {
  165. para = new MIFElement("Para");
  166. // get font
  167. textFlow.addElement(para);
  168. }
  169. /**
  170. * @see org.apache.fop.fo.FOEventHandler#endBlock(Block)
  171. */
  172. public void endBlock(Block bl) {
  173. para.finish(true);
  174. para = null;
  175. }
  176. /**
  177. *
  178. * @param inl Inline that is starting.
  179. */
  180. public void startInline(Inline inl){
  181. }
  182. /**
  183. *
  184. * @param inl Inline that is ending.
  185. */
  186. public void endInline(Inline inl){
  187. }
  188. /**
  189. * @see org.apache.fop.fo.FOEventHandler#startTable(Table)
  190. */
  191. public void startTable(Table tbl) {
  192. }
  193. /**
  194. * @see org.apache.fop.fo.FOEventHandler#endTable(Table)
  195. */
  196. public void endTable(Table tbl) {
  197. }
  198. /**
  199. *
  200. * @param tc TableColumn that is starting;
  201. */
  202. public void startColumn(TableColumn tc) {
  203. }
  204. /**
  205. *
  206. * @param tc TableColumn that is ending;
  207. */
  208. public void endColumn(TableColumn tc) {
  209. }
  210. /**
  211. * @see org.apache.fop.fo.FOEventHandler#startHeader(TableBody)
  212. */
  213. public void startHeader(TableBody th) {
  214. }
  215. /**
  216. * @see org.apache.fop.fo.FOEventHandler#endHeader(TableBody)
  217. */
  218. public void endHeader(TableBody th) {
  219. }
  220. /**
  221. * @see org.apache.fop.fo.FOEventHandler#startFooter(TableBody)
  222. */
  223. public void startFooter(TableBody tf) {
  224. }
  225. /**
  226. * @see org.apache.fop.fo.FOEventHandler#endFooter(TableBody)
  227. */
  228. public void endFooter(TableBody tf) {
  229. }
  230. /**
  231. * @see org.apache.fop.fo.FOEventHandler#startBody(TableBody)
  232. */
  233. public void startBody(TableBody tb) {
  234. }
  235. /**
  236. * @see org.apache.fop.fo.FOEventHandler#endBody(TableBody)
  237. */
  238. public void endBody(TableBody tb) {
  239. }
  240. /**
  241. * @see org.apache.fop.fo.FOEventHandler#startRow(TableRow)
  242. */
  243. public void startRow(TableRow tr) {
  244. }
  245. /**
  246. * @see org.apache.fop.fo.FOEventHandler#endRow(TableRow)
  247. */
  248. public void endRow(TableRow tr) {
  249. }
  250. /**
  251. * @see org.apache.fop.fo.FOEventHandler#startCell(TableCell)
  252. */
  253. public void startCell(TableCell tc) {
  254. }
  255. /**
  256. * @see org.apache.fop.fo.FOEventHandler#endCell(TableCell)
  257. */
  258. public void endCell(TableCell tc) {
  259. }
  260. // Lists
  261. /**
  262. * @see org.apache.fop.fo.FOEventHandler#startList(ListBlock)
  263. */
  264. public void startList(ListBlock lb) {
  265. }
  266. /**
  267. * @see org.apache.fop.fo.FOEventHandler#endList(ListBlock)
  268. */
  269. public void endList(ListBlock lb) {
  270. }
  271. /**
  272. * @see org.apache.fop.fo.FOEventHandler#startListItem(ListItem)
  273. */
  274. public void startListItem(ListItem li) {
  275. }
  276. /**
  277. * @see org.apache.fop.fo.FOEventHandler#endListItem(ListItem)
  278. */
  279. public void endListItem(ListItem li) {
  280. }
  281. /**
  282. * @see org.apache.fop.fo.FOEventHandler#startListLabel()
  283. */
  284. public void startListLabel() {
  285. }
  286. /**
  287. * @see org.apache.fop.fo.FOEventHandler#endListLabel()
  288. */
  289. public void endListLabel() {
  290. }
  291. /**
  292. * @see org.apache.fop.fo.FOEventHandler#startListBody()
  293. */
  294. public void startListBody() {
  295. }
  296. /**
  297. * @see org.apache.fop.fo.FOEventHandler#endListBody()
  298. */
  299. public void endListBody() {
  300. }
  301. // Static Regions
  302. /**
  303. * @see org.apache.fop.fo.FOEventHandler#startStatic()
  304. */
  305. public void startStatic() {
  306. }
  307. /**
  308. * @see org.apache.fop.fo.FOEventHandler#endStatic()
  309. */
  310. public void endStatic() {
  311. }
  312. /**
  313. * @see org.apache.fop.fo.FOEventHandler#startMarkup()
  314. */
  315. public void startMarkup() {
  316. }
  317. /**
  318. * @see org.apache.fop.fo.FOEventHandler#endMarkup()
  319. */
  320. public void endMarkup() {
  321. }
  322. /**
  323. * @see org.apache.fop.fo.FOEventHandler#startLink(BasicLink basicLink)
  324. */
  325. public void startLink(BasicLink basicLink) {
  326. }
  327. /**
  328. * @see org.apache.fop.fo.FOEventHandler#endLink()
  329. */
  330. public void endLink() {
  331. }
  332. /**
  333. * @see org.apache.fop.fo.FOEventHandler#image(ExternalGraphic)
  334. */
  335. public void image(ExternalGraphic eg) {
  336. }
  337. /**
  338. * @see org.apache.fop.fo.FOEventHandler#pageRef()
  339. */
  340. public void pageRef() {
  341. }
  342. /**
  343. * @see org.apache.fop.fo.FOEventHandler#foreignObject(InstreamForeignObject)
  344. */
  345. public void foreignObject(InstreamForeignObject ifo) {
  346. }
  347. /**
  348. * @see org.apache.fop.fo.FOEventHandler#startFootnote(Footnote)
  349. */
  350. public void startFootnote(Footnote footnote) {
  351. }
  352. /**
  353. * @see org.apache.fop.fo.FOEventHandler#endFootnote(Footnote)
  354. */
  355. public void endFootnote(Footnote footnote) {
  356. }
  357. /**
  358. * @see org.apache.fop.fo.FOEventHandler#startFootnoteBody(FootnoteBody)
  359. */
  360. public void startFootnoteBody(FootnoteBody body) {
  361. }
  362. /**
  363. * @see org.apache.fop.fo.FOEventHandler#endFootnoteBody(FootnoteBody)
  364. */
  365. public void endFootnoteBody(FootnoteBody body) {
  366. }
  367. /**
  368. * @see org.apache.fop.fo.FOEventHandler#leader(Leader)
  369. */
  370. public void leader(Leader l) {
  371. }
  372. /**
  373. * @see org.apache.fop.fo.FOEventHandler#characters(char[], int, int)
  374. */
  375. public void characters(char data[], int start, int length) {
  376. if (para != null) {
  377. String str = new String(data, start, length);
  378. str = str.trim();
  379. // break into nice length chunks
  380. if (str.length() == 0) {
  381. return;
  382. }
  383. MIFElement line = new MIFElement("ParaLine");
  384. MIFElement prop = new MIFElement("TextRectID");
  385. prop.setValue("2");
  386. line.addElement(prop);
  387. prop = new MIFElement("String");
  388. prop.setValue("\"" + str + "\"");
  389. line.addElement(prop);
  390. para.addElement(line);
  391. }
  392. }
  393. /**
  394. *
  395. * @param pagenum PageNumber that is starting.
  396. */
  397. public void startPageNumber(PageNumber pagenum) {
  398. }
  399. /**
  400. *
  401. * @param pagenum PageNumber that is ending.
  402. */
  403. public void endPageNumber(PageNumber pagenum) {
  404. }
  405. }