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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "FOP" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.datatypes;
  41. import org.apache.fop.pdf.PDFGoTo;
  42. import org.apache.fop.layout.AreaContainer;
  43. // Java
  44. import java.util.Hashtable;
  45. import java.util.Vector;
  46. import java.util.Enumeration;
  47. import java.util.NoSuchElementException;
  48. import org.apache.fop.layout.Area;
  49. import org.apache.fop.apps.FOPException;
  50. public class IDReferences {
  51. private Hashtable
  52. idReferences,
  53. idValidation;
  54. final static int ID_PADDING = 5000; // space to add before id y position
  55. /**
  56. * Constructor for IDReferences
  57. */
  58. public IDReferences()
  59. {
  60. idReferences = new Hashtable();
  61. idValidation = new Hashtable();
  62. }
  63. /**
  64. * Creates and configures the specified id.
  65. *
  66. * @param id The id to initialize
  67. * @param area The area where this id was encountered
  68. * @exception FOPException
  69. */
  70. public void initializeID(String id, Area area) throws FOPException
  71. {
  72. createID(id);
  73. configureID(id,area);
  74. }
  75. /**
  76. * Creates id entry
  77. *
  78. * @param id The id to create
  79. * @param area The area where this id was encountered
  80. * @exception FOPException
  81. */
  82. public void createID(String id) throws FOPException
  83. {
  84. if ( id!=null && !id.equals("") ) {
  85. if ( doesIDExist(id) ) {
  86. throw new FOPException("The id \""+id+"\" already exists in this document");
  87. }
  88. else {
  89. createNewId(id);
  90. removeFromIdValidationList(id);
  91. }
  92. }
  93. }
  94. /**
  95. * Configures this id
  96. *
  97. * @param id The id to configure
  98. * @param area The area where the id was encountered
  99. */
  100. public void configureID(String id, Area area)
  101. {
  102. if ( id!=null && !id.equals("") ) {
  103. setPosition(id,area.getPage().getBody().getXPosition()+area.getTableCellXOffset()-ID_PADDING,area.getPage().getBody().getYPosition() - area.getAbsoluteHeight()+ID_PADDING);
  104. setPageNumber(id,area.getPage().getNumber());
  105. area.getPage().addToIDList(id);
  106. }
  107. }
  108. /**
  109. * Adds id to validation list to be validated . This should be used if it is unsure whether the id is valid
  110. *
  111. * @param id id to be added
  112. */
  113. public void addToIdValidationList(String id)
  114. {
  115. idValidation.put(id,"");
  116. }
  117. /**
  118. * Removes id from validation list. This should be used if the id has been determined to be valid
  119. *
  120. * @param id the id to remove
  121. */
  122. public void removeFromIdValidationList(String id)
  123. {
  124. idValidation.remove(id);
  125. }
  126. /**
  127. * Removes id from IDReferences
  128. *
  129. * @param id The id to remove
  130. * @exception FOPException
  131. */
  132. public void removeID(String id)
  133. {
  134. idReferences.remove(id);
  135. }
  136. /**
  137. * Determines whether all id's are valid
  138. *
  139. * @return true if all id's are valid, false otherwise
  140. */
  141. public boolean isEveryIdValid()
  142. {
  143. return(idValidation.size()==0);
  144. }
  145. /**
  146. * Returns all invalid id's still remaining in the validation list
  147. *
  148. * @return invalid ids from validation list
  149. */
  150. public String getInvalidIds()
  151. {
  152. StringBuffer list=new StringBuffer();
  153. Enumeration enum=idValidation.keys();
  154. while(enum.hasMoreElements())
  155. {
  156. list.append("\n\"").append(enum.nextElement().toString()).append("\" ") ;
  157. }
  158. return list.toString();
  159. }
  160. /**
  161. * Determines whether specified id already exists in IDReferences
  162. *
  163. * @param id the id to search for
  164. * @return true if ID was found, false otherwise
  165. */
  166. public boolean doesIDExist(String id)
  167. {
  168. return idReferences.containsKey(id);
  169. }
  170. /**
  171. * Determines whether the GoTo reference for the specified id is defined
  172. *
  173. * @param id the id to search for
  174. * @return true if GoTo reference is defined, false otherwise
  175. */
  176. public boolean doesGoToReferenceExist(String id)
  177. {
  178. IDNode node = (IDNode)idReferences.get(id);
  179. return node.isThereInternalLinkGoTo();
  180. }
  181. /**
  182. * Returns the reference to the GoTo object used for the internal link
  183. *
  184. * @param id the id whose reference to use
  185. * @return reference to GoTo object
  186. */
  187. public String getInternalLinkGoToReference(String id)
  188. {
  189. IDNode node = (IDNode)idReferences.get(id);
  190. return node.getInternalLinkGoToReference();
  191. }
  192. /**
  193. * creates an Internal Link GoTo object for this id
  194. *
  195. * @param id The id for which to set the Internal Link Go To
  196. * @param objectNumber
  197. * The object number to use for the GoTo object
  198. * @return the object reference of the new GoTo object
  199. */
  200. public String createInternalLinkGoTo(String id, int objectNumber)
  201. {
  202. IDNode node = (IDNode)idReferences.get(id); // retrieve id node
  203. node.createInternalLinkGoTo(objectNumber); // create Internal Link GoTo object
  204. return node.getInternalLinkGoToReference(); //return Internal Link Go To object reference
  205. }
  206. /**
  207. * Adds an id to IDReferences
  208. *
  209. * @param id the id to add
  210. */
  211. public void createNewId(String id)
  212. {
  213. IDNode node=new IDNode(id);
  214. idReferences.put(id,node);
  215. }
  216. /**
  217. * Returns the PDFGoTo object for the specified id
  218. *
  219. * @param id the id for which the PDFGoTo to be retrieved is associated
  220. * @return the PDFGoTo object associated with the specified id
  221. */
  222. public PDFGoTo getPDFGoTo(String id)
  223. {
  224. IDNode node=(IDNode)idReferences.get(id);
  225. return node.getInternalLinkGoTo();
  226. }
  227. /**
  228. * sets the page reference for the internal link's GoTo. The GoTo will jump to this page reference.
  229. *
  230. * @param pageReference
  231. * the page reference to which the internal link GoTo should jump
  232. * ex. 23 0 R
  233. */
  234. public void setInternalGoToPageReference(String id, String pageReference)
  235. {
  236. IDNode node=(IDNode)idReferences.get(id);
  237. if (node != null) {
  238. node.setInternalLinkGoToPageReference(pageReference);
  239. }
  240. }
  241. /**
  242. * Sets the page number for the specified id
  243. *
  244. * @param id The id whose page number is being set
  245. * @param pageNumber The page number of the specified id
  246. */
  247. public void setPageNumber(String id, int pageNumber)
  248. {
  249. IDNode node=(IDNode)idReferences.get(id);
  250. node.setPageNumber(pageNumber);
  251. }
  252. /**
  253. * Returns the page number where the specified id is found
  254. *
  255. * @param id The id whose page number to return
  256. * @return the page number of the id, or null if the id does not exist
  257. */
  258. public String getPageNumber(String id)
  259. {
  260. if ( doesIDExist(id) ) {
  261. IDNode node=(IDNode)idReferences.get(id);
  262. return node.getPageNumber();
  263. }
  264. else {
  265. addToIdValidationList(id);
  266. return null;
  267. }
  268. }
  269. /**
  270. * Sets the x and y position of specified id
  271. *
  272. * @param id the id whose position is to be set
  273. * @param x x position of id
  274. * @param y y position of id
  275. */
  276. public void setPosition(String id, int x, int y)
  277. {
  278. IDNode node=(IDNode)idReferences.get(id);
  279. node.setPosition(x,y);
  280. }
  281. }