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.

BookmarkData.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.area;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.apache.fop.fo.pagination.bookmarks.Bookmark;
  22. import org.apache.fop.fo.pagination.bookmarks.BookmarkTree;
  23. /**
  24. * An instance of this class is either a PDF bookmark-tree and
  25. * its child bookmark-items, or a bookmark-item and the child
  26. * child bookmark-items under it.
  27. */
  28. public class BookmarkData extends AbstractOffDocumentItem implements Resolvable {
  29. private List<BookmarkData> subData = new java.util.ArrayList<BookmarkData>();
  30. // bookmark-title for this fo:bookmark
  31. private String bookmarkTitle;
  32. // indicator of whether to initially display/hide child bookmarks of this object
  33. private boolean showChildren = true;
  34. // ID Reference for this bookmark
  35. private String idRef;
  36. // PageViewport that the idRef item refers to
  37. private PageViewport pageRef;
  38. // unresolved idrefs by this bookmark and child bookmarks below it
  39. private Map<String, List<Resolvable>> unresolvedIDRefs
  40. = new java.util.HashMap<String, List<Resolvable>>();
  41. /**
  42. * Create a new bookmark data object.
  43. * This should only be called by the bookmark-tree item because
  44. * it has no idref item that needs to be resolved.
  45. *
  46. * @param bookmarkTree fo:bookmark-tree for this document
  47. */
  48. public BookmarkData(BookmarkTree bookmarkTree) {
  49. this.idRef = null;
  50. this.whenToProcess = END_OF_DOC;
  51. // top level defined in Rec to show all child bookmarks
  52. this.showChildren = true;
  53. for (int count = 0; count < bookmarkTree.getBookmarks().size(); count++) {
  54. Bookmark bkmk = (Bookmark)(bookmarkTree.getBookmarks()).get(count);
  55. addSubData(createBookmarkData(bkmk));
  56. }
  57. }
  58. /**
  59. * Create a new pdf bookmark data object.
  60. * This is used by the bookmark-items to create a data object
  61. * with a idref. During processing, this idref will be
  62. * subsequently resolved to a particular PageViewport.
  63. *
  64. * @param bookmark the fo:bookmark object
  65. */
  66. public BookmarkData(Bookmark bookmark) {
  67. this.bookmarkTitle = bookmark.getBookmarkTitle();
  68. this.showChildren = bookmark.showChildItems();
  69. this.idRef = bookmark.getInternalDestination();
  70. }
  71. private void putUnresolved(String id, BookmarkData bd) {
  72. List<Resolvable> refs = unresolvedIDRefs.get(id);
  73. if (refs == null) {
  74. refs = new java.util.ArrayList<Resolvable>();
  75. unresolvedIDRefs.put(id, refs);
  76. }
  77. refs.add(bd);
  78. }
  79. /**
  80. * Create a new bookmark data root object.
  81. * This constructor is called by the AreaTreeParser when the
  82. * <bookmarkTree> element is read from the XML file
  83. */
  84. public BookmarkData() {
  85. idRef = null;
  86. whenToProcess = END_OF_DOC;
  87. showChildren = true;
  88. }
  89. /**
  90. * Create a new bookmark data object.
  91. * This constructor is called by the AreaTreeParser when a
  92. * <bookmark> element is read from the XML file.
  93. *
  94. * @param title the bookmark's title
  95. * @param showChildren whether to initially display the bookmark's children
  96. * @param pv the target PageViewport
  97. * @param idRef the target ID
  98. */
  99. public BookmarkData(String title, boolean showChildren, PageViewport pv, String idRef) {
  100. bookmarkTitle = title;
  101. this.showChildren = showChildren;
  102. pageRef = pv;
  103. this.idRef = idRef;
  104. }
  105. /**
  106. * Get the idref for this bookmark-item
  107. *
  108. * @return the idref for the bookmark-item
  109. */
  110. public String getIDRef() {
  111. return idRef;
  112. }
  113. /**
  114. * Add a child bookmark data object.
  115. * This adds a child bookmark in the bookmark hierarchy.
  116. *
  117. * @param sub the child bookmark data
  118. */
  119. public void addSubData(BookmarkData sub) {
  120. subData.add(sub);
  121. if (sub.pageRef == null) {
  122. putUnresolved(sub.getIDRef(), sub);
  123. String[] ids = sub.getIDRefs();
  124. for (String id : ids) {
  125. putUnresolved(id, sub);
  126. }
  127. }
  128. }
  129. /**
  130. * Get the title for this bookmark object.
  131. *
  132. * @return the bookmark title
  133. */
  134. public String getBookmarkTitle() {
  135. return bookmarkTitle;
  136. }
  137. /**
  138. * Indicator of whether to initially display child bookmarks.
  139. *
  140. * @return true to initially display child bookmarks, false otherwise
  141. */
  142. public boolean showChildItems() {
  143. return showChildren;
  144. }
  145. /**
  146. * Get the size of child data objects.
  147. *
  148. * @return the number of child bookmark data
  149. */
  150. public int getCount() {
  151. return subData.size();
  152. }
  153. /**
  154. * Get the child data object.
  155. *
  156. * @param count the index to get
  157. * @return the child bookmark data
  158. */
  159. public BookmarkData getSubData(int count) {
  160. return subData.get(count);
  161. }
  162. /**
  163. * Get the PageViewport object that this bookmark refers to
  164. *
  165. * @return the PageViewport that this bookmark points to
  166. */
  167. public PageViewport getPageViewport() {
  168. return pageRef;
  169. }
  170. /**
  171. * Check if this resolvable object has been resolved.
  172. * A BookmarkData object is considered resolved once the idrefs for it
  173. * and for all of its child bookmark-items have been resolved.
  174. *
  175. * @return true if this object has been resolved
  176. */
  177. public boolean isResolved() {
  178. return unresolvedIDRefs == null || (unresolvedIDRefs.size() == 0);
  179. }
  180. /**
  181. * {@inheritDoc}
  182. */
  183. public String[] getIDRefs() {
  184. return unresolvedIDRefs.keySet().toArray(
  185. new String[unresolvedIDRefs.keySet().size()]);
  186. }
  187. /**
  188. * Resolve this resolvable object.
  189. * This resolves the idref of this object and if possible also
  190. * resolves id references of child elements that have the same
  191. * id reference.
  192. *
  193. * {@inheritDoc}
  194. */
  195. public void resolveIDRef(String id, List<PageViewport> pages) {
  196. if (id.equals(idRef)) {
  197. //Own ID has been resolved, so note the page
  198. pageRef = pages.get(0);
  199. //Note: Determining the placement inside the page is the renderer's job.
  200. }
  201. //Notify all child bookmarks
  202. List<Resolvable> refs = unresolvedIDRefs.get(id);
  203. if (refs != null) {
  204. for (Resolvable res : refs) {
  205. res.resolveIDRef(id, pages);
  206. }
  207. }
  208. unresolvedIDRefs.remove(id);
  209. }
  210. /**
  211. * {@inheritDoc}
  212. */
  213. public String getName() {
  214. return "Bookmarks";
  215. }
  216. /**
  217. * Create and return the bookmark data for this bookmark
  218. * This creates a bookmark data with the destination
  219. * and adds all the data from child bookmarks
  220. *
  221. * @param bookmark the Bookmark object for which a bookmark entry should be
  222. * created
  223. * @return the new bookmark data
  224. */
  225. private BookmarkData createBookmarkData(Bookmark bookmark) {
  226. BookmarkData data = new BookmarkData(bookmark);
  227. for (int count = 0; count < bookmark.getChildBookmarks().size(); count++) {
  228. Bookmark bkmk = (Bookmark)(bookmark.getChildBookmarks()).get(count);
  229. data.addSubData(createBookmarkData(bkmk));
  230. }
  231. return data;
  232. }
  233. }