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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Map;
  21. import org.xml.sax.Locator;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.fo.FONode;
  24. import org.apache.fop.fo.FObj;
  25. import org.apache.fop.fo.PropertyList;
  26. import org.apache.fop.fo.ValidationException;
  27. /**
  28. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_layout-master-set">
  29. * <code>fo:layout-master-set</code></a> object.
  30. *
  31. * This class maintains the set of simple page master and
  32. * page sequence masters.
  33. * The masters are stored so that the page sequence can obtain
  34. * the required page master to create a page.
  35. * The page sequence masters can be reset as they hold state
  36. * information for a page sequence.
  37. */
  38. public class LayoutMasterSet extends FObj {
  39. private Map<String, SimplePageMaster> simplePageMasters;
  40. private Map<String, PageSequenceMaster> pageSequenceMasters;
  41. /**
  42. * Create a LayoutMasterSet instance that is a child of the given
  43. * parent {@link FONode}.
  44. *
  45. * @param parent {@link FONode} that is the parent of this object
  46. */
  47. public LayoutMasterSet(FONode parent) {
  48. super(parent);
  49. }
  50. /** {@inheritDoc} */
  51. public void bind(PropertyList pList) throws FOPException {
  52. // No properties in layout-master-set.
  53. }
  54. /** {@inheritDoc} */
  55. protected void startOfNode() throws FOPException {
  56. getRoot().setLayoutMasterSet(this);
  57. simplePageMasters = new java.util.HashMap<String, SimplePageMaster>();
  58. pageSequenceMasters = new java.util.HashMap<String, PageSequenceMaster>();
  59. }
  60. /** {@inheritDoc} */
  61. protected void endOfNode() throws FOPException {
  62. if (firstChild == null) {
  63. missingChildElementError("(simple-page-master|page-sequence-master)+");
  64. }
  65. checkRegionNames();
  66. resolveSubSequenceReferences();
  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. private void resolveSubSequenceReferences() throws ValidationException {
  108. for (PageSequenceMaster psm : pageSequenceMasters.values()) {
  109. for (SubSequenceSpecifier subSequenceSpecifier : psm.getSubSequenceSpecifier()) {
  110. subSequenceSpecifier.resolveReferences(this);
  111. }
  112. }
  113. }
  114. /**
  115. * Add a simple page master.
  116. * The name is checked to throw an error if already added.
  117. * @param sPM simple-page-master to add
  118. * @throws ValidationException if there's a problem with name uniqueness
  119. */
  120. protected void addSimplePageMaster(SimplePageMaster sPM)
  121. throws ValidationException {
  122. // check for duplication of master-name
  123. String masterName = sPM.getMasterName();
  124. if (existsName(masterName)) {
  125. getFOValidationEventProducer().masterNameNotUnique(this,
  126. getName(),
  127. masterName, sPM.getLocator());
  128. }
  129. this.simplePageMasters.put(masterName, sPM);
  130. }
  131. private boolean existsName(String masterName) {
  132. return (simplePageMasters.containsKey(masterName)
  133. || pageSequenceMasters.containsKey(masterName));
  134. }
  135. /**
  136. * Get a simple page master by name.
  137. * This is used by the page sequence to get a page master for
  138. * creating pages.
  139. * @param masterName the name of the page master
  140. * @return the requested simple-page-master
  141. */
  142. public SimplePageMaster getSimplePageMaster(String masterName) {
  143. return simplePageMasters.get(masterName);
  144. }
  145. /**
  146. * Add a page sequence master.
  147. * The name is checked to throw an error if already added.
  148. * @param masterName name for the master
  149. * @param pSM PageSequenceMaster instance
  150. * @throws ValidationException if there's a problem with name uniqueness
  151. */
  152. protected void addPageSequenceMaster(String masterName,
  153. PageSequenceMaster pSM)
  154. throws ValidationException {
  155. // check against duplication of master-name
  156. if (existsName(masterName)) {
  157. getFOValidationEventProducer().masterNameNotUnique(this,
  158. getName(),
  159. masterName, pSM.getLocator());
  160. }
  161. this.pageSequenceMasters.put(masterName, pSM);
  162. }
  163. /**
  164. * Get a page sequence master by name.
  165. * This is used by the page sequence to get a page master for
  166. * creating pages.
  167. * @param masterName name of the master
  168. * @return the requested PageSequenceMaster instance
  169. */
  170. public PageSequenceMaster getPageSequenceMaster(String masterName) {
  171. return this.pageSequenceMasters.get(masterName);
  172. }
  173. /**
  174. * Checks whether or not a region name exists in this master set.
  175. * @param regionName name of the region
  176. * @return true when the region name specified has a region in this LayoutMasterSet
  177. */
  178. public boolean regionNameExists(String regionName) {
  179. for (SimplePageMaster spm : simplePageMasters.values()) {
  180. if (spm.regionNameExists(regionName)) {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. /** {@inheritDoc} */
  187. public String getLocalName() {
  188. return "layout-master-set";
  189. }
  190. /**
  191. * {@inheritDoc}
  192. * @return {@link org.apache.fop.fo.Constants#FO_LAYOUT_MASTER_SET}
  193. */
  194. public int getNameId() {
  195. return FO_LAYOUT_MASTER_SET;
  196. }
  197. /**
  198. * Returns the default name of the region to which the flow or static-content having
  199. * the given flow-name is assigned.
  200. *
  201. * @param flowName the value of the flow-name property
  202. * @return the default region name ("xsl-region-body", "xsl-region-before", etc.)
  203. */
  204. public String getDefaultRegionNameFor(String flowName) {
  205. for (SimplePageMaster spm : simplePageMasters.values()) {
  206. for (Region region : spm.getRegions().values()) {
  207. if (region.getRegionName().equals(flowName)) {
  208. return region.getDefaultRegionName();
  209. }
  210. }
  211. }
  212. assert flowName.equals("xsl-before-float-separator") || flowName.equals("xsl-footnote-separator");
  213. return flowName;
  214. }
  215. }