1 19 20 package org.netbeans.modules.javadoc.search; 21 22 import java.util.StringTokenizer ; 23 import java.io.Reader ; 24 import java.io.InputStreamReader ; 25 import java.io.BufferedReader ; 26 import java.net.URL ; 27 28 import javax.swing.text.html.parser.ParserDelegator ; 29 import javax.swing.text.html.HTMLEditorKit ; 30 import javax.swing.text.html.HTML ; 31 import javax.swing.text.MutableAttributeSet ; 32 33 import org.openide.ErrorManager; 34 import org.openide.util.NbBundle; 35 import org.openide.filesystems.FileObject; 36 37 40 41 class SearchThreadJdk12 extends IndexSearchThread { 42 43 private Reader in; 44 private URL 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 LOCK = new Object (); 52 53 public SearchThreadJdk12 ( String toFind, 54 FileObject fo, 55 IndexSearchThread.DocIndexItemConsumer diiConsumer, boolean caseSensitive ) { 56 57 super( toFind, fo, diiConsumer, caseSensitive); 58 60 if ( fo.isFolder() ) { 61 63 folder = fo; 67 currentIndexNumber = (int)(Character.toUpperCase( lastField.charAt(0) )) - 'A' + 1; 69 if ( currentIndexNumber < 1 ) { 70 currentIndexNumber = 1; 71 } 72 else if ( currentIndexNumber > 26 ) { 73 currentIndexNumber = 27; 74 } 75 76 81 findFileObject( 0 ); 82 83 splitedIndex = true; 84 } 85 else { 86 try { 87 contextURL = this.indexRoot.getURL(); 88 } 90 catch ( org.openide.filesystems.FileStateInvalidException e ) { 91 throw new InternalError ( "Can't create documentation folder URL - file state invalid" ); } 93 94 splitedIndex = false; 95 } 96 } 97 98 public void stopSearch() { 99 Reader 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 e ) { 110 ErrorManager.getDefault().notify(e); 111 } 112 } 113 114 public void run () { 115 116 ParserDelegator pd = new ParserDelegator (); 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 break; 139 } 140 } 141 142 try { 143 synchronized (LOCK) { 144 if (stopSearch) { 145 break; 146 } 147 in = new BufferedReader ( new InputStreamReader ( indexRoot.getInputStream () )); 148 } 149 pd.parse( in, sc = new SearchCallbackJdk12( splitedIndex, caseSensitive ), true ); 150 } 151 catch ( java.io.IOException e ) { 152 } 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 e ) { 167 } 169 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 187 188 if ( currentIndexNumber < 0 || currentIndexNumber > 27 ) { 189 indexRoot = null; 190 return; 191 } 192 193 String fileName = "index-" + currentIndexNumber; 195 if ( folder == null ) { 196 indexRoot = null; 197 return; 198 } 199 200 indexRoot = folder.getFileObject( fileName, "html" ); 202 if ( indexRoot != null ) { 203 try { 204 contextURL = this.indexRoot.getURL(); 205 } 206 catch ( org.openide.filesystems.FileStateInvalidException e ) { 207 throw new InternalError ( "Can't create documentation folder URL - file state invalid" ); } 209 } 210 else { 211 212 currentIndexNumber += direction > 0 ? 1 : -1; 213 } 214 } 215 while ( indexRoot == null ); 216 217 } 218 219 221 222 223 224 static private final String STR_CLASS = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_CLASS" ); static private final String STR_INTERFACE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_INTERFACE" ); static private final String STR_EXCEPTION = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_EXCEPTION" ); static private final String STR_CONSTRUCTOR = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_CONSTRUCTOR" ); static private final String STR_METHOD = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_METHOD" ); static private final String STR_ERROR = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_ERROR" ); static private final String STR_VARIABLE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_VARIABLE" ); static private final String STR_STATIC = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_STATIC" ); static private final String STR_DASH = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_DASH" ); static private final String STR_PACKAGE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK12_PACKAGE" ); private static final String STR_ENUM = NbBundle.getMessage(SearchThreadJdk12.class, "JDK15_ENUM"); private static final String STR_ANNTYPE = NbBundle.getMessage(SearchThreadJdk12.class, "JDK15_ANNOTATION_TYPE"); 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_DESCRIPTION = 4; 242 static private final int IN_DESCRIPTION_SUFFIX = 5; 243 244 247 248 private class SearchCallbackJdk12 extends HTMLEditorKit.ParserCallback { 249 250 private String 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 t, MutableAttributeSet 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 val = a.getAttribute( HTML.Attribute.HREF ); 275 if ( val != null ) { 276 hrefVal = (String ) 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 ; } 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 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 e ) { 308 ErrorManager.getDefault().notify(e); 309 } 310 } 311 312 String text = new String ( data ); 313 314 if ( splited ) { 315 char first = Character.toUpperCase( lastField.charAt( 0 ) ); 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 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 text = new String ( data ); 337 338 350 351 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 st = new StringTokenizer ( text ); 361 String 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 396 if (currentDii.getPackage() != null) { 397 where = IN_DESCRIPTION_SUFFIX; 398 } else if ( text.endsWith( "." ) ) { 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 remark = String.valueOf(data); 407 currentDii.setRemark( currentDii.getRemark() + remark); 408 String declaringClass = remark.trim(); 409 if( !(".".equals(declaringClass))){ currentDii.setDeclaringClass(declaringClass); 411 insertDocIndexItem( currentDii ); 412 } 413 } 414 else 415 where = IN_BALAST; 416 417 } 418 430 } 431 } 432 | Popular Tags |