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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 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();
  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 (int i = 0; i < conditionalPageMasterRefs.size(); i++) {
  114. ConditionalPageMasterReference cpmr
  115. = (ConditionalPageMasterReference)conditionalPageMasterRefs.get(i);
  116. if (cpmr.isValid(isOddPage, isFirstPage, isLastPage, isBlankPage)) {
  117. return cpmr.getMasterReference();
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * Adds a new conditional page master reference.
  124. * @param cpmr the new conditional reference
  125. */
  126. public void addConditionalPageMasterReference(ConditionalPageMasterReference cpmr) {
  127. this.conditionalPageMasterRefs.add(cpmr);
  128. if (cpmr.getPagePosition() == EN_LAST) {
  129. this.hasPagePositionLast = true;
  130. }
  131. if (cpmr.getPagePosition() == EN_ONLY) {
  132. this.hasPagePositionOnly = true;
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. public void reset() {
  137. this.numberConsumed = 0;
  138. }
  139. /** {@inheritDoc} */
  140. public boolean goToPrevious() {
  141. if (numberConsumed == 0) {
  142. return false;
  143. } else {
  144. numberConsumed--;
  145. return true;
  146. }
  147. }
  148. /** {@inheritDoc} */
  149. public boolean hasPagePositionLast() {
  150. return this.hasPagePositionLast;
  151. }
  152. /** {@inheritDoc} */
  153. public boolean hasPagePositionOnly() {
  154. return this.hasPagePositionOnly;
  155. }
  156. /** {@inheritDoc} */
  157. public String getLocalName() {
  158. return "repeatable-page-master-alternatives";
  159. }
  160. /**
  161. * {@inheritDoc}
  162. * @return {@link org.apache.fop.fo.Constants#FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES}
  163. */
  164. public int getNameId() {
  165. return FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES;
  166. }
  167. }