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

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