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.

ConditionalPageMasterReference.java 5.2KB

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