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

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