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

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