27
27
28
28
import java .io .IOException ;
29
29
import java .io .Writer ;
30
+ import java .util .ArrayList ;
30
31
import java .util .Collection ;
32
+ import java .util .HashMap ;
31
33
import java .util .List ;
34
+ import java .util .Map ;
32
35
33
36
import javax .lang .model .element .Element ;
34
37
import javax .lang .model .element .ExecutableElement ;
@@ -74,6 +77,8 @@ public class AbstractIndexWriter extends HtmlDocletWriter {
74
77
75
78
protected Navigation navBar ;
76
79
80
+ protected final Map <Character , List <SearchIndexItem >> tagSearchIndexMap ;
81
+
77
82
/**
78
83
* This constructor will be used by {@link SplitIndexWriter}. Initializes
79
84
* path to this file and relative path from this file.
@@ -88,6 +93,9 @@ protected AbstractIndexWriter(HtmlConfiguration configuration,
88
93
super (configuration , path );
89
94
this .indexBuilder = indexBuilder ;
90
95
this .navBar = new Navigation (null , configuration , PageMode .INDEX , path );
96
+ Collection <SearchIndexItem > items =
97
+ searchItems .get (SearchIndexItem .Category .SEARCH_TAGS );
98
+ this .tagSearchIndexMap = buildSearchTagIndex (items );
91
99
}
92
100
93
101
/**
@@ -182,29 +190,29 @@ protected void addDescription(Content dl, Element element) {
182
190
public Void visitModule (ModuleElement e , Void p ) {
183
191
if (configuration .showModules ) {
184
192
addDescription (e , dl , si );
185
- configuration . moduleSearchIndex .add (si );
193
+ searchItems .add (si );
186
194
}
187
195
return null ;
188
196
}
189
197
190
198
@ Override
191
199
public Void visitPackage (PackageElement e , Void p ) {
192
200
addDescription (e , dl , si );
193
- configuration . packageSearchIndex .add (si );
201
+ searchItems .add (si );
194
202
return null ;
195
203
}
196
204
197
205
@ Override
198
206
public Void visitType (TypeElement e , Void p ) {
199
207
addDescription (e , dl , si );
200
- configuration . typeSearchIndex .add (si );
208
+ searchItems .add (si );
201
209
return null ;
202
210
}
203
211
204
212
@ Override
205
213
protected Void defaultAction (Element e , Void p ) {
206
214
addDescription (e , dl , si );
207
- configuration . memberSearchIndex .add (si );
215
+ searchItems .add (si );
208
216
return null ;
209
217
}
210
218
@@ -424,32 +432,32 @@ public String getNameForIndex(String unicode) {
424
432
protected void createSearchIndexFiles () throws DocFileIOException {
425
433
if (configuration .showModules ) {
426
434
createSearchIndexFile (DocPaths .MODULE_SEARCH_INDEX_JS ,
427
- configuration . moduleSearchIndex ,
435
+ searchItems . get ( SearchIndexItem . Category . MODULES ) ,
428
436
"moduleSearchIndex" );
429
437
}
430
438
if (!configuration .packages .isEmpty ()) {
431
439
SearchIndexItem si = new SearchIndexItem ();
432
440
si .setCategory (SearchIndexItem .Category .PACKAGES );
433
441
si .setLabel (resources .getText ("doclet.All_Packages" ));
434
442
si .setUrl (DocPaths .ALLPACKAGES_INDEX .getPath ());
435
- configuration . packageSearchIndex .add (si );
443
+ searchItems .add (si );
436
444
}
437
445
createSearchIndexFile (DocPaths .PACKAGE_SEARCH_INDEX_JS ,
438
- configuration . packageSearchIndex ,
446
+ searchItems . get ( SearchIndexItem . Category . PACKAGES ) ,
439
447
"packageSearchIndex" );
440
448
SearchIndexItem si = new SearchIndexItem ();
441
449
si .setCategory (SearchIndexItem .Category .TYPES );
442
450
si .setLabel (resources .getText ("doclet.All_Classes" ));
443
451
si .setUrl (DocPaths .ALLCLASSES_INDEX .getPath ());
444
- configuration . typeSearchIndex .add (si );
452
+ searchItems .add (si );
445
453
createSearchIndexFile (DocPaths .TYPE_SEARCH_INDEX_JS ,
446
- configuration . typeSearchIndex ,
454
+ searchItems . get ( SearchIndexItem . Category . TYPES ) ,
447
455
"typeSearchIndex" );
448
456
createSearchIndexFile (DocPaths .MEMBER_SEARCH_INDEX_JS ,
449
- configuration . memberSearchIndex ,
457
+ searchItems . get ( SearchIndexItem . Category . MEMBERS ) ,
450
458
"memberSearchIndex" );
451
459
createSearchIndexFile (DocPaths .TAG_SEARCH_INDEX_JS ,
452
- configuration . tagSearchIndex ,
460
+ searchItems . get ( SearchIndexItem . Category . SEARCH_TAGS ) ,
453
461
"tagSearchIndex" );
454
462
}
455
463
@@ -466,6 +474,8 @@ protected void createSearchIndexFile(DocPath searchIndexJS,
466
474
String varName )
467
475
throws DocFileIOException
468
476
{
477
+ // The file needs to be created even if there are no searchIndex items
478
+ // File could be written straight-through, without an intermediate StringBuilder
469
479
if (!searchIndex .isEmpty ()) {
470
480
StringBuilder searchVar = new StringBuilder ("[" );
471
481
boolean first = true ;
@@ -488,4 +498,18 @@ protected void createSearchIndexFile(DocPath searchIndexJS,
488
498
}
489
499
}
490
500
}
501
+
502
+ protected static Map <Character , List <SearchIndexItem >> buildSearchTagIndex (
503
+ Collection <? extends SearchIndexItem > searchItems )
504
+ {
505
+ Map <Character , List <SearchIndexItem >> map = new HashMap <>();
506
+ for (SearchIndexItem sii : searchItems ) {
507
+ String tagLabel = sii .getLabel ();
508
+ Character unicode = (tagLabel .length () == 0 )
509
+ ? '*'
510
+ : Character .toUpperCase (tagLabel .charAt (0 ));
511
+ map .computeIfAbsent (unicode , k -> new ArrayList <>()).add (sii );
512
+ }
513
+ return map ;
514
+ }
491
515
}
0 commit comments