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.

Root.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.List;
  21. import java.util.Locale;
  22. import org.xml.sax.Locator;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.fo.FOEventHandler;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.FOTreeBuilderContext;
  27. import org.apache.fop.fo.FObj;
  28. import org.apache.fop.fo.PropertyList;
  29. import org.apache.fop.fo.ValidationException;
  30. import org.apache.fop.fo.extensions.destination.Destination;
  31. import org.apache.fop.fo.pagination.bookmarks.BookmarkTree;
  32. import org.apache.fop.fo.properties.CommonAccessibility;
  33. import org.apache.fop.fo.properties.CommonAccessibilityHolder;
  34. import org.apache.fop.fo.properties.CommonHyphenation;
  35. /**
  36. * Class modeling the <a href="http://www.w3.org/TR/xsl/#fo_root">
  37. * <code>fo:root</code></a> formatting object.
  38. * Contains page masters, page-sequences.
  39. */
  40. public class Root extends FObj implements CommonAccessibilityHolder {
  41. private CommonAccessibility commonAccessibility;
  42. private int mediaUsage;
  43. private LayoutMasterSet layoutMasterSet;
  44. private Declarations declarations;
  45. private BookmarkTree bookmarkTree;
  46. private List<Destination> destinationList;
  47. private List<PageSequence> pageSequences;
  48. private Locale locale;
  49. // temporary until above list populated
  50. private boolean pageSequenceFound;
  51. /**
  52. * Keeps count of page number from over PageSequence instances
  53. */
  54. private int endingPageNumberOfPreviousSequence;
  55. private int totalPagesGenerated;
  56. /**
  57. * Context class used while building the FO tree.
  58. */
  59. private FOTreeBuilderContext builderContext;
  60. /**
  61. * FOEventHandler object for this FO Tree
  62. */
  63. private FOEventHandler foEventHandler;
  64. private PageSequence lastSeq;
  65. public void setLastSeq(PageSequence seq) {
  66. lastSeq = seq;
  67. }
  68. public PageSequence getLastSeq() {
  69. return lastSeq;
  70. }
  71. /**
  72. * Base constructor
  73. *
  74. * @param parent {@link FONode} that is the parent of this object
  75. * Note: parent should be null for the fo:root.
  76. */
  77. public Root(FONode parent) {
  78. super(parent);
  79. pageSequences = new java.util.ArrayList<PageSequence>();
  80. }
  81. /** {@inheritDoc} */
  82. public void bind(PropertyList pList) throws FOPException {
  83. super.bind(pList);
  84. commonAccessibility = CommonAccessibility.getInstance(pList);
  85. mediaUsage = pList.get(PR_MEDIA_USAGE).getEnum();
  86. String language = pList.get(PR_LANGUAGE).getString();
  87. String country = pList.get(PR_COUNTRY).getString();
  88. locale = CommonHyphenation.toLocale(language, country);
  89. }
  90. /** {@inheritDoc} */
  91. public void startOfNode() throws FOPException {
  92. foEventHandler.startRoot(this);
  93. }
  94. /** {@inheritDoc} */
  95. public void endOfNode() throws FOPException {
  96. if (!pageSequenceFound || layoutMasterSet == null) {
  97. missingChildElementError("(layout-master-set, declarations?, "
  98. + "bookmark-tree?, (page-sequence|fox:external-document)+)");
  99. }
  100. foEventHandler.endRoot(this);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. * <br>XSL 1.0 Spec: (layout-master-set,declarations?,page-sequence+)
  105. * <br>FOP: (layout-master-set, declarations?, fox:bookmarks?, page-sequence+)
  106. */
  107. protected void validateChildNode(Locator loc, String nsURI, String localName)
  108. throws ValidationException {
  109. if (FO_URI.equals(nsURI)) {
  110. if (localName.equals("layout-master-set")) {
  111. if (layoutMasterSet != null) {
  112. tooManyNodesError(loc, "fo:layout-master-set");
  113. }
  114. } else if (localName.equals("declarations")) {
  115. if (layoutMasterSet == null) {
  116. nodesOutOfOrderError(loc, "fo:layout-master-set", "fo:declarations");
  117. } else if (declarations != null) {
  118. tooManyNodesError(loc, "fo:declarations");
  119. } else if (bookmarkTree != null && getUserAgent().validateStrictly()) {
  120. nodesOutOfOrderError(loc, "fo:declarations", "fo:bookmark-tree");
  121. } else if (pageSequenceFound) {
  122. nodesOutOfOrderError(loc, "fo:declarations", "fo:page-sequence");
  123. }
  124. } else if (localName.equals("bookmark-tree")) {
  125. if (layoutMasterSet == null) {
  126. nodesOutOfOrderError(loc, "fo:layout-master-set", "fo:bookmark-tree");
  127. } else if (bookmarkTree != null) {
  128. tooManyNodesError(loc, "fo:bookmark-tree");
  129. } else if (pageSequenceFound) {
  130. nodesOutOfOrderError(loc, "fo:bookmark-tree", "fo:page-sequence");
  131. }
  132. } else if (localName.equals("page-sequence")) {
  133. if (layoutMasterSet == null) {
  134. nodesOutOfOrderError(loc, "fo:layout-master-set", "fo:page-sequence");
  135. } else {
  136. pageSequenceFound = true;
  137. }
  138. } else {
  139. invalidChildError(loc, nsURI, localName);
  140. }
  141. } else {
  142. if (FOX_URI.equals(nsURI)) {
  143. if ("external-document".equals(localName)) {
  144. pageSequenceFound = true;
  145. }
  146. }
  147. //invalidChildError(loc, nsURI, localName);
  148. //Ignore non-FO elements under root
  149. }
  150. }
  151. /**
  152. * @param loc location in the source file
  153. * @param child the {@link FONode} to validate against
  154. * @throws ValidationException if the incoming node is not a valid child for the given FO
  155. */
  156. protected void validateChildNode(Locator loc, FONode child) throws ValidationException {
  157. if (child instanceof AbstractPageSequence) {
  158. pageSequenceFound = true;
  159. }
  160. }
  161. /** {@inheritDoc} */
  162. public CommonAccessibility getCommonAccessibility() {
  163. return commonAccessibility;
  164. }
  165. /**
  166. * Sets the FOEventHandler object that this Root is attached to
  167. * @param foEventHandler the FOEventHandler object
  168. */
  169. public void setFOEventHandler(FOEventHandler foEventHandler) {
  170. this.foEventHandler = foEventHandler;
  171. }
  172. /**
  173. * This method overrides the FONode version. The FONode version calls the
  174. * method by the same name for the parent object. Since Root is at the top
  175. * of the tree, it returns the actual FOEventHandler object. Thus, any FONode
  176. * can use this chain to find which FOEventHandler it is being built for.
  177. * @return the FOEventHandler implementation that this Root is attached to
  178. */
  179. public FOEventHandler getFOEventHandler() {
  180. return foEventHandler;
  181. }
  182. /**
  183. * Sets the builder context for this FO tree.
  184. * @param context the builder context to be used
  185. */
  186. public void setBuilderContext(FOTreeBuilderContext context) {
  187. this.builderContext = context;
  188. }
  189. /** {@inheritDoc} */
  190. public FOTreeBuilderContext getBuilderContext() {
  191. return this.builderContext;
  192. }
  193. /**
  194. * Gets the last page number generated by the previous page-sequence
  195. * @return the last page number, 0 if no page sequences yet generated
  196. */
  197. public int getEndingPageNumberOfPreviousSequence() {
  198. return endingPageNumberOfPreviousSequence;
  199. }
  200. /**
  201. * Returns the total number of pages generated by FOP
  202. * (May not equal endingPageNumberOfPreviousSequence due to
  203. * initial-page-number property on fo:page-sequences.)
  204. * @return the last page number, 0 if no page sequences yet generated
  205. */
  206. public int getTotalPagesGenerated() {
  207. return totalPagesGenerated;
  208. }
  209. /**
  210. * Notify additional pages generated to increase the totalPagesGenerated counter
  211. * @param lastPageNumber the last page number generated by the sequence
  212. * @param additionalPages the total pages generated by the sequence (for statistics)
  213. * @throws IllegalArgumentException for negative additional page counts
  214. */
  215. public void notifyPageSequenceFinished(int lastPageNumber, int additionalPages)
  216. throws IllegalArgumentException {
  217. if (additionalPages >= 0) {
  218. totalPagesGenerated += additionalPages;
  219. endingPageNumberOfPreviousSequence = lastPageNumber;
  220. } else {
  221. throw new IllegalArgumentException(
  222. "Number of additional pages must be zero or greater.");
  223. }
  224. }
  225. /**
  226. * Returns the number of PageSequence instances.
  227. * @return the number of PageSequence instances
  228. */
  229. public int getPageSequenceCount() {
  230. return pageSequences.size();
  231. }
  232. /**
  233. * Some properties, such as 'force-page-count', require a
  234. * page-sequence to know about some properties of the next.
  235. * @param current the current PageSequence
  236. * @return succeeding PageSequence; null if none
  237. */
  238. public PageSequence getSucceedingPageSequence(PageSequence current) {
  239. int currentIndex = pageSequences.indexOf(current);
  240. if (currentIndex == -1) {
  241. return null;
  242. }
  243. if (currentIndex < (pageSequences.size() - 1)) {
  244. return pageSequences.get(currentIndex + 1);
  245. } else {
  246. return null;
  247. }
  248. }
  249. /**
  250. * Adds the specified page sequence.
  251. *
  252. * @param pageSequence The page sequence to add
  253. */
  254. public void addPageSequence(PageSequence pageSequence) {
  255. pageSequences.add(pageSequence);
  256. }
  257. /**
  258. * Returns the last page sequence (current while parsing).
  259. *
  260. * @return The last page sequence or null
  261. */
  262. public PageSequence getLastPageSequence() {
  263. if (getPageSequenceCount() > 0) {
  264. return pageSequences.get(getPageSequenceCount() - 1);
  265. } else {
  266. return null;
  267. }
  268. }
  269. /**
  270. * Returns the associated LayoutMasterSet.
  271. * @return the LayoutMasterSet instance
  272. */
  273. public LayoutMasterSet getLayoutMasterSet() {
  274. return this.layoutMasterSet;
  275. }
  276. /**
  277. * Sets the associated LayoutMasterSet.
  278. * @param layoutMasterSet the LayoutMasterSet to use
  279. */
  280. public void setLayoutMasterSet(LayoutMasterSet layoutMasterSet) {
  281. this.layoutMasterSet = layoutMasterSet;
  282. }
  283. /**
  284. * Returns the associated Declarations.
  285. * @return the Declarations instance
  286. */
  287. public Declarations getDeclarations() {
  288. return this.declarations;
  289. }
  290. /**
  291. * Sets the associated Declarations.
  292. * @param declarations the Declarations to use
  293. */
  294. public void setDeclarations(Declarations declarations) {
  295. this.declarations = declarations;
  296. }
  297. /**
  298. * Set the BookmarkTree object for this FO
  299. * @param bookmarkTree the BookmarkTree object
  300. */
  301. public void setBookmarkTree(BookmarkTree bookmarkTree) {
  302. this.bookmarkTree = bookmarkTree;
  303. }
  304. /**
  305. * Add a Destination object to this FO
  306. * @param destination the Destination object to add
  307. */
  308. public void addDestination(Destination destination) {
  309. if (destinationList == null) {
  310. destinationList = new java.util.ArrayList<Destination>();
  311. }
  312. destinationList.add(destination);
  313. }
  314. /**
  315. * Public accessor for the list of Destination objects for this FO
  316. * @return the Destination object
  317. */
  318. public List getDestinationList() {
  319. return destinationList;
  320. }
  321. /**
  322. * Public accessor for the BookmarkTree object for this FO
  323. * @return the BookmarkTree object
  324. */
  325. public BookmarkTree getBookmarkTree() {
  326. return bookmarkTree;
  327. }
  328. /** {@inheritDoc} */
  329. public Root getRoot() {
  330. return this;
  331. }
  332. /** {@inheritDoc} */
  333. public String getLocalName() {
  334. return "root";
  335. }
  336. /**
  337. * {@inheritDoc}
  338. * @return {@link org.apache.fop.fo.Constants#FO_ROOT}
  339. */
  340. public int getNameId() {
  341. return FO_ROOT;
  342. }
  343. /** @return locale proprty. */
  344. public Locale getLocale() {
  345. return locale;
  346. }
  347. }