KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > eventfilesystem > POIFSReaderRegistry


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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         
18
19 package org.apache.poi.poifs.eventfilesystem;
20
21 import java.util.*;
22
23 import org.apache.poi.poifs.filesystem.DocumentDescriptor;
24 import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
25
26 /**
27  * A registry for POIFSReaderListeners and the DocumentDescriptors of
28  * the documents those listeners are interested in
29  *
30  * @author Marc Johnson (mjohnson at apache dot org)
31  * @version %I%, %G%
32  */

33
34 class POIFSReaderRegistry
35 {
36
37     // the POIFSReaderListeners who listen to all POIFSReaderEvents
38
private Set omnivorousListeners;
39
40     // Each mapping in this Map has a key consisting of a
41
// POIFSReaderListener and a value cosisting of a Set of
42
// DocumentDescriptors for the documents that POIFSReaderListener
43
// is interested in; used to efficiently manage the registry
44
private Map selectiveListeners;
45
46     // Each mapping in this Map has a key consisting of a
47
// DocumentDescriptor and a value consisting of a Set of
48
// POIFSReaderListeners for the document matching that
49
// DocumentDescriptor; used when a document is found, to quickly
50
// get the listeners interested in that document
51
private Map chosenDocumentDescriptors;
52
53     /**
54      * Construct the registry
55      */

56
57     POIFSReaderRegistry()
58     {
59         omnivorousListeners = new HashSet();
60         selectiveListeners = new HashMap();
61         chosenDocumentDescriptors = new HashMap();
62     }
63
64     /**
65      * register a POIFSReaderListener for a particular document
66      *
67      * @param listener the listener
68      * @param path the path of the document of interest
69      * @param documentName the name of the document of interest
70      */

71
72     void registerListener(final POIFSReaderListener listener,
73                           final POIFSDocumentPath path,
74                           final String JavaDoc documentName)
75     {
76         if (!omnivorousListeners.contains(listener))
77         {
78
79             // not an omnivorous listener (if it was, this method is a
80
// no-op)
81
Set descriptors = ( Set ) selectiveListeners.get(listener);
82
83             if (descriptors == null)
84             {
85
86                 // this listener has not registered before
87
descriptors = new HashSet();
88                 selectiveListeners.put(listener, descriptors);
89             }
90             DocumentDescriptor descriptor = new DocumentDescriptor(path,
91                                                 documentName);
92
93             if (descriptors.add(descriptor))
94             {
95
96                 // this listener wasn't already listening for this
97
// document -- add the listener to the set of
98
// listeners for this document
99
Set listeners =
100                     ( Set ) chosenDocumentDescriptors.get(descriptor);
101
102                 if (listeners == null)
103                 {
104
105                     // nobody was listening for this document before
106
listeners = new HashSet();
107                     chosenDocumentDescriptors.put(descriptor, listeners);
108                 }
109                 listeners.add(listener);
110             }
111         }
112     }
113
114     /**
115      * register for all documents
116      *
117      * @param listener the listener who wants to get all documents
118      */

119
120     void registerListener(final POIFSReaderListener listener)
121     {
122         if (!omnivorousListeners.contains(listener))
123         {
124
125             // wasn't already listening for everything, so drop
126
// anything listener might have been listening for and
127
// then add the listener to the set of omnivorous
128
// listeners
129
removeSelectiveListener(listener);
130             omnivorousListeners.add(listener);
131         }
132     }
133
134     /**
135      * get am iterator of listeners for a particular document
136      *
137      * @param path the document path
138      * @param name the name of the document
139      *
140      * @return an Iterator POIFSReaderListeners; may be empty
141      */

142
143     Iterator getListeners(final POIFSDocumentPath path, final String JavaDoc name)
144     {
145         Set rval = new HashSet(omnivorousListeners);
146         Set selectiveListeners =
147             ( Set ) chosenDocumentDescriptors.get(new DocumentDescriptor(path,
148                 name));
149
150         if (selectiveListeners != null)
151         {
152             rval.addAll(selectiveListeners);
153         }
154         return rval.iterator();
155     }
156
157     private void removeSelectiveListener(final POIFSReaderListener listener)
158     {
159         Set selectedDescriptors = ( Set ) selectiveListeners.remove(listener);
160
161         if (selectedDescriptors != null)
162         {
163             Iterator iter = selectedDescriptors.iterator();
164
165             while (iter.hasNext())
166             {
167                 dropDocument(listener, ( DocumentDescriptor ) iter.next());
168             }
169         }
170     }
171
172     private void dropDocument(final POIFSReaderListener listener,
173                               final DocumentDescriptor descriptor)
174     {
175         Set listeners = ( Set ) chosenDocumentDescriptors.get(descriptor);
176
177         listeners.remove(listener);
178         if (listeners.size() == 0)
179         {
180             chosenDocumentDescriptors.remove(descriptor);
181         }
182     }
183 } // end package scope class POIFSReaderRegistry
184

185
Popular Tags