1 19 20 package org.netbeans.editor.ext.html.dtd; 21 22 import java.util.*; 23 import java.io.Reader ; 24 import java.lang.ref.WeakReference ; 25 26 32 public class Registry { 33 34 36 private static Map dtdMap = new HashMap(); 37 38 39 private static List providers = new ArrayList(); 40 41 42 private static LinkedList listeners = new LinkedList(); 43 44 47 private static Map provider2dtds = new HashMap(); 48 49 50 56 public void addInvalidateListener( InvalidateListener listener ) { 57 synchronized( listeners ) { 58 listeners.add( new WeakReference ( listener ) ); 59 } 60 } 61 62 67 public void removeInvalidateListener( InvalidateListener listener ) { 68 synchronized( listeners ) { 69 for( Iterator it = listeners.iterator(); it.hasNext(); ) { 71 WeakReference ref = (WeakReference )it.next(); 72 InvalidateListener obj = (InvalidateListener)ref.get(); 73 if( obj == null || obj == listener ) it.remove(); 75 } 76 } 77 } 78 79 84 public static void fireInvalidateEvent( Set identifiers ) { 85 for( Iterator it = identifiers.iterator(); it.hasNext(); ) { 87 dtdMap.remove( it.next() ); 88 } 89 90 java.util.List targets = new ArrayList(); 92 synchronized( listeners ) { 93 for( Iterator it = listeners.iterator(); it.hasNext(); ) { 94 WeakReference ref = (WeakReference )it.next(); 95 InvalidateListener obj = (InvalidateListener)ref.get(); 96 if( obj == null ) { 97 it.remove(); 98 } else { 99 targets.add( obj ); 100 } 101 } 102 } 103 104 InvalidateEvent evt = new InvalidateEvent( identifiers ); 106 for( Iterator it = targets.iterator(); it.hasNext(); ) { 107 InvalidateListener l = (InvalidateListener)it.next(); 108 l.dtdInvalidated( evt ); 109 } 110 } 111 112 113 119 public static void registerReaderProvider( ReaderProvider provider ) { 120 providers.add( provider ); 121 } 122 123 125 public static void invalidateReaderProvider( ReaderProvider provider ) { 126 Set identifiers = (Set)provider2dtds.get( provider ); 127 128 if( identifiers != null ) { 129 fireInvalidateEvent( identifiers ); 132 133 provider2dtds.remove( provider ); 135 } 136 } 137 138 141 public static void unregisterReaderProvider( ReaderProvider provider ) { 142 providers.remove( provider ); 144 145 invalidateReaderProvider( provider ); 146 } 147 148 149 150 153 private static DTD getWeak( String identifier ) { 154 WeakReference ref = (WeakReference )dtdMap.get( identifier ); 155 if( ref == null ) return null; 157 158 DTD dtd = (DTD)ref.get(); 159 if( dtd == null ) dtdMap.remove( identifier ); 161 162 return dtd; 163 } 164 165 166 private static void putWeak( String identifier, DTD dtd ) { 167 dtdMap.put( identifier, new WeakReference ( dtd ) ); 168 } 169 170 171 private static ReaderProvider getProvider( String identifier, String fileName ) { 172 for( Iterator i = providers.iterator(); i.hasNext(); ) { 173 ReaderProvider prov = (ReaderProvider)i.next(); 174 Reader reader = prov.getReaderForIdentifier( identifier, fileName ); 175 if( reader != null ) return prov; 176 } 177 return null; 178 } 179 180 private static DTD parseDTD( String identifier, String fileName ) { 181 ReaderProvider prov = getProvider( identifier, fileName ); 182 if( prov == null ) return null; 183 try { 184 DTD dtd = new DTDParser().createDTD( prov, identifier, fileName ); 185 if( dtd != null ) { 186 Set dtdSet = (Set)provider2dtds.get( prov ); 187 if( dtdSet == null ) { 188 dtdSet = new HashSet(); 189 provider2dtds.put( prov, dtdSet ); 190 } 191 192 dtdSet.add( identifier ); 193 putWeak( identifier, dtd ); 194 } 195 return dtd; 196 } catch( DTDParser.WrongDTDException exc ) { 197 return null; 199 } 200 } 201 202 214 public static DTD getDTD( String identifier, String fileName ) { 215 DTD dtd = getWeak( identifier ); 216 217 if( dtd == null ) dtd = parseDTD( identifier, fileName ); 218 219 return dtd; 220 } 221 } 222 | Popular Tags |