KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > example > SAReaderProvider


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.editor.example;
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
27 public class SAReaderProvider implements ReaderProvider {
28
29     private static final String JavaDoc DTD_FOLDER = "DTDs"; // NOI18 // NOI18N
30
private static final String JavaDoc CATALOG_FILE_NAME = "catalog"; // NOI18N
31

32     Map mapping = null;
33     boolean valid = false;
34     File dtdSetFolder;
35
36     public static void setupReaders() {
37         //We are not able to track changes!
38
File rootFolder = Editor.getDistributionDirectory();
39         
40     //We are not able to track changes!
41
File dtdFolder = findFile( rootFolder, DTD_FOLDER );
42         if( dtdFolder != null) {
43             processSubfolders( dtdFolder );
44         }
45     }
46     
47     
48
49     public SAReaderProvider( File folder ) {
50         dtdSetFolder = folder;
51         revalidate(true);
52     //We will not be able to catch changes in folder!
53
}
54
55     public Collection getIdentifiers() {
56         return valid ? mapping.keySet() : new HashSet(0);
57     }
58     
59     private static File findFile(File folder, String JavaDoc fileName) {
60        if (fileName == null || folder == null) {
61           return null;
62        }
63        
64        File[] files = folder.listFiles();
65        
66        for (int cntr = 0; cntr < files.length; cntr++) {
67           if (fileName.equals(files[cntr].getName())) {
68          return files[cntr];
69       }
70        }
71        return null;
72     }
73
74     public Reader getReaderForIdentifier( String JavaDoc identifier, String JavaDoc filename) {
75         if( !valid ) return null;
76         
77         String JavaDoc fileName = (String JavaDoc)mapping.get( identifier );
78         if( fileName == null ) return null;
79         if( dtdSetFolder == null ) return null;
80         
81         File file = findFile( dtdSetFolder, fileName );
82         if( file == null ) return null;
83         
84         try {
85             return new InputStreamReader( new FileInputStream (file ) );
86         } catch( FileNotFoundException exc ) {
87             return null;
88         }
89     }
90
91     private void invalidate() {
92         if( valid ) {
93             valid = false;
94             Registry.invalidateReaderProvider( this );
95         }
96     }
97
98     private boolean revalidate( boolean flag ) {
99         if( mapping == null || flag ) {
100             File catalog = findFile( dtdSetFolder, CATALOG_FILE_NAME );
101
102             if( catalog == null ) {
103                 mapping = null;
104             } else try {
105                 mapping = parseCatalog( new InputStreamReader( new FileInputStream( catalog ) ) );
106             } catch( FileNotFoundException exc ) {
107                 mapping = null;
108             }
109             
110             if( mapping == null ) {
111                 invalidate();
112                 return false;
113             }
114         }
115         
116         // check the availabilily
117
Collection files = mapping.values();
118         boolean all = true;
119         for( Iterator it = files.iterator(); it.hasNext(); ) {
120             String JavaDoc fname = (String JavaDoc)it.next();
121             if( findFile( dtdSetFolder, fname ) == null ) {
122                 all = false;
123                 break;
124             }
125         }
126         if( !all ) invalidate();
127         valid = all;
128         return valid;
129     }
130
131     private Map parseCatalog( Reader catalogReader ) {
132         HashMap hashmap = new HashMap();
133         LineNumberReader reader = new LineNumberReader( catalogReader );
134         
135         for( ;; ) {
136             String JavaDoc line;
137
138             try {
139                 line = reader.readLine();
140             } catch( IOException exc ) {
141                 return null;
142             }
143             
144             if( line == null ) break;
145             
146             StringTokenizer st = new StringTokenizer( line );
147             if( st.hasMoreTokens() && "PUBLIC".equals( st.nextToken() ) && st.hasMoreTokens() ) { // NOI18N
148
st.nextToken( "\"" ); // NOI18N
149
if( !st.hasMoreTokens() ) continue;
150                 String JavaDoc id = st.nextToken( "\"" ); // NOI18N
151

152         if( !st.hasMoreTokens() ) continue;
153                 st.nextToken( " \t\n\r\f" ); // NOI18N
154

155         if( !st.hasMoreTokens() ) continue;
156                 String JavaDoc file = st.nextToken();
157                 hashmap.put( id, file );
158             }
159         }
160         return hashmap;
161     }
162
163     private static void processSubfolders( File dtdFolder ) {
164         File[] files = dtdFolder.listFiles();
165     
166         for (int cntr = 0; cntr < files.length; cntr++) {
167         File file = files[cntr];
168         
169         if (file != null && file.isDirectory()) {
170                addFolder( file );
171         }
172         }
173     }
174
175     static Map folder2provider = new HashMap();
176
177     
178     private static void removeSubfolders() {
179         Iterator it = folder2provider.entrySet().iterator();
180         folder2provider = new HashMap();
181         while( it.hasNext() ) {
182             Map.Entry entry = (Map.Entry)it.next();
183             ReaderProvider prov = (ReaderProvider)entry.getValue();
184             Registry.unregisterReaderProvider( prov );
185         }
186     }
187
188     private static void addFolder( File folder ) {
189         SAReaderProvider prov = new SAReaderProvider( folder );
190         folder2provider.put( folder.getName(), prov );
191         Registry.registerReaderProvider( prov );
192     }
193
194     private static void removeFolder( File folder ) {
195         SAReaderProvider prov = (SAReaderProvider)folder2provider.remove( folder.getName() );
196         if( prov != null ) Registry.unregisterReaderProvider( prov );
197     }
198
199 }
200
201
Popular Tags