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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.mif;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.xml.sax.SAXException;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.fo.FOEventHandler;
  26. import org.apache.fop.fo.FOText;
  27. import org.apache.fop.fo.flow.BasicLink;
  28. import org.apache.fop.fo.flow.Block;
  29. import org.apache.fop.fo.flow.Character;
  30. import org.apache.fop.fo.flow.ExternalGraphic;
  31. import org.apache.fop.fo.flow.Footnote;
  32. import org.apache.fop.fo.flow.FootnoteBody;
  33. import org.apache.fop.fo.flow.Inline;
  34. import org.apache.fop.fo.flow.InstreamForeignObject;
  35. import org.apache.fop.fo.flow.Leader;
  36. import org.apache.fop.fo.flow.ListBlock;
  37. import org.apache.fop.fo.flow.ListItem;
  38. import org.apache.fop.fo.flow.ListItemBody;
  39. import org.apache.fop.fo.flow.ListItemLabel;
  40. import org.apache.fop.fo.flow.PageNumber;
  41. import org.apache.fop.fo.flow.table.Table;
  42. import org.apache.fop.fo.flow.table.TableBody;
  43. import org.apache.fop.fo.flow.table.TableCell;
  44. import org.apache.fop.fo.flow.table.TableColumn;
  45. import org.apache.fop.fo.flow.table.TableFooter;
  46. import org.apache.fop.fo.flow.table.TableHeader;
  47. import org.apache.fop.fo.flow.table.TableRow;
  48. import org.apache.fop.fo.pagination.Flow;
  49. import org.apache.fop.fo.pagination.PageSequence;
  50. import org.apache.fop.fo.pagination.PageSequenceMaster;
  51. import org.apache.fop.fo.pagination.SimplePageMaster;
  52. import org.apache.fop.fo.pagination.StaticContent;
  53. import org.apache.fop.fonts.FontSetup;
  54. // TODO: do we really want every method throwing a SAXException
  55. /**
  56. * The MIF Handler.
  57. * This generates MIF output using the structure events from
  58. * the FO Tree sent to this structure handler.
  59. * This builds an MIF file and writes it to the output.
  60. */
  61. public class MIFHandler extends FOEventHandler {
  62. /** Logger */
  63. private static Log log = LogFactory.getLog(MIFHandler.class);
  64. /** the MIFFile instance */
  65. protected MIFFile mifFile;
  66. /** the OutputStream to write to */
  67. protected OutputStream outStream;
  68. // current state elements
  69. private MIFElement textFlow;
  70. private MIFElement para;
  71. /**
  72. * Creates a new MIF handler on a given OutputStream.
  73. * @param ua FOUserAgent instance for this process
  74. * @param os OutputStream to write to
  75. */
  76. public MIFHandler(FOUserAgent ua, OutputStream os) {
  77. super(ua);
  78. outStream = os;
  79. boolean base14Kerning = false; //TODO - FIXME
  80. FontSetup.setup(fontInfo, null, ua.getResourceResolver(), base14Kerning);
  81. }
  82. /** {@inheritDoc} */
  83. public void startDocument() throws SAXException {
  84. log.fatal("The MIF Handler is non-functional at this time. Please help resurrect it!");
  85. mifFile = new MIFFile();
  86. try {
  87. mifFile.output(outStream);
  88. } catch (IOException ioe) {
  89. throw new SAXException(ioe);
  90. }
  91. }
  92. /** {@inheritDoc} */
  93. public void endDocument() throws SAXException {
  94. // finish all open elements
  95. mifFile.finish(true);
  96. try {
  97. mifFile.output(outStream);
  98. outStream.flush();
  99. } catch (IOException ioe) {
  100. throw new SAXException(ioe);
  101. }
  102. }
  103. /** {@inheritDoc} */
  104. public void startPageSequence(PageSequence pageSeq) {
  105. // get the layout master set
  106. // setup the pages for this sequence
  107. String name = pageSeq.getMasterReference();
  108. SimplePageMaster spm = pageSeq.getRoot().getLayoutMasterSet().getSimplePageMaster(name);
  109. if (spm == null) {
  110. PageSequenceMaster psm
  111. = 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. /** {@inheritDoc} */
  142. public void endPageSequence(PageSequence pageSeq) {
  143. }
  144. /** {@inheritDoc} */
  145. public void startFlow(Flow fl) {
  146. // start text flow in body region
  147. textFlow = new MIFElement("TextFlow");
  148. }
  149. /** {@inheritDoc} */
  150. public void endFlow(Flow fl) {
  151. textFlow.finish(true);
  152. mifFile.addElement(textFlow);
  153. textFlow = null;
  154. }
  155. /** {@inheritDoc} */
  156. public void startBlock(Block bl) {
  157. para = new MIFElement("Para");
  158. // get font
  159. textFlow.addElement(para);
  160. }
  161. /** {@inheritDoc} */
  162. public void endBlock(Block bl) {
  163. para.finish(true);
  164. para = null;
  165. }
  166. /** {@inheritDoc} */
  167. public void startInline(Inline inl) {
  168. }
  169. /** {@inheritDoc} */
  170. public void endInline(Inline inl) {
  171. }
  172. /** {@inheritDoc} */
  173. public void startTable(Table tbl) {
  174. }
  175. /** {@inheritDoc} */
  176. public void endTable(Table tbl) {
  177. }
  178. /** {@inheritDoc} */
  179. public void startColumn(TableColumn tc) {
  180. }
  181. /** {@inheritDoc} */
  182. public void endColumn(TableColumn tc) {
  183. }
  184. /** {@inheritDoc} */
  185. public void startHeader(TableHeader th) {
  186. }
  187. /** {@inheritDoc} */
  188. public void endHeader(TableHeader th) {
  189. }
  190. /** {@inheritDoc} */
  191. public void startFooter(TableFooter tf) {
  192. }
  193. /** {@inheritDoc} */
  194. public void endFooter(TableFooter tf) {
  195. }
  196. /** {@inheritDoc} */
  197. public void startBody(TableBody tb) {
  198. }
  199. /** {@inheritDoc} */
  200. public void endBody(TableBody tb) {
  201. }
  202. /** {@inheritDoc} */
  203. public void startRow(TableRow tr) {
  204. }
  205. /** {@inheritDoc} */
  206. public void endRow(TableRow tr) {
  207. }
  208. /** {@inheritDoc} */
  209. public void startCell(TableCell tc) {
  210. }
  211. /** {@inheritDoc} */
  212. public void endCell(TableCell tc) {
  213. }
  214. /** {@inheritDoc} */
  215. public void startList(ListBlock lb) {
  216. }
  217. /** {@inheritDoc} */
  218. public void endList(ListBlock lb) {
  219. }
  220. /** {@inheritDoc} */
  221. public void startListItem(ListItem li) {
  222. }
  223. /** {@inheritDoc} */
  224. public void endListItem(ListItem li) {
  225. }
  226. /** {@inheritDoc} */
  227. public void startListLabel(ListItemLabel listItemLabel) {
  228. }
  229. /** {@inheritDoc} */
  230. public void endListLabel(ListItemLabel listItemLabel) {
  231. }
  232. /** {@inheritDoc} */
  233. public void startListBody(ListItemBody listItemBody) {
  234. }
  235. /** {@inheritDoc} */
  236. public void endListBody(ListItemBody listItemBody) {
  237. }
  238. /** {@inheritDoc} */
  239. public void startStatic(StaticContent staticContent) {
  240. }
  241. /** {@inheritDoc} */
  242. public void endStatic(StaticContent staticContent) {
  243. }
  244. /** {@inheritDoc} */
  245. public void startMarkup() {
  246. }
  247. /** {@inheritDoc} */
  248. public void endMarkup() {
  249. }
  250. /** {@inheritDoc} */
  251. public void startLink(BasicLink basicLink) {
  252. }
  253. /** {@inheritDoc} */
  254. public void endLink(BasicLink basicLink) {
  255. }
  256. /** {@inheritDoc} */
  257. public void image(ExternalGraphic eg) {
  258. }
  259. /** {@inheritDoc} */
  260. public void pageRef() {
  261. }
  262. /** {@inheritDoc} */
  263. public void startInstreamForeignObject(InstreamForeignObject ifo) {
  264. }
  265. /** {@inheritDoc} */
  266. public void endInstreamForeignObject(InstreamForeignObject ifo) {
  267. }
  268. /** {@inheritDoc} */
  269. public void startFootnote(Footnote footnote) {
  270. }
  271. /** {@inheritDoc} */
  272. public void endFootnote(Footnote footnote) {
  273. }
  274. /** {@inheritDoc} */
  275. public void startFootnoteBody(FootnoteBody body) {
  276. }
  277. /** {@inheritDoc} */
  278. public void endFootnoteBody(FootnoteBody body) {
  279. }
  280. /** {@inheritDoc} */
  281. public void startLeader(Leader l) {
  282. }
  283. /** {@inheritDoc} */
  284. public void endLeader(Leader l) {
  285. }
  286. public void character(Character c) {
  287. appendCharacters ( new String ( new char[] {c.getCharacter()} ) );
  288. }
  289. /** {@inheritDoc} */
  290. public void characters(FOText foText) {
  291. appendCharacters ( foText.getCharSequence().toString() );
  292. }
  293. /** {@inheritDoc} */
  294. public void startPageNumber(PageNumber pagenum) {
  295. }
  296. /** {@inheritDoc} */
  297. public void endPageNumber(PageNumber pagenum) {
  298. }
  299. private void appendCharacters ( String str ) {
  300. if (para != null) {
  301. str = str.trim();
  302. // break into nice length chunks
  303. if (str.length() == 0) {
  304. return;
  305. }
  306. MIFElement line = new MIFElement("ParaLine");
  307. MIFElement prop = new MIFElement("TextRectID");
  308. prop.setValue("2");
  309. line.addElement(prop);
  310. prop = new MIFElement("String");
  311. prop.setValue("\"" + str + "\"");
  312. line.addElement(prop);
  313. para.addElement(line);
  314. }
  315. }
  316. }