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

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