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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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;
  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 (!hasRegion(flowName) && !flowName.equals("xsl-before-float-separator")
  180. && !flowName.equals("xsl-footnote-separator")) {
  181. getFOValidationEventProducer().flowNameNotMapped(this, flow.getName(),
  182. flowName, flow.getLocator());
  183. }
  184. }
  185. private boolean hasRegion(String flowName) {
  186. LayoutMasterSet set = getRoot().getLayoutMasterSet();
  187. PageSequenceMaster psm = set.getPageSequenceMaster(masterReference);
  188. return (psm != null) ? psm.getLayoutMasterSet().regionNameExists(flowName)
  189. : set.getSimplePageMaster(masterReference).regionNameExists(flowName);
  190. }
  191. /**
  192. * Get the static content FO node from the flow map.
  193. * This gets the static content flow for the given flow name.
  194. *
  195. * @param name the flow name to find
  196. * @return the static content FO node
  197. */
  198. public StaticContent getStaticContent(String name) {
  199. return (StaticContent) flowMap.get(name);
  200. }
  201. /**
  202. * Accessor method for the fo:title associated with this fo:page-sequence
  203. * @return titleFO for this object
  204. */
  205. public Title getTitleFO() {
  206. return titleFO;
  207. }
  208. /**
  209. * Public accessor for getting the MainFlow to which this PageSequence is
  210. * attached.
  211. * @return the MainFlow object to which this PageSequence is attached.
  212. */
  213. public Flow getMainFlow() {
  214. return mainFlow;
  215. }
  216. /**
  217. * Determine if this PageSequence already has a flow with the given flow-name
  218. * Used for validation of incoming fo:flow or fo:static-content objects
  219. * @param flowName The flow-name to search for
  220. * @return true if flow-name already defined within this page sequence,
  221. * false otherwise
  222. */
  223. public boolean hasFlowName(String flowName) {
  224. return flowMap.containsKey(flowName);
  225. }
  226. /** @return the flow map for this page-sequence */
  227. public Map<String, FONode> getFlowMap() {
  228. return this.flowMap;
  229. }
  230. /**
  231. * Public accessor for determining the next page master to use within this page sequence.
  232. * @param page the page number of the page to be created
  233. * @param isFirstPage indicator whether this page is the first page of the
  234. * page sequence
  235. * @param isLastPage indicator whether this page is the last page of the
  236. * page sequence
  237. * @param isBlank indicator whether the page will be blank
  238. * @return the SimplePageMaster to use for this page
  239. * @throws PageProductionException if there's a problem determining the page master
  240. */
  241. public SimplePageMaster getNextSimplePageMaster(
  242. int page, boolean isFirstPage, boolean isLastPage, boolean isBlank)
  243. throws PageProductionException {
  244. if (pageSequenceMaster == null) {
  245. return simplePageMaster;
  246. }
  247. boolean isOddPage = ((page % 2) != 0);
  248. if (log.isDebugEnabled()) {
  249. log.debug("getNextSimplePageMaster(page=" + page
  250. + " isOdd=" + isOddPage
  251. + " isFirst=" + isFirstPage
  252. + " isLast=" + isLastPage
  253. + " isBlank=" + isBlank + ")");
  254. }
  255. return pageSequenceMaster.getNextSimplePageMaster(isOddPage,
  256. isFirstPage, isLastPage, isBlank, getMainFlow().getFlowName());
  257. }
  258. /**
  259. * Used to set the "cursor position" for the page masters to the previous item.
  260. * @return true if there is a previous item, false if the current one was the first one.
  261. */
  262. public boolean goToPreviousSimplePageMaster() {
  263. return pageSequenceMaster == null || pageSequenceMaster.goToPreviousSimplePageMaster();
  264. }
  265. /** @return true if the page-sequence has a page-master with page-position="last" */
  266. public boolean hasPagePositionLast() {
  267. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionLast();
  268. }
  269. /** @return true if the page-sequence has a page-master with page-position="only" */
  270. public boolean hasPagePositionOnly() {
  271. return pageSequenceMaster != null && pageSequenceMaster.hasPagePositionOnly();
  272. }
  273. /**
  274. * Get the value of the <code>master-reference</code> trait.
  275. * @return the "master-reference" trait
  276. */
  277. public String getMasterReference() {
  278. return masterReference;
  279. }
  280. /** {@inheritDoc} */
  281. public String getLocalName() {
  282. return "page-sequence";
  283. }
  284. /**
  285. * {@inheritDoc}
  286. * @return {@link org.apache.fop.fo.Constants#FO_PAGE_SEQUENCE}
  287. */
  288. public int getNameId() {
  289. return FO_PAGE_SEQUENCE;
  290. }
  291. public Locale getLocale() {
  292. return locale;
  293. }
  294. /**
  295. * Get the value of the <code>reference-orientation</code> trait.
  296. * @return the reference orientation trait value
  297. */
  298. public int getReferenceOrientation() {
  299. if (referenceOrientation != null) {
  300. return referenceOrientation.getValue();
  301. } else {
  302. return 0;
  303. }
  304. }
  305. /**
  306. * {@inheritDoc}
  307. */
  308. public Direction getInlineProgressionDirection() {
  309. if (writingModeTraits != null) {
  310. return writingModeTraits.getInlineProgressionDirection();
  311. } else {
  312. return Direction.LR;
  313. }
  314. }
  315. /**
  316. * {@inheritDoc}
  317. */
  318. public Direction getBlockProgressionDirection() {
  319. if (writingModeTraits != null) {
  320. return writingModeTraits.getBlockProgressionDirection();
  321. } else {
  322. return Direction.TB;
  323. }
  324. }
  325. /**
  326. * {@inheritDoc}
  327. */
  328. public Direction getColumnProgressionDirection() {
  329. if (writingModeTraits != null) {
  330. return writingModeTraits.getColumnProgressionDirection();
  331. } else {
  332. return Direction.LR;
  333. }
  334. }
  335. /**
  336. * {@inheritDoc}
  337. */
  338. public Direction getRowProgressionDirection() {
  339. if (writingModeTraits != null) {
  340. return writingModeTraits.getRowProgressionDirection();
  341. } else {
  342. return Direction.TB;
  343. }
  344. }
  345. /**
  346. * {@inheritDoc}
  347. */
  348. public Direction getShiftDirection() {
  349. if (writingModeTraits != null) {
  350. return writingModeTraits.getShiftDirection();
  351. } else {
  352. return Direction.TB;
  353. }
  354. }
  355. /**
  356. * {@inheritDoc}
  357. */
  358. public WritingMode getWritingMode() {
  359. if (writingModeTraits != null) {
  360. return writingModeTraits.getWritingMode();
  361. } else {
  362. return WritingMode.LR_TB;
  363. }
  364. }
  365. /**
  366. * {@inheritDoc}
  367. */
  368. public boolean getExplicitWritingMode() {
  369. if (writingModeTraits != null) {
  370. return writingModeTraits.getExplicitWritingMode();
  371. } else {
  372. return false;
  373. }
  374. }
  375. @Override
  376. protected Stack<DelimitedTextRange> collectDelimitedTextRanges(Stack<DelimitedTextRange> ranges,
  377. DelimitedTextRange currentRange) {
  378. // collect ranges from static content flows
  379. Map<String, FONode> flows = getFlowMap();
  380. if (flows != null) {
  381. for (FONode fn : flows.values()) {
  382. if (fn instanceof StaticContent) {
  383. ranges = ((StaticContent) fn).collectDelimitedTextRanges(ranges);
  384. }
  385. }
  386. }
  387. // collect ranges in main flow
  388. Flow main = getMainFlow();
  389. if (main != null) {
  390. ranges = main.collectDelimitedTextRanges(ranges);
  391. }
  392. return ranges;
  393. }
  394. @Override
  395. protected boolean isBidiBoundary(boolean propagate) {
  396. return true;
  397. }
  398. /**
  399. * Releases a page-sequence's children after the page-sequence has been fully processed.
  400. */
  401. public void releasePageSequence() {
  402. this.mainFlow = null;
  403. this.flowMap.clear();
  404. }
  405. public SimplePageMaster getLastSimplePageMaster(int page, boolean isFirstPage, boolean isBlank) {
  406. boolean isOddPage = ((page % 2) != 0); // please findbugs...
  407. log.debug("getNextSimplePageMaster(page=" + page + " isOdd=" + isOddPage + " isFirst="
  408. + isFirstPage + " isLast=true" + " isBlank=" + isBlank + ")");
  409. if (pageSequenceMaster == null) {
  410. return simplePageMaster;
  411. }
  412. return pageSequenceMaster.getLastSimplePageMaster(isOddPage, isFirstPage, isBlank, getMainFlow()
  413. .getFlowName());
  414. }
  415. }