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.

RepeatablePageMasterReference.java 5.9KB

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. // XML
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.fo.FONode;
  23. import org.apache.fop.fo.FObj;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.ValidationException;
  26. import org.apache.fop.fo.properties.Property;
  27. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  28. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_repeatable-page-master-reference">
  30. * <code>fo:repeatable-page-master-reference</code></a> object.
  31. * This handles a reference with a specified number of repeating
  32. * instances of the referenced page master (may have no limit).
  33. */
  34. public class RepeatablePageMasterReference extends FObj
  35. implements SubSequenceSpecifier {
  36. // The value of properties relevant for fo:repeatable-page-master-reference.
  37. private String masterReference;
  38. // The simple page master referenced
  39. private SimplePageMaster master;
  40. private Property maximumRepeats;
  41. // End of property values
  42. private static final int INFINITE = -1;
  43. private int numberConsumed;
  44. /**
  45. * Base constructor
  46. *
  47. * @param parent {@link FONode} that is the parent of this object
  48. */
  49. public RepeatablePageMasterReference(FONode parent) {
  50. super(parent);
  51. }
  52. /** {@inheritDoc} */
  53. public void bind(PropertyList pList) throws FOPException {
  54. masterReference = pList.get(PR_MASTER_REFERENCE).getString();
  55. maximumRepeats = pList.get(PR_MAXIMUM_REPEATS);
  56. if (masterReference == null || masterReference.equals("")) {
  57. missingPropertyError("master-reference");
  58. }
  59. }
  60. /** {@inheritDoc} */
  61. public void startOfNode() throws FOPException {
  62. PageSequenceMaster pageSequenceMaster = (PageSequenceMaster) parent;
  63. if (masterReference == null) {
  64. missingPropertyError("master-reference");
  65. } else {
  66. pageSequenceMaster.addSubsequenceSpecifier(this);
  67. }
  68. }
  69. /**
  70. * {@inheritDoc}
  71. * <br>XSL Content Model: empty
  72. */
  73. protected void validateChildNode(Locator loc, String nsURI, String localName)
  74. throws ValidationException {
  75. invalidChildError(loc, nsURI, localName);
  76. }
  77. /** {@inheritDoc} */
  78. public SimplePageMaster getNextPageMaster(boolean isOddPage,
  79. boolean isFirstPage,
  80. boolean isLastPage,
  81. boolean isEmptyPage) {
  82. if (getMaximumRepeats() != INFINITE && numberConsumed >= getMaximumRepeats()) {
  83. return null;
  84. }
  85. numberConsumed++;
  86. return master;
  87. }
  88. public SimplePageMaster getLastPageMaster(boolean isOddPage, boolean isFirstPage, boolean isEmptyPage,
  89. BlockLevelEventProducer blockLevelEventProducer) {
  90. return getNextPageMaster(isOddPage, isFirstPage, true, isEmptyPage);
  91. }
  92. /**
  93. * Get the value of the <code>maximum-repeats</code> property.
  94. * @return the "maximum-repeats" property
  95. */
  96. public int getMaximumRepeats() {
  97. if (maximumRepeats.getEnum() == EN_NO_LIMIT) {
  98. return INFINITE;
  99. } else {
  100. int mr = maximumRepeats.getNumeric().getValue();
  101. if (mr < 0) {
  102. log.debug("negative maximum-repeats: "
  103. + this.maximumRepeats);
  104. mr = 0;
  105. }
  106. return mr;
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. public void reset() {
  111. this.numberConsumed = 0;
  112. }
  113. /** {@inheritDoc} */
  114. public boolean goToPrevious() {
  115. if (numberConsumed == 0) {
  116. return false;
  117. } else {
  118. numberConsumed--;
  119. return true;
  120. }
  121. }
  122. /** {@inheritDoc} */
  123. public boolean hasPagePositionLast() {
  124. return false;
  125. }
  126. /** {@inheritDoc} */
  127. public boolean hasPagePositionOnly() {
  128. return false;
  129. }
  130. /** {@inheritDoc} */
  131. public String getLocalName() {
  132. return "repeatable-page-master-reference";
  133. }
  134. /**
  135. * {@inheritDoc}
  136. * @return {@link org.apache.fop.fo.Constants#FO_REPEATABLE_PAGE_MASTER_REFERENCE}
  137. */
  138. public int getNameId() {
  139. return FO_REPEATABLE_PAGE_MASTER_REFERENCE;
  140. }
  141. /** {@inheritDoc} */
  142. public void resolveReferences(LayoutMasterSet layoutMasterSet) throws ValidationException {
  143. master = layoutMasterSet.getSimplePageMaster(masterReference);
  144. if (master == null) {
  145. BlockLevelEventProducer.Provider.get(
  146. getUserAgent().getEventBroadcaster())
  147. .noMatchingPageMaster(this, parent.getName(), masterReference, getLocator());
  148. }
  149. }
  150. /** {@inheritDoc} */
  151. public boolean canProcess(String flowName) {
  152. assert master != null;
  153. return master.getRegion(FO_REGION_BODY).getRegionName().equals(flowName);
  154. }
  155. /** {@inheritDoc} */
  156. public boolean isInfinite() {
  157. return getMaximumRepeats() == INFINITE;
  158. }
  159. /** {@inheritDoc} */
  160. public boolean isReusable() {
  161. return false;
  162. }
  163. }