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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.area.extensions;
  18. import org.apache.fop.area.PageViewport;
  19. import org.apache.fop.area.Resolveable;
  20. import org.apache.fop.area.TreeExt;
  21. import org.apache.fop.area.AreaTree;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.HashMap;
  25. /**
  26. * This class holds the PDF bookmark extension data.
  27. * This implements Resolveable and TreeExt so that it can be
  28. * added to the area tree as a resolveable tree extension.
  29. */
  30. public class BookmarkData implements Resolveable, TreeExt {
  31. private ArrayList subData = new ArrayList();
  32. private HashMap idRefs = new HashMap();
  33. // area tree for the top level object to notify when resolved
  34. private AreaTree areaTree = null;
  35. private String idRef;
  36. private PageViewport pageRef = null;
  37. private String label = null;
  38. /**
  39. * Create a new bookmark data object.
  40. * This should only be call by the top level element as the
  41. * id reference will be null.
  42. */
  43. public BookmarkData() {
  44. idRef = null;
  45. }
  46. /**
  47. * Create a new pdf bookmark data object.
  48. * This is used by the outlines to create a data object
  49. * with a id reference. The id reference is to be resolved.
  50. *
  51. * @param id the id reference
  52. */
  53. public BookmarkData(String id) {
  54. idRef = id;
  55. idRefs.put(idRef, this);
  56. }
  57. /**
  58. * Set the area tree.
  59. * This should only be called for the top level element.
  60. * The area tree is used once resolving is complete.
  61. *
  62. * @param at the area tree for the current document
  63. */
  64. public void setAreaTree(AreaTree at) {
  65. areaTree = at;
  66. }
  67. /**
  68. * Get the id reference for this data.
  69. *
  70. * @return the id reference
  71. */
  72. public String getID() {
  73. return idRef;
  74. }
  75. /**
  76. * Add the child bookmark data object.
  77. * This adds a child bookmark in the bookmark hierarchy.
  78. *
  79. * @param sub the child bookmark data
  80. */
  81. public void addSubData(BookmarkData sub) {
  82. subData.add(sub);
  83. idRefs.put(sub.getID(), sub);
  84. String[] ids = sub.getIDs();
  85. for (int count = 0; count < ids.length; count++) {
  86. idRefs.put(ids[count], sub);
  87. }
  88. }
  89. /**
  90. * Set the label for this bookmark.
  91. *
  92. * @param l the string label
  93. */
  94. public void setLabel(String l) {
  95. label = l;
  96. }
  97. /**
  98. * Get the label for this bookmark object.
  99. *
  100. * @return the label string
  101. */
  102. public String getLabel() {
  103. return label;
  104. }
  105. /**
  106. * Get the size of child data objects.
  107. *
  108. * @return the number of child bookmark data
  109. */
  110. public int getCount() {
  111. return subData.size();
  112. }
  113. /**
  114. * Get the child data object.
  115. *
  116. * @param count the index to get
  117. * @return the child bookmark data
  118. */
  119. public BookmarkData getSubData(int count) {
  120. return (BookmarkData)subData.get(count);
  121. }
  122. /**
  123. * Get the page that this resolves to.
  124. *
  125. * @return the PageViewport that this extension resolves to
  126. */
  127. public PageViewport getPage() {
  128. return pageRef;
  129. }
  130. /**
  131. * Check if this tree extension is resolveable.
  132. *
  133. * @return true since this also implements Resolveable
  134. */
  135. public boolean isResolveable() {
  136. return true;
  137. }
  138. /**
  139. * Get the mime type of this area tree extension.
  140. *
  141. * @return this tree extension applies to pdf
  142. */
  143. public String getMimeType() {
  144. return "application/pdf";
  145. }
  146. /**
  147. * Get the name of this area tree extension.
  148. *
  149. * @return the name of the PDF bookmark extension is "Bookmark"
  150. */
  151. public String getName() {
  152. return "Bookmark";
  153. }
  154. /**
  155. * Check if this resolveable object has been resolved.
  156. * Once the id reference is null then it has been resolved.
  157. *
  158. * @return true if this has been resolved
  159. */
  160. public boolean isResolved() {
  161. return idRefs == null;
  162. }
  163. /**
  164. * Get the id references held by this object.
  165. * Also includes all id references of all children.
  166. *
  167. * @return the array of id references
  168. */
  169. public String[] getIDs() {
  170. return (String[])idRefs.keySet().toArray(new String[] {});
  171. }
  172. /**
  173. * Resolve this resolveable object.
  174. * This resolves the id reference and if possible also
  175. * resolves id references of child elements that have the same
  176. * id reference.
  177. *
  178. * @param id the id reference being resolved
  179. * @param pages the list of pages the the id reference resolves to
  180. */
  181. public void resolve(String id, List pages) {
  182. // this method is buggy
  183. if (!id.equals(idRef)) {
  184. BookmarkData bd = (BookmarkData)idRefs.get(id);
  185. idRefs.remove(id);
  186. if (bd != null) {
  187. bd.resolve(id, pages);
  188. if (bd.isResolved()) {
  189. checkFinish();
  190. }
  191. } else if (idRef == null) {
  192. checkFinish();
  193. }
  194. } else {
  195. if (pages != null) {
  196. pageRef = (PageViewport)pages.get(0);
  197. }
  198. // TODO get rect area of id on page
  199. idRefs.remove(idRef);
  200. checkFinish();
  201. }
  202. }
  203. private void checkFinish() {
  204. if (idRefs.size() == 0) {
  205. idRefs = null;
  206. if (areaTree != null) {
  207. areaTree.handleTreeExtension(this, TreeExt.AFTER_PAGE);
  208. }
  209. }
  210. }
  211. }