1 2 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 33 34 class POIFSReaderRegistry 35 { 36 37 private Set omnivorousListeners; 39 40 private Map selectiveListeners; 45 46 private Map chosenDocumentDescriptors; 52 53 56 57 POIFSReaderRegistry() 58 { 59 omnivorousListeners = new HashSet(); 60 selectiveListeners = new HashMap(); 61 chosenDocumentDescriptors = new HashMap(); 62 } 63 64 71 72 void registerListener(final POIFSReaderListener listener, 73 final POIFSDocumentPath path, 74 final String documentName) 75 { 76 if (!omnivorousListeners.contains(listener)) 77 { 78 79 Set descriptors = ( Set ) selectiveListeners.get(listener); 82 83 if (descriptors == null) 84 { 85 86 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 Set listeners = 100 ( Set ) chosenDocumentDescriptors.get(descriptor); 101 102 if (listeners == null) 103 { 104 105 listeners = new HashSet(); 107 chosenDocumentDescriptors.put(descriptor, listeners); 108 } 109 listeners.add(listener); 110 } 111 } 112 } 113 114 119 120 void registerListener(final POIFSReaderListener listener) 121 { 122 if (!omnivorousListeners.contains(listener)) 123 { 124 125 removeSelectiveListener(listener); 130 omnivorousListeners.add(listener); 131 } 132 } 133 134 142 143 Iterator getListeners(final POIFSDocumentPath path, final String 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 } 185 | Popular Tags |