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.1KB

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