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

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