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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.ArrayList;
  21. import java.util.List;
  22. import org.xml.sax.Locator;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.fo.FONode;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.PropertyList;
  27. import org.apache.fop.fo.ValidationException;
  28. import org.apache.fop.fo.properties.Property;
  29. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  30. /**
  31. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_repeatable-page-master-alternatives">
  32. * <code>fo:repeatable-page-master-alternatives</code></a> object.
  33. * This contains a list of conditional-page-master-reference
  34. * and the page master is found from the reference that
  35. * matches the page number and emptyness.
  36. */
  37. public class RepeatablePageMasterAlternatives extends FObj
  38. implements SubSequenceSpecifier {
  39. // The value of properties relevant for fo:repeatable-page-master-alternatives.
  40. private Property maximumRepeats;
  41. // End of property values
  42. private static final int INFINITE = -1;
  43. private int numberConsumed;
  44. private List<ConditionalPageMasterReference> conditionalPageMasterRefs;
  45. private boolean hasPagePositionLast;
  46. private boolean hasPagePositionOnly;
  47. /**
  48. * Base constructor
  49. *
  50. * @param parent {@link FONode} that is the parent of this object
  51. */
  52. public RepeatablePageMasterAlternatives(FONode parent) {
  53. super(parent);
  54. }
  55. /** {@inheritDoc} */
  56. public void bind(PropertyList pList) throws FOPException {
  57. maximumRepeats = pList.get(PR_MAXIMUM_REPEATS);
  58. }
  59. /** {@inheritDoc} */
  60. public void startOfNode() throws FOPException {
  61. conditionalPageMasterRefs = new java.util.ArrayList<ConditionalPageMasterReference>();
  62. assert parent.getName().equals("fo:page-sequence-master"); //Validation by the parent
  63. PageSequenceMaster pageSequenceMaster = (PageSequenceMaster)parent;
  64. pageSequenceMaster.addSubsequenceSpecifier(this);
  65. }
  66. /** {@inheritDoc} */
  67. public void endOfNode() throws FOPException {
  68. if (firstChild == null) {
  69. missingChildElementError("(conditional-page-master-reference+)");
  70. }
  71. }
  72. /**
  73. * {@inheritDoc}
  74. * <br>XSL/FOP: (conditional-page-master-reference+)
  75. */
  76. protected void validateChildNode(Locator loc, String nsURI, String localName)
  77. throws ValidationException {
  78. if (FO_URI.equals(nsURI)) {
  79. if (!localName.equals("conditional-page-master-reference")) {
  80. invalidChildError(loc, nsURI, localName);
  81. }
  82. }
  83. }
  84. /**
  85. * Get the value of the <code>maximum-repeats</code> property?
  86. * @return the "maximum-repeats" property
  87. */
  88. public int getMaximumRepeats() {
  89. if (maximumRepeats.getEnum() == EN_NO_LIMIT) {
  90. return INFINITE;
  91. } else {
  92. int mr = maximumRepeats.getNumeric().getValue();
  93. if (mr < 0) {
  94. log.debug("negative maximum-repeats: "
  95. + this.maximumRepeats);
  96. mr = 0;
  97. }
  98. return mr;
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. public SimplePageMaster getNextPageMaster(boolean isOddPage,
  103. boolean isFirstPage,
  104. boolean isLastPage,
  105. boolean isBlankPage) {
  106. if (!isInfinite() && numberConsumed >= getMaximumRepeats()) {
  107. return null;
  108. }
  109. numberConsumed++;
  110. for (ConditionalPageMasterReference cpmr : conditionalPageMasterRefs) {
  111. if (cpmr.isValid(isOddPage, isFirstPage, isLastPage, isBlankPage)) {
  112. return cpmr.getMaster();
  113. }
  114. }
  115. return null;
  116. }
  117. public SimplePageMaster getLastPageMaster(boolean isOddPage, boolean isFirstPage, boolean isBlankPage,
  118. BlockLevelEventProducer blockLevelEventProducer) {
  119. for (ConditionalPageMasterReference cpmr : conditionalPageMasterRefs) {
  120. if (cpmr.isValid(isOddPage, isFirstPage, true, isBlankPage)) {
  121. return cpmr.getMaster();
  122. }
  123. }
  124. blockLevelEventProducer.lastPageMasterReferenceMissing(this, getLocator());
  125. for (ConditionalPageMasterReference cpmr : conditionalPageMasterRefs) {
  126. if (cpmr.isValid(isOddPage, isFirstPage, false, isBlankPage)) {
  127. return cpmr.getMaster();
  128. }
  129. }
  130. throw new PageProductionException("Last page master not found: oddpage=" + isOddPage
  131. + " firstpage=" + isFirstPage + " blankpage=" + isBlankPage);
  132. }
  133. /**
  134. * Adds a new conditional page master reference.
  135. * @param cpmr the new conditional reference
  136. */
  137. public void addConditionalPageMasterReference(ConditionalPageMasterReference cpmr) {
  138. this.conditionalPageMasterRefs.add(cpmr);
  139. if (cpmr.getPagePosition() == EN_LAST) {
  140. this.hasPagePositionLast = true;
  141. }
  142. if (cpmr.getPagePosition() == EN_ONLY) {
  143. this.hasPagePositionOnly = true;
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. public void reset() {
  148. this.numberConsumed = 0;
  149. }
  150. /** {@inheritDoc} */
  151. public boolean goToPrevious() {
  152. if (numberConsumed == 0) {
  153. return false;
  154. } else {
  155. numberConsumed--;
  156. return true;
  157. }
  158. }
  159. /** {@inheritDoc} */
  160. public boolean hasPagePositionLast() {
  161. return this.hasPagePositionLast;
  162. }
  163. /** {@inheritDoc} */
  164. public boolean hasPagePositionOnly() {
  165. return this.hasPagePositionOnly;
  166. }
  167. /** {@inheritDoc} */
  168. public String getLocalName() {
  169. return "repeatable-page-master-alternatives";
  170. }
  171. /**
  172. * {@inheritDoc}
  173. * @return {@link org.apache.fop.fo.Constants#FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES}
  174. */
  175. public int getNameId() {
  176. return FO_REPEATABLE_PAGE_MASTER_ALTERNATIVES;
  177. }
  178. /** {@inheritDoc} */
  179. public void resolveReferences(LayoutMasterSet layoutMasterSet) throws ValidationException {
  180. for (ConditionalPageMasterReference conditionalPageMasterReference
  181. : conditionalPageMasterRefs) {
  182. conditionalPageMasterReference.resolveReferences(layoutMasterSet);
  183. }
  184. }
  185. /** {@inheritDoc} */
  186. public boolean canProcess(String flowName) {
  187. boolean willTerminate = true;
  188. //Look for rest spm that cannot terminate
  189. ArrayList<ConditionalPageMasterReference> rest
  190. = new ArrayList<ConditionalPageMasterReference>();
  191. for (ConditionalPageMasterReference cpmr
  192. : conditionalPageMasterRefs) {
  193. if (cpmr.isValid(true, false, false, false)
  194. || cpmr.isValid(false, false, false, false)) {
  195. rest.add(cpmr);
  196. }
  197. }
  198. if (!rest.isEmpty()) {
  199. willTerminate = false;
  200. for (ConditionalPageMasterReference cpmr : rest) {
  201. willTerminate |= cpmr.getMaster().getRegion(FO_REGION_BODY).getRegionName()
  202. .equals(flowName);
  203. }
  204. }
  205. return willTerminate;
  206. }
  207. /** {@inheritDoc} */
  208. public boolean isInfinite() {
  209. return getMaximumRepeats() == INFINITE;
  210. }
  211. /** {@inheritDoc} */
  212. public boolean isReusable() {
  213. return false;
  214. }
  215. }