You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConditionalPageMasterReference.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.pagination;
  8. import org.apache.fop.fo.*;
  9. import org.apache.fop.fo.properties.*;
  10. import org.apache.fop.apps.FOPException;
  11. import org.xml.sax.Attributes;
  12. /**
  13. * A conditional-page-master-reference formatting object.
  14. * This is a reference to a page master with a set of conditions.
  15. * The conditions must be satisfied for the referenced master to
  16. * be used.
  17. * This element is must be the child of a repeatable-page-master-alternatives
  18. * element.
  19. */
  20. public class ConditionalPageMasterReference extends FObj {
  21. private RepeatablePageMasterAlternatives repeatablePageMasterAlternatives;
  22. private String masterName;
  23. private int pagePosition;
  24. private int oddOrEven;
  25. private int blankOrNotBlank;
  26. public ConditionalPageMasterReference(FONode parent) {
  27. super(parent);
  28. }
  29. public void handleAttrs(Attributes attlist) throws FOPException {
  30. super.handleAttrs(attlist);
  31. if (getProperty("master-reference") != null) {
  32. setMasterName(getProperty("master-reference").getString());
  33. }
  34. validateParent(parent);
  35. setPagePosition(this.properties.get("page-position").getEnum());
  36. setOddOrEven(this.properties.get("odd-or-even").getEnum());
  37. setBlankOrNotBlank(this.properties.get("blank-or-not-blank").getEnum());
  38. }
  39. protected void setMasterName(String masterName) {
  40. this.masterName = masterName;
  41. }
  42. /**
  43. * Returns the "master-name" attribute of this page master reference
  44. */
  45. public String getMasterName() {
  46. return masterName;
  47. }
  48. /**
  49. * Check if the conditions for this reference are met.
  50. * checks the page number and emptyness to determine if this
  51. * matches.
  52. */
  53. protected boolean isValid(int currentPageNumber, boolean thisIsFirstPage,
  54. boolean isEmptyPage) {
  55. // page-position
  56. boolean okOnPagePosition = true; // default is 'any'
  57. switch (getPagePosition()) {
  58. case PagePosition.FIRST:
  59. if (!thisIsFirstPage)
  60. okOnPagePosition = false;
  61. break;
  62. case PagePosition.LAST:
  63. // how the hell do you know at this point?
  64. getLogger().warn("LAST PagePosition NYI");
  65. okOnPagePosition = true;
  66. break;
  67. case PagePosition.REST:
  68. if (thisIsFirstPage)
  69. okOnPagePosition = false;
  70. break;
  71. case PagePosition.ANY:
  72. okOnPagePosition = true;
  73. }
  74. // odd or even
  75. boolean okOnOddOrEven = true; // default is 'any'
  76. int ooe = getOddOrEven();
  77. boolean isOddPage = ((currentPageNumber % 2) == 1) ? true : false;
  78. if ((OddOrEven.ODD == ooe) &&!isOddPage) {
  79. okOnOddOrEven = false;
  80. }
  81. if ((OddOrEven.EVEN == ooe) && isOddPage) {
  82. okOnOddOrEven = false;
  83. }
  84. // experimental check for blank-or-not-blank
  85. boolean okOnBlankOrNotBlank = true; // default is 'any'
  86. int bnb = getBlankOrNotBlank();
  87. if ((BlankOrNotBlank.BLANK == bnb) &&!isEmptyPage) {
  88. okOnBlankOrNotBlank = false;
  89. } else if ((BlankOrNotBlank.NOT_BLANK == bnb) && isEmptyPage) {
  90. okOnBlankOrNotBlank = false;
  91. }
  92. return (okOnOddOrEven && okOnPagePosition && okOnBlankOrNotBlank);
  93. }
  94. protected void setPagePosition(int pagePosition) {
  95. this.pagePosition = pagePosition;
  96. }
  97. protected int getPagePosition() {
  98. return this.pagePosition;
  99. }
  100. protected void setOddOrEven(int oddOrEven) {
  101. this.oddOrEven = oddOrEven;
  102. }
  103. protected int getOddOrEven() {
  104. return this.oddOrEven;
  105. }
  106. protected void setBlankOrNotBlank(int blankOrNotBlank) {
  107. this.blankOrNotBlank = blankOrNotBlank;
  108. }
  109. protected int getBlankOrNotBlank() {
  110. return this.blankOrNotBlank;
  111. }
  112. /**
  113. * Check that the parent is the right type of formatting object
  114. * repeatable-page-master-alternatives.
  115. */
  116. protected void validateParent(FONode parent) throws FOPException {
  117. if (parent.getName().equals("fo:repeatable-page-master-alternatives")) {
  118. this.repeatablePageMasterAlternatives =
  119. (RepeatablePageMasterAlternatives)parent;
  120. if (getMasterName() == null) {
  121. getLogger().warn("single-page-master-reference"
  122. + "does not have a master-name and so is being ignored");
  123. } else {
  124. this.repeatablePageMasterAlternatives.addConditionalPageMasterReference(this);
  125. }
  126. } else {
  127. throw new FOPException("fo:conditional-page-master-reference must be child "
  128. + "of fo:repeatable-page-master-alternatives, not "
  129. + parent.getName());
  130. }
  131. }
  132. }