KickJava   Java API By Example, From Geeks To Geeks.

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


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

40
41 class SearchThreadJdk12 extends IndexSearchThread {
42
43     private Reader JavaDoc in;
44     private URL JavaDoc contextURL;
45
46     private boolean stopSearch = false;
47
48     private boolean splitedIndex = false;
49     private int currentIndexNumber;
50     private FileObject folder = null;
51     private final Object JavaDoc LOCK = new Object JavaDoc();
52     
53     public SearchThreadJdk12 ( String JavaDoc toFind,
54                                FileObject fo,
55                                IndexSearchThread.DocIndexItemConsumer diiConsumer, boolean caseSensitive ) {
56
57         super( toFind, fo, diiConsumer, caseSensitive);
58         //this.caseSensitive = caseSensitive;
59

60         if ( fo.isFolder() ) {
61             // Documentation uses splited index - resolve the right file
62

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

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

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

187             
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.class, "JDK12_CLASS" ); //NOI18N
225
static private final String JavaDoc STR_INTERFACE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_INTERFACE" ); //NOI18N
226
static private final String JavaDoc STR_EXCEPTION = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_EXCEPTION" ); //NOI18N
227
static private final String JavaDoc STR_CONSTRUCTOR = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_CONSTRUCTOR" ); //NOI18N
228
static private final String JavaDoc STR_METHOD = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_METHOD" ); //NOI18N
229
static private final String JavaDoc STR_ERROR = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_ERROR" ); //NOI18N
230
static private final String JavaDoc STR_VARIABLE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_VARIABLE" ); //NOI18N
231
static private final String JavaDoc STR_STATIC = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_STATIC" ); //NOI18N
232
static private final String JavaDoc STR_DASH = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_DASH" ); //NOI18N
233
static private final String JavaDoc STR_PACKAGE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_PACKAGE" ); //NOI18N
234
private static final String JavaDoc STR_ENUM = NbBundle.getMessage(SearchThreadJdk12.class, "JDK15_ENUM"); //NOI18N
235
private static final String JavaDoc STR_ANNTYPE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK15_ANNOTATION_TYPE"); //NOI18N
236

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

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

350                 
351                 //text = text.toUpperCase();
352

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

396                 if (currentDii.getPackage() != null) {
397                     where = IN_DESCRIPTION_SUFFIX;
398                 } else if ( text.endsWith( "." ) ) { // NOI18N
399
where = IN_DESCRIPTION_SUFFIX;
400                     currentDii.setPackage( text.substring( text.lastIndexOf( ' ' ) ).trim() );
401                 }
402                 else
403                     where = IN_BALAST;
404             }
405             else if ( where == IN_DESCRIPTION_SUFFIX ) {
406                 String JavaDoc remark = String.valueOf(data);
407                 currentDii.setRemark( currentDii.getRemark() + remark);
408                 String JavaDoc declaringClass = remark.trim();
409                 if( !(".".equals(declaringClass))){ //NOI18N
410
currentDii.setDeclaringClass(declaringClass);
411                     insertDocIndexItem( currentDii );
412                 }
413             }
414             else
415                 where = IN_BALAST;
416
417         }
418         /*
419         private boolean getContainsText(String text){//, boolean caseSensitive){
420             if( lastField.length() != 0 && text.indexOf( lastField ) != -1 )
421                 return true;
422             else if ( middleField.length() != 0 && text.indexOf( middleField ) != -1 )
423                 return true;
424             else if ( reminder.length() != 0 && text.indexOf( reminder ) != -1 )
425                 return true;
426             else
427                 return false;
428         }
429         */

430     }
431 }
432
Popular Tags