1 17 18 19 20 package org.apache.fop.area; 21 22 import org.apache.commons.io.IOUtils; 23 import org.apache.fop.apps.FOPException; 24 import org.apache.fop.apps.FOUserAgent; 25 import org.apache.fop.fonts.FontInfo; 26 import org.xml.sax.SAXException ; 27 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.FileInputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.io.ObjectInputStream ; 37 import java.io.OutputStream ; 38 import java.io.BufferedOutputStream ; 39 import java.io.BufferedInputStream ; 40 41 47 public class CachedRenderPagesModel extends RenderPagesModel { 48 private Map pageMap = new HashMap (); 49 50 51 protected File baseDir; 52 53 61 public CachedRenderPagesModel (FOUserAgent userAgent, String outputFormat, 62 FontInfo fontInfo, OutputStream stream) throws FOPException { 63 super(userAgent, outputFormat, fontInfo, stream); 64 this.baseDir = new File (System.getProperty("java.io.tmpdir")); 65 } 66 67 70 protected boolean checkPreparedPages(PageViewport newpage, boolean renderUnresolved) { 71 for (Iterator iter = prepared.iterator(); iter.hasNext();) { 72 PageViewport p = (PageViewport)iter.next(); 73 if (p.isResolved() || renderUnresolved) { 74 if (p != newpage) { 75 try { 76 String name = (String )pageMap.get(p); 78 File tempFile = new File (baseDir, name); 79 log.debug("Loading page from: " + tempFile); 80 ObjectInputStream in = new ObjectInputStream ( 81 new BufferedInputStream ( 82 new FileInputStream (tempFile))); 83 try { 84 p.loadPage(in); 85 } finally { 86 IOUtils.closeQuietly(in); 87 } 88 if (!tempFile.delete()) { 89 log.warn("Temporary file could not be deleted: " + tempFile); 90 } 91 pageMap.remove(p); 92 } catch (Exception e) { 93 log.error(e); 94 } 95 } 96 97 try { 98 renderer.renderPage(p); 99 if (!p.isResolved()) { 100 String [] idrefs = p.getIDRefs(); 101 for (int count = 0; count < idrefs.length; count++) { 102 log.warn("Page " + p.getPageNumberString() 103 + ": Unresolved id reference \"" + idrefs[count] 104 + "\" found."); 105 } 106 } 107 } catch (Exception e) { 108 log.error(e); 110 } 111 p.clear(); 112 iter.remove(); 113 } else { 114 if (!renderer.supportsOutOfOrder()) { 115 break; 116 } 117 } 118 } 119 if (newpage != null && newpage.getPage() != null) { 120 savePage(newpage); 121 newpage.clear(); 122 } 123 return renderer.supportsOutOfOrder() || prepared.isEmpty(); 124 } 125 126 132 protected void savePage(PageViewport page) { 133 try { 134 ObjectOutputStream tempstream; 136 String fname = "fop-page-" + page.toString() + ".ser"; 137 File tempFile = new File (baseDir, fname); 138 tempFile.deleteOnExit(); 139 tempstream = new ObjectOutputStream (new BufferedOutputStream ( 140 new FileOutputStream (tempFile))); 141 try { 142 page.savePage(tempstream); 143 } finally { 144 IOUtils.closeQuietly(tempstream); 145 } 146 pageMap.put(page, fname); 147 if (log.isDebugEnabled()) { 148 log.debug("Page saved to temporary file: " + tempFile); 149 } 150 } catch (Exception e) { 151 log.error(e); 152 } 153 } 154 155 156 public void endDocument() throws SAXException { 157 super.endDocument(); 158 } 159 } 160 161 | Popular Tags |