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.

IDTracker.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. /**
  27. * Used by the AreaTreeHandler to keep track of ID reference usage
  28. * on a PageViewport level.
  29. */
  30. public class IDTracker {
  31. private static final Log log // CSOK: ConstantName
  32. = LogFactory.getLog(IDTracker.class);
  33. // HashMap of ID's whose area is located on one or more consecutive
  34. // PageViewports. Each ID has an arraylist of PageViewports that
  35. // form the defined area of this ID
  36. private Map idLocations = new java.util.HashMap();
  37. // idref's whose target PageViewports have yet to be identified
  38. // Each idref has a HashSet of Resolvable objects containing that idref
  39. private Map unresolvedIDRefs = new java.util.HashMap();
  40. private Set unfinishedIDs = new java.util.HashSet();
  41. private Set alreadyResolvedIDs = new java.util.HashSet();
  42. /**
  43. * Tie a PageViewport with an ID found on a child area of the PV. Note that
  44. * an area with a given ID may be on more than one PV, hence an ID may have
  45. * more than one PV associated with it.
  46. *
  47. * @param id the property ID of the area
  48. * @param pv a page viewport that contains the area with this ID
  49. */
  50. public void associateIDWithPageViewport(String id, PageViewport pv) {
  51. if (log.isDebugEnabled()) {
  52. log.debug("associateIDWithPageViewport(" + id + ", " + pv + ")");
  53. }
  54. List pvList = (List) idLocations.get(id);
  55. if (pvList == null) { // first time ID located
  56. pvList = new ArrayList();
  57. idLocations.put(id, pvList);
  58. pvList.add(pv);
  59. // signal the PageViewport that it is the first PV to contain this id:
  60. pv.setFirstWithID(id);
  61. /*
  62. * See if this ID is in the unresolved idref list, if so resolve
  63. * Resolvable objects tied to it.
  64. */
  65. if (!unfinishedIDs.contains(id)) {
  66. tryIDResolution(id, pv, pvList);
  67. }
  68. } else {
  69. /* TODO: The check is a quick-fix to avoid a waste
  70. * when adding inline-ids to the page */
  71. if (!pvList.contains(pv)) {
  72. pvList.add(pv);
  73. }
  74. }
  75. }
  76. /**
  77. * This method tie an ID to the areaTreeHandler until this one is ready to
  78. * be processed. This is used in page-number-citation-last processing so we
  79. * know when an id can be resolved.
  80. *
  81. * @param id the id of the object being processed
  82. */
  83. public void signalPendingID(String id) {
  84. if (log.isDebugEnabled()) {
  85. log.debug("signalPendingID(" + id + ")");
  86. }
  87. unfinishedIDs.add(id);
  88. }
  89. /**
  90. * Signals that all areas for the formatting object with the given ID have
  91. * been generated. This is used to determine when page-number-citation-last
  92. * ref-ids can be resolved.
  93. *
  94. * @param id the id of the formatting object which was just finished
  95. */
  96. public void signalIDProcessed(String id) {
  97. if (log.isDebugEnabled()) {
  98. log.debug("signalIDProcessed(" + id + ")");
  99. }
  100. alreadyResolvedIDs.add(id);
  101. if (!unfinishedIDs.contains(id)) {
  102. return;
  103. }
  104. unfinishedIDs.remove(id);
  105. List pvList = (List) idLocations.get(id);
  106. Set todo = (Set) unresolvedIDRefs.get(id);
  107. if (todo != null) {
  108. for (Iterator iter = todo.iterator(); iter.hasNext();) {
  109. Resolvable res = (Resolvable) iter.next();
  110. res.resolveIDRef(id, pvList);
  111. }
  112. unresolvedIDRefs.remove(id);
  113. }
  114. }
  115. /**
  116. * Check if an ID has already been resolved
  117. *
  118. * @param id the id to check
  119. * @return true if the ID has been resolved
  120. */
  121. public boolean alreadyResolvedID(String id) {
  122. return (alreadyResolvedIDs.contains(id));
  123. }
  124. /**
  125. * Tries to resolve all unresolved ID references on the given page.
  126. *
  127. * @param id ID to resolve
  128. * @param pv page viewport whose ID refs to resolve
  129. * @param pvList of PageViewports
  130. */
  131. private void tryIDResolution(String id, PageViewport pv, List pvList) {
  132. Set todo = (Set) unresolvedIDRefs.get(id);
  133. if (todo != null) {
  134. for (Iterator iter = todo.iterator(); iter.hasNext();) {
  135. Resolvable res = (Resolvable) iter.next();
  136. if (!unfinishedIDs.contains(id)) {
  137. res.resolveIDRef(id, pvList);
  138. } else {
  139. return;
  140. }
  141. }
  142. alreadyResolvedIDs.add(id);
  143. unresolvedIDRefs.remove(id);
  144. }
  145. }
  146. /**
  147. * Tries to resolve all unresolved ID references on the given page.
  148. *
  149. * @param pv page viewport whose ID refs to resolve
  150. */
  151. public void tryIDResolution(PageViewport pv) {
  152. String[] ids = pv.getIDRefs();
  153. if (ids != null) {
  154. for (int i = 0; i < ids.length; i++) {
  155. List pvList = (List) idLocations.get(ids[i]);
  156. if (pvList != null) {
  157. tryIDResolution(ids[i], pv, pvList);
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * Get the list of page viewports that have an area with a given id.
  164. *
  165. * @param id the id to lookup
  166. * @return the list of PageViewports
  167. */
  168. public List getPageViewportsContainingID(String id) {
  169. return (List) idLocations.get(id);
  170. }
  171. /**
  172. * Add an Resolvable object with an unresolved idref
  173. *
  174. * @param idref the idref whose target id has not yet been located
  175. * @param res the Resolvable object needing the idref to be resolved
  176. */
  177. public void addUnresolvedIDRef(String idref, Resolvable res) {
  178. Set todo = (Set) unresolvedIDRefs.get(idref);
  179. if (todo == null) {
  180. todo = new java.util.HashSet();
  181. unresolvedIDRefs.put(idref, todo);
  182. }
  183. // add Resolvable object to this HashSet
  184. todo.add(res);
  185. }
  186. }