KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > datatypes > IDReferences


1 /*
2  * $Id: IDReferences.java,v 1.14.2.5 2003/02/25 10:48:28 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.datatypes;
52
53 import org.apache.fop.pdf.PDFGoTo;
54 import org.apache.fop.layout.Area;
55 import org.apache.fop.apps.FOPException;
56
57 // Java
58
import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60
61 /**
62   IDReferences contains a map of IDs and the objects to which
63   they refer. It also contains a list of references to IDs which
64   have yet to be encountered.
65
66   Modified by Mark Lillywhite mark-fop@inomial.com. Added
67   getInvalidElements() so that StreamRenderer can tell what
68   hasn't been determined yet.
69
70   Modified by lmckenzi@ca.ibm.com
71   Sometimes IDs are created, but not validated. This code fixes
72   the incorrect complaint that the ID already exists which prevents
73   basic-links from working (sometimes).
74
75   */

76 public class IDReferences {
77     private HashMap JavaDoc idReferences, idValidation, idUnvalidated;
78
79     static final int ID_PADDING = 5000; // space to add before id y position
80

81     /**
82      * Constructor for IDReferences
83      */

84     public IDReferences() {
85         idReferences = new HashMap JavaDoc();
86         idValidation = new HashMap JavaDoc();
87         idUnvalidated = new HashMap JavaDoc();
88     }
89
90
91     /**
92      * Creates and configures the specified id.
93      *
94      * @param id The id to initialize
95      * @param area The area where this id was encountered
96      * @exception FOPException
97      */

98     public void initializeID(String JavaDoc id, Area area) throws FOPException {
99         createID(id);
100         configureID(id, area);
101     }
102
103
104     /**
105      * Creates id entry
106      *
107      * @param id The id to create
108      * @param area The area where this id was encountered
109      * @exception FOPException
110      */

111     public void createID(String JavaDoc id) throws FOPException {
112         if (id != null &&!id.equals("")) {
113             if (doesUnvalidatedIDExist(id)) {
114                 removeFromUnvalidatedIDList(id);
115                 //Steve's (gears@apache.org) comment: Is this right?
116
removeFromIdValidationList(id);
117             }
118             else if (doesIDExist(id)) {
119                 throw new FOPException("The id \"" + id
120                                        + "\" already exists in this document");
121             } else {
122                 createNewId(id);
123                 removeFromIdValidationList(id);
124             }
125
126         }
127     }
128
129     /**
130      * Creates id entry that hasn't been validated
131      *
132      * @param id The id to create
133      */

134     public void createUnvalidatedID(String JavaDoc id) {
135         if (id != null &&!id.equals("")) {
136             if (!doesIDExist(id)) {
137                 createNewId(id);
138                 addToUnvalidatedIdList(id);
139             }
140         }
141     }
142
143     /**
144      * Adds created id list of unvalidated ids that have already
145      * been created. This should be used if it is unsure whether
146      * the id is valid but it must be anyhow.
147      *
148      * @param id The id to create
149      */

150     public void addToUnvalidatedIdList(String JavaDoc id) {
151         idUnvalidated.put(id,"");
152     }
153
154     /**
155      * Removes id from list of unvalidated ids.
156      * This should be used if the id has been determined
157      * to be valid.
158      *
159      * @param id The id to remove
160      */

161     public void removeFromUnvalidatedIDList(String JavaDoc id) {
162         idUnvalidated.remove(id);
163     }
164
165     /**
166      * Determines whether specified id already exists in
167      * idUnvalidated
168      *
169      * @param id The id to search for
170      * @return true if ID was found, false otherwise
171      */

172     public boolean doesUnvalidatedIDExist(String JavaDoc id) {
173         return idUnvalidated.containsKey(id);
174     }
175
176     /**
177      * Configures this id
178      *
179      * @param id The id to configure
180      * @param area The area where the id was encountered
181      */

182     public void configureID(String JavaDoc id, Area area) {
183         if (id != null && !id.equals("")) {
184             setPosition(id,
185                         area.getPage().getBody().getXPosition()
186                         + area.getTableCellXOffset() - ID_PADDING,
187                         area.getPage().getBody().getYPosition()
188                         - area.getAbsoluteHeight() + ID_PADDING);
189             setPageNumber(id, area.getPage().getFormattedNumber());
190             area.getPage().addToIDList(id);
191         }
192     }
193
194     /**
195      * Adds id to validation list to be validated. This should be
196      * used if it is unsure whether the id is valid.
197      *
198      * @param id id to be added
199      */

200     public void addToIdValidationList(String JavaDoc id) {
201         idValidation.put(id, "");
202     }
203
204
205
206     /**
207      * Removes id from validation list. This should be used if the id has been determined to be valid
208      *
209      * @param id the id to remove
210      */

211     public void removeFromIdValidationList(String JavaDoc id) {
212         idValidation.remove(id);
213     }
214
215
216     /**
217      * Removes id from IDReferences
218      *
219      * @param id The id to remove
220      */

221     public void removeID(String JavaDoc id) {
222         idReferences.remove(id);
223     }
224
225
226     /**
227      * Determines whether all id's are valid
228      *
229      * @return true if all id's are valid, false otherwise
230      */

231     public boolean isEveryIdValid() {
232         return (idValidation.size() == 0);
233     }
234
235
236
237     /**
238      * Returns all invalid id's still remaining in the validation list
239      *
240      * @return invalid ids from validation list
241      */

242     public String JavaDoc getInvalidIds() {
243         StringBuffer JavaDoc list = new StringBuffer JavaDoc();
244         Iterator JavaDoc iterator = idValidation.keySet().iterator();
245         while (iterator.hasNext()) {
246             list.append("\n\"").append(iterator.next().toString()).append("\" ");
247         }
248         return list.toString();
249     }
250
251
252     /**
253      * Determines whether specified id already exists in IDReferences
254      *
255      * @param id the id to search for
256      * @return true if ID was found, false otherwise
257      */

258     public boolean doesIDExist(String JavaDoc id) {
259         return idReferences.containsKey(id);
260     }
261
262
263     /**
264      * Determines whether the GoTo reference for the specified id is defined
265      *
266      * @param id the id to search for
267      * @return true if GoTo reference is defined, false otherwise
268      */

269     public boolean doesGoToReferenceExist(String JavaDoc id) {
270         IDNode node = (IDNode)idReferences.get(id);
271         return node.isThereInternalLinkGoTo();
272     }
273
274
275
276
277     /**
278      * Returns the reference to the GoTo object used for the internal link
279      *
280      * @param id the id whose reference to use
281      * @return reference to GoTo object
282      */

283     public String JavaDoc getInternalLinkGoToReference(String JavaDoc id) {
284         IDNode node = (IDNode)idReferences.get(id);
285         return node.getInternalLinkGoToReference();
286     }
287
288
289
290     /**
291      * creates an Internal Link GoTo object for this id
292      *
293      * @param id The id for which to set the Internal Link Go To
294      * @param objectNumber
295      * The object number to use for the GoTo object
296      * @return the object reference of the new GoTo object
297      */

298     public String JavaDoc createInternalLinkGoTo(String JavaDoc id, int objectNumber) {
299         IDNode node = (IDNode)idReferences.get(id); // retrieve id node
300
node.createInternalLinkGoTo(objectNumber); // create Internal Link GoTo object
301
return node.getInternalLinkGoToReference(); // return Internal Link Go To object reference
302
}
303
304
305
306     /**
307      * Adds an id to IDReferences
308      *
309      * @param id the id to add
310      */

311     public void createNewId(String JavaDoc id) {
312         IDNode node = new IDNode(id);
313         idReferences.put(id, node);
314     }
315
316
317     /**
318      * Returns the PDFGoTo object for the specified id
319      *
320      * @param id the id for which the PDFGoTo to be retrieved is associated
321      * @return the PDFGoTo object associated with the specified id
322      */

323     public PDFGoTo getPDFGoTo(String JavaDoc id) {
324         IDNode node = (IDNode)idReferences.get(id);
325         return node.getInternalLinkGoTo();
326     }
327
328
329     /**
330      * sets the page reference for the internal link's GoTo. The GoTo will jump to this page reference.
331      *
332      * @param pageReference
333      * the page reference to which the internal link GoTo should jump
334      * ex. 23 0 R
335      */

336     public void setInternalGoToPageReference(String JavaDoc id,
337             String JavaDoc pageReference) {
338         IDNode node = (IDNode)idReferences.get(id);
339         if (node != null) {
340             node.setInternalLinkGoToPageReference(pageReference);
341         }
342     }
343
344
345     /**
346      * Sets the page number for the specified id
347      *
348      * @param id The id whose page number is being set
349      * @param pageNumber The page number of the specified id
350      */

351     public void setPageNumber(String JavaDoc id, String JavaDoc pageNumber) {
352         IDNode node = (IDNode)idReferences.get(id);
353         node.setPageNumber(pageNumber);
354     }
355
356
357     /**
358      * Returns the page number where the specified id is found
359      *
360      * @param id The id whose page number to return
361      * @return the page number of the id, or null if the id does not exist
362      */

363     public String JavaDoc getPageNumber(String JavaDoc id) {
364         if (doesIDExist(id)) {
365             IDNode node = (IDNode)idReferences.get(id);
366             return node.getPageNumber();
367         } else {
368             addToIdValidationList(id);
369             return null;
370         }
371     }
372
373
374     /**
375      * Sets the x and y position of specified id
376      *
377      * @param id the id whose position is to be set
378      * @param x x position of id
379      * @param y y position of id
380      */

381     public void setPosition(String JavaDoc id, int x, int y) {
382         IDNode node = (IDNode)idReferences.get(id);
383         node.setPosition(x, y);
384     }
385
386     public Iterator JavaDoc getInvalidElements() {
387         return idValidation.keySet().iterator();
388     }
389     
390     /**
391      * Returns a destination reference for the node with the
392      * specified id. If id does not exist, returns null.
393      * Destination format is: [ ?objectId 0 R /XYZ ?x ?y null ]
394      *
395      * @param id The id whose destination reference to return
396      * @return Destination reference for this node
397      */

398     public String JavaDoc getDestinationRef(String JavaDoc id) {
399         if (doesIDExist(id)) {
400             IDNode node = (IDNode)idReferences.get(id);
401             return "[ " + node.getPageReference() + " /XYZ " +
402             node.getXPosition()/1000f + " " + node.getYPosition()/1000f + " null ]";
403         } else {
404             addToIdValidationList(id);
405             return null;
406         }
407     }
408     
409 }
410
Popular Tags