Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

StreamRenderer.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package org.apache.fop.apps;
  2. import java.io.OutputStream;
  3. import java.io.IOException;
  4. import java.util.Vector;
  5. import java.util.Enumeration;
  6. import org.xml.sax.SAXException;
  7. import org.apache.fop.layout.FontInfo;
  8. import org.apache.fop.layout.Page;
  9. import org.apache.fop.render.Renderer;
  10. import org.apache.fop.layout.AreaTree;
  11. import org.apache.fop.datatypes.IDReferences;
  12. import org.apache.fop.extensions.ExtensionObj;
  13. import org.apache.fop.fo.pagination.PageSequence;
  14. import org.apache.log.Logger;
  15. /**
  16. This class acts as a bridge between the XML:FO parser
  17. and the formatting/rendering classes. It will queue
  18. PageSequences up until all the IDs required by them
  19. are satisfied, at which time it will render the
  20. pages.<P>
  21. StreamRenderer is created by Driver and called from
  22. FOTreeBuilder when a PageSequence is created,
  23. and AreaTree when a Page is formatted.<P>
  24. */
  25. public class StreamRenderer {
  26. private static final boolean MEM_PROFILE_WITH_GC = false;
  27. /**
  28. Somewhere to get our stats from.
  29. */
  30. private Runtime runtime = Runtime.getRuntime();
  31. /**
  32. Keep track of the number of pages rendered.
  33. */
  34. int pageCount = 0;
  35. /**
  36. Keep track of heap memory allocated,
  37. for statistical purposes.
  38. */
  39. private long initialMemory;
  40. /**
  41. Keep track of time used by renderer.
  42. */
  43. private long startTime;
  44. /**
  45. The stream to which this rendering is to be
  46. written to. <B>Note</B> that some renderers
  47. do not render to a stream, and that this
  48. member can therefore be null.
  49. */
  50. private OutputStream outputStream;
  51. /**
  52. The renderer being used.
  53. */
  54. private Renderer renderer;
  55. /**
  56. The FontInfo for this renderer.
  57. */
  58. private FontInfo fontInfo = new FontInfo();
  59. /**
  60. The list of pages waiting to be renderered.
  61. */
  62. private Vector renderQueue = new Vector();
  63. /**
  64. The current set of IDReferences, passed to the
  65. areatrees and pages. This is used by the AreaTree
  66. as a single map of all IDs.
  67. */
  68. private IDReferences idReferences = new IDReferences();
  69. /**
  70. * The list of extensions.
  71. */
  72. private Vector extensions = new Vector();
  73. private Logger log;
  74. public StreamRenderer(OutputStream outputStream, Renderer renderer) {
  75. this.outputStream = outputStream;
  76. this.renderer = renderer;
  77. }
  78. public void setLogger(Logger logger) {
  79. log = logger;
  80. }
  81. public IDReferences getIDReferences() {
  82. return idReferences;
  83. }
  84. public void addExtension(ExtensionObj ext) {
  85. extensions.addElement(ext);
  86. }
  87. public void startRenderer()
  88. throws SAXException {
  89. pageCount = 0;
  90. if (MEM_PROFILE_WITH_GC)
  91. System.gc(); // This takes time but gives better results
  92. initialMemory = runtime.totalMemory() - runtime.freeMemory();
  93. startTime = System.currentTimeMillis();
  94. try {
  95. renderer.setupFontInfo(fontInfo);
  96. renderer.startRenderer(outputStream);
  97. } catch (IOException e) {
  98. throw new SAXException(e);
  99. }
  100. }
  101. public void stopRenderer()
  102. throws SAXException {
  103. /*
  104. Force the processing of any more queue elements,
  105. even if they are not resolved.
  106. */
  107. try {
  108. processQueue(true);
  109. renderer.stopRenderer();
  110. } catch (FOPException e) {
  111. throw new SAXException(e);
  112. }
  113. catch (IOException e) {
  114. throw new SAXException(e);
  115. }
  116. if (MEM_PROFILE_WITH_GC)
  117. System.gc(); // This takes time but gives better results
  118. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  119. long memoryUsed = (memoryNow - initialMemory) / 1024L;
  120. log.debug("Initial heap size: " + (initialMemory/1024L) + "Kb");
  121. log.debug("Current heap size: " + (memoryNow/1024L) + "Kb");
  122. log.debug("Total memory used: " + memoryUsed + "Kb");
  123. if (!MEM_PROFILE_WITH_GC) {
  124. log.debug(" Memory use is indicative; no GC was performed");
  125. log.debug(" These figures should not be used comparatively");
  126. }
  127. long timeUsed = System.currentTimeMillis() - startTime;
  128. log.debug("Total time used: " + timeUsed + "ms");
  129. log.debug("Pages rendererd: " + pageCount);
  130. log.debug("Avg render time: " + (timeUsed / pageCount) + "ms/page");
  131. }
  132. /**
  133. Format the PageSequence. The PageSequence
  134. formats Pages and adds them to the AreaTree,
  135. which subsequently calls the StreamRenderer
  136. instance (this) again to render the page.
  137. At this time the page might be printed
  138. or it might be queued. A page might not
  139. be renderable immediately if the IDReferences
  140. are not all valid. In this case we defer
  141. the rendering until they are all valid.
  142. */
  143. public void render(PageSequence pageSequence)
  144. throws SAXException {
  145. AreaTree a = new AreaTree(this);
  146. a.setFontInfo(fontInfo);
  147. for(Enumeration e = extensions.elements(); e.hasMoreElements(); ) {
  148. ExtensionObj ext = (ExtensionObj)e.nextElement();
  149. try {
  150. ext.format(a);
  151. } catch (FOPException fope) {
  152. throw new SAXException(fope);
  153. }
  154. }
  155. try {
  156. pageSequence.format(a);
  157. } catch (FOPException e) {
  158. throw new SAXException(e);
  159. }
  160. }
  161. public synchronized void queuePage(Page page)
  162. throws FOPException, IOException {
  163. /*
  164. Try to optimise on the common case that there are
  165. no pages pending and that all ID references are
  166. valid on the current pages. This short-cuts the
  167. pipeline and renders the area immediately.
  168. */
  169. if ((renderQueue.size() == 0) && idReferences.isEveryIdValid()) {
  170. //renderer.render(page, outputStream);
  171. } else {
  172. addToRenderQueue(page);
  173. }
  174. pageCount++;
  175. }
  176. private synchronized void addToRenderQueue(Page page)
  177. throws FOPException, IOException {
  178. RenderQueueEntry entry = new RenderQueueEntry(page);
  179. renderQueue.addElement(entry);
  180. /*
  181. The just-added entry could (possibly) resolve the
  182. waiting entries, so we try to process the queue
  183. now to see.
  184. */
  185. processQueue(false);
  186. }
  187. /**
  188. Try to process the queue from the first entry forward.
  189. If an entry can't be processed, then the queue can't
  190. move forward, so return.
  191. */
  192. private synchronized void processQueue(boolean force)
  193. throws FOPException, IOException {
  194. while (renderQueue.size() > 0) {
  195. RenderQueueEntry entry = (RenderQueueEntry) renderQueue.elementAt(0);
  196. if ((!force) && (!entry.isResolved()))
  197. break;
  198. //renderer.render(entry.getPage(), outputStream);
  199. /* TODO
  200. Enumeration rootEnumeration =
  201. entry.getAreaTree().getExtensions().elements();
  202. while (rootEnumeration.hasMoreElements())
  203. renderTree.addExtension((ExtensionObj) rootEnumeration.nextElement());
  204. */
  205. renderQueue.removeElementAt(0);
  206. }
  207. }
  208. /**
  209. A RenderQueueEntry consists of the Page to be queued,
  210. plus a list of outstanding ID references that need to be
  211. resolved before the Page can be renderered.<P>
  212. */
  213. class RenderQueueEntry extends Object {
  214. /*
  215. The Page that has outstanding ID references.
  216. */
  217. private Page page;
  218. /*
  219. A list of ID references (names).
  220. */
  221. private Vector unresolvedIdReferences = new Vector();
  222. public RenderQueueEntry(Page page) {
  223. this.page = page;
  224. Enumeration e = idReferences.getInvalidElements();
  225. while (e.hasMoreElements())
  226. unresolvedIdReferences.addElement(e.nextElement());
  227. }
  228. public Page getPage() {
  229. return page;
  230. }
  231. /**
  232. See if the outstanding references are resolved
  233. in the current copy of IDReferences.
  234. */
  235. public boolean isResolved() {
  236. if ((unresolvedIdReferences.size() == 0) || idReferences.isEveryIdValid())
  237. return true;
  238. //
  239. // See if any of the unresolved references are still unresolved.
  240. //
  241. Enumeration e = unresolvedIdReferences.elements();
  242. while (e.hasMoreElements())
  243. if (!idReferences.doesIDExist((String) e.nextElement()))
  244. return false;
  245. unresolvedIdReferences.removeAllElements();
  246. return true;
  247. }
  248. }
  249. public Page getNextPage(Page current, boolean isWithinPageSequence,
  250. boolean isFirstCall) {
  251. Page nextPage = null;
  252. int pageIndex = 0;
  253. if (isFirstCall)
  254. pageIndex = renderQueue.size();
  255. else
  256. pageIndex = renderQueue.indexOf(current);
  257. if ((pageIndex + 1) < renderQueue.size()) {
  258. nextPage = (Page)renderQueue.elementAt(pageIndex + 1);
  259. if (isWithinPageSequence
  260. &&!nextPage.getPageSequence().equals(current.getPageSequence())) {
  261. nextPage = null;
  262. }
  263. }
  264. return nextPage;
  265. }
  266. public Page getPreviousPage(Page current, boolean isWithinPageSequence,
  267. boolean isFirstCall) {
  268. Page previousPage = null;
  269. int pageIndex = 0;
  270. if (isFirstCall)
  271. pageIndex = renderQueue.size();
  272. else
  273. pageIndex = renderQueue.indexOf(current);
  274. // System.out.println("Page index = " + pageIndex);
  275. if ((pageIndex - 1) >= 0) {
  276. previousPage = (Page)renderQueue.elementAt(pageIndex - 1);
  277. PageSequence currentPS = current.getPageSequence();
  278. // System.out.println("Current PS = '" + currentPS + "'");
  279. PageSequence previousPS = previousPage.getPageSequence();
  280. // System.out.println("Previous PS = '" + previousPS + "'");
  281. if (isWithinPageSequence &&!previousPS.equals(currentPS)) {
  282. // System.out.println("Outside page sequence");
  283. previousPage = null;
  284. }
  285. }
  286. return previousPage;
  287. }
  288. }