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.

CachedRenderPagesModel.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. import java.io.BufferedInputStream;
  20. import java.io.BufferedOutputStream;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.ObjectInputStream;
  26. import java.io.ObjectOutputStream;
  27. import java.io.OutputStream;
  28. import java.util.HashMap;
  29. import java.util.Iterator;
  30. import java.util.Map;
  31. import org.xml.sax.SAXException;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.events.ResourceEventProducer;
  36. import org.apache.fop.fonts.FontInfo;
  37. /**
  38. * A simple cached render pages model.
  39. * If the page is prepared for later rendering then this saves
  40. * the page contents to a file and once the page is resolved
  41. * the contents are reloaded.
  42. */
  43. public class CachedRenderPagesModel extends RenderPagesModel {
  44. private Map pageMap = new HashMap();
  45. /** Base directory to save temporary file in, typically points to the user's temp dir. */
  46. protected File baseDir;
  47. /**
  48. * Main Constructor
  49. * @param userAgent FOUserAgent object for process
  50. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  51. * @param fontInfo FontInfo object
  52. * @param stream OutputStream
  53. * @throws FOPException if the renderer cannot be properly initialized
  54. */
  55. public CachedRenderPagesModel (FOUserAgent userAgent, String outputFormat,
  56. FontInfo fontInfo, OutputStream stream) throws FOPException {
  57. super(userAgent, outputFormat, fontInfo, stream);
  58. this.baseDir = new File(System.getProperty("java.io.tmpdir"));
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. protected boolean checkPreparedPages(PageViewport newpage, boolean renderUnresolved) {
  64. for (Iterator iter = prepared.iterator(); iter.hasNext();) {
  65. PageViewport pageViewport = (PageViewport)iter.next();
  66. if (pageViewport.isResolved() || renderUnresolved) {
  67. if (pageViewport != newpage) {
  68. try {
  69. // load page from cache
  70. String name = (String)pageMap.get(pageViewport);
  71. File tempFile = new File(baseDir, name);
  72. log.debug("Loading page from: " + tempFile);
  73. ObjectInputStream in = new ObjectInputStream(
  74. new BufferedInputStream(
  75. new FileInputStream(tempFile)));
  76. try {
  77. pageViewport.loadPage(in);
  78. } finally {
  79. IOUtils.closeQuietly(in);
  80. }
  81. if (!tempFile.delete()) {
  82. ResourceEventProducer eventProducer
  83. = ResourceEventProducer.Provider.get(
  84. renderer.getUserAgent().getEventBroadcaster());
  85. eventProducer.cannotDeleteTempFile(this, tempFile);
  86. }
  87. pageMap.remove(pageViewport);
  88. } catch (Exception e) {
  89. AreaEventProducer eventProducer
  90. = AreaEventProducer.Provider.get(
  91. renderer.getUserAgent().getEventBroadcaster());
  92. eventProducer.pageLoadError(this, pageViewport.getPageNumberString(), e);
  93. }
  94. }
  95. renderPage(pageViewport);
  96. pageViewport.clear();
  97. iter.remove();
  98. } else {
  99. if (!renderer.supportsOutOfOrder()) {
  100. break;
  101. }
  102. }
  103. }
  104. if (newpage != null && newpage.getPage() != null) {
  105. savePage(newpage);
  106. newpage.clear();
  107. }
  108. return renderer.supportsOutOfOrder() || prepared.isEmpty();
  109. }
  110. /**
  111. * Save a page.
  112. * It saves the contents of the page to a file.
  113. *
  114. * @param page the page to prepare
  115. */
  116. protected void savePage(PageViewport page) {
  117. try {
  118. // save page to cache
  119. ObjectOutputStream tempstream;
  120. String fname = "fop-page-" + page.toString() + ".ser";
  121. File tempFile = new File(baseDir, fname);
  122. tempFile.deleteOnExit();
  123. tempstream = new ObjectOutputStream(new BufferedOutputStream(
  124. new FileOutputStream(tempFile)));
  125. try {
  126. page.savePage(tempstream);
  127. } finally {
  128. IOUtils.closeQuietly(tempstream);
  129. }
  130. pageMap.put(page, fname);
  131. if (log.isDebugEnabled()) {
  132. log.debug("Page saved to temporary file: " + tempFile);
  133. }
  134. } catch (IOException ioe) {
  135. AreaEventProducer eventProducer
  136. = AreaEventProducer.Provider.get(
  137. renderer.getUserAgent().getEventBroadcaster());
  138. eventProducer.pageSaveError(this, page.getPageNumberString(), ioe);
  139. }
  140. }
  141. /** {@inheritDoc} */
  142. public void endDocument() throws SAXException {
  143. super.endDocument();
  144. }
  145. }