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.

RepeatablePageMasterAlternatives.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /* $Id$ */
  18. package org.apache.fop.fo.pagination;
  19. // Java
  20. import java.util.List;
  21. import org.xml.sax.Locator;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.fo.FONode;
  24. import org.apache.fop.fo.FObj;
  25. import org.apache.fop.fo.PropertyList;
  26. import org.apache.fop.fo.ValidationException;
  27. import org.apache.fop.fo.properties.Property;
  28. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_repeatable-page-master-alternatives">
  30. * <code>fo:repeatable-page-master-alternatives</code></a> object.
  31. * This contains a list of conditional-page-master-reference
  32. * and the page master is found from the reference that
  33. * matches the page number and emptyness.
  34. */
  35. public class RepeatablePageMasterAlternatives extends FObj
  36. implements SubSequenceSpecifier {
  37. // The value of properties relevant for fo:repeatable-page-master-alternatives.
  38. private Property maximumRepeats;
  39. // End of property values
  40. private static final int INFINITE = -1;
  41. private int numberConsumed = 0;
  42. private List<ConditionalPageMasterReference> conditionalPageMasterRefs;
  43. private boolean hasPagePositionLast = false;
  44. private boolean hasPagePositionOnly = false;
  45. /**
  46. * Base constructor
  47. *
  48. * @param parent {@link FONode} that is the parent of this object
  49. */
  50. public RepeatablePageMasterAlternatives(FONode parent) {
  51. super(parent);
  52. }
  53. /** {@inheritDoc} */
  54. public void bind(PropertyList pList) throws FOPException {
  55. maximumRepeats = pList.get(PR_MAXIMUM_REPEATS);
  56. }
  57. /** {@inheritDoc} */
  58. protected void startOfNode() throws FOPException {
  59. conditionalPageMasterRefs = new java.util.ArrayList<ConditionalPageMasterReference>();
  60. assert parent.getName().equals("fo:page-sequence-master"); //Validation by the parent
  61. PageSequenceMaster pageSequenceMaster = (PageSequenceMaster)parent;
  62. pageSequenceMaster.addSubsequenceSpecifier(this);
  63. }
  64. /** {@inheritDoc} */
  65. protected void endOfNode() throws FOPException {
  66. if (firstChild == null) {
  67. missingChildElementError("(conditional-page-master-reference+)");
  68. }
  69. }
  70. /**
  71. * {@inheritDoc}
  72. * <br>XSL/FOP: (conditional-page-master-reference+)
  73. */
  74. protected void validateChildNode(Locator loc, String nsURI, String localName)
  75. throws ValidationException {
  76. if (FO_URI.equals(nsURI)) {
  77. if (!localName.equals("conditional-page-master-reference")) {
  78. invalidChildError(loc, nsURI, localName);
  79. }
  80. }
  81. }
  82. /**
  83. * Get the value of the <code>maximum-repeats</code> property?
  84. * @return the "maximum-repeats" property
  85. */
  86. public int getMaximumRepeats() {
  87. if (maximumRepeats.getEnum() == EN_NO_LIMIT) {
  88. return INFINITE;
  89. } else {
  90. int mr = maximumRepeats.getNumeric().getValue();
  91. if (mr < 0) {
  92. log.debug("negative maximum-repeats: "
  93. + this.maximumRepeats);
  94. mr = 0;
  95. }
  96. return mr;
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. public String getNextPageMasterName(boolean isOddPage,
  101. boolean isFirstPage,
  102. boolean isLastPage,
  103. boolean isBlankPage) {
  104. if (getMaximumRepeats() != INFINITE) {
  105. if (numberConsumed < getMaximumRepeats()) {
  106. numberConsumed++;
  107. } else {
  108. return null;
  109. }
  110. } else {
  111. numberConsumed++;
  112. }
  113. for (ConditionalPageMasterReference cpmr : conditionalPageMasterRefs) {
  114. if (cpmr.isValid(isOddPage, isFirstPage, isLastPage, isBlankPage)) {
  115. return cpmr.getMasterReference();
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * Adds a new conditional page master reference.
  122. * @param cpmr the new conditional reference
  123. */
  124. public void addConditionalPageMasterReference(ConditionalPageMasterReference cpmr) {
  125. this.conditionalPageMasterRefs.add(cpmr);
  126. if (cpmr.getPagePosition() == EN_LAST) {
  127. this.hasPagePositionLast = true;
  128. }
  129. if (cpmr.getPagePosition() == EN_ONLY) {
  130. this.hasPagePositionOnly = true;
  131. }
  132. }
  133. /** {@inheritDoc} */
  134. public void reset() {
  135. this.numberConsumed = 0;
  136. }
  137. /** {@inheritDoc} */
  138. public boolean goToPrevious() {
  139. if (numberConsumed == 0) {
  140. return false;
  141. } else {
  142. numberConsumed--;
  143. return true;
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. public boolean hasPagePositionLast() {
  148. return this.hasPagePositionLast;
  149. }
  150. /** {@inheritDoc} */
  151. public boolean hasPagePositionOnly() {
  152. return this.hasPagePositionOnly;
  153. }
  154. /** {@inheritDoc} */
  155. public String getLocalName() {
  156. return "repeatable-page-master-alternatives";
  157. }
  158. /**
  159. * {@inheritDoc}
  160. * @return {@link org.apache.fop.fo.Constants#FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES}
  161. */
  162. public int getNameId() {
  163. return FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES;
  164. }
  165. }