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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * $Id: BookmarkData.java,v 1.5 2003/03/05 20:40:18 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.extensions;
  52. import org.apache.fop.area.PageViewport;
  53. import org.apache.fop.area.Resolveable;
  54. import org.apache.fop.area.TreeExt;
  55. import org.apache.fop.area.AreaTree;
  56. import java.util.ArrayList;
  57. import java.util.List;
  58. import java.util.HashMap;
  59. /**
  60. * This class holds the PDF bookmark extension data.
  61. * This implements Resolveable and TreeExt so that it can be
  62. * added to the area tree as a resolveable tree extension.
  63. */
  64. public class BookmarkData implements Resolveable, TreeExt {
  65. private ArrayList subData = new ArrayList();
  66. private HashMap idRefs = new HashMap();
  67. // area tree for the top level object to notify when resolved
  68. private AreaTree areaTree = null;
  69. private String idRef;
  70. private PageViewport pageRef = null;
  71. private String label = null;
  72. /**
  73. * Create a new bookmark data object.
  74. * This should only be call by the top level element as the
  75. * id reference will be null.
  76. */
  77. public BookmarkData() {
  78. idRef = null;
  79. }
  80. /**
  81. * Create a new pdf bookmark data object.
  82. * This is used by the outlines to create a data object
  83. * with a id reference. The id reference is to be resolved.
  84. *
  85. * @param id the id reference
  86. */
  87. public BookmarkData(String id) {
  88. idRef = id;
  89. idRefs.put(idRef, this);
  90. }
  91. /**
  92. * Set the area tree.
  93. * This should only be called for the top level element.
  94. * The area tree is used once resolving is complete.
  95. *
  96. * @param at the area tree for the current document
  97. */
  98. public void setAreaTree(AreaTree at) {
  99. areaTree = at;
  100. }
  101. /**
  102. * Get the id reference for this data.
  103. *
  104. * @return the id reference
  105. */
  106. public String getID() {
  107. return idRef;
  108. }
  109. /**
  110. * Add the child bookmark data object.
  111. * This adds a child bookmark in the bookmark hierarchy.
  112. *
  113. * @param sub the child bookmark data
  114. */
  115. public void addSubData(BookmarkData sub) {
  116. subData.add(sub);
  117. idRefs.put(sub.getID(), sub);
  118. String[] ids = sub.getIDs();
  119. for (int count = 0; count < ids.length; count++) {
  120. idRefs.put(ids[count], sub);
  121. }
  122. }
  123. /**
  124. * Set the label for this bookmark.
  125. *
  126. * @param l the string label
  127. */
  128. public void setLabel(String l) {
  129. label = l;
  130. }
  131. /**
  132. * Get the label for this bookmark object.
  133. *
  134. * @return the label string
  135. */
  136. public String getLabel() {
  137. return label;
  138. }
  139. /**
  140. * Get the size of child data objects.
  141. *
  142. * @return the number of child bookmark data
  143. */
  144. public int getCount() {
  145. return subData.size();
  146. }
  147. /**
  148. * Get the child data object.
  149. *
  150. * @param count the index to get
  151. * @return the child bookmark data
  152. */
  153. public BookmarkData getSubData(int count) {
  154. return (BookmarkData)subData.get(count);
  155. }
  156. /**
  157. * Get the page that this resolves to.
  158. *
  159. * @return the PageViewport that this extension resolves to
  160. */
  161. public PageViewport getPage() {
  162. return pageRef;
  163. }
  164. /**
  165. * Check if this tree extension is resolveable.
  166. *
  167. * @return true since this also implements Resolveable
  168. */
  169. public boolean isResolveable() {
  170. return true;
  171. }
  172. /**
  173. * Get the mime type of this area tree extension.
  174. *
  175. * @return this tree extension applies to pdf
  176. */
  177. public String getMimeType() {
  178. return "application/pdf";
  179. }
  180. /**
  181. * Get the name of this area tree extension.
  182. *
  183. * @return the name of the PDF bookmark extension is "Bookmark"
  184. */
  185. public String getName() {
  186. return "Bookmark";
  187. }
  188. /**
  189. * Check if this resolveable object has been resolved.
  190. * Once the id reference is null then it has been resolved.
  191. *
  192. * @return true if this has been resolved
  193. */
  194. public boolean isResolved() {
  195. return idRefs == null;
  196. }
  197. /**
  198. * Get the id references held by this object.
  199. * Also includes all id references of all children.
  200. *
  201. * @return the array of id references
  202. */
  203. public String[] getIDs() {
  204. return (String[])idRefs.keySet().toArray(new String[] {});
  205. }
  206. /**
  207. * Resolve this resolveable object.
  208. * This resolves the id reference and if possible also
  209. * resolves id references of child elements that have the same
  210. * id reference.
  211. *
  212. * @param id the id reference being resolved
  213. * @param pages the list of pages the the id reference resolves to
  214. */
  215. public void resolve(String id, List pages) {
  216. // this method is buggy
  217. if (!id.equals(idRef)) {
  218. BookmarkData bd = (BookmarkData)idRefs.get(id);
  219. idRefs.remove(id);
  220. if (bd != null) {
  221. bd.resolve(id, pages);
  222. if (bd.isResolved()) {
  223. checkFinish();
  224. }
  225. } else if (idRef == null) {
  226. checkFinish();
  227. }
  228. } else {
  229. if (pages != null) {
  230. pageRef = (PageViewport)pages.get(0);
  231. }
  232. // TODO
  233. // get rect area of id on page
  234. idRefs.remove(idRef);
  235. checkFinish();
  236. }
  237. }
  238. private void checkFinish() {
  239. if (idRefs.size() == 0) {
  240. idRefs = null;
  241. if (areaTree != null) {
  242. areaTree.handleTreeExtension(this, TreeExt.AFTER_PAGE);
  243. }
  244. }
  245. }
  246. }