KickJava   Java API By Example, From Geeks To Geeks.

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


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: RenderPagesModel.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.area;
21
22 // Java
23
import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 // XML
29
import org.xml.sax.SAXException JavaDoc;
30
31 // FOP
32
import org.apache.fop.apps.FOPException;
33 import org.apache.fop.apps.FOUserAgent;
34 import org.apache.fop.fonts.FontInfo;
35 import org.apache.fop.render.Renderer;
36
37 /**
38  * This uses the AreaTreeModel to store the pages
39  * Each page is either rendered if ready or prepared
40  * for later rendering.
41  * Once a page is rendered it is cleared to release the
42  * contents but the PageViewport is retained. So even
43  * though the pages are stored the contents are discarded.
44  */

45 public class RenderPagesModel extends AreaTreeModel {
46     /**
47      * The renderer that will render the pages.
48      */

49     protected Renderer renderer;
50     
51     /**
52      * Pages that have been prepared but not rendered yet.
53      */

54     protected List JavaDoc prepared = new java.util.ArrayList JavaDoc();
55     private List JavaDoc pendingODI = new java.util.ArrayList JavaDoc();
56     private List JavaDoc endDocODI = new java.util.ArrayList JavaDoc();
57
58     /**
59      * Create a new render pages model with the given renderer.
60      * @param userAgent FOUserAgent object for process
61      * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
62      * @param fontInfo FontInfo object
63      * @param stream OutputStream
64      * @throws FOPException if the renderer cannot be properly initialized
65      */

66     public RenderPagesModel (FOUserAgent userAgent, String JavaDoc outputFormat,
67         FontInfo fontInfo, OutputStream JavaDoc stream) throws FOPException {
68
69         super();
70         renderer = userAgent.getRendererFactory().createRenderer(
71                 userAgent, outputFormat);
72
73         try {
74             renderer.setupFontInfo(fontInfo);
75             // check that the "any,normal,400" font exists
76
if (!fontInfo.isSetupValid()) {
77                 throw new FOPException(
78                     "No default font defined by OutputConverter");
79             }
80             renderer.startRenderer(stream);
81         } catch (IOException JavaDoc e) {
82             throw new FOPException(e);
83         }
84     }
85
86     /**
87      * Start a new page sequence.
88      * This tells the renderer that a new page sequence has
89      * started with the given title.
90      * @param title the title of the new page sequence
91      */

92     public void startPageSequence(LineArea title) {
93         super.startPageSequence(title);
94         if (renderer.supportsOutOfOrder()) {
95             renderer.startPageSequence(title);
96         }
97     }
98
99     /**
100      * Add a page to the render page model.
101      * If the page is finished it can be rendered immediately.
102      * If the page needs resolving then if the renderer supports
103      * out of order rendering it can prepare the page. Otherwise
104      * the page is added to a queue.
105      * @param page the page to add to the model
106      */

107     public void addPage(PageViewport page) {
108         super.addPage(page);
109
110         // for links the renderer needs to prepare the page
111
// it is more appropriate to do this after queued pages but
112
// it will mean that the renderer has not prepared a page that
113
// could be referenced
114
boolean ready = renderer.supportsOutOfOrder() && page.isResolved();
115         if (ready) {
116             if (!renderer.supportsOutOfOrder() && page.getPageSequence().isFirstPage(page)) {
117                 renderer.startPageSequence(this.currentPageSequence.getTitle());
118             }
119             try {
120                 renderer.renderPage(page);
121             } catch (RuntimeException JavaDoc re) {
122                 throw re;
123             } catch (Exception JavaDoc e) {
124                 //TODO use error handler to handle this FOP or IO Exception or propagate exception
125
String JavaDoc err = "Error while rendering page " + page.getPageNumberString();
126                 log.error(err, e);
127                 throw new IllegalStateException JavaDoc("Fatal error occurred. Cannot continue. "
128                         + e.getClass().getName() + ": " + err);
129             }
130             page.clear();
131         } else {
132             preparePage(page);
133         }
134
135
136         // check prepared pages
137
boolean cont = checkPreparedPages(page, false);
138
139         if (cont) {
140             processOffDocumentItems(pendingODI);
141             pendingODI.clear();
142         }
143     }
144
145     /**
146      * Check prepared pages
147      *
148      * @param newpage the new page being added
149      * @param renderUnresolved render pages with unresolved idref's
150      * (done at end-of-document processing)
151      * @return true if the current page should be rendered
152      * false if the renderer doesn't support out of order
153      * rendering and there are pending pages
154      */

155     protected boolean checkPreparedPages(PageViewport newpage, boolean
156         renderUnresolved) {
157         for (Iterator JavaDoc iter = prepared.iterator(); iter.hasNext();) {
158             PageViewport p = (PageViewport)iter.next();
159             if (p.isResolved() || renderUnresolved) {
160                 if (!renderer.supportsOutOfOrder() && p.getPageSequence().isFirstPage(p)) {
161                     renderer.startPageSequence(this.currentPageSequence.getTitle());
162                 }
163                 try {
164                     renderer.renderPage(p);
165                     if (!p.isResolved()) {
166                         String JavaDoc[] idrefs = p.getIDRefs();
167                         for (int count = 0; count < idrefs.length; count++) {
168                             log.warn("Page " + p.getPageNumberString()
169                                 + ": Unresolved id reference \"" + idrefs[count]
170                                 + "\" found.");
171                         }
172                     }
173                 } catch (Exception JavaDoc e) {
174                     // use error handler to handle this FOP or IO Exception
175
log.error(e);
176                 }
177                 p.clear();
178                 iter.remove();
179             } else {
180                 // if keeping order then stop at first page not resolved
181
if (!renderer.supportsOutOfOrder()) {
182                     break;
183                 }
184             }
185         }
186         return renderer.supportsOutOfOrder() || prepared.isEmpty();
187     }
188
189     /**
190      * Prepare a page.
191      * An unresolved page can be prepared if the renderer supports
192      * it and the page will be rendered later.
193      * @param page the page to prepare
194      */

195     protected void preparePage(PageViewport page) {
196         if (renderer.supportsOutOfOrder()) {
197             renderer.preparePage(page);
198         }
199         prepared.add(page);
200     }
201
202     /**
203      * @see org.apache.fop.area.AreaTreeModel#handleOffDocumentItem(OffDocumentItem)
204      */

205     public void handleOffDocumentItem(OffDocumentItem oDI) {
206         switch(oDI.getWhenToProcess()) {
207             case OffDocumentItem.IMMEDIATELY:
208                 renderer.processOffDocumentItem(oDI);
209                 break;
210             case OffDocumentItem.AFTER_PAGE:
211                 pendingODI.add(oDI);
212                 break;
213             case OffDocumentItem.END_OF_DOC:
214                 endDocODI.add(oDI);
215                 break;
216             default:
217                 throw new RuntimeException JavaDoc();
218         }
219     }
220
221     private void processOffDocumentItems(List JavaDoc list) {
222         for (int count = 0; count < list.size(); count++) {
223             OffDocumentItem oDI = (OffDocumentItem)list.get(count);
224             renderer.processOffDocumentItem(oDI);
225         }
226     }
227
228     /**
229      * End the document. Render any end document OffDocumentItems
230      * @see org.apache.fop.area.AreaTreeModel#endDocument()
231      */

232     public void endDocument() throws SAXException JavaDoc {
233         // render any pages that had unresolved ids
234
checkPreparedPages(null, true);
235
236         processOffDocumentItems(pendingODI);
237         pendingODI.clear();
238         processOffDocumentItems(endDocODI);
239
240         try {
241             renderer.stopRenderer();
242         } catch (IOException JavaDoc ex) {
243             throw new SAXException JavaDoc(ex);
244         }
245     }
246 }
247
248
Popular Tags