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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /**
  28. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_repeatable-page-master-reference">
  29. * <code>fo:repeatable-page-master-reference</code></a> object.
  30. * This handles a reference with a specified number of repeating
  31. * instances of the referenced page master (may have no limit).
  32. */
  33. public class RepeatablePageMasterReference extends FObj
  34. implements SubSequenceSpecifier {
  35. // The value of properties relevant for fo:repeatable-page-master-reference.
  36. private String masterReference;
  37. private Property maximumRepeats;
  38. // End of property values
  39. private static final int INFINITE = -1;
  40. private int numberConsumed = 0;
  41. /**
  42. * Base constructor
  43. *
  44. * @param parent {@link FONode} that is the parent of this object
  45. */
  46. public RepeatablePageMasterReference(FONode parent) {
  47. super(parent);
  48. }
  49. /** {@inheritDoc} */
  50. public void bind(PropertyList pList) throws FOPException {
  51. masterReference = pList.get(PR_MASTER_REFERENCE).getString();
  52. maximumRepeats = pList.get(PR_MAXIMUM_REPEATS);
  53. if (masterReference == null || masterReference.equals("")) {
  54. missingPropertyError("master-reference");
  55. }
  56. }
  57. /** {@inheritDoc} */
  58. protected void startOfNode() throws FOPException {
  59. PageSequenceMaster pageSequenceMaster = (PageSequenceMaster) parent;
  60. if (masterReference == null) {
  61. missingPropertyError("master-reference");
  62. } else {
  63. pageSequenceMaster.addSubsequenceSpecifier(this);
  64. }
  65. }
  66. /**
  67. * {@inheritDoc}
  68. * <br>XSL Content Model: empty
  69. */
  70. protected void validateChildNode(Locator loc, String nsURI, String localName)
  71. throws ValidationException {
  72. invalidChildError(loc, nsURI, localName);
  73. }
  74. /** {@inheritDoc} */
  75. public String getNextPageMasterName(boolean isOddPage,
  76. boolean isFirstPage,
  77. boolean isLastPage,
  78. boolean isEmptyPage) {
  79. if (getMaximumRepeats() != INFINITE) {
  80. if (numberConsumed < getMaximumRepeats()) {
  81. numberConsumed++;
  82. } else {
  83. return null;
  84. }
  85. }
  86. return masterReference;
  87. }
  88. /**
  89. * Get the value of the <code>maximum-repeats</code> property.
  90. * @return the "maximum-repeats" property
  91. */
  92. public int getMaximumRepeats() {
  93. if (maximumRepeats.getEnum() == EN_NO_LIMIT) {
  94. return INFINITE;
  95. } else {
  96. int mr = maximumRepeats.getNumeric().getValue();
  97. if (mr < 0) {
  98. log.debug("negative maximum-repeats: "
  99. + this.maximumRepeats);
  100. mr = 0;
  101. }
  102. return mr;
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. public void reset() {
  107. this.numberConsumed = 0;
  108. }
  109. /** {@inheritDoc} */
  110. public boolean goToPrevious() {
  111. if (numberConsumed == 0) {
  112. return false;
  113. } else {
  114. numberConsumed--;
  115. return true;
  116. }
  117. }
  118. /** {@inheritDoc} */
  119. public boolean hasPagePositionLast() {
  120. return false;
  121. }
  122. /** {@inheritDoc} */
  123. public boolean hasPagePositionOnly() {
  124. return false;
  125. }
  126. /** {@inheritDoc} */
  127. public String getLocalName() {
  128. return "repeatable-page-master-reference";
  129. }
  130. /**
  131. * {@inheritDoc}
  132. * @return {@link org.apache.fop.fo.Constants#FO_REPEATABLE_PAGE_MASTER_REFERENCE}
  133. */
  134. public int getNameId() {
  135. return FO_REPEATABLE_PAGE_MASTER_REFERENCE;
  136. }
  137. }