1 19 20 package org.netbeans.modules.editor.html; 21 22 import java.io.*; 23 import java.util.*; 24 import org.netbeans.editor.ext.html.dtd.ReaderProvider; 25 import org.netbeans.editor.ext.html.dtd.Registry; 26 import org.openide.filesystems.*; 27 28 public class NbReaderProvider implements ReaderProvider { 29 30 private static final String DTD_FOLDER = "DTDs"; private static final String CATALOG_FILE_NAME = "catalog"; 33 Map mapping = null; 34 boolean valid = false; 35 FileObject dtdSetFolder; 36 37 public static void setupReaders() { 38 FileObject rootFolder = Repository.getDefault().getDefaultFileSystem().getRoot(); 39 rootFolder.addFileChangeListener( new RootFolderListener() ); 40 41 FileObject dtdFolder = rootFolder.getFileObject( DTD_FOLDER ); 42 if( dtdFolder != null) { 43 dtdFolder.addFileChangeListener( new DTDFolderListener() ); 44 processSubfolders( dtdFolder ); 45 } 46 } 47 48 49 50 public NbReaderProvider( FileObject folder ) { 51 dtdSetFolder = folder; 52 revalidate(true); 53 folder.addFileChangeListener( new ProviderFolderListener() ); 54 } 55 56 public Collection getIdentifiers() { 57 return valid ? mapping.keySet() : new HashSet(0); 58 } 59 60 public Reader getReaderForIdentifier( String identifier, String filename) { 61 if( !valid ) return null; 62 63 String fileName = (String )mapping.get( identifier ); 64 if( fileName == null ) return null; 65 if( dtdSetFolder == null ) return null; 66 67 FileObject file = dtdSetFolder.getFileObject( fileName ); 68 if( fileName == null ) return null; 69 70 try { 71 return new InputStreamReader( file.getInputStream() ); 72 } catch( FileNotFoundException exc ) { 73 return null; 74 } 75 } 76 77 private void invalidate() { 78 if( valid ) { 79 valid = false; 80 Registry.invalidateReaderProvider( this ); 81 } 82 } 83 84 private boolean revalidate( boolean flag ) { 85 if( mapping == null || flag ) { 86 FileObject catalog = dtdSetFolder.getFileObject( CATALOG_FILE_NAME ); 87 88 if( catalog == null ) { 89 mapping = null; 90 } else try { 91 mapping = parseCatalog( new InputStreamReader( catalog.getInputStream() ) ); 92 } catch( FileNotFoundException exc ) { 93 mapping = null; 94 } 95 96 if( mapping == null ) { 97 invalidate(); 98 return false; 99 } 100 } 101 102 Collection files = mapping.values(); 104 boolean all = true; 105 for( Iterator it = files.iterator(); it.hasNext(); ) { 106 String fname = (String )it.next(); 107 if( dtdSetFolder.getFileObject( fname ) == null ) { 108 all = false; 109 break; 110 } 111 } 112 if( !all ) invalidate(); 113 valid = all; 114 return valid; 115 } 116 117 private Map parseCatalog( Reader catalogReader ) { 118 HashMap hashmap = new HashMap(); 119 LineNumberReader reader = new LineNumberReader( catalogReader ); 120 121 for( ;; ) { 122 String line; 123 124 try { 125 line = reader.readLine(); 126 } catch( IOException exc ) { 127 return null; 128 } 129 130 if( line == null ) break; 131 132 StringTokenizer st = new StringTokenizer( line ); 133 if( st.hasMoreTokens() && "PUBLIC".equals( st.nextToken() ) && st.hasMoreTokens() ) { st.nextToken( "\"" ); if( !st.hasMoreTokens() ) continue; 136 String id = st.nextToken( "\"" ); 138 if( !st.hasMoreTokens() ) continue; 139 st.nextToken( " \t\n\r\f" ); 141 if( !st.hasMoreTokens() ) continue; 142 String file = st.nextToken(); 143 hashmap.put( id, file ); 144 } 145 } 146 return hashmap; 147 } 148 149 private static void processSubfolders( FileObject dtdFolder ) { 150 FileObject folder; 151 for( Enumeration en = dtdFolder.getFolders(false); en.hasMoreElements(); ) { 152 folder = (FileObject)en.nextElement(); 153 addFolder( folder ); 154 } 155 } 156 157 static Map folder2provider = new HashMap(); 158 159 160 private static void removeSubfolders() { 161 Iterator it = folder2provider.entrySet().iterator(); 162 folder2provider = new HashMap(); 163 while( it.hasNext() ) { 164 Map.Entry entry = (Map.Entry)it.next(); 165 ReaderProvider prov = (ReaderProvider)entry.getValue(); 166 Registry.unregisterReaderProvider( prov ); 167 } 168 } 169 170 private static void addFolder( FileObject folder ) { 171 NbReaderProvider prov = new NbReaderProvider( folder ); 172 folder2provider.put( folder.getNameExt(), prov ); 173 Registry.registerReaderProvider( prov ); 174 } 175 176 private static void removeFolder( FileObject folder ) { 177 NbReaderProvider prov = (NbReaderProvider)folder2provider.remove( folder.getNameExt() ); 178 if( prov != null ) Registry.unregisterReaderProvider( prov ); 179 } 180 181 182 private class ProviderFolderListener extends FileChangeAdapter { 183 184 public void fileDataCreated( FileEvent fev ) { 185 FileObject file = fev.getFile(); 186 if( !valid ) { 187 boolean flag = true; 188 revalidate( flag ); 189 } 190 } 191 192 public void fileDeleted( FileEvent fev ) { 193 if( valid ) 194 revalidate( true ); 195 } 196 197 public void fileRenamed( FileRenameEvent fev ) { 198 invalidate(); 199 revalidate(true); 200 } 201 202 public void fileChanged( FileEvent fev ) { 203 invalidate(); 204 revalidate(true); 205 } 206 } 207 208 209 private static class RootFolderListener extends FileChangeAdapter { 210 211 public void fileFolderCreated( FileEvent fev ) { 212 FileObject file = fev.getFile(); 213 if( DTD_FOLDER.equals( file.getNameExt() ) ) { 214 file.addFileChangeListener( new DTDFolderListener() ); 215 NbReaderProvider.processSubfolders( file ); 216 } 217 } 218 219 public void fileDeleted( FileEvent fev ) { 220 FileObject file = fev.getFile(); 221 if( DTD_FOLDER.equals( file.getNameExt() ) ) 222 NbReaderProvider.removeSubfolders(); 223 } 224 } 225 226 227 private static class DTDFolderListener extends FileChangeAdapter { 228 229 public void fileFolderCreated( FileEvent fev ) { 230 FileObject file = fev.getFile(); 231 NbReaderProvider.addFolder( file ); 232 } 233 234 public void fileDeleted( FileEvent fev ) { 235 FileObject file = fev.getFile(); 236 NbReaderProvider.removeFolder( file ); 237 } 238 } 239 240 } 241 | Popular Tags |