KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > util > PdfReferencedObjects


1 package com.etymon.pjx.util;
2
3 import java.io.*;
4 import java.util.*;
5 import com.etymon.pjx.*;
6
7 /**
8    Examines a specified object and returns the set of all objects it
9    references. This class implements the {@link PdfObjectFilter
10    PdfObjectFilter} interface. The {@link #preFilter(PdfObject)
11    preFilter(PdfObject)} method can be overridden in a subclass to
12    pre-process the object and its contents. This class is
13    synchronized.
14    @author Nassib Nassar
15 */

16 public class PdfReferencedObjects implements PdfObjectFilter {
17
18     /**
19        The total set of referenced objects (stored as indirect
20        references).
21      */

22     protected Set _ref_master;
23
24     /**
25        The current set of referenced objects (stored as indirect
26        references).
27      */

28     protected Set _ref;
29
30     /**
31        The manager to use for resolving references.
32      */

33     protected PdfManager _m;
34     
35     /**
36        Constructs a <code>PdfReferencedObjects</code> instance.
37        @param manager the manager associated with the document.
38      */

39     public PdfReferencedObjects(PdfManager manager) {
40
41         _m = manager;
42         
43     }
44
45     /**
46        Returns the set of all objects referenced by the specified
47        PDF object. This method calls {@link
48        PdfObject#filter(PdfObjectFilter)
49        PdfObject.filter(PdfObjectFilter)} to process objects
50        recursively.
51        @param obj the object to examine.
52        @throws PdfFormatException
53      */

54     public Set getReferenced(PdfObject obj) throws IOException, PdfFormatException {
55         synchronized (this) {
56             synchronized (_m) {
57
58                 // get initial set of objects
59
_ref_master = new HashSet();
60                 _ref = new HashSet();
61                 PdfObject newObj = _m.getObjectIndirect(obj);
62                 newObj.filter(this);
63
64                 _ref_master = _ref;
65                 _ref = new HashSet();
66                 
67                 // now loop until all references have
68
// been resolved
69
Set left = new HashSet(_ref_master);
70                 while (left.isEmpty() == false) {
71
72                     // take one element
73
PdfReference t = (PdfReference)left.iterator().next();
74                     left.remove(t);
75
76                     newObj = _m.getObjectIndirect(t);
77                     newObj.filter(this);
78
79                     left.addAll(_ref);
80                     _ref_master.addAll(_ref);
81                     _ref.clear();
82                     
83                 }
84
85                 _ref = null;
86                 Set r = _ref_master;
87                 _ref_master = null;
88                 return r;
89
90             }
91         }
92     }
93
94     /**
95        This method is used by {@link #getReferenced(PdfObject)
96        getReferenced(PdfObject)} and <b>should not be called
97        externally</b>; however, it may be overridden in subclasses
98        in order to pre-process the objects. (It is not
99        synchronized.)
100        @param obj the object to filter.
101        @return the filtered object.
102        @throws PdfFormatException
103      */

104     public PdfObject preFilter(PdfObject obj) throws PdfFormatException {
105         return obj;
106     }
107     
108     /**
109        This method is used by {@link #getReferenced(PdfObject)
110        getReferenced(PdfObject)} and <b>should not be called
111        externally</b>. (It is not synchronized.)
112        @param obj the object to filter.
113        @return the filtered object.
114        @throws PdfFormatException
115      */

116     public PdfObject postFilter(PdfObject obj) throws PdfFormatException {
117         if (obj instanceof PdfReference) {
118             if (_ref_master.contains(obj) == false) {
119                 _ref.add(obj);
120             }
121         }
122         return obj;
123     }
124     
125 }
126
Popular Tags