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.

IDReferences.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.datatypes;
  8. import org.apache.fop.pdf.PDFGoTo;
  9. import org.apache.fop.layout.AreaContainer;
  10. // Java
  11. import java.util.Hashtable;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import java.util.NoSuchElementException;
  15. import org.apache.fop.layout.Area;
  16. import org.apache.fop.apps.FOPException;
  17. /**
  18. IDReferences contains a map of IDs and the objects to which
  19. they refer. It also contains a list of references to IDs which
  20. have yet to be encountered.
  21. Modified by Mark Lillywhite mark-fop@inomial.com. Added
  22. getInvalidElements() so that StreamRenderer cna tell what
  23. hasn't been determined yet.
  24. Modified by lmckenzi@ca.ibm.com
  25. Sometimes IDs are created, but not validated. This code fixes
  26. the incorrect complaint that the ID already exists which prevents
  27. basic-links from working (sometimes).
  28. */
  29. public class IDReferences {
  30. private Hashtable idReferences, idValidation, idUnvalidated;
  31. final static int ID_PADDING = 5000; // space to add before id y position
  32. /**
  33. * Constructor for IDReferences
  34. */
  35. public IDReferences() {
  36. idReferences = new Hashtable();
  37. idValidation = new Hashtable();
  38. idUnvalidated = new Hashtable();
  39. }
  40. /**
  41. * Creates and configures the specified id.
  42. *
  43. * @param id The id to initialize
  44. * @param area The area where this id was encountered
  45. * @exception FOPException
  46. */
  47. public void initializeID(String id, Area area) throws FOPException {
  48. createID(id);
  49. configureID(id, area);
  50. }
  51. /**
  52. * Creates id entry
  53. *
  54. * @param id The id to create
  55. * @param area The area where this id was encountered
  56. * @exception FOPException
  57. */
  58. public void createID(String id) throws FOPException {
  59. if (id != null &&!id.equals("")) {
  60. if (doesUnvalidatedIDExist(id)) {
  61. removeFromUnvalidatedIDList(id);
  62. //Steve's (gears@apache.org) comment: Is this right?
  63. removeFromIdValidationList(id);
  64. }
  65. else if (doesIDExist(id)) {
  66. throw new FOPException("The id \"" + id
  67. + "\" already exists in this document");
  68. } else {
  69. createNewId(id);
  70. removeFromIdValidationList(id);
  71. }
  72. }
  73. }
  74. /**
  75. * Creates id entry that hasn't been validated
  76. *
  77. * @param id The id to create
  78. */
  79. public void createUnvalidatedID(String id) {
  80. if (id != null &&!id.equals("")) {
  81. if (!doesIDExist(id)) {
  82. createNewId(id);
  83. addToUnvalidatedIdList(id);
  84. }
  85. }
  86. }
  87. /**
  88. * Adds created id list of unvalidated ids that have already
  89. * been created. This should be used if it is unsure whether
  90. * the id is valid but it must be anyhow.
  91. *
  92. * @param id The id to create
  93. */
  94. public void addToUnvalidatedIdList(String id) {
  95. idUnvalidated.put(id,"");
  96. }
  97. /**
  98. * Removes id from list of unvalidated ids.
  99. * This should be used if the id has been determined
  100. * to be valid.
  101. *
  102. * @param id The id to remove
  103. */
  104. public void removeFromUnvalidatedIDList(String id) {
  105. idUnvalidated.remove(id);
  106. }
  107. /**
  108. * Determines whether specified id already exists in
  109. * idUnvalidated
  110. *
  111. * @param id The id to search for
  112. * @return true if ID was found, false otherwise
  113. */
  114. public boolean doesUnvalidatedIDExist(String id) {
  115. return idUnvalidated.containsKey(id);
  116. }
  117. /**
  118. * Configures this id
  119. *
  120. * @param id The id to configure
  121. * @param area The area where the id was encountered
  122. */
  123. public void configureID(String id, Area area) {
  124. if (id != null &&!id.equals("")) {
  125. setPosition(id,
  126. area.getPage().getBody().getXPosition()
  127. + area.getTableCellXOffset() - ID_PADDING,
  128. area.getPage().getBody().getYPosition()
  129. - area.getAbsoluteHeight() + ID_PADDING);
  130. setPageNumber(id, area.getPage().getNumber());
  131. area.getPage().addToIDList(id);
  132. }
  133. }
  134. /**
  135. * Adds id to validation list to be validated . This should be used if it is unsure whether the id is valid
  136. *
  137. * @param id id to be added
  138. */
  139. public void addToIdValidationList(String id) {
  140. idValidation.put(id, "");
  141. }
  142. /**
  143. * Removes id from validation list. This should be used if the id has been determined to be valid
  144. *
  145. * @param id the id to remove
  146. */
  147. public void removeFromIdValidationList(String id) {
  148. idValidation.remove(id);
  149. }
  150. /**
  151. * Removes id from IDReferences
  152. *
  153. * @param id The id to remove
  154. */
  155. public void removeID(String id) {
  156. idReferences.remove(id);
  157. }
  158. /**
  159. * Determines whether all id's are valid
  160. *
  161. * @return true if all id's are valid, false otherwise
  162. */
  163. public boolean isEveryIdValid() {
  164. return (idValidation.size() == 0);
  165. }
  166. /**
  167. * Returns all invalid id's still remaining in the validation list
  168. *
  169. * @return invalid ids from validation list
  170. */
  171. public String getInvalidIds() {
  172. StringBuffer list = new StringBuffer();
  173. Enumeration enum = idValidation.keys();
  174. while (enum.hasMoreElements()) {
  175. list.append("\n\"").append(enum.nextElement().toString()).append("\" ");
  176. }
  177. return list.toString();
  178. }
  179. /**
  180. * Determines whether specified id already exists in IDReferences
  181. *
  182. * @param id the id to search for
  183. * @return true if ID was found, false otherwise
  184. */
  185. public boolean doesIDExist(String id) {
  186. return idReferences.containsKey(id);
  187. }
  188. /**
  189. * Determines whether the GoTo reference for the specified id is defined
  190. *
  191. * @param id the id to search for
  192. * @return true if GoTo reference is defined, false otherwise
  193. */
  194. public boolean doesGoToReferenceExist(String id) {
  195. IDNode node = (IDNode)idReferences.get(id);
  196. return node.isThereInternalLinkGoTo();
  197. }
  198. /**
  199. * Returns the reference to the GoTo object used for the internal link
  200. *
  201. * @param id the id whose reference to use
  202. * @return reference to GoTo object
  203. */
  204. public String getInternalLinkGoToReference(String id) {
  205. IDNode node = (IDNode)idReferences.get(id);
  206. return node.getInternalLinkGoToReference();
  207. }
  208. /**
  209. * creates an Internal Link GoTo object for this id
  210. *
  211. * @param id The id for which to set the Internal Link Go To
  212. * @param objectNumber
  213. * The object number to use for the GoTo object
  214. * @return the object reference of the new GoTo object
  215. */
  216. public String createInternalLinkGoTo(String id, int objectNumber) {
  217. IDNode node = (IDNode)idReferences.get(id); // retrieve id node
  218. node.createInternalLinkGoTo(objectNumber); // create Internal Link GoTo object
  219. return node.getInternalLinkGoToReference(); // return Internal Link Go To object reference
  220. }
  221. /**
  222. * Adds an id to IDReferences
  223. *
  224. * @param id the id to add
  225. */
  226. public void createNewId(String id) {
  227. IDNode node = new IDNode(id);
  228. idReferences.put(id, node);
  229. }
  230. /**
  231. * Returns the PDFGoTo object for the specified id
  232. *
  233. * @param id the id for which the PDFGoTo to be retrieved is associated
  234. * @return the PDFGoTo object associated with the specified id
  235. */
  236. public PDFGoTo getPDFGoTo(String id) {
  237. IDNode node = (IDNode)idReferences.get(id);
  238. return node.getInternalLinkGoTo();
  239. }
  240. /**
  241. * sets the page reference for the internal link's GoTo. The GoTo will jump to this page reference.
  242. *
  243. * @param pageReference
  244. * the page reference to which the internal link GoTo should jump
  245. * ex. 23 0 R
  246. */
  247. public void setInternalGoToPageReference(String id,
  248. String pageReference) {
  249. IDNode node = (IDNode)idReferences.get(id);
  250. if (node != null) {
  251. node.setInternalLinkGoToPageReference(pageReference);
  252. }
  253. }
  254. /**
  255. * Sets the page number for the specified id
  256. *
  257. * @param id The id whose page number is being set
  258. * @param pageNumber The page number of the specified id
  259. */
  260. public void setPageNumber(String id, int pageNumber) {
  261. IDNode node = (IDNode)idReferences.get(id);
  262. node.setPageNumber(pageNumber);
  263. }
  264. /**
  265. * Returns the page number where the specified id is found
  266. *
  267. * @param id The id whose page number to return
  268. * @return the page number of the id, or null if the id does not exist
  269. */
  270. public String getPageNumber(String id) {
  271. if (doesIDExist(id)) {
  272. IDNode node = (IDNode)idReferences.get(id);
  273. return node.getPageNumber();
  274. } else {
  275. addToIdValidationList(id);
  276. return null;
  277. }
  278. }
  279. /**
  280. * Sets the x and y position of specified id
  281. *
  282. * @param id the id whose position is to be set
  283. * @param x x position of id
  284. * @param y y position of id
  285. */
  286. public void setPosition(String id, int x, int y) {
  287. IDNode node = (IDNode)idReferences.get(id);
  288. node.setPosition(x, y);
  289. }
  290. public Enumeration getInvalidElements() {
  291. return idValidation.keys();
  292. }
  293. }