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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * $Id: RenderPagesModel.java,v 1.3 2003/03/05 15:19:31 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.area;
  52. // FOP
  53. import org.apache.fop.render.Renderer;
  54. // Java
  55. import java.util.List;
  56. import java.util.Iterator;
  57. /**
  58. * This uses the store pages model to store the pages
  59. * each page is either rendered if ready or prepared
  60. * for later rendering.
  61. * Once a page is rendered it is cleared to release the
  62. * contents but the PageViewport is retained. So even
  63. * though the pages are stored the contents are discarded.
  64. */
  65. public class RenderPagesModel extends StorePagesModel {
  66. /**
  67. * The renderer that will render the pages.
  68. */
  69. protected Renderer renderer;
  70. /**
  71. * Pages that have been prepared but not rendered yet.
  72. */
  73. protected List prepared = new java.util.ArrayList();
  74. private List pendingExt = new java.util.ArrayList();
  75. private List endDocExt = new java.util.ArrayList();
  76. /**
  77. * Create a new render pages model with the given renderer.
  78. * @param rend the renderer to render pages to
  79. */
  80. public RenderPagesModel(Renderer rend) {
  81. renderer = rend;
  82. }
  83. /**
  84. * Start a new page sequence.
  85. * This tells the renderer that a new page sequence has
  86. * started with the given title.
  87. * @param title the title of the new page sequence
  88. */
  89. public void startPageSequence(Title title) {
  90. super.startPageSequence(title);
  91. renderer.startPageSequence(title);
  92. }
  93. /**
  94. * Add a page to the render page model.
  95. * If the page is finished it can be rendered immediately.
  96. * If the page needs resolving then if the renderer supports
  97. * out of order rendering it can prepare the page. Otherwise
  98. * the page is added to a queue.
  99. * @param page the page to add to the model
  100. */
  101. public void addPage(PageViewport page) {
  102. super.addPage(page);
  103. // for links the renderer needs to prepare the page
  104. // it is more appropriate to do this after queued pages but
  105. // it will mean that the renderer has not prepared a page that
  106. // could be referenced
  107. boolean done = renderer.supportsOutOfOrder() && page.isResolved();
  108. if (done) {
  109. try {
  110. renderer.renderPage(page);
  111. } catch (Exception e) {
  112. // use error handler to handle this FOP or IO Exception
  113. e.printStackTrace();
  114. }
  115. page.clear();
  116. } else {
  117. preparePage(page);
  118. }
  119. // check prepared pages
  120. boolean cont = checkPreparedPages(page);
  121. if (cont) {
  122. renderExtensions(pendingExt);
  123. pendingExt.clear();
  124. }
  125. }
  126. /**
  127. * Check prepared pages
  128. *
  129. * @param newpage the new page being added
  130. * @return true if the current page should be rendered
  131. * false if the renderer doesn't support out of order
  132. * rendering and there are pending pages
  133. */
  134. protected boolean checkPreparedPages(PageViewport newpage) {
  135. for (Iterator iter = prepared.iterator(); iter.hasNext();) {
  136. PageViewport p = (PageViewport)iter.next();
  137. if (p.isResolved()) {
  138. try {
  139. renderer.renderPage(p);
  140. } catch (Exception e) {
  141. // use error handler to handle this FOP or IO Exception
  142. e.printStackTrace();
  143. }
  144. p.clear();
  145. iter.remove();
  146. } else {
  147. // if keeping order then stop at first page not resolved
  148. if (!renderer.supportsOutOfOrder()) {
  149. break;
  150. }
  151. }
  152. }
  153. return renderer.supportsOutOfOrder() || prepared.isEmpty();
  154. }
  155. /**
  156. * Prepare a page.
  157. * An unresolved page can be prepared if the renderer supports
  158. * it and the page will be rendered later.
  159. * @param page the page to prepare
  160. */
  161. protected void preparePage(PageViewport page) {
  162. if (renderer.supportsOutOfOrder()) {
  163. renderer.preparePage(page);
  164. }
  165. prepared.add(page);
  166. }
  167. /**
  168. * Add an extension to this model.
  169. * If handle immediately then send directly to the renderer.
  170. * The after page ones are handled after the next page is added.
  171. * End of document extensions are added to a list to be
  172. * handled at the end.
  173. * @param ext the extension
  174. * @param when when to render the extension
  175. */
  176. public void addExtension(TreeExt ext, int when) {
  177. switch(when) {
  178. case TreeExt.IMMEDIATELY:
  179. renderer.renderExtension(ext);
  180. break;
  181. case TreeExt.AFTER_PAGE:
  182. pendingExt.add(ext);
  183. break;
  184. case TreeExt.END_OF_DOC:
  185. endDocExt.add(ext);
  186. break;
  187. }
  188. }
  189. private void renderExtensions(List list) {
  190. for (int count = 0; count < list.size(); count++) {
  191. TreeExt ext = (TreeExt)list.get(count);
  192. renderer.renderExtension(ext);
  193. }
  194. }
  195. /**
  196. * End the document. Render any end document extensions.
  197. */
  198. public void endDocument() {
  199. // render any pages that had unresolved ids
  200. checkPreparedPages(null);
  201. renderExtensions(pendingExt);
  202. pendingExt.clear();
  203. renderExtensions(endDocExt);
  204. }
  205. }