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

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