KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > CachedRenderPagesModel


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
18 /* $Id: CachedRenderPagesModel.java 426576 2006-07-28 15:44:37Z jeremias $ */
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 JavaDoc;
27
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.BufferedOutputStream JavaDoc;
39 import java.io.BufferedInputStream JavaDoc;
40
41 /**
42  * A simple cached render pages model.
43  * If the page is prepared for later rendering then this saves
44  * the page contents to a file and once the page is resolved
45  * the contents are reloaded.
46  */

47 public class CachedRenderPagesModel extends RenderPagesModel {
48     private Map JavaDoc pageMap = new HashMap JavaDoc();
49
50     /** Base directory to save temporary file in, typically points to the user's temp dir. */
51     protected File JavaDoc baseDir;
52     
53     /**
54      * Main Constructor
55      * @param userAgent FOUserAgent object for process
56      * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
57      * @param fontInfo FontInfo object
58      * @param stream OutputStream
59      * @throws FOPException if the renderer cannot be properly initialized
60      */

61     public CachedRenderPagesModel (FOUserAgent userAgent, String JavaDoc outputFormat,
62             FontInfo fontInfo, OutputStream JavaDoc stream) throws FOPException {
63         super(userAgent, outputFormat, fontInfo, stream);
64         this.baseDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
65     }
66
67     /**
68      * @see org.apache.fop.area.RenderPagesModel#checkPreparedPages(PageViewport, boolean)
69      */

70     protected boolean checkPreparedPages(PageViewport newpage, boolean renderUnresolved) {
71         for (Iterator JavaDoc iter = prepared.iterator(); iter.hasNext();) {
72             PageViewport p = (PageViewport)iter.next();
73             if (p.isResolved() || renderUnresolved) {
74                 if (p != newpage) {
75                     try {
76                         // load page from cache
77
String JavaDoc name = (String JavaDoc)pageMap.get(p);
78                         File JavaDoc tempFile = new File JavaDoc(baseDir, name);
79                         log.debug("Loading page from: " + tempFile);
80                         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(
81                                              new BufferedInputStream JavaDoc(
82                                                new FileInputStream JavaDoc(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 JavaDoc e) {
93                         log.error(e);
94                     }
95                 }
96
97                 try {
98                     renderer.renderPage(p);
99                     if (!p.isResolved()) {
100                         String JavaDoc[] 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 JavaDoc e) {
108                     // use error handler to handle this FOP or IO Exception
109
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     /**
127      * Save a page.
128      * It saves the contents of the page to a file.
129      *
130      * @param page the page to prepare
131      */

132     protected void savePage(PageViewport page) {
133         try {
134             // save page to cache
135
ObjectOutputStream JavaDoc tempstream;
136             String JavaDoc fname = "fop-page-" + page.toString() + ".ser";
137             File JavaDoc tempFile = new File JavaDoc(baseDir, fname);
138             tempFile.deleteOnExit();
139             tempstream = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(
140                                                 new FileOutputStream JavaDoc(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 JavaDoc e) {
151             log.error(e);
152         }
153     }
154
155     /** @see org.apache.fop.area.RenderPagesModel#endDocument() */
156     public void endDocument() throws SAXException JavaDoc {
157         super.endDocument();
158     }
159 }
160
161
Popular Tags