KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > search > SearchThreadJdk12_japan


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.javadoc.search;
21
22 import java.util.NoSuchElementException JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import javax.swing.text.html.parser.ParserDelegator JavaDoc;
30 import javax.swing.text.html.HTMLEditorKit JavaDoc;
31 import javax.swing.text.html.HTML JavaDoc;
32 import javax.swing.text.MutableAttributeSet JavaDoc;
33
34 import org.openide.ErrorManager;
35 import org.openide.util.NbBundle;
36 import org.openide.filesystems.FileObject;
37
38 /** This class implements the index search through documenation
39  * generated by Jdk 1.2 standard doclet
40  */

41
42 class SearchThreadJdk12_japan extends IndexSearchThread {
43
44     private Reader JavaDoc in;
45     private URL JavaDoc contextURL;
46
47     private boolean stopSearch = false;
48
49     private boolean splitedIndex = false;
50     private int currentIndexNumber;
51     private FileObject folder = null;
52     private String JavaDoc JapanEncoding;
53     private final Object JavaDoc LOCK = new Object JavaDoc();
54     
55     public SearchThreadJdk12_japan(String JavaDoc toFind, FileObject fo, IndexSearchThread.DocIndexItemConsumer diiConsumer, boolean caseSensitive, String JavaDoc JapanEncoding) {
56
57         super( toFind, fo, diiConsumer, caseSensitive );
58     this.JapanEncoding = JapanEncoding;
59         
60         if ( fo.isFolder() ) {
61             // Documentation uses splited index - resolve the right file
62

63             
64             // This is just a try in most cases the fileNumber should be
65
// the right one but when some index files are missing we have
66
// to find the right one
67
folder = fo;
68             currentIndexNumber = (int)(Character.toUpperCase( lastField.charAt(0) )) - 'A' + 1;//toFind.charAt(0) )) - 'A' + 1;
69

70             if ( currentIndexNumber < 1 ) {
71                 currentIndexNumber = 1;
72             }
73             else if ( currentIndexNumber > 26 ) {
74                 currentIndexNumber = 27;
75             }
76                 
77             /*
78             if ( currentIndexNumber < 1 || currentIndexNumber > 26 ) {
79                 currentIndexNumber = 27;
80             }
81             */

82             findFileObject( 0 );
83             
84             splitedIndex = true;
85         }
86         else {
87             try {
88                 contextURL = this.indexRoot.getURL();
89                 //contextURL = this.fo.getParent().getURL();
90
}
91             catch ( org.openide.filesystems.FileStateInvalidException e ) {
92                 throw new InternalError JavaDoc( "Can't create documentation folder URL - file state invalid" ); // NOI18N
93
}
94             
95             splitedIndex = false;
96         }
97     }
98
99     public void stopSearch() {
100         Reader JavaDoc br;
101         synchronized (LOCK) {
102             stopSearch = true;
103             br = in;
104         }
105         
106         try {
107             if (br != null)
108                 br.close();
109         }
110         catch ( java.io.IOException JavaDoc e ) {
111             ErrorManager.getDefault().notify(e);
112         }
113     }
114
115     public void run () {
116
117         ParserDelegator JavaDoc pd = new ParserDelegator JavaDoc();
118         
119         if ( indexRoot == null || lastField == null || lastField.length() == 0) {
120             taskFinished();
121             return;
122         }
123
124         
125         SearchCallbackJdk12_japan sc = null;
126
127         int theDirection = 0;
128         
129         do {
130             if ( sc != null ) {
131                 
132                 if (sc.badFile != theDirection ) {
133                     break;
134                 }
135                 
136                 findFileObject( sc.badFile );
137                 if ( indexRoot == null ) {
138                     // No other file to search
139
break;
140                 }
141             }
142
143             try {
144                 synchronized (LOCK) {
145                     if (stopSearch) {
146                         break;
147                     }
148                     in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( indexRoot.getInputStream (), JapanEncoding ));
149                 }
150         // System.out.println("Encoding: " + JapanEncoding);
151
pd.parse( in, sc = new SearchCallbackJdk12_japan( splitedIndex, caseSensitive ), true );
152             }
153             catch ( java.io.IOException JavaDoc e ) {
154                // Do nothing
155
}
156             
157             if ( sc.badFile != 0 && theDirection == 0 ) {
158                 theDirection = sc.badFile;
159             }
160         }
161         while ( sc.badFile != 0 );
162
163         try {
164             if (in != null) {
165                 in.close();
166             }
167         }
168         catch ( java.io.IOException JavaDoc e ) {
169             // Do nothing
170
}
171         //is.searchEnded();
172
taskFinished();
173     }
174     
175     void findFileObject( int direction ) {
176
177         
178         if ( direction < 0 ) {
179             currentIndexNumber--;
180         }
181         else if ( direction > 0 ) {
182             currentIndexNumber++;
183         }
184         
185         do {
186             
187             // Assure the only one direction of looking for Files
188
if ( currentIndexNumber < 0 || currentIndexNumber > 27 ) {
189                 indexRoot = null;
190                 return;
191             }
192
193             String JavaDoc fileName = "index-" + currentIndexNumber; // NOI18N
194

195             if ( folder == null ) {
196                 indexRoot = null;
197                 return;
198             }
199
200             indexRoot = folder.getFileObject( fileName, "html" ); // NOI18N
201

202             if ( indexRoot != null ) {
203                 try {
204                     contextURL = this.indexRoot.getURL();
205                 }
206                 catch ( org.openide.filesystems.FileStateInvalidException e ) {
207                     throw new InternalError JavaDoc( "Can't create documentation folder URL - file state invalid" ); // NOI18N
208
}
209             }
210             else {
211                 
212                 currentIndexNumber += direction > 0 ? 1 : -1;
213             }
214         }
215         while ( indexRoot == null );
216         
217     }
218
219     // Inner classes ------------------------------------------------------------------------------------
220

221
222     /* These are constants for the inner class */
223     
224     static private final String JavaDoc STR_CLASS = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_CLASS" ); //NOI18N
225
static private final String JavaDoc STR_INTERFACE = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_INTERFACE" ); //NOI18N
226
static private final String JavaDoc STR_EXCEPTION = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_EXCEPTION" ); //NOI18N
227
static private final String JavaDoc STR_CONSTRUCTOR = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_CONSTRUCTOR" ); //NOI18N
228
static private final String JavaDoc STR_METHOD = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_METHOD" ); //NOI18N
229
static private final String JavaDoc STR_ERROR = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_ERROR" ); //NOI18N
230
static private final String JavaDoc STR_VARIABLE = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_VARIABLE" ); //NOI18N
231
static private final String JavaDoc STR_STATIC = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_STATIC" ); //NOI18N
232
static private final String JavaDoc STR_DASH = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_DASH" ); //NOI18N
233
static private final String JavaDoc STR_PACKAGE = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_PACKAGE" ); //NOI18N
234
private static final String JavaDoc STR_ENUM = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK15_ENUM"); //NOI18N
235
private static final String JavaDoc STR_ANNTYPE = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK15_ANNOTATION_TYPE"); //NOI18N
236

237     static private final String JavaDoc STR_CONSTRUCTOR_JA = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_CONSTRUCTOR_JA" ); //NOI18N
238
static private final String JavaDoc STR_METHOD_JA = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_METHOD_JA" ); //NOI18N
239
static private final String JavaDoc STR_VARIABLE_JA = NbBundle.getMessage(SearchThreadJdk12_japan.class, "JDK12_VARIABLE_JA" ); //NOI18N
240

241     static private final int IN_BALAST = 0;
242     static private final int IN_DT = 1;
243     static private final int IN_AREF = 2;
244 // static private final int IN_B = 3;
245
static private final int IN_DESCRIPTION = 4;
246     static private final int IN_DESCRIPTION_SUFFIX = 5;
247     
248     /** This inner class parses the JDK 1.2 Documentation index and returns
249      * found indexItems.
250      */

251
252     private class SearchCallbackJdk12_japan extends HTMLEditorKit.ParserCallback JavaDoc {
253
254         private String JavaDoc hrefVal;
255         private DocIndexItem currentDii = null;
256         private int where = IN_BALAST;
257
258         private boolean splited;
259         private boolean stopOnNext = false;
260         
261         private int badFile = 0;
262         
263         int printText = 0;
264         
265         SearchCallbackJdk12_japan( boolean splited, boolean caseSensitive ) {
266             super();
267             this.splited = splited;
268         }
269         
270         public void handleStartTag(HTML.Tag JavaDoc t, MutableAttributeSet JavaDoc a, int pos) {
271
272             if ( t == HTML.Tag.DT ) {
273                 where = IN_DT;
274                 currentDii = null;
275             }
276             else if ( t == HTML.Tag.A && where == IN_DT ) {
277                 where = IN_AREF;
278                 Object JavaDoc val = a.getAttribute( HTML.Attribute.HREF );
279                 if ( val != null ) {
280                     hrefVal = (String JavaDoc) val.toString();
281                     currentDii = new DocIndexItem( null, null, contextURL, hrefVal );
282                 }
283             }
284             else if ( t == HTML.Tag.A && (where == IN_DESCRIPTION_SUFFIX || where == IN_DESCRIPTION) ) {
285                 ; // Just ignore
286
}
287             else if ( t == HTML.Tag.B && where == IN_AREF ) {
288                 where = IN_AREF;
289             }
290             else {
291                 where = IN_BALAST;
292             }
293         }
294
295         public void handleEndTag(HTML.Tag JavaDoc t, int pos) {
296             if (t == HTML.Tag.DT && where != IN_BALAST) {
297                 where = IN_BALAST;
298             }
299         }
300
301         public void handleText(char[] data, int pos) {
302             
303             if ( where == IN_AREF ) {
304                 
305                 if ( stopOnNext ) {
306                     try {
307                         in.close();
308                         where = IN_BALAST;
309                         return;
310                     }
311                     catch ( java.io.IOException JavaDoc e ) {
312                         ErrorManager.getDefault().notify(e);
313                     }
314                 }
315                 
316                 String JavaDoc text = new String JavaDoc( data );
317                 
318                 if ( splited ) {
319                     // it is possible that we search wrong file
320
char first = Character.toUpperCase( lastField.charAt( 0 ) );
321                     char curr = Character.toUpperCase( data[0] );
322                     if ( first != curr ) {
323                         
324                         badFile = first < curr ? -1 : 1;
325                         try {
326                            in.close();
327                            where = IN_BALAST;
328                            return;
329                         }
330                         catch ( java.io.IOException JavaDoc e ) {
331                             ErrorManager.getDefault().notify(e);
332                         }
333                     }
334                     
335                 }
336                 currentDii.setField( text.trim() );
337                     where = IN_DESCRIPTION;
338             }
339             else if ( where == IN_DESCRIPTION ) {
340                 String JavaDoc text = new String JavaDoc( data );
341                 /*
342                 // Stop suffering if we are behind the searched words
343                 if ( text.substring( 0, Math.min(toFind.length(), text.length()) ).compareTo( toFind ) > 0 ) {
344                     try {
345                         System.out.println("Stoping suffering");
346                         in.close();
347                     }
348                     catch ( java.io.IOException e ) {
349                         ErrorManager.getDefault().notify(e);
350                     }
351                 }
352                 */

353                 
354                 //text = text.toUpperCase();
355

356                 int dashIdx = text.indexOf(STR_DASH);
357                 if (dashIdx < 0) {
358                     return;
359                 }
360                 text = text.substring(dashIdx - 1);
361                 currentDii.setRemark( text );
362
363                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( text );
364                 String JavaDoc token;
365                 
366                 boolean isStatic = false;
367                 try {
368                     token = st.nextToken();
369                     if ( token.equals( STR_DASH ) )
370                         token = st.nextToken();
371
372                     if ( token.equalsIgnoreCase( STR_STATIC ) ) {
373                         isStatic = true;
374                         token = st.nextToken();
375                     }
376                 } catch (NoSuchElementException JavaDoc ex) { // see #71978
377
where = IN_DESCRIPTION_SUFFIX;
378                     return;
379                 }
380                 
381                 if ( token.equalsIgnoreCase( STR_CLASS ) )
382                     currentDii.setIconIndex( DocSearchIcons.ICON_CLASS );
383                 else if ( token.equalsIgnoreCase( STR_INTERFACE ) )
384                     currentDii.setIconIndex( DocSearchIcons.ICON_INTERFACE );
385                 else if ( token.equalsIgnoreCase( STR_ENUM ) )
386                     currentDii.setIconIndex( DocSearchIcons.ICON_ENUM );
387                 else if ( token.equalsIgnoreCase( STR_ANNTYPE ) )
388                     currentDii.setIconIndex( DocSearchIcons.ICON_ANNTYPE );
389                 else if ( token.equalsIgnoreCase( STR_EXCEPTION ) )
390                     currentDii.setIconIndex( DocSearchIcons.ICON_EXCEPTION );
391                 else if ( token.equalsIgnoreCase( STR_ERROR ) )
392                     currentDii.setIconIndex( DocSearchIcons.ICON_ERROR );
393                 else if ( token.equalsIgnoreCase( STR_PACKAGE ) )
394                     currentDii.setIconIndex( DocSearchIcons.ICON_PACKAGE );
395                 else if ( token.equalsIgnoreCase( STR_CONSTRUCTOR ) )
396                     currentDii.setIconIndex( DocSearchIcons.ICON_CONSTRUCTOR );
397                 else if ( token.equalsIgnoreCase( STR_METHOD ) )
398                     currentDii.setIconIndex( isStatic ? DocSearchIcons.ICON_METHOD_ST : DocSearchIcons.ICON_METHOD );
399                 else if ( token.equalsIgnoreCase( STR_VARIABLE ) )
400                     currentDii.setIconIndex( isStatic ? DocSearchIcons.ICON_VARIABLE_ST : DocSearchIcons.ICON_VARIABLE );
401
402                 // Add the item when all information is available
403
//insertDocIndexItem( currentDii );
404

405                 if (currentDii.getPackage() != null) {
406                     where = IN_DESCRIPTION_SUFFIX;
407                 } else if ( text.endsWith( "." ) ) { // NOI18N
408
where = IN_DESCRIPTION_SUFFIX;
409                     currentDii.setPackage( text.substring( text.lastIndexOf( ' ' ) ).trim() );
410                 }
411                 else
412                     where = IN_BALAST;
413             }
414             else if ( where == IN_DESCRIPTION_SUFFIX ) {
415                 boolean isStatic = false;
416                 String JavaDoc remark = String.valueOf(data);
417                 currentDii.setRemark( currentDii.getRemark() + remark);
418                 String JavaDoc declaringClass = remark.trim();
419                 if( !(".".equals(declaringClass))){ //NOI18N
420
if (currentDii.getDeclaringClass() == null) {
421                         currentDii.setDeclaringClass(declaringClass);
422
423                         // System.out.println("Data: " + text );
424
remark = remark.toUpperCase();
425                         if( remark.indexOf( STR_STATIC ) != -1 )
426                             isStatic = true;
427
428                             if( remark.indexOf( STR_CONSTRUCTOR_JA ) != -1 )
429                                 currentDii.setIconIndex(DocSearchIcons.ICON_CONSTRUCTOR );
430                             else if( remark.indexOf( STR_METHOD_JA ) != -1 )
431                                 currentDii.setIconIndex( isStatic ? DocSearchIcons.ICON_METHOD_ST : DocSearchIcons.ICON_METHOD );
432                             else if( remark.indexOf( STR_VARIABLE_JA ) != -1 )
433                                 currentDii.setIconIndex( isStatic ? DocSearchIcons.ICON_VARIABLE_ST : DocSearchIcons.ICON_VARIABLE );
434                         insertDocIndexItem( currentDii );
435                     }
436                 }
437             }
438             else
439                 where = IN_BALAST;
440
441         }
442
443     }
444
445 }
446
Popular Tags