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

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