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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 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();
  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 (SubSequenceSpecifier)subSequenceSpecifiers
  109. .get(currentSubSequenceNumber);
  110. }
  111. return null;
  112. }
  113. /**
  114. * Resets the subsequence specifiers subsystem.
  115. */
  116. public void reset() {
  117. currentSubSequenceNumber = -1;
  118. currentSubSequence = null;
  119. if (subSequenceSpecifiers != null) {
  120. for (int i = 0; i < subSequenceSpecifiers.size(); i++) {
  121. ((SubSequenceSpecifier)subSequenceSpecifiers.get(i)).reset();
  122. }
  123. }
  124. }
  125. /**
  126. * Used to set the "cursor position" for the page masters to the previous item.
  127. * @return true if there is a previous item, false if the current one was the first one.
  128. */
  129. public boolean goToPreviousSimplePageMaster() {
  130. if (currentSubSequence != null) {
  131. boolean success = currentSubSequence.goToPrevious();
  132. if (!success) {
  133. if (currentSubSequenceNumber > 0) {
  134. currentSubSequenceNumber--;
  135. currentSubSequence = (SubSequenceSpecifier)subSequenceSpecifiers
  136. .get(currentSubSequenceNumber);
  137. } else {
  138. currentSubSequence = null;
  139. }
  140. }
  141. }
  142. return (currentSubSequence != null);
  143. }
  144. /** @return true if the page-sequence-master has a page-master with page-position="last" */
  145. public boolean hasPagePositionLast() {
  146. return (currentSubSequence != null
  147. && currentSubSequence.hasPagePositionLast());
  148. }
  149. /** @return true if the page-sequence-master has a page-master with page-position="only" */
  150. public boolean hasPagePositionOnly() {
  151. return (currentSubSequence != null
  152. && currentSubSequence.hasPagePositionOnly());
  153. }
  154. /**
  155. * Returns the next simple-page-master.
  156. * @param isOddPage True if the next page number is odd
  157. * @param isFirstPage True if the next page is the first
  158. * @param isLastPage True if the next page is the last
  159. * @param isBlankPage True if the next page is blank
  160. * @return the requested page master
  161. * @throws PageProductionException if there's a problem determining the next page master
  162. */
  163. public SimplePageMaster getNextSimplePageMaster(boolean isOddPage,
  164. boolean isFirstPage,
  165. boolean isLastPage,
  166. boolean isBlankPage)
  167. throws PageProductionException {
  168. if (currentSubSequence == null) {
  169. currentSubSequence = getNextSubSequence();
  170. if (currentSubSequence == null) {
  171. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  172. getUserAgent().getEventBroadcaster());
  173. eventProducer.missingSubsequencesInPageSequenceMaster(this,
  174. masterName, getLocator());
  175. }
  176. }
  177. String pageMasterName = currentSubSequence
  178. .getNextPageMasterName(isOddPage, isFirstPage, isLastPage, isBlankPage);
  179. boolean canRecover = true;
  180. while (pageMasterName == null) {
  181. SubSequenceSpecifier nextSubSequence = getNextSubSequence();
  182. if (nextSubSequence == null) {
  183. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  184. getUserAgent().getEventBroadcaster());
  185. eventProducer.pageSequenceMasterExhausted(this,
  186. masterName, canRecover, getLocator());
  187. currentSubSequence.reset();
  188. canRecover = false;
  189. } else {
  190. currentSubSequence = nextSubSequence;
  191. }
  192. pageMasterName = currentSubSequence
  193. .getNextPageMasterName(isOddPage, isFirstPage, isLastPage, isBlankPage);
  194. }
  195. SimplePageMaster pageMaster = this.layoutMasterSet
  196. .getSimplePageMaster(pageMasterName);
  197. if (pageMaster == null) {
  198. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  199. getUserAgent().getEventBroadcaster());
  200. eventProducer.noMatchingPageMaster(this,
  201. masterName, pageMasterName, getLocator());
  202. }
  203. return pageMaster;
  204. }
  205. /** {@inheritDoc} */
  206. public String getLocalName() {
  207. return "page-sequence-master";
  208. }
  209. /**
  210. * {@inheritDoc}
  211. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE_MASTER}
  212. */
  213. public int getNameId() {
  214. return FO_PAGE_SEQUENCE_MASTER;
  215. }
  216. }