| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.indexer; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.PrintWriter; |
| 10 |
import java.nio.charset.StandardCharsets; |
| 11 |
import java.nio.file.FileVisitResult; |
| 12 |
import java.nio.file.Files; |
| 13 |
import java.nio.file.Path; |
| 14 |
import java.nio.file.Paths; |
| 15 |
import java.nio.file.SimpleFileVisitor; |
| 16 |
import java.nio.file.attribute.BasicFileAttributes; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.HashSet; |
| 20 |
import java.util.StringTokenizer; |
| 21 |
import org.h2.util.StringUtils; |
| 22 |
|
| 23 |
/** |
| 24 |
* The indexer creates the fulltext index of the HTML documentation. |
| 25 |
* It is used for the built-in HTML javascript search. |
| 26 |
*/ |
| 27 |
public class Indexer { |
| 28 |
|
| 29 |
private static final int MIN_WORD_SIZE = 3; |
| 30 |
private static final int MAX_RELATIONS = 30; |
| 31 |
private static final String VERY_COMMON = |
| 32 |
";the;be;to;of;and;a;in;that;have;i;it;for;not;on;with;he;as;you;do;at;" + |
| 33 |
"this;but;his;by;from;they;we;say;her;she;or;an;will;my;one;all;would;" + |
| 34 |
"there;their;what;so;up;out;if;about;who;get;which;go;me;when;make;" + |
| 35 |
"can;like;no;just;him;know;take;into;your;good;some;" + |
| 36 |
"could;them;see;other;than;then;now;look;only;come;its;over;think;" + |
| 37 |
"also;back;after;use;two;how;our;work;first;well;way;even;new;want;" + |
| 38 |
"because;any;these;give;most;us;"; |
| 39 |
|
| 40 |
private final ArrayList<Page> pages = new ArrayList<>(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Lower case word to Word map. |
| 44 |
*/ |
| 45 |
private final HashMap<String, Word> words = new HashMap<>(); |
| 46 |
private final HashSet<String> noIndex = new HashSet<>(); |
| 47 |
private ArrayList <Word>wordList; |
| 48 |
private PrintWriter output; |
| 49 |
private Page page; |
| 50 |
private boolean title; |
| 51 |
private boolean heading; |
| 52 |
private String ignored; |
| 53 |
|
| 54 |
/** |
| 55 |
* This method is called when executing this application from the command |
| 56 |
* line. |
| 57 |
* |
| 58 |
* @param args the command line parameters |
| 59 |
* @throws Exception on failure |
| 60 |
*/ |
| 61 |
public static void main(String... args) throws Exception { |
| 62 |
new Indexer().run(args); |
| 63 |
} |
| 64 |
|
| 65 |
private void run(String... args) throws Exception { |
| 66 |
String dir = "docs"; |
| 67 |
String destDir = "docs/html"; |
| 68 |
for (int i = 0; i < args.length; i++) { |
| 69 |
if (args[i].equals("-dir")) { |
| 70 |
dir = args[++i]; |
| 71 |
} else if (args[i].equals("-destDir")) { |
| 72 |
destDir = args[++i]; |
| 73 |
} |
| 74 |
} |
| 75 |
Path directory = Paths.get(dir); |
| 76 |
setNoIndex("index.html", "html/header.html", "html/search.html", |
| 77 |
"html/frame.html", "html/fragments.html", |
| 78 |
"html/sourceError.html", "html/source.html", |
| 79 |
"html/mainWeb.html", "javadoc/index.html", |
| 80 |
"javadoc/classes.html", "javadoc/allclasses-frame.html", |
| 81 |
"javadoc/allclasses-noframe.html", |
| 82 |
"javadoc/constant-values.html", "javadoc/overview-frame.html", |
| 83 |
"javadoc/overview-summary.html", "javadoc/serialized-form.html"); |
| 84 |
output = new PrintWriter(Files.newBufferedWriter(Paths.get(destDir + "/index.js"))); |
| 85 |
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { |
| 86 |
@Override |
| 87 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 88 |
readPages(directory.relativize(file).toString().replace('\\', '/'), file); |
| 89 |
return FileVisitResult.CONTINUE; |
| 90 |
} |
| 91 |
}); |
| 92 |
output.println("var pages=new Array();"); |
| 93 |
output.println("var ref=new Array();"); |
| 94 |
output.println("var ignored='';"); |
| 95 |
output.println("function Page(title, file) { "); |
| 96 |
output.println(" this.title=title; this.file=file;"); |
| 97 |
output.println("}"); |
| 98 |
output.println("function load() {"); |
| 99 |
sortWords(); |
| 100 |
removeOverflowRelations(); |
| 101 |
sortPages(); |
| 102 |
listPages(); |
| 103 |
listWords(); |
| 104 |
output.println("}"); |
| 105 |
output.close(); |
| 106 |
} |
| 107 |
|
| 108 |
private void setNoIndex(String... strings) { |
| 109 |
for (String s : strings) { |
| 110 |
noIndex.add(s); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private void sortWords() { |
| 115 |
for (String name : new ArrayList<>(words.keySet())) { |
| 116 |
if (name.endsWith("s")) { |
| 117 |
String singular = name.substring(0, name.length() - 1); |
| 118 |
if (words.containsKey(singular)) { |
| 119 |
Word wp = words.get(name); |
| 120 |
Word ws = words.get(singular); |
| 121 |
ws.addAll(wp); |
| 122 |
words.remove(name); |
| 123 |
} |
| 124 |
} else if (name.startsWith("abc")) { |
| 125 |
words.remove(name); |
| 126 |
} |
| 127 |
} |
| 128 |
wordList = new ArrayList<>(words.values()); |
| 129 |
// ignored very common words (to shrink the index) |
| 130 |
StringBuilder ignoredBuff = new StringBuilder(";"); |
| 131 |
int maxSize = pages.size() / 4; |
| 132 |
for (int i = 0; i < wordList.size(); i++) { |
| 133 |
Word word = wordList.get(i); |
| 134 |
String search = ";" + word.name.toLowerCase() + ";"; |
| 135 |
int idxCommon = VERY_COMMON.indexOf(search); |
| 136 |
if (word.pages.size() >= maxSize || idxCommon >= 0) { |
| 137 |
wordList.remove(i); |
| 138 |
ignoredBuff.append(word.name); |
| 139 |
ignoredBuff.append(';'); |
| 140 |
i--; |
| 141 |
} |
| 142 |
} |
| 143 |
ignored = ignoredBuff.toString(); |
| 144 |
// TODO support A, B, C,... class links in the index file and use them |
| 145 |
// for combined AND searches |
| 146 |
wordList.sort((w0, w1) -> w0.name.compareToIgnoreCase(w1.name)); |
| 147 |
} |
| 148 |
|
| 149 |
private void removeOverflowRelations() { |
| 150 |
for (Word word : wordList) { |
| 151 |
ArrayList<Weight> weights = word.getSortedWeights(); |
| 152 |
int max = MAX_RELATIONS; |
| 153 |
if (weights.size() > max) { |
| 154 |
while (max < weights.size()) { |
| 155 |
Weight weight = weights.get(max); |
| 156 |
if (weight.value < Weight.HEADER) { |
| 157 |
break; |
| 158 |
} |
| 159 |
max++; |
| 160 |
} |
| 161 |
} |
| 162 |
while (max < weights.size()) { |
| 163 |
Weight weight = weights.get(max); |
| 164 |
weights.remove(max); |
| 165 |
weight.page.relations--; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
private void sortPages() { |
| 171 |
pages.sort((p0, p1) -> Integer.compare(p1.relations, p0.relations)); |
| 172 |
for (int i = 0; i < pages.size(); i++) { |
| 173 |
pages.get(i).id = i; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private void listPages() { |
| 178 |
for (Page p : pages) { |
| 179 |
output.println("pages[" + p.id + "]=new Page('" |
| 180 |
+ convertUTF(p.title) + "', '" + p.fileName + "');"); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Read the pages of a file. |
| 186 |
* |
| 187 |
* @param fileName the file name |
| 188 |
* @param file the path |
| 189 |
*/ |
| 190 |
void readPages(String fileName, Path file) throws IOException { |
| 191 |
String lower = StringUtils.toLowerEnglish(fileName); |
| 192 |
if (!lower.endsWith(".html") && !lower.endsWith(".htm")) { |
| 193 |
return; |
| 194 |
} |
| 195 |
if (!noIndex.contains(fileName)) { |
| 196 |
page = new Page(pages.size(), fileName); |
| 197 |
pages.add(page); |
| 198 |
readPage(file); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private void listWords() { |
| 203 |
output.println("// words: " + wordList.size()); |
| 204 |
StringBuilder buff = new StringBuilder(); |
| 205 |
String first = ""; |
| 206 |
int firstLen = 1; |
| 207 |
int totalRelations = 0; |
| 208 |
for (Word word : wordList) { |
| 209 |
ArrayList<Weight> weights = word.getSortedWeights(); |
| 210 |
String lower = StringUtils.toLowerEnglish(word.name); |
| 211 |
if (!first.equals(lower.substring(0, firstLen))) { |
| 212 |
if (buff.length() > 0) { |
| 213 |
output.println("ref['" + convertUTF(first) + "']='" + buff.toString() + "';"); |
| 214 |
buff = new StringBuilder(); |
| 215 |
} |
| 216 |
first = lower.substring(0, firstLen); |
| 217 |
} |
| 218 |
if (buff.length() > 0) { |
| 219 |
buff.append(';'); |
| 220 |
} |
| 221 |
buff.append(convertUTF(word.name)); |
| 222 |
buff.append('='); |
| 223 |
String weightString = "r"; |
| 224 |
totalRelations += weights.size(); |
| 225 |
for (int j = 0; j < weights.size(); j++) { |
| 226 |
Weight weight = weights.get(j); |
| 227 |
Page p = weight.page; |
| 228 |
if (j > 0) { |
| 229 |
buff.append(","); |
| 230 |
} |
| 231 |
String ws; |
| 232 |
if (weight.value >= Weight.TITLE) { |
| 233 |
ws = "t"; |
| 234 |
} else if (weight.value >= Weight.HEADER) { |
| 235 |
ws = "h"; |
| 236 |
} else { |
| 237 |
ws = "r"; |
| 238 |
} |
| 239 |
if (!ws.equals(weightString)) { |
| 240 |
weightString = ws; |
| 241 |
buff.append(ws); |
| 242 |
} |
| 243 |
buff.append(p.id); |
| 244 |
} |
| 245 |
} |
| 246 |
output.println("ref['" + convertUTF(first) + "']='" + buff.toString() + "';"); |
| 247 |
output.println("// totalRelations: " + totalRelations); |
| 248 |
output.println("ignored='" + ignored.toLowerCase() + "';"); |
| 249 |
} |
| 250 |
|
| 251 |
private void readPage(Path file) throws IOException { |
| 252 |
String text = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); |
| 253 |
StringTokenizer t = new StringTokenizer(text, "<> \r\n", true); |
| 254 |
boolean inTag = false; |
| 255 |
title = false; |
| 256 |
heading = false; |
| 257 |
while (t.hasMoreTokens()) { |
| 258 |
String token = t.nextToken(); |
| 259 |
if (token.length() == 1) { |
| 260 |
char c = token.charAt(0); |
| 261 |
switch (c) { |
| 262 |
case '<': { |
| 263 |
if (inTag) { |
| 264 |
process("???"); |
| 265 |
} |
| 266 |
inTag = true; |
| 267 |
if (!t.hasMoreTokens()) { |
| 268 |
break; |
| 269 |
} |
| 270 |
token = t.nextToken(); |
| 271 |
if (token.startsWith("/")) { |
| 272 |
title = false; |
| 273 |
heading = false; |
| 274 |
} else if (token.equalsIgnoreCase("title")) { |
| 275 |
title = true; |
| 276 |
} else if (token.length() == 2 && Character.toLowerCase(token.charAt(0)) == 'h' |
| 277 |
&& Character.isDigit(token.charAt(1))) { |
| 278 |
heading = true; |
| 279 |
} |
| 280 |
// TODO maybe skip script tags? |
| 281 |
break; |
| 282 |
} |
| 283 |
case '>': { |
| 284 |
if (!inTag) { |
| 285 |
process("???"); |
| 286 |
} |
| 287 |
inTag = false; |
| 288 |
break; |
| 289 |
} |
| 290 |
case '\r': |
| 291 |
case '\n': |
| 292 |
case ' ': |
| 293 |
break; |
| 294 |
default: |
| 295 |
if (!inTag) { |
| 296 |
process(token); |
| 297 |
} |
| 298 |
} |
| 299 |
} else { |
| 300 |
if (!inTag) { |
| 301 |
process(token); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (page.title == null || page.title.trim().length() == 0) { |
| 307 |
String title = file.getFileName().toString(); |
| 308 |
System.out.println("Error: not title found in " + title); |
| 309 |
page.title = title; |
| 310 |
} |
| 311 |
page.title = page.title.trim(); |
| 312 |
} |
| 313 |
|
| 314 |
private void process(String text) { |
| 315 |
text = HtmlConverter.convertHtmlToString(text); |
| 316 |
if (title) { |
| 317 |
if (page.title == null) { |
| 318 |
page.title = text; |
| 319 |
} else { |
| 320 |
page.title = page.title + " " + text; |
| 321 |
} |
| 322 |
} |
| 323 |
int weight; |
| 324 |
if (title) { |
| 325 |
weight = Weight.TITLE; |
| 326 |
} else if (heading) { |
| 327 |
weight = Weight.HEADER; |
| 328 |
} else { |
| 329 |
weight = Weight.PARAGRAPH; |
| 330 |
} |
| 331 |
// this list of constants needs to be the same in search.js |
| 332 |
// (char) 160: nbsp |
| 333 |
StringTokenizer t = new StringTokenizer(text, |
| 334 |
" \t\r\n\"'.,:;!&/\\?%@`[]{}()+-=<>|*^~#$" + (char) 160, false); |
| 335 |
while (t.hasMoreTokens()) { |
| 336 |
String token = t.nextToken(); |
| 337 |
if (token.length() < MIN_WORD_SIZE) { |
| 338 |
continue; |
| 339 |
} |
| 340 |
if (Character.isDigit(token.charAt(0))) { |
| 341 |
continue; |
| 342 |
} |
| 343 |
String lower = StringUtils.toLowerEnglish(token); |
| 344 |
Word word = words.get(lower); |
| 345 |
if (word == null) { |
| 346 |
word = new Word(token); |
| 347 |
words.put(lower, word); |
| 348 |
} else if (!word.name.equals(token)) { |
| 349 |
word.name = token.compareTo(word.name) > 0 ? token : word.name; |
| 350 |
} |
| 351 |
page.totalWeight += weight; |
| 352 |
word.addPage(page, weight); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
private static String convertUTF(String s) { |
| 357 |
s = StringUtils.quoteJavaString(s); |
| 358 |
s = s.substring(1, s.length() - 1); |
| 359 |
return s; |
| 360 |
} |
| 361 |
|
| 362 |
} |