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.

RenderPagesModel.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.area;
  19. // Java
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.xml.sax.SAXException;
  25. import org.apache.fop.apps.FOPException;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.fonts.FontInfo;
  28. import org.apache.fop.render.Renderer;
  29. /**
  30. * This uses the AreaTreeModel to store the pages
  31. * Each page is either rendered if ready or prepared
  32. * for later rendering.
  33. * Once a page is rendered it is cleared to release the
  34. * contents but the PageViewport is retained. So even
  35. * though the pages are stored the contents are discarded.
  36. */
  37. public class RenderPagesModel extends AreaTreeModel {
  38. /**
  39. * The renderer that will render the pages.
  40. */
  41. protected Renderer renderer;
  42. /**
  43. * Pages that have been prepared but not rendered yet.
  44. */
  45. protected List prepared = new java.util.ArrayList();
  46. private List pendingODI = new java.util.ArrayList();
  47. private List endDocODI = new java.util.ArrayList();
  48. /**
  49. * Create a new render pages model with the given renderer.
  50. * @param userAgent FOUserAgent object for process
  51. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  52. * @param fontInfo FontInfo object
  53. * @param stream OutputStream
  54. * @throws FOPException if the renderer cannot be properly initialized
  55. */
  56. public RenderPagesModel (FOUserAgent userAgent, String outputFormat,
  57. FontInfo fontInfo, OutputStream stream) throws FOPException {
  58. super();
  59. renderer = userAgent.getRendererFactory().createRenderer(
  60. userAgent, outputFormat);
  61. try {
  62. renderer.setupFontInfo(fontInfo);
  63. // check that the "any,normal,400" font exists
  64. if (!fontInfo.isSetupValid()) {
  65. throw new FOPException(
  66. "No default font defined by OutputConverter");
  67. }
  68. renderer.startRenderer(stream);
  69. } catch (IOException e) {
  70. throw new FOPException(e);
  71. }
  72. }
  73. /** {@inheritDoc} */
  74. public void startPageSequence(PageSequence pageSequence) {
  75. super.startPageSequence(pageSequence);
  76. if (renderer.supportsOutOfOrder()) {
  77. renderer.startPageSequence(getCurrentPageSequence());
  78. }
  79. }
  80. /**
  81. * Add a page to the render page model.
  82. * If the page is finished it can be rendered immediately.
  83. * If the page needs resolving then if the renderer supports
  84. * out of order rendering it can prepare the page. Otherwise
  85. * the page is added to a queue.
  86. * @param page the page to add to the model
  87. */
  88. public void addPage(PageViewport page) {
  89. super.addPage(page);
  90. // for links the renderer needs to prepare the page
  91. // it is more appropriate to do this after queued pages but
  92. // it will mean that the renderer has not prepared a page that
  93. // could be referenced
  94. boolean ready = renderer.supportsOutOfOrder() && page.isResolved();
  95. if (ready) {
  96. if (!renderer.supportsOutOfOrder() && page.getPageSequence().isFirstPage(page)) {
  97. renderer.startPageSequence(getCurrentPageSequence());
  98. }
  99. try {
  100. renderer.renderPage(page);
  101. } catch (RuntimeException re) {
  102. String err = "Error while rendering page " + page.getPageNumberString();
  103. log.error(err, re);
  104. throw re;
  105. } catch (Exception e) {
  106. //TODO use error handler to handle this FOP or IO Exception or propagate exception
  107. String err = "Error while rendering page " + page.getPageNumberString();
  108. log.error(err, e);
  109. throw new IllegalStateException("Fatal error occurred. Cannot continue. "
  110. + e.getClass().getName() + ": " + err);
  111. }
  112. page.clear();
  113. } else {
  114. preparePage(page);
  115. }
  116. // check prepared pages
  117. boolean cont = checkPreparedPages(page, false);
  118. if (cont) {
  119. processOffDocumentItems(pendingODI);
  120. pendingODI.clear();
  121. }
  122. }
  123. /**
  124. * Check prepared pages
  125. *
  126. * @param newPageViewport the new page being added
  127. * @param renderUnresolved render pages with unresolved idref's
  128. * (done at end-of-document processing)
  129. * @return true if the current page should be rendered
  130. * false if the renderer doesn't support out of order
  131. * rendering and there are pending pages
  132. */
  133. protected boolean checkPreparedPages(PageViewport newPageViewport, boolean
  134. renderUnresolved) {
  135. for (Iterator iter = prepared.iterator(); iter.hasNext();) {
  136. PageViewport pageViewport = (PageViewport)iter.next();
  137. if (pageViewport.isResolved() || renderUnresolved) {
  138. if (!renderer.supportsOutOfOrder()
  139. && pageViewport.getPageSequence().isFirstPage(pageViewport)) {
  140. renderer.startPageSequence(getCurrentPageSequence());
  141. }
  142. try {
  143. renderer.renderPage(pageViewport);
  144. if (!pageViewport.isResolved()) {
  145. String[] idrefs = pageViewport.getIDRefs();
  146. for (int count = 0; count < idrefs.length; count++) {
  147. log.warn("Page " + pageViewport.getPageNumberString()
  148. + ": Unresolved id reference \"" + idrefs[count]
  149. + "\" found.");
  150. }
  151. }
  152. } catch (Exception e) {
  153. // use error handler to handle this FOP or IO Exception
  154. log.error("Error while rendering page " + pageViewport.getPageIndex(), e);
  155. if (e instanceof RuntimeException) {
  156. throw (RuntimeException)e;
  157. }
  158. }
  159. pageViewport.clear();
  160. iter.remove();
  161. } else {
  162. // if keeping order then stop at first page not resolved
  163. if (!renderer.supportsOutOfOrder()) {
  164. break;
  165. }
  166. }
  167. }
  168. return renderer.supportsOutOfOrder() || prepared.isEmpty();
  169. }
  170. /**
  171. * Prepare a page.
  172. * An unresolved page can be prepared if the renderer supports
  173. * it and the page will be rendered later.
  174. * @param page the page to prepare
  175. */
  176. protected void preparePage(PageViewport page) {
  177. if (renderer.supportsOutOfOrder()) {
  178. renderer.preparePage(page);
  179. }
  180. prepared.add(page);
  181. }
  182. /**
  183. * {@inheritDoc}
  184. */
  185. public void handleOffDocumentItem(OffDocumentItem oDI) {
  186. switch(oDI.getWhenToProcess()) {
  187. case OffDocumentItem.IMMEDIATELY:
  188. renderer.processOffDocumentItem(oDI);
  189. break;
  190. case OffDocumentItem.AFTER_PAGE:
  191. pendingODI.add(oDI);
  192. break;
  193. case OffDocumentItem.END_OF_DOC:
  194. endDocODI.add(oDI);
  195. break;
  196. default:
  197. throw new RuntimeException();
  198. }
  199. }
  200. private void processOffDocumentItems(List list) {
  201. for (int count = 0; count < list.size(); count++) {
  202. OffDocumentItem oDI = (OffDocumentItem)list.get(count);
  203. renderer.processOffDocumentItem(oDI);
  204. }
  205. }
  206. /**
  207. * End the document. Render any end document OffDocumentItems
  208. * {@inheritDoc}
  209. */
  210. public void endDocument() throws SAXException {
  211. // render any pages that had unresolved ids
  212. checkPreparedPages(null, true);
  213. processOffDocumentItems(pendingODI);
  214. pendingODI.clear();
  215. processOffDocumentItems(endDocODI);
  216. try {
  217. renderer.stopRenderer();
  218. } catch (IOException ex) {
  219. throw new SAXException(ex);
  220. }
  221. }
  222. }