KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > html > NbReaderProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc DTD_FOLDER = "DTDs"; // NOI18 // NOI18N
31
private static final String JavaDoc CATALOG_FILE_NAME = "catalog"; // NOI18N
32

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 JavaDoc identifier, String JavaDoc filename) {
61         if( !valid ) return null;
62         
63         String JavaDoc fileName = (String JavaDoc)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         // check the availabilily
103
Collection files = mapping.values();
104         boolean all = true;
105         for( Iterator it = files.iterator(); it.hasNext(); ) {
106             String JavaDoc fname = (String JavaDoc)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 JavaDoc 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() ) { // NOI18N
134
st.nextToken( "\"" ); // NOI18N
135
if( !st.hasMoreTokens() ) continue;
136                 String JavaDoc id = st.nextToken( "\"" ); // NOI18N
137

138         if( !st.hasMoreTokens() ) continue;
139                 st.nextToken( " \t\n\r\f" ); // NOI18N
140

141         if( !st.hasMoreTokens() ) continue;
142                 String JavaDoc 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