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.

PageSequence.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.PropertyList;
  25. import org.apache.fop.fo.ValidationException;
  26. /**
  27. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_page-sequence">
  28. * <code>fo:page-sequence</code></a> object.
  29. */
  30. public class PageSequence extends AbstractPageSequence {
  31. // The value of properties relevant for fo:page-sequence.
  32. private String country;
  33. private String language;
  34. private String masterReference;
  35. //private int writingMode; //XSL 1.1
  36. // End of property values
  37. // There doesn't seem to be anything in the spec requiring flows
  38. // to be in the order given, only that they map to the regions
  39. // defined in the page sequence, so all we need is this one hashmap
  40. // the set of flows includes StaticContent flows also
  41. /** Map of flows to their flow name (flow-name, Flow) */
  42. private Map<String, Flow> flowMap;
  43. /**
  44. * The currentSimplePageMaster is either the page master for the
  45. * whole page sequence if master-reference refers to a simple-page-master,
  46. * or the simple page master produced by the page sequence master otherwise.
  47. * The pageSequenceMaster is null if master-reference refers to a
  48. * simple-page-master.
  49. */
  50. private SimplePageMaster simplePageMaster;
  51. private PageSequenceMaster pageSequenceMaster;
  52. /**
  53. * The fo:title object for this page-sequence.
  54. */
  55. private Title titleFO;
  56. /**
  57. * The fo:flow object for this page-sequence.
  58. */
  59. private Flow mainFlow = null;
  60. /**
  61. * Create a PageSequence instance that is a child of the
  62. * given {@link FONode}.
  63. *
  64. * @param parent the parent {@link FONode}
  65. */
  66. public PageSequence(FONode parent) {
  67. super(parent);
  68. }
  69. /** {@inheritDoc} */
  70. public void bind(PropertyList pList) throws FOPException {
  71. super.bind(pList);
  72. country = pList.get(PR_COUNTRY).getString();
  73. language = pList.get(PR_LANGUAGE).getString();
  74. masterReference = pList.get(PR_MASTER_REFERENCE).getString();
  75. //writingMode = pList.getWritingMode();
  76. if (masterReference == null || masterReference.equals("")) {
  77. missingPropertyError("master-reference");
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. protected void startOfNode() throws FOPException {
  82. super.startOfNode();
  83. flowMap = new java.util.HashMap<String, Flow>();
  84. this.simplePageMaster
  85. = getRoot().getLayoutMasterSet().getSimplePageMaster(masterReference);
  86. if (simplePageMaster == null) {
  87. this.pageSequenceMaster
  88. = getRoot().getLayoutMasterSet().getPageSequenceMaster(masterReference);
  89. if (pageSequenceMaster == null) {
  90. getFOValidationEventProducer().masterNotFound(this, getName(),
  91. masterReference, getLocator());
  92. }
  93. }
  94. getFOEventHandler().startPageSequence(this);
  95. }
  96. /** {@inheritDoc} */
  97. protected void endOfNode() throws FOPException {
  98. if (mainFlow == null) {
  99. missingChildElementError("(title?,static-content*,flow)");
  100. }
  101. getFOEventHandler().endPageSequence(this);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. XSL Content Model: (title?,static-content*,flow)
  106. */
  107. protected void validateChildNode(Locator loc, String nsURI, String localName)
  108. throws ValidationException {
  109. if (FO_URI.equals(nsURI)) {
  110. if ("title".equals(localName)) {
  111. if (titleFO != null) {
  112. tooManyNodesError(loc, "fo:title");
  113. } else if (!flowMap.isEmpty()) {
  114. nodesOutOfOrderError(loc, "fo:title", "fo:static-content");
  115. } else if (mainFlow != null) {
  116. nodesOutOfOrderError(loc, "fo:title", "fo:flow");
  117. }
  118. } else if ("static-content".equals(localName)) {
  119. if (mainFlow != null) {
  120. nodesOutOfOrderError(loc, "fo:static-content", "fo:flow");
  121. }
  122. } else if ("flow".equals(localName)) {
  123. if (mainFlow != null) {
  124. tooManyNodesError(loc, "fo:flow");
  125. }
  126. } else {
  127. invalidChildError(loc, nsURI, localName);
  128. }
  129. }
  130. }
  131. /**
  132. * {@inheritDoc}
  133. * TODO see if addChildNode() should also be called for fo's other than
  134. * fo:flow.
  135. */
  136. public void addChildNode(FONode child) throws FOPException {
  137. int childId = child.getNameId();
  138. switch (childId) {
  139. case FO_TITLE:
  140. this.titleFO = (Title)child;
  141. break;
  142. case FO_FLOW:
  143. this.mainFlow = (Flow)child;
  144. addFlow(mainFlow);
  145. break;
  146. case FO_STATIC_CONTENT:
  147. addFlow((StaticContent)child);
  148. flowMap.put(((Flow)child).getFlowName(), (Flow)child);
  149. break;
  150. default:
  151. super.addChildNode(child);
  152. }
  153. }
  154. /**
  155. * Add a flow or static content, mapped by its flow-name.
  156. * The flow-name is used to associate the flow with a region on a page,
  157. * based on the region-names given to the regions in the page-master
  158. * used to generate that page.
  159. * @param flow the {@link Flow} instance to be added
  160. * @throws org.apache.fop.fo.ValidationException if the fo:flow maps
  161. * to an invalid page-region
  162. */
  163. private void addFlow(Flow flow) throws ValidationException {
  164. String flowName = flow.getFlowName();
  165. if (hasFlowName(flowName)) {
  166. getFOValidationEventProducer().duplicateFlowNameInPageSequence(this, flow.getName(),
  167. flowName, flow.getLocator());
  168. }
  169. if (!getRoot().getLayoutMasterSet().regionNameExists(flowName)
  170. && !flowName.equals("xsl-before-float-separator")
  171. && !flowName.equals("xsl-footnote-separator")) {
  172. getFOValidationEventProducer().flowNameNotMapped(this, flow.getName(),
  173. flowName, flow.getLocator());
  174. }
  175. }
  176. /**
  177. * Get the static content FO node from the flow map.
  178. * This gets the static content flow for the given flow name.
  179. *
  180. * @param name the flow name to find
  181. * @return the static content FO node
  182. */
  183. public StaticContent getStaticContent(String name) {
  184. return (StaticContent) flowMap.get(name);
  185. }
  186. /**
  187. * Accessor method for the fo:title associated with this fo:page-sequence
  188. * @return titleFO for this object
  189. */
  190. public Title getTitleFO() {
  191. return titleFO;
  192. }
  193. /**
  194. * Public accessor for getting the MainFlow to which this PageSequence is
  195. * attached.
  196. * @return the MainFlow object to which this PageSequence is attached.
  197. */
  198. public Flow getMainFlow() {
  199. return mainFlow;
  200. }
  201. /**
  202. * Determine if this PageSequence already has a flow with the given flow-name
  203. * Used for validation of incoming fo:flow or fo:static-content objects
  204. * @param flowName The flow-name to search for
  205. * @return true if flow-name already defined within this page sequence,
  206. * false otherwise
  207. */
  208. public boolean hasFlowName(String flowName) {
  209. return flowMap.containsKey(flowName);
  210. }
  211. /** @return the flow map for this page-sequence */
  212. public Map<String, Flow> getFlowMap() {
  213. return this.flowMap;
  214. }
  215. /**
  216. * Public accessor for determining the next page master to use within this page sequence.
  217. * @param page the page number of the page to be created
  218. * @param isFirstPage indicator whether this page is the first page of the
  219. * page sequence
  220. * @param isLastPage indicator whether this page is the last page of the
  221. * page sequence
  222. * @param isBlank indicator whether the page will be blank
  223. * @return the SimplePageMaster to use for this page
  224. * @throws PageProductionException if there's a problem determining the page master
  225. */
  226. public SimplePageMaster getNextSimplePageMaster
  227. (int page, boolean isFirstPage, boolean isLastPage, boolean isBlank)
  228. throws PageProductionException {
  229. if (pageSequenceMaster == null) {
  230. return simplePageMaster;
  231. }
  232. boolean isOddPage = ((page % 2) == 1);
  233. if (log.isDebugEnabled()) {
  234. log.debug("getNextSimplePageMaster(page=" + page
  235. + " isOdd=" + isOddPage
  236. + " isFirst=" + isFirstPage
  237. + " isLast=" + isLastPage
  238. + " isBlank=" + isBlank + ")");
  239. }
  240. return pageSequenceMaster.getNextSimplePageMaster(isOddPage,
  241. isFirstPage, isLastPage, isBlank);
  242. }
  243. /**
  244. * Used to set the "cursor position" for the page masters to the previous item.
  245. * @return true if there is a previous item, false if the current one was the first one.
  246. */
  247. public boolean goToPreviousSimplePageMaster() {
  248. return pageSequenceMaster == null || pageSequenceMaster.goToPreviousSimplePageMaster();
  249. }
  250. /** @return true if the page-sequence has a page-master with page-position="last" */
  251. public boolean hasPagePositionLast() {
  252. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionLast();
  253. }
  254. /** @return true if the page-sequence has a page-master with page-position="only" */
  255. public boolean hasPagePositionOnly() {
  256. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionOnly();
  257. }
  258. /**
  259. * Get the value of the <code>master-reference</code> property.
  260. * @return the "master-reference" property
  261. */
  262. public String getMasterReference() {
  263. return masterReference;
  264. }
  265. /** {@inheritDoc} */
  266. public String getLocalName() {
  267. return "page-sequence";
  268. }
  269. /**
  270. * {@inheritDoc}
  271. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE}
  272. */
  273. public int getNameId() {
  274. return FO_PAGE_SEQUENCE;
  275. }
  276. /**
  277. * Get the value of the <code>country</code> property.
  278. * @return the country property value
  279. */
  280. public String getCountry() {
  281. return this.country;
  282. }
  283. /**
  284. * Get the value of the <code>language</code> property.
  285. * @return the language property value
  286. */
  287. public String getLanguage() {
  288. return this.language;
  289. }
  290. /**
  291. * Releases a page-sequence's children after the page-sequence has been fully processed.
  292. */
  293. public void releasePageSequence() {
  294. this.mainFlow = null;
  295. this.flowMap.clear();
  296. }
  297. }