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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 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();
  84. this.simplePageMaster = getRoot().getLayoutMasterSet().getSimplePageMaster(masterReference);
  85. if (this.simplePageMaster == null) {
  86. this.pageSequenceMaster
  87. = getRoot().getLayoutMasterSet().getPageSequenceMaster(masterReference);
  88. if (this.pageSequenceMaster == null) {
  89. getFOValidationEventProducer().masterNotFound(this, getName(),
  90. masterReference, getLocator());
  91. }
  92. }
  93. getFOEventHandler().startPageSequence(this);
  94. }
  95. /** {@inheritDoc} */
  96. protected void endOfNode() throws FOPException {
  97. if (mainFlow == null) {
  98. missingChildElementError("(title?,static-content*,flow)");
  99. }
  100. getFOEventHandler().endPageSequence(this);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. XSL Content Model: (title?,static-content*,flow)
  105. */
  106. protected void validateChildNode(Locator loc, String nsURI, String localName)
  107. throws ValidationException {
  108. if (FO_URI.equals(nsURI)) {
  109. if (localName.equals("title")) {
  110. if (titleFO != null) {
  111. tooManyNodesError(loc, "fo:title");
  112. } else if (flowMap.size() > 0) {
  113. nodesOutOfOrderError(loc, "fo:title", "fo:static-content");
  114. } else if (mainFlow != null) {
  115. nodesOutOfOrderError(loc, "fo:title", "fo:flow");
  116. }
  117. } else if (localName.equals("static-content")) {
  118. if (mainFlow != null) {
  119. nodesOutOfOrderError(loc, "fo:static-content", "fo:flow");
  120. }
  121. } else if (localName.equals("flow")) {
  122. if (mainFlow != null) {
  123. tooManyNodesError(loc, "fo:flow");
  124. }
  125. } else {
  126. invalidChildError(loc, nsURI, localName);
  127. }
  128. }
  129. }
  130. /**
  131. * {@inheritDoc}
  132. * @todo see if addChildNode() should also be called for fo's other than
  133. * fo:flow.
  134. */
  135. public void addChildNode(FONode child) throws FOPException {
  136. int childId = child.getNameId();
  137. if (childId == FO_TITLE) {
  138. this.titleFO = (Title) child;
  139. } else if (childId == FO_FLOW) {
  140. this.mainFlow = (Flow) child;
  141. addFlow(mainFlow);
  142. } else if (childId == FO_STATIC_CONTENT) {
  143. addFlow((StaticContent) child);
  144. String flowName = ((StaticContent) child).getFlowName();
  145. flowMap.put(flowName, child);
  146. }
  147. }
  148. /**
  149. * Add a flow or static content, mapped by its flow-name.
  150. * The flow-name is used to associate the flow with a region on a page,
  151. * based on the region-names given to the regions in the page-master
  152. * used to generate that page.
  153. * @param flow the {@link Flow} instance to be added
  154. * @throws org.apache.fop.fo.ValidationException if the fo:flow maps
  155. * to an invalid page-region
  156. */
  157. private void addFlow(Flow flow) throws ValidationException {
  158. String flowName = flow.getFlowName();
  159. if (hasFlowName(flowName)) {
  160. getFOValidationEventProducer().duplicateFlowNameInPageSequence(this, flow.getName(),
  161. flowName, flow.getLocator());
  162. }
  163. if (!getRoot().getLayoutMasterSet().regionNameExists(flowName)
  164. && !flowName.equals("xsl-before-float-separator")
  165. && !flowName.equals("xsl-footnote-separator")) {
  166. getFOValidationEventProducer().flowNameNotMapped(this, flow.getName(),
  167. flowName, flow.getLocator());
  168. }
  169. }
  170. /**
  171. * Get the static content FO node from the flow map.
  172. * This gets the static content flow for the given flow name.
  173. *
  174. * @param name the flow name to find
  175. * @return the static content FO node
  176. */
  177. public StaticContent getStaticContent(String name) {
  178. return (StaticContent) flowMap.get(name);
  179. }
  180. /**
  181. * Accessor method for the fo:title associated with this fo:page-sequence
  182. * @return titleFO for this object
  183. */
  184. public Title getTitleFO() {
  185. return titleFO;
  186. }
  187. /**
  188. * Public accessor for getting the MainFlow to which this PageSequence is
  189. * attached.
  190. * @return the MainFlow object to which this PageSequence is attached.
  191. */
  192. public Flow getMainFlow() {
  193. return mainFlow;
  194. }
  195. /**
  196. * Determine if this PageSequence already has a flow with the given flow-name
  197. * Used for validation of incoming fo:flow or fo:static-content objects
  198. * @param flowName The flow-name to search for
  199. * @return true if flow-name already defined within this page sequence,
  200. * false otherwise
  201. */
  202. public boolean hasFlowName(String flowName) {
  203. return flowMap.containsKey(flowName);
  204. }
  205. /** @return the flow map for this page-sequence */
  206. public Map getFlowMap() {
  207. return this.flowMap;
  208. }
  209. /**
  210. * Public accessor for determining the next page master to use within this page sequence.
  211. * @param page the page number of the page to be created
  212. * @param isFirstPage indicator whether this page is the first page of the
  213. * page sequence
  214. * @param isLastPage indicator whether this page is the last page of the
  215. * page sequence
  216. * @param isOnlyPage indicator whether this page is the only page of the
  217. * page sequence
  218. * @param isBlank indicator whether the page will be blank
  219. * @return the SimplePageMaster to use for this page
  220. * @throws PageProductionException if there's a problem determining the page master
  221. */
  222. public SimplePageMaster getNextSimplePageMaster(int page,
  223. boolean isFirstPage,
  224. boolean isLastPage,
  225. boolean isOnlyPage,
  226. boolean isBlank) throws PageProductionException {
  227. if (pageSequenceMaster == null) {
  228. return simplePageMaster;
  229. }
  230. boolean isOddPage = ((page % 2) == 1);
  231. if (log.isDebugEnabled()) {
  232. log.debug("getNextSimplePageMaster(page=" + page
  233. + " isOdd=" + isOddPage
  234. + " isFirst=" + isFirstPage
  235. + " isLast=" + isLastPage
  236. + " isOnly=" + isOnlyPage
  237. + " isBlank=" + isBlank + ")");
  238. }
  239. return pageSequenceMaster.getNextSimplePageMaster(isOddPage,
  240. isFirstPage, isLastPage, isOnlyPage, isBlank);
  241. }
  242. /**
  243. * Used to set the "cursor position" for the page masters to the previous item.
  244. * @return true if there is a previous item, false if the current one was the first one.
  245. */
  246. public boolean goToPreviousSimplePageMaster() {
  247. if (pageSequenceMaster == null) {
  248. return true;
  249. } else {
  250. return pageSequenceMaster.goToPreviousSimplePageMaster();
  251. }
  252. }
  253. /** @return true if the page-sequence has a page-master with page-position="last" */
  254. public boolean hasPagePositionLast() {
  255. if (pageSequenceMaster == null) {
  256. return false;
  257. } else {
  258. return pageSequenceMaster.hasPagePositionLast();
  259. }
  260. }
  261. /** @return true if the page-sequence has a page-master with page-position="only" */
  262. public boolean hasPagePositionOnly() {
  263. if (pageSequenceMaster == null) {
  264. return false;
  265. } else {
  266. return pageSequenceMaster.hasPagePositionOnly();
  267. }
  268. }
  269. /**
  270. * Get the value of the <code>master-reference</code> property.
  271. * @return the "master-reference" property
  272. */
  273. public String getMasterReference() {
  274. return masterReference;
  275. }
  276. /** {@inheritDoc} */
  277. public String getLocalName() {
  278. return "page-sequence";
  279. }
  280. /**
  281. * {@inheritDoc}
  282. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE}
  283. */
  284. public int getNameId() {
  285. return FO_PAGE_SEQUENCE;
  286. }
  287. /**
  288. * Get the value of the <code>country</code> property.
  289. * @return the country property value
  290. */
  291. public String getCountry() {
  292. return this.country;
  293. }
  294. /**
  295. * Get the value of the <code>language</code> property.
  296. * @return the language property value
  297. */
  298. public String getLanguage() {
  299. return this.language;
  300. }
  301. /**
  302. * Releases a page-sequence's children after the page-sequence has been fully processed.
  303. */
  304. public void releasePageSequence() {
  305. this.mainFlow = null;
  306. this.flowMap.clear();
  307. }
  308. }