您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConditionalPageMasterReference.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // XML
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.fo.FONode;
  23. import org.apache.fop.fo.FObj;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.ValidationException;
  26. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  27. /**
  28. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_conditional-page-master-reference">
  29. * <code>fo:conditional-page-master-reference</code></a> object.
  30. *
  31. * This is a reference to a page master with a set of conditions.
  32. * The conditions must be satisfied for the referenced master to
  33. * be used.
  34. * This element is must be the child of a repeatable-page-master-alternatives
  35. * element.
  36. */
  37. public class ConditionalPageMasterReference extends FObj {
  38. // The value of properties relevant for fo:conditional-page-master-reference.
  39. private String masterReference;
  40. // The simple page master referenced
  41. private SimplePageMaster master;
  42. private int pagePosition;
  43. private int oddOrEven;
  44. private int blankOrNotBlank;
  45. // End of property values
  46. /**
  47. * Create a ConditionalPageMasterReference instance that is a
  48. * child of the given {@link FONode}.
  49. *
  50. * @param parent {@link FONode} that is the parent of this object
  51. */
  52. public ConditionalPageMasterReference(FONode parent) {
  53. super(parent);
  54. }
  55. /** {@inheritDoc} */
  56. public void bind(PropertyList pList) throws FOPException {
  57. masterReference = pList.get(PR_MASTER_REFERENCE).getString();
  58. pagePosition = pList.get(PR_PAGE_POSITION).getEnum();
  59. oddOrEven = pList.get(PR_ODD_OR_EVEN).getEnum();
  60. blankOrNotBlank = pList.get(PR_BLANK_OR_NOT_BLANK).getEnum();
  61. if (masterReference == null || masterReference.equals("")) {
  62. missingPropertyError("master-reference");
  63. }
  64. }
  65. /** {@inheritDoc} */
  66. public void startOfNode() throws FOPException {
  67. getConcreteParent().addConditionalPageMasterReference(this);
  68. }
  69. private RepeatablePageMasterAlternatives getConcreteParent() {
  70. return (RepeatablePageMasterAlternatives) parent;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. * <br>XSL Content Model: empty
  75. */
  76. protected void validateChildNode(Locator loc, String nsURI, String localName)
  77. throws ValidationException {
  78. invalidChildError(loc, nsURI, localName);
  79. }
  80. /**
  81. * Check if the conditions for this reference are met.
  82. * checks the page number and emptyness to determine if this
  83. * matches.
  84. * @param isOddPage True if page number odd
  85. * @param isFirstPage True if page is first page
  86. * @param isLastPage True if page is last page
  87. * @param isBlankPage True if page is blank
  88. * @return True if the conditions for this reference are met
  89. */
  90. protected boolean isValid(boolean isOddPage,
  91. boolean isFirstPage,
  92. boolean isLastPage,
  93. boolean isBlankPage) {
  94. return (
  95. // page-position
  96. (pagePosition == EN_ANY
  97. || (pagePosition == EN_FIRST && isFirstPage)
  98. || (pagePosition == EN_LAST && isLastPage)
  99. || (pagePosition == EN_ONLY && (isFirstPage && isLastPage))
  100. || (pagePosition == EN_REST && !(isFirstPage || isLastPage))
  101. )
  102. // odd-or-even
  103. && (oddOrEven == EN_ANY
  104. || (oddOrEven == EN_ODD && isOddPage)
  105. || (oddOrEven == EN_EVEN && !isOddPage)
  106. )
  107. // blank-or-not-blank
  108. && (blankOrNotBlank == EN_ANY
  109. || (blankOrNotBlank == EN_BLANK && isBlankPage)
  110. || (blankOrNotBlank == EN_NOT_BLANK && !isBlankPage)
  111. ));
  112. }
  113. /**
  114. * Get the value for the <code>master-reference</code> property.
  115. * @return the "master-reference" property
  116. */
  117. public SimplePageMaster getMaster() {
  118. return master;
  119. }
  120. /**
  121. * Get the value for the <code>page-position</code> property.
  122. * @return the page-position property value
  123. */
  124. public int getPagePosition() {
  125. return this.pagePosition;
  126. }
  127. /** {@inheritDoc} */
  128. public String getLocalName() {
  129. return "conditional-page-master-reference";
  130. }
  131. /**
  132. * {@inheritDoc}
  133. * @return {@link org.apache.fop.fo.Constants#FO_CONDITIONAL_PAGE_MASTER_REFERENCE}
  134. */
  135. public int getNameId() {
  136. return FO_CONDITIONAL_PAGE_MASTER_REFERENCE;
  137. }
  138. /**
  139. * called by the parent RepeatablePageMasterAlternatives to resolve object references
  140. * from simple page master reference names
  141. * @param layoutMasterSet the layout-master-set
  142. * @throws ValidationException when a named reference cannot be resolved
  143. * */
  144. public void resolveReferences(LayoutMasterSet layoutMasterSet) throws ValidationException {
  145. master = layoutMasterSet.getSimplePageMaster(masterReference);
  146. if (master == null) {
  147. BlockLevelEventProducer.Provider.get(
  148. getUserAgent().getEventBroadcaster())
  149. .noMatchingPageMaster(this, parent.getName(), masterReference, getLocator());
  150. }
  151. }
  152. }