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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. import java.util.Locale;
  20. import java.util.Map;
  21. import java.util.Stack;
  22. import org.xml.sax.Locator;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.complexscripts.bidi.DelimitedTextRange;
  25. import org.apache.fop.datatypes.Numeric;
  26. import org.apache.fop.fo.FONode;
  27. import org.apache.fop.fo.PropertyList;
  28. import org.apache.fop.fo.ValidationException;
  29. import org.apache.fop.fo.properties.CommonHyphenation;
  30. import org.apache.fop.traits.Direction;
  31. import org.apache.fop.traits.WritingMode;
  32. import org.apache.fop.traits.WritingModeTraits;
  33. import org.apache.fop.traits.WritingModeTraitsGetter;
  34. /**
  35. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_page-sequence">
  36. * <code>fo:page-sequence</code></a> object.
  37. */
  38. public class PageSequence extends AbstractPageSequence implements WritingModeTraitsGetter {
  39. private String masterReference;
  40. private Numeric referenceOrientation;
  41. private WritingModeTraits writingModeTraits;
  42. private Locale locale;
  43. // There doesn't seem to be anything in the spec requiring flows
  44. // to be in the order given, only that they map to the regions
  45. // defined in the page sequence, so all we need is this one hashmap
  46. // the set of flows includes StaticContent flows also
  47. /** Map of flows to their flow name (flow-name, Flow) */
  48. private Map<String, FONode> flowMap;
  49. /**
  50. * The currentSimplePageMaster is either the page master for the
  51. * whole page sequence if master-reference refers to a simple-page-master,
  52. * or the simple page master produced by the page sequence master otherwise.
  53. * The pageSequenceMaster is null if master-reference refers to a
  54. * simple-page-master.
  55. */
  56. private SimplePageMaster simplePageMaster;
  57. private PageSequenceMaster pageSequenceMaster;
  58. /**
  59. * The fo:title object for this page-sequence.
  60. */
  61. private Title titleFO;
  62. /**
  63. * The fo:flow object for this page-sequence.
  64. */
  65. private Flow mainFlow = null;
  66. /**
  67. * Create a PageSequence instance that is a child of the
  68. * given {@link FONode}.
  69. *
  70. * @param parent the parent {@link FONode}
  71. */
  72. public PageSequence(FONode parent) {
  73. super(parent);
  74. }
  75. /** {@inheritDoc} */
  76. public void bind(PropertyList pList) throws FOPException {
  77. super.bind(pList);
  78. String country = pList.get(PR_COUNTRY).getString();
  79. String language = pList.get(PR_LANGUAGE).getString();
  80. locale = CommonHyphenation.toLocale(language, country);
  81. masterReference = pList.get(PR_MASTER_REFERENCE).getString();
  82. referenceOrientation = pList.get(PR_REFERENCE_ORIENTATION).getNumeric();
  83. writingModeTraits = new WritingModeTraits(
  84. WritingMode.valueOf(pList.get(PR_WRITING_MODE).getEnum()),
  85. pList.getExplicit(PR_WRITING_MODE) != null);
  86. if (masterReference == null || masterReference.equals("")) {
  87. missingPropertyError("master-reference");
  88. }
  89. }
  90. /** {@inheritDoc} */
  91. public void startOfNode() throws FOPException {
  92. super.startOfNode();
  93. flowMap = new java.util.HashMap<String, FONode>();
  94. this.simplePageMaster
  95. = getRoot().getLayoutMasterSet().getSimplePageMaster(masterReference);
  96. if (simplePageMaster == null) {
  97. this.pageSequenceMaster
  98. = getRoot().getLayoutMasterSet().getPageSequenceMaster(masterReference);
  99. if (pageSequenceMaster == null) {
  100. getFOValidationEventProducer().masterNotFound(this, getName(),
  101. masterReference, getLocator());
  102. }
  103. }
  104. getFOEventHandler().startPageSequence(this);
  105. }
  106. /** {@inheritDoc} */
  107. public void endOfNode() throws FOPException {
  108. if (mainFlow == null) {
  109. missingChildElementError("(title?,static-content*,flow)");
  110. }
  111. getFOEventHandler().endPageSequence(this);
  112. }
  113. /**
  114. * {@inheritDoc}
  115. XSL Content Model: (title?,static-content*,flow)
  116. */
  117. protected void validateChildNode(Locator loc, String nsURI, String localName)
  118. throws ValidationException {
  119. if (FO_URI.equals(nsURI)) {
  120. if ("title".equals(localName)) {
  121. if (titleFO != null) {
  122. tooManyNodesError(loc, "fo:title");
  123. } else if (!flowMap.isEmpty()) {
  124. nodesOutOfOrderError(loc, "fo:title", "fo:static-content");
  125. } else if (mainFlow != null) {
  126. nodesOutOfOrderError(loc, "fo:title", "fo:flow");
  127. }
  128. } else if ("static-content".equals(localName)) {
  129. if (mainFlow != null) {
  130. nodesOutOfOrderError(loc, "fo:static-content", "fo:flow");
  131. }
  132. } else if ("flow".equals(localName)) {
  133. if (mainFlow != null) {
  134. tooManyNodesError(loc, "fo:flow");
  135. }
  136. } else {
  137. invalidChildError(loc, nsURI, localName);
  138. }
  139. }
  140. }
  141. /**
  142. * {@inheritDoc}
  143. * TODO see if addChildNode() should also be called for fo's other than
  144. * fo:flow.
  145. */
  146. public void addChildNode(FONode child) throws FOPException {
  147. int childId = child.getNameId();
  148. switch (childId) {
  149. case FO_TITLE:
  150. this.titleFO = (Title)child;
  151. break;
  152. case FO_FLOW:
  153. this.mainFlow = (Flow)child;
  154. addFlow(mainFlow);
  155. break;
  156. case FO_STATIC_CONTENT:
  157. addFlow((StaticContent)child);
  158. flowMap.put(((Flow)child).getFlowName(), (Flow)child);
  159. break;
  160. default:
  161. super.addChildNode(child);
  162. }
  163. }
  164. /**
  165. * Add a flow or static content, mapped by its flow-name.
  166. * The flow-name is used to associate the flow with a region on a page,
  167. * based on the region-names given to the regions in the page-master
  168. * used to generate that page.
  169. * @param flow the {@link Flow} instance to be added
  170. * @throws org.apache.fop.fo.ValidationException if the fo:flow maps
  171. * to an invalid page-region
  172. */
  173. private void addFlow(Flow flow) throws ValidationException {
  174. String flowName = flow.getFlowName();
  175. if (hasFlowName(flowName)) {
  176. getFOValidationEventProducer().duplicateFlowNameInPageSequence(this, flow.getName(),
  177. flowName, flow.getLocator());
  178. }
  179. if (!getRoot().getLayoutMasterSet().regionNameExists(flowName)
  180. && !flowName.equals("xsl-before-float-separator")
  181. && !flowName.equals("xsl-footnote-separator")) {
  182. getFOValidationEventProducer().flowNameNotMapped(this, flow.getName(),
  183. flowName, flow.getLocator());
  184. }
  185. }
  186. /**
  187. * Get the static content FO node from the flow map.
  188. * This gets the static content flow for the given flow name.
  189. *
  190. * @param name the flow name to find
  191. * @return the static content FO node
  192. */
  193. public StaticContent getStaticContent(String name) {
  194. return (StaticContent) flowMap.get(name);
  195. }
  196. /**
  197. * Accessor method for the fo:title associated with this fo:page-sequence
  198. * @return titleFO for this object
  199. */
  200. public Title getTitleFO() {
  201. return titleFO;
  202. }
  203. /**
  204. * Public accessor for getting the MainFlow to which this PageSequence is
  205. * attached.
  206. * @return the MainFlow object to which this PageSequence is attached.
  207. */
  208. public Flow getMainFlow() {
  209. return mainFlow;
  210. }
  211. /**
  212. * Determine if this PageSequence already has a flow with the given flow-name
  213. * Used for validation of incoming fo:flow or fo:static-content objects
  214. * @param flowName The flow-name to search for
  215. * @return true if flow-name already defined within this page sequence,
  216. * false otherwise
  217. */
  218. public boolean hasFlowName(String flowName) {
  219. return flowMap.containsKey(flowName);
  220. }
  221. /** @return the flow map for this page-sequence */
  222. public Map<String, FONode> getFlowMap() {
  223. return this.flowMap;
  224. }
  225. /**
  226. * Public accessor for determining the next page master to use within this page sequence.
  227. * @param page the page number of the page to be created
  228. * @param isFirstPage indicator whether this page is the first page of the
  229. * page sequence
  230. * @param isLastPage indicator whether this page is the last page of the
  231. * page sequence
  232. * @param isBlank indicator whether the page will be blank
  233. * @return the SimplePageMaster to use for this page
  234. * @throws PageProductionException if there's a problem determining the page master
  235. */
  236. public SimplePageMaster getNextSimplePageMaster(
  237. int page, boolean isFirstPage, boolean isLastPage, boolean isBlank)
  238. throws PageProductionException {
  239. if (pageSequenceMaster == null) {
  240. return simplePageMaster;
  241. }
  242. boolean isOddPage = ((page % 2) == 1);
  243. if (log.isDebugEnabled()) {
  244. log.debug("getNextSimplePageMaster(page=" + page
  245. + " isOdd=" + isOddPage
  246. + " isFirst=" + isFirstPage
  247. + " isLast=" + isLastPage
  248. + " isBlank=" + isBlank + ")");
  249. }
  250. return pageSequenceMaster.getNextSimplePageMaster(isOddPage,
  251. isFirstPage, isLastPage, isBlank, getMainFlow().getFlowName());
  252. }
  253. /**
  254. * Used to set the "cursor position" for the page masters to the previous item.
  255. * @return true if there is a previous item, false if the current one was the first one.
  256. */
  257. public boolean goToPreviousSimplePageMaster() {
  258. return pageSequenceMaster == null || pageSequenceMaster.goToPreviousSimplePageMaster();
  259. }
  260. /** @return true if the page-sequence has a page-master with page-position="last" */
  261. public boolean hasPagePositionLast() {
  262. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionLast();
  263. }
  264. /** @return true if the page-sequence has a page-master with page-position="only" */
  265. public boolean hasPagePositionOnly() {
  266. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionOnly();
  267. }
  268. /**
  269. * Get the value of the <code>master-reference</code> trait.
  270. * @return the "master-reference" trait
  271. */
  272. public String getMasterReference() {
  273. return masterReference;
  274. }
  275. /** {@inheritDoc} */
  276. public String getLocalName() {
  277. return "page-sequence";
  278. }
  279. /**
  280. * {@inheritDoc}
  281. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE}
  282. */
  283. public int getNameId() {
  284. return FO_PAGE_SEQUENCE;
  285. }
  286. public Locale getLocale() {
  287. return locale;
  288. }
  289. /**
  290. * Get the value of the <code>reference-orientation</code> trait.
  291. * @return the reference orientation trait value
  292. */
  293. public int getReferenceOrientation() {
  294. if (referenceOrientation != null) {
  295. return referenceOrientation.getValue();
  296. } else {
  297. return 0;
  298. }
  299. }
  300. /**
  301. * {@inheritDoc}
  302. */
  303. public Direction getInlineProgressionDirection() {
  304. if (writingModeTraits != null) {
  305. return writingModeTraits.getInlineProgressionDirection();
  306. } else {
  307. return Direction.LR;
  308. }
  309. }
  310. /**
  311. * {@inheritDoc}
  312. */
  313. public Direction getBlockProgressionDirection() {
  314. if (writingModeTraits != null) {
  315. return writingModeTraits.getBlockProgressionDirection();
  316. } else {
  317. return Direction.TB;
  318. }
  319. }
  320. /**
  321. * {@inheritDoc}
  322. */
  323. public Direction getColumnProgressionDirection() {
  324. if (writingModeTraits != null) {
  325. return writingModeTraits.getColumnProgressionDirection();
  326. } else {
  327. return Direction.LR;
  328. }
  329. }
  330. /**
  331. * {@inheritDoc}
  332. */
  333. public Direction getRowProgressionDirection() {
  334. if (writingModeTraits != null) {
  335. return writingModeTraits.getRowProgressionDirection();
  336. } else {
  337. return Direction.TB;
  338. }
  339. }
  340. /**
  341. * {@inheritDoc}
  342. */
  343. public Direction getShiftDirection() {
  344. if (writingModeTraits != null) {
  345. return writingModeTraits.getShiftDirection();
  346. } else {
  347. return Direction.TB;
  348. }
  349. }
  350. /**
  351. * {@inheritDoc}
  352. */
  353. public WritingMode getWritingMode() {
  354. if (writingModeTraits != null) {
  355. return writingModeTraits.getWritingMode();
  356. } else {
  357. return WritingMode.LR_TB;
  358. }
  359. }
  360. /**
  361. * {@inheritDoc}
  362. */
  363. public boolean getExplicitWritingMode() {
  364. if (writingModeTraits != null) {
  365. return writingModeTraits.getExplicitWritingMode();
  366. } else {
  367. return false;
  368. }
  369. }
  370. @Override
  371. protected Stack collectDelimitedTextRanges(Stack ranges, DelimitedTextRange currentRange) {
  372. // collect ranges from static content flows
  373. Map<String, FONode> flows = getFlowMap();
  374. if (flows != null) {
  375. for (FONode fn : flows.values()) {
  376. if (fn instanceof StaticContent) {
  377. ranges = ((StaticContent) fn).collectDelimitedTextRanges(ranges);
  378. }
  379. }
  380. }
  381. // collect ranges in main flow
  382. Flow main = getMainFlow();
  383. if (main != null) {
  384. ranges = main.collectDelimitedTextRanges(ranges);
  385. }
  386. return ranges;
  387. }
  388. @Override
  389. protected boolean isBidiBoundary(boolean propagate) {
  390. return true;
  391. }
  392. /**
  393. * Releases a page-sequence's children after the page-sequence has been fully processed.
  394. */
  395. public void releasePageSequence() {
  396. this.mainFlow = null;
  397. this.flowMap.clear();
  398. }
  399. }