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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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. * Returns the associated LayoutMasterSet.
  251. * @return the LayoutMasterSet instance
  252. */
  253. public LayoutMasterSet getLayoutMasterSet() {
  254. return this.layoutMasterSet;
  255. }
  256. /**
  257. * Sets the associated LayoutMasterSet.
  258. * @param layoutMasterSet the LayoutMasterSet to use
  259. */
  260. public void setLayoutMasterSet(LayoutMasterSet layoutMasterSet) {
  261. this.layoutMasterSet = layoutMasterSet;
  262. }
  263. /**
  264. * Returns the associated Declarations.
  265. * @return the Declarations instance
  266. */
  267. public Declarations getDeclarations() {
  268. return this.declarations;
  269. }
  270. /**
  271. * Sets the associated Declarations.
  272. * @param declarations the Declarations to use
  273. */
  274. public void setDeclarations(Declarations declarations) {
  275. this.declarations = declarations;
  276. }
  277. /**
  278. * Set the BookmarkTree object for this FO
  279. * @param bookmarkTree the BookmarkTree object
  280. */
  281. public void setBookmarkTree(BookmarkTree bookmarkTree) {
  282. this.bookmarkTree = bookmarkTree;
  283. }
  284. /**
  285. * Add a Destination object to this FO
  286. * @param destination the Destination object to add
  287. */
  288. public void addDestination(Destination destination) {
  289. if (destinationList == null) {
  290. destinationList = new java.util.ArrayList<Destination>();
  291. }
  292. destinationList.add(destination);
  293. }
  294. /**
  295. * Public accessor for the list of Destination objects for this FO
  296. * @return the Destination object
  297. */
  298. public List getDestinationList() {
  299. return destinationList;
  300. }
  301. /**
  302. * Public accessor for the BookmarkTree object for this FO
  303. * @return the BookmarkTree object
  304. */
  305. public BookmarkTree getBookmarkTree() {
  306. return bookmarkTree;
  307. }
  308. /** {@inheritDoc} */
  309. public Root getRoot() {
  310. return this;
  311. }
  312. /** {@inheritDoc} */
  313. public String getLocalName() {
  314. return "root";
  315. }
  316. /**
  317. * {@inheritDoc}
  318. * @return {@link org.apache.fop.fo.Constants#FO_ROOT}
  319. */
  320. public int getNameId() {
  321. return FO_ROOT;
  322. }
  323. public Locale getLocale() {
  324. return locale;
  325. }
  326. }