1 17 18 19 20 package org.apache.fop.area; 21 22 import java.awt.Rectangle ; 23 import java.awt.geom.Rectangle2D ; 24 import java.io.ObjectOutputStream ; 25 import java.io.ObjectInputStream ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.apache.fop.fo.Constants; 37 import org.apache.fop.fo.extensions.ExtensionAttachment; 38 import org.apache.fop.fo.pagination.SimplePageMaster; 39 40 48 public class PageViewport extends AreaTreeObject implements Resolvable, Cloneable { 49 50 private Page page; 51 private Rectangle2D viewArea; 52 private String simplePageMasterName; 53 54 58 private String pageKey; 59 60 private int pageNumber = -1; 61 private String pageNumberString = null; 62 private int pageIndex = -1; private boolean blank; 64 65 private transient PageSequence pageSequence; 66 67 70 private Map unresolvedIDRefs = new java.util.HashMap (); 74 75 private Map pendingResolved = null; 76 77 private Map markerFirstStart = null; 80 private Map markerLastStart = null; 81 private Map markerFirstAny = null; 82 private Map markerLastEnd = null; 83 private Map markerLastAny = null; 84 85 private List extensionAttachments = null; 88 89 92 protected static Log log = LogFactory.getLog(PageViewport.class); 93 94 101 public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr, boolean blank) { 102 this.simplePageMasterName = spm.getMasterName(); 103 this.extensionAttachments = spm.getExtensionAttachments(); 104 this.blank = blank; 105 int pageWidth = spm.getPageWidth().getValue(); 106 int pageHeight = spm.getPageHeight().getValue(); 107 this.pageNumber = pageNumber; 108 this.pageNumberString = pageStr; 109 this.viewArea = new Rectangle (0, 0, pageWidth, pageHeight); 110 this.page = new Page(spm); 111 createSpan(false); 112 } 113 114 118 public PageViewport(PageViewport original) { 119 if (original.extensionAttachments != null) { 120 this.extensionAttachments = new java.util.ArrayList (original.extensionAttachments); 121 } 122 this.pageNumber = original.pageNumber; 123 this.pageNumberString = original.pageNumberString; 124 this.page = (Page)original.page.clone(); 125 this.viewArea = (Rectangle2D )original.viewArea.clone(); 126 this.simplePageMasterName = original.simplePageMasterName; 127 this.blank = original.blank; 128 } 129 130 138 public PageViewport(Rectangle2D viewArea, int pageNumber, String pageStr, 139 String simplePageMasterName, boolean blank) { 140 this.viewArea = viewArea; 141 this.pageNumber = pageNumber; 142 this.pageNumberString = pageStr; 143 this.simplePageMasterName = simplePageMasterName; 144 this.blank = blank; 145 } 146 147 151 public void setPageSequence(PageSequence seq) { 152 this.pageSequence = seq; 153 } 154 155 156 public PageSequence getPageSequence() { 157 return this.pageSequence; 158 } 159 160 164 public Rectangle2D getViewArea() { 165 return viewArea; 166 } 167 168 172 public Page getPage() { 173 return page; 174 } 175 176 180 public void setPage(Page page) { 181 this.page = page; 182 } 183 184 188 public int getPageNumber() { 189 return pageNumber; 190 } 191 192 196 public String getPageNumberString() { 197 return pageNumberString; 198 } 199 200 205 public void setPageIndex(int index) { 206 this.pageIndex = index; 207 } 208 209 213 public int getPageIndex() { 214 return this.pageIndex; 215 } 216 217 221 public void setKey(String key) { 222 this.pageKey = key; 223 } 224 225 232 public String getKey() { 233 if (this.pageKey == null) { 234 throw new IllegalStateException ("No page key set on the PageViewport: " + toString()); 235 } 236 return this.pageKey; 237 } 238 239 249 public void addUnresolvedIDRef(String idref, Resolvable res) { 250 if (unresolvedIDRefs == null) { 251 unresolvedIDRefs = new HashMap (); 252 } 253 List list = (List )unresolvedIDRefs.get(idref); 254 if (list == null) { 255 list = new ArrayList (); 256 unresolvedIDRefs.put(idref, list); 257 } 258 list.add(res); 259 } 260 261 265 public boolean isResolved() { 266 return unresolvedIDRefs == null 267 || unresolvedIDRefs.size() == 0; 268 } 269 270 274 public String [] getIDRefs() { 275 return (unresolvedIDRefs == null) ? null 276 : (String []) unresolvedIDRefs.keySet().toArray(new String [] {}); 277 } 278 279 282 public void resolveIDRef(String id, List pages) { 283 if (page == null) { 284 if (pendingResolved == null) { 285 pendingResolved = new HashMap (); 286 } 287 pendingResolved.put(id, pages); 288 } else { 289 if (unresolvedIDRefs != null) { 290 List todo = (List )unresolvedIDRefs.get(id); 291 if (todo != null) { 292 for (int count = 0; count < todo.size(); count++) { 293 Resolvable res = (Resolvable)todo.get(count); 294 res.resolveIDRef(id, pages); 295 } 296 } 297 } 298 } 299 if (unresolvedIDRefs != null && pages != null) { 300 unresolvedIDRefs.remove(id); 301 if (unresolvedIDRefs.isEmpty()) { 302 unresolvedIDRefs = null; 303 } 304 } 305 } 306 307 327 public void addMarkers(Map marks, boolean starting, 328 boolean isfirst, boolean islast) { 329 330 if (marks == null) { 331 return; 332 } 333 if (log.isDebugEnabled()) { 334 log.debug("--" + marks.keySet() + ": " 335 + (starting ? "starting" : "ending") 336 + (isfirst ? ", first" : "") 337 + (islast ? ", last" : "")); 338 } 339 340 if (starting) { 342 if (isfirst) { 343 if (markerFirstStart == null) { 344 markerFirstStart = new HashMap (); 345 } 346 if (markerFirstAny == null) { 347 markerFirstAny = new HashMap (); 348 } 349 for (Iterator iter = marks.keySet().iterator(); iter.hasNext();) { 351 Object key = iter.next(); 352 if (!markerFirstStart.containsKey(key)) { 353 markerFirstStart.put(key, marks.get(key)); 354 if (log.isTraceEnabled()) { 355 log.trace("page " + pageNumberString + ": " 356 + "Adding marker " + key + " to FirstStart"); 357 } 358 } 359 if (!markerFirstAny.containsKey(key)) { 360 markerFirstAny.put(key, marks.get(key)); 361 if (log.isTraceEnabled()) { 362 log.trace("page " + pageNumberString + ": " 363 + "Adding marker " + key + " to FirstAny"); 364 } 365 } 366 } 367 if (markerLastStart == null) { 368 markerLastStart = new HashMap (); 369 } 370 markerLastStart.putAll(marks); 372 if (log.isTraceEnabled()) { 373 log.trace("page " + pageNumberString + ": " 374 + "Adding all markers to LastStart"); 375 } 376 } else { 377 if (markerFirstAny == null) { 378 markerFirstAny = new HashMap (); 379 } 380 for (Iterator iter = marks.keySet().iterator(); iter.hasNext();) { 382 Object key = iter.next(); 383 if (!markerFirstAny.containsKey(key)) { 384 markerFirstAny.put(key, marks.get(key)); 385 if (log.isTraceEnabled()) { 386 log.trace("page " + pageNumberString + ": " 387 + "Adding marker " + key + " to FirstAny"); 388 } 389 } 390 } 391 } 392 } else { 393 if (islast) { 395 if (markerLastEnd == null) { 396 markerLastEnd = new HashMap (); 397 } 398 markerLastEnd.putAll(marks); 400 if (log.isTraceEnabled()) { 401 log.trace("page " + pageNumberString + ": " 402 + "Adding all markers to LastEnd"); 403 } 404 } 405 if (markerLastAny == null) { 406 markerLastAny = new HashMap (); 407 } 408 markerLastAny.putAll(marks); 410 if (log.isTraceEnabled()) { 411 log.trace("page " + pageNumberString + ": " 412 + "Adding all markers to LastAny"); 413 } 414 } 415 } 416 417 426 public Object getMarker(String name, int pos) { 427 Object mark = null; 428 String posName = null; 429 switch (pos) { 430 case Constants.EN_FSWP: 431 if (markerFirstStart != null) { 432 mark = markerFirstStart.get(name); 433 posName = "FSWP"; 434 } 435 if (mark == null && markerFirstAny != null) { 436 mark = markerFirstAny.get(name); 437 posName = "FirstAny after " + posName; 438 } 439 break; 440 case Constants.EN_FIC: 441 if (markerFirstAny != null) { 442 mark = markerFirstAny.get(name); 443 posName = "FIC"; 444 } 445 break; 446 case Constants.EN_LSWP: 447 if (markerLastStart != null) { 448 mark = markerLastStart.get(name); 449 posName = "LSWP"; 450 } 451 if (mark == null && markerLastAny != null) { 452 mark = markerLastAny.get(name); 453 posName = "LastAny after " + posName; 454 } 455 break; 456 case Constants.EN_LEWP: 457 if (markerLastEnd != null) { 458 mark = markerLastEnd.get(name); 459 posName = "LEWP"; 460 } 461 if (mark == null && markerLastAny != null) { 462 mark = markerLastAny.get(name); 463 posName = "LastAny after " + posName; 464 } 465 break; 466 default: 467 throw new RuntimeException (); 468 } 469 if (log.isTraceEnabled()) { 470 log.trace("page " + pageNumberString + ": " + "Retrieving marker " + name 471 + " at position " + posName); 472 } 473 return mark; 474 } 475 476 477 public void dumpMarkers() { 478 if (log.isTraceEnabled()) { 479 log.trace("FirstAny: " + this.markerFirstAny); 480 log.trace("FirstStart: " + this.markerFirstStart); 481 log.trace("LastAny: " + this.markerLastAny); 482 log.trace("LastEnd: " + this.markerLastEnd); 483 log.trace("LastStart: " + this.markerLastStart); 484 } 485 } 486 487 494 public void savePage(ObjectOutputStream out) throws Exception { 495 page.setUnresolvedReferences(unresolvedIDRefs); 497 out.writeObject(page); 498 page = null; 499 } 500 501 509 public void loadPage(ObjectInputStream in) throws Exception { 510 page = (Page) in.readObject(); 511 unresolvedIDRefs = page.getUnresolvedReferences(); 512 if (unresolvedIDRefs != null && pendingResolved != null) { 513 for (Iterator iter = pendingResolved.keySet().iterator(); 514 iter.hasNext();) { 515 String id = (String ) iter.next(); 516 resolveIDRef(id, (List )pendingResolved.get(id)); 517 } 518 pendingResolved = null; 519 } 520 } 521 522 527 public Object clone() { 528 return new PageViewport(this); 529 } 530 531 536 public void clear() { 537 page = null; 538 } 539 540 543 public String toString() { 544 StringBuffer sb = new StringBuffer (64); 545 sb.append("PageViewport: page="); 546 sb.append(getPageNumberString()); 547 return sb.toString(); 548 } 549 550 551 public String getSimplePageMasterName() { 552 return this.simplePageMasterName; 553 } 554 555 559 public void addExtensionAttachment(ExtensionAttachment attachment) { 560 if (this.extensionAttachments == null) { 561 this.extensionAttachments = new java.util.ArrayList (); 562 } 563 extensionAttachments.add(attachment); 564 } 565 566 567 public List getExtensionAttachments() { 568 if (this.extensionAttachments == null) { 569 return Collections.EMPTY_LIST; 570 } else { 571 return this.extensionAttachments; 572 } 573 } 574 575 576 public boolean isBlank() { 577 return this.blank; 578 } 579 580 584 public BodyRegion getBodyRegion() { 585 return (BodyRegion) getPage().getRegionViewport( 586 Constants.FO_REGION_BODY).getRegionReference(); 587 } 588 589 596 public Span createSpan(boolean spanAll) { 597 return getBodyRegion().getMainReference().createSpan(spanAll); 598 } 599 600 606 public Span getCurrentSpan() { 607 return getBodyRegion().getMainReference().getCurrentSpan(); 608 } 609 610 616 public NormalFlow getCurrentFlow() { 617 return getCurrentSpan().getCurrentFlow(); 618 } 619 620 626 public NormalFlow moveToNextFlow() { 627 return getCurrentSpan().moveToNextFlow(); 628 } 629 630 639 public RegionReference getRegionReference(int id) { 640 return getPage().getRegionViewport(id).getRegionReference(); 641 } 642 } 643 | Popular Tags |