1 17 18 19 20 package org.apache.fop.area; 21 22 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.List ; 26 import java.util.Iterator ; 27 28 import org.xml.sax.SAXException ; 30 31 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 45 public class RenderPagesModel extends AreaTreeModel { 46 49 protected Renderer renderer; 50 51 54 protected List prepared = new java.util.ArrayList (); 55 private List pendingODI = new java.util.ArrayList (); 56 private List endDocODI = new java.util.ArrayList (); 57 58 66 public RenderPagesModel (FOUserAgent userAgent, String outputFormat, 67 FontInfo fontInfo, OutputStream stream) throws FOPException { 68 69 super(); 70 renderer = userAgent.getRendererFactory().createRenderer( 71 userAgent, outputFormat); 72 73 try { 74 renderer.setupFontInfo(fontInfo); 75 if (!fontInfo.isSetupValid()) { 77 throw new FOPException( 78 "No default font defined by OutputConverter"); 79 } 80 renderer.startRenderer(stream); 81 } catch (IOException e) { 82 throw new FOPException(e); 83 } 84 } 85 86 92 public void startPageSequence(LineArea title) { 93 super.startPageSequence(title); 94 if (renderer.supportsOutOfOrder()) { 95 renderer.startPageSequence(title); 96 } 97 } 98 99 107 public void addPage(PageViewport page) { 108 super.addPage(page); 109 110 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 re) { 122 throw re; 123 } catch (Exception e) { 124 String err = "Error while rendering page " + page.getPageNumberString(); 126 log.error(err, e); 127 throw new IllegalStateException ("Fatal error occurred. Cannot continue. " 128 + e.getClass().getName() + ": " + err); 129 } 130 page.clear(); 131 } else { 132 preparePage(page); 133 } 134 135 136 boolean cont = checkPreparedPages(page, false); 138 139 if (cont) { 140 processOffDocumentItems(pendingODI); 141 pendingODI.clear(); 142 } 143 } 144 145 155 protected boolean checkPreparedPages(PageViewport newpage, boolean 156 renderUnresolved) { 157 for (Iterator 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 [] 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 e) { 174 log.error(e); 176 } 177 p.clear(); 178 iter.remove(); 179 } else { 180 if (!renderer.supportsOutOfOrder()) { 182 break; 183 } 184 } 185 } 186 return renderer.supportsOutOfOrder() || prepared.isEmpty(); 187 } 188 189 195 protected void preparePage(PageViewport page) { 196 if (renderer.supportsOutOfOrder()) { 197 renderer.preparePage(page); 198 } 199 prepared.add(page); 200 } 201 202 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 (); 218 } 219 } 220 221 private void processOffDocumentItems(List 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 232 public void endDocument() throws SAXException { 233 checkPreparedPages(null, true); 235 236 processOffDocumentItems(pendingODI); 237 pendingODI.clear(); 238 processOffDocumentItems(endDocODI); 239 240 try { 241 renderer.stopRenderer(); 242 } catch (IOException ex) { 243 throw new SAXException (ex); 244 } 245 } 246 } 247 248 | Popular Tags |