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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.mif;
  8. import org.apache.fop.apps.StructureHandler;
  9. import org.apache.fop.layout.FontInfo;
  10. import org.apache.fop.fo.pagination.*;
  11. import org.apache.fop.fo.flow.*;
  12. import org.apache.fop.fo.*;
  13. import org.apache.fop.apps.FOPException;
  14. // Java
  15. import java.io.*;
  16. import java.util.*;
  17. import org.xml.sax.SAXException;
  18. // do we really want every method throwing a SAXException
  19. /**
  20. * The MIF Handler.
  21. * This generates MIF output using the structure events from
  22. * the FO Tree sent to this structure handler.
  23. * This builds an MIF file and writes it to the output.
  24. */
  25. public class MIFHandler extends StructureHandler {
  26. protected MIFFile mifFile;
  27. protected OutputStream outStream;
  28. private FontInfo fontInfo = new FontInfo();
  29. // current state elements
  30. MIFElement textFlow;
  31. MIFElement para;
  32. /**
  33. */
  34. public MIFHandler(OutputStream os) {
  35. outStream = os;
  36. // use pdf fonts for now, this is only for resolving names
  37. org.apache.fop.render.pdf.FontSetup.setup(fontInfo, null);
  38. }
  39. public FontInfo getFontInfo() {
  40. return fontInfo;
  41. }
  42. public void startDocument() throws SAXException {
  43. mifFile = new MIFFile();
  44. try {
  45. mifFile.output(outStream);
  46. } catch(IOException ioe) {
  47. throw new SAXException(ioe);
  48. }
  49. }
  50. public void endDocument() throws SAXException {
  51. // finish all open elements
  52. mifFile.finish(true);
  53. try {
  54. mifFile.output(outStream);
  55. outStream.flush();
  56. } catch(IOException ioe) {
  57. throw new SAXException(ioe);
  58. }
  59. }
  60. /**
  61. * Start the page sequence.
  62. * This creates the pages in the MIF document that will be used
  63. * by the following flows and static areas.
  64. */
  65. public void startPageSequence(PageSequence pageSeq, Title seqTitle, LayoutMasterSet lms) {
  66. // get the layout master set
  67. // setup the pages for this sequence
  68. String name = pageSeq.getProperty("master-reference").getString();
  69. SimplePageMaster spm = lms.getSimplePageMaster(name);
  70. if(spm == null) {
  71. PageSequenceMaster psm = lms.getPageSequenceMaster(name);
  72. } else {
  73. // create simple master with regions
  74. MIFElement prop = new MIFElement("PageType");
  75. prop.setValue("BodyPage");
  76. MIFElement page = new MIFElement("Page");
  77. page.addElement(prop);
  78. prop = new MIFElement("PageBackground");
  79. prop.setValue("'Default'");
  80. page.addElement(prop);
  81. // build regions
  82. MIFElement textRect = new MIFElement("TextRect");
  83. prop = new MIFElement("ID");
  84. prop.setValue("1");
  85. textRect.addElement(prop);
  86. prop = new MIFElement("ShapeRect");
  87. prop.setValue("0.0 841.889 453.543 0.0");
  88. textRect.addElement(prop);
  89. page.addElement(textRect);
  90. textRect = new MIFElement("TextRect");
  91. prop = new MIFElement("ID");
  92. prop.setValue("2");
  93. textRect.addElement(prop);
  94. prop = new MIFElement("ShapeRect");
  95. prop.setValue("0.0 841.889 453.543 187.65");
  96. textRect.addElement(prop);
  97. page.addElement(textRect);
  98. mifFile.addPage(page);
  99. }
  100. }
  101. public void endPageSequence(PageSequence pageSeq) throws FOPException {
  102. }
  103. public void startFlow(Flow fl) {
  104. // start text flow in body region
  105. textFlow = new MIFElement("TextFlow");
  106. }
  107. public void endFlow(Flow fl) {
  108. textFlow.finish(true);
  109. mifFile.addElement(textFlow);
  110. textFlow = null;
  111. }
  112. public void startBlock(Block bl) {
  113. para = new MIFElement("Para");
  114. // get font
  115. textFlow.addElement(para);
  116. }
  117. public void endBlock(Block bl) {
  118. para.finish(true);
  119. para = null;
  120. }
  121. public void characters(char data[], int start, int length) {
  122. if(para != null) {
  123. String str = new String(data, start, length);
  124. str = str.trim();
  125. // break into nice length chunks
  126. if(str.length() == 0) {
  127. return;
  128. }
  129. MIFElement line = new MIFElement("ParaLine");
  130. MIFElement prop = new MIFElement("TextRectID");
  131. prop.setValue("2");
  132. line.addElement(prop);
  133. prop = new MIFElement("String");
  134. prop.setValue("\"" + str + "\"");
  135. line.addElement(prop);
  136. para.addElement(line);
  137. }
  138. }
  139. }