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.

LayoutMasterSet.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Iterator;
  21. import java.util.Map;
  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. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_layout-master-set">
  30. * <code>fo:layout-master-set</code></a> object.
  31. *
  32. * This class maintains the set of simple page master and
  33. * page sequence masters.
  34. * The masters are stored so that the page sequence can obtain
  35. * the required page master to create a page.
  36. * The page sequence masters can be reset as they hold state
  37. * information for a page sequence.
  38. */
  39. public class LayoutMasterSet extends FObj {
  40. private Map<String, SimplePageMaster> simplePageMasters;
  41. private Map<String, PageSequenceMaster> pageSequenceMasters;
  42. /**
  43. * Create a LayoutMasterSet instance that is a child of the given
  44. * parent {@link FONode}.
  45. *
  46. * @param parent {@link FONode} that is the parent of this object
  47. */
  48. public LayoutMasterSet(FONode parent) {
  49. super(parent);
  50. }
  51. /** {@inheritDoc} */
  52. public void bind(PropertyList pList) throws FOPException {
  53. // No properties in layout-master-set.
  54. }
  55. /** {@inheritDoc} */
  56. protected void startOfNode() throws FOPException {
  57. getRoot().setLayoutMasterSet(this);
  58. simplePageMasters = new java.util.HashMap<String, SimplePageMaster>();
  59. pageSequenceMasters = new java.util.HashMap<String, PageSequenceMaster>();
  60. }
  61. /** {@inheritDoc} */
  62. protected void endOfNode() throws FOPException {
  63. if (firstChild == null) {
  64. missingChildElementError("(simple-page-master|page-sequence-master)+");
  65. }
  66. checkRegionNames();
  67. }
  68. /**
  69. * {@inheritDoc}
  70. * <br>XSL/FOP: (simple-page-master|page-sequence-master)+
  71. */
  72. protected void validateChildNode(Locator loc, String nsURI, String localName)
  73. throws ValidationException {
  74. if (FO_URI.equals(nsURI)) {
  75. if (!localName.equals("simple-page-master")
  76. && !localName.equals("page-sequence-master")) {
  77. invalidChildError(loc, nsURI, localName);
  78. }
  79. }
  80. }
  81. /**
  82. * Section 7.25.7: check to see that if a region-name is a
  83. * duplicate, that it maps to the same fo region-class.
  84. * @throws ValidationException if there's a name duplication
  85. */
  86. private void checkRegionNames() throws ValidationException {
  87. // (user-entered) region-name to default region map.
  88. Map<String, String> allRegions = new java.util.HashMap<String, String>();
  89. for (SimplePageMaster simplePageMaster : simplePageMasters.values()) {
  90. Map<String, Region> spmRegions = simplePageMaster.getRegions();
  91. for (Region region : spmRegions.values()) {
  92. if (allRegions.containsKey(region.getRegionName())) {
  93. String defaultRegionName
  94. = allRegions.get(region.getRegionName());
  95. if (!defaultRegionName.equals(region.getDefaultRegionName())) {
  96. getFOValidationEventProducer().regionNameMappedToMultipleRegionClasses(this,
  97. region.getRegionName(),
  98. defaultRegionName,
  99. region.getDefaultRegionName(), getLocator());
  100. }
  101. }
  102. allRegions.put(region.getRegionName(),
  103. region.getDefaultRegionName());
  104. }
  105. }
  106. }
  107. /**
  108. * Add a simple page master.
  109. * The name is checked to throw an error if already added.
  110. * @param sPM simple-page-master to add
  111. * @throws ValidationException if there's a problem with name uniqueness
  112. */
  113. protected void addSimplePageMaster(SimplePageMaster sPM)
  114. throws ValidationException {
  115. // check for duplication of master-name
  116. String masterName = sPM.getMasterName();
  117. if (existsName(masterName)) {
  118. getFOValidationEventProducer().masterNameNotUnique(this,
  119. getName(),
  120. masterName, sPM.getLocator());
  121. }
  122. this.simplePageMasters.put(masterName, sPM);
  123. }
  124. private boolean existsName(String masterName) {
  125. return (simplePageMasters.containsKey(masterName)
  126. || pageSequenceMasters.containsKey(masterName));
  127. }
  128. /**
  129. * Get a simple page master by name.
  130. * This is used by the page sequence to get a page master for
  131. * creating pages.
  132. * @param masterName the name of the page master
  133. * @return the requested simple-page-master
  134. */
  135. public SimplePageMaster getSimplePageMaster(String masterName) {
  136. return this.simplePageMasters.get(masterName);
  137. }
  138. /**
  139. * Add a page sequence master.
  140. * The name is checked to throw an error if already added.
  141. * @param masterName name for the master
  142. * @param pSM PageSequenceMaster instance
  143. * @throws ValidationException if there's a problem with name uniqueness
  144. */
  145. protected void addPageSequenceMaster(String masterName,
  146. PageSequenceMaster pSM)
  147. throws ValidationException {
  148. // check against duplication of master-name
  149. if (existsName(masterName)) {
  150. getFOValidationEventProducer().masterNameNotUnique(this,
  151. getName(),
  152. masterName, pSM.getLocator());
  153. }
  154. this.pageSequenceMasters.put(masterName, pSM);
  155. }
  156. /**
  157. * Get a page sequence master by name.
  158. * This is used by the page sequence to get a page master for
  159. * creating pages.
  160. * @param masterName name of the master
  161. * @return the requested PageSequenceMaster instance
  162. */
  163. public PageSequenceMaster getPageSequenceMaster(String masterName) {
  164. return this.pageSequenceMasters.get(masterName);
  165. }
  166. /**
  167. * Checks whether or not a region name exists in this master set.
  168. * @param regionName name of the region
  169. * @return true when the region name specified has a region in this LayoutMasterSet
  170. */
  171. public boolean regionNameExists(String regionName) {
  172. for (SimplePageMaster spm : simplePageMasters.values()) {
  173. if (spm.regionNameExists(regionName)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /** {@inheritDoc} */
  180. public String getLocalName() {
  181. return "layout-master-set";
  182. }
  183. /**
  184. * {@inheritDoc}
  185. * @return {@link org.apache.fop.fo.Constants#FO_LAYOUT_MASTER_SET}
  186. */
  187. public int getNameId() {
  188. return FO_LAYOUT_MASTER_SET;
  189. }
  190. }