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.

PageSequenceMaster.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.layoutmgr.BlockLevelEventProducer;
  28. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_page-sequence-master">
  30. * <code>fo:page-sequence-master</code></a> object.
  31. *
  32. * This class handles a list of subsequence specifiers
  33. * which are simple or complex references to page-masters.
  34. */
  35. public class PageSequenceMaster extends FObj {
  36. // The value of properties relevant for fo:page-sequence-master.
  37. private String masterName;
  38. // End of property values
  39. private LayoutMasterSet layoutMasterSet;
  40. private List<SubSequenceSpecifier> subSequenceSpecifiers;
  41. private SubSequenceSpecifier currentSubSequence;
  42. private int currentSubSequenceNumber = -1;
  43. // The terminology may be confusing. A 'page-sequence-master' consists
  44. // of a sequence of what the XSL spec refers to as
  45. // 'sub-sequence-specifiers'. These are, in fact, simple or complex
  46. // references to page-masters. So the methods use the former
  47. // terminology ('sub-sequence-specifiers', or SSS),
  48. // but the actual FO's are MasterReferences.
  49. /**
  50. * Create a PageSequenceMaster instance that is a child of the
  51. * given {@link FONode}.
  52. *
  53. * @param parent {@link FONode} that is the parent of this object
  54. */
  55. public PageSequenceMaster(FONode parent) {
  56. super(parent);
  57. }
  58. /** {@inheritDoc} */
  59. public void bind(PropertyList pList) throws FOPException {
  60. masterName = pList.get(PR_MASTER_NAME).getString();
  61. if (masterName == null || masterName.equals("")) {
  62. missingPropertyError("master-name");
  63. }
  64. }
  65. /** {@inheritDoc} */
  66. protected void startOfNode() throws FOPException {
  67. subSequenceSpecifiers = new java.util.ArrayList<SubSequenceSpecifier>();
  68. layoutMasterSet = parent.getRoot().getLayoutMasterSet();
  69. layoutMasterSet.addPageSequenceMaster(masterName, this);
  70. }
  71. /** {@inheritDoc} */
  72. protected void endOfNode() throws FOPException {
  73. if (firstChild == null) {
  74. missingChildElementError("(single-page-master-reference|"
  75. + "repeatable-page-master-reference|repeatable-page-master-alternatives)+");
  76. }
  77. }
  78. /**
  79. * {@inheritDoc}
  80. * <br>XSL/FOP: (single-page-master-reference|repeatable-page-master-reference|
  81. * repeatable-page-master-alternatives)+
  82. */
  83. protected void validateChildNode(Locator loc, String nsURI, String localName)
  84. throws ValidationException {
  85. if (FO_URI.equals(nsURI)) {
  86. if (!"single-page-master-reference".equals(localName)
  87. && !"repeatable-page-master-reference".equals(localName)
  88. && !"repeatable-page-master-alternatives".equals(localName)) {
  89. invalidChildError(loc, nsURI, localName);
  90. }
  91. }
  92. }
  93. /**
  94. * Adds a new suqsequence specifier to the page sequence master.
  95. * @param pageMasterReference the subsequence to add
  96. */
  97. protected void addSubsequenceSpecifier(SubSequenceSpecifier pageMasterReference) {
  98. subSequenceSpecifiers.add(pageMasterReference);
  99. }
  100. /**
  101. * Returns the next subsequence specifier
  102. * @return a subsequence specifier
  103. */
  104. private SubSequenceSpecifier getNextSubSequence() {
  105. currentSubSequenceNumber++;
  106. if (currentSubSequenceNumber >= 0
  107. && currentSubSequenceNumber < subSequenceSpecifiers.size()) {
  108. return subSequenceSpecifiers.get(currentSubSequenceNumber);
  109. }
  110. return null;
  111. }
  112. /**
  113. * Resets the subsequence specifiers subsystem.
  114. */
  115. public void reset() {
  116. currentSubSequenceNumber = -1;
  117. currentSubSequence = null;
  118. if (subSequenceSpecifiers != null) {
  119. for (SubSequenceSpecifier subSequenceSpecifier : subSequenceSpecifiers) {
  120. subSequenceSpecifier.reset();
  121. }
  122. }
  123. }
  124. /**
  125. * Used to set the "cursor position" for the page masters to the previous item.
  126. * @return true if there is a previous item, false if the current one was the first one.
  127. */
  128. public boolean goToPreviousSimplePageMaster() {
  129. if (currentSubSequence != null) {
  130. boolean success = currentSubSequence.goToPrevious();
  131. if (!success) {
  132. if (currentSubSequenceNumber > 0) {
  133. currentSubSequenceNumber--;
  134. currentSubSequence = subSequenceSpecifiers
  135. .get(currentSubSequenceNumber);
  136. } else {
  137. currentSubSequence = null;
  138. }
  139. }
  140. }
  141. return (currentSubSequence != null);
  142. }
  143. /** @return true if the page-sequence-master has a page-master with page-position="last" */
  144. public boolean hasPagePositionLast() {
  145. return (currentSubSequence != null
  146. && currentSubSequence.hasPagePositionLast());
  147. }
  148. /** @return true if the page-sequence-master has a page-master with page-position="only" */
  149. public boolean hasPagePositionOnly() {
  150. return (currentSubSequence != null
  151. && currentSubSequence.hasPagePositionOnly());
  152. }
  153. /**
  154. * Returns the next simple-page-master.
  155. * @param isOddPage True if the next page number is odd
  156. * @param isFirstPage True if the next page is the first
  157. * @param isLastPage True if the next page is the last
  158. * @param isBlankPage True if the next page is blank
  159. * @return the requested page master
  160. * @throws PageProductionException if there's a problem determining the next page master
  161. */
  162. public SimplePageMaster getNextSimplePageMaster(boolean isOddPage,
  163. boolean isFirstPage,
  164. boolean isLastPage,
  165. boolean isBlankPage)
  166. throws PageProductionException {
  167. if (currentSubSequence == null) {
  168. currentSubSequence = getNextSubSequence();
  169. if (currentSubSequence == null) {
  170. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  171. getUserAgent().getEventBroadcaster());
  172. eventProducer.missingSubsequencesInPageSequenceMaster(this,
  173. masterName, getLocator());
  174. }
  175. }
  176. String pageMasterName = currentSubSequence
  177. .getNextPageMasterName(isOddPage, isFirstPage, isLastPage, isBlankPage);
  178. boolean canRecover = true;
  179. while (pageMasterName == null) {
  180. SubSequenceSpecifier nextSubSequence = getNextSubSequence();
  181. if (nextSubSequence == null) {
  182. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  183. getUserAgent().getEventBroadcaster());
  184. eventProducer.pageSequenceMasterExhausted(this,
  185. masterName, canRecover, getLocator());
  186. currentSubSequence.reset();
  187. canRecover = false;
  188. } else {
  189. currentSubSequence = nextSubSequence;
  190. }
  191. pageMasterName = currentSubSequence
  192. .getNextPageMasterName(isOddPage, isFirstPage, isLastPage, isBlankPage);
  193. }
  194. SimplePageMaster pageMaster = this.layoutMasterSet
  195. .getSimplePageMaster(pageMasterName);
  196. if (pageMaster == null) {
  197. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  198. getUserAgent().getEventBroadcaster());
  199. eventProducer.noMatchingPageMaster(this,
  200. masterName, pageMasterName, getLocator());
  201. }
  202. return pageMaster;
  203. }
  204. /** {@inheritDoc} */
  205. public String getLocalName() {
  206. return "page-sequence-master";
  207. }
  208. /**
  209. * {@inheritDoc}
  210. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE_MASTER}
  211. */
  212. public int getNameId() {
  213. return FO_PAGE_SEQUENCE_MASTER;
  214. }
  215. }