2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Marcos CARDINOT
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.xcos.palette;
15 import java.io.BufferedReader;
17 import java.io.FileInputStream;
18 import java.io.FilenameFilter;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.nio.charset.StandardCharsets;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Hashtable;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.document.Field;
32 import org.apache.lucene.document.StringField;
33 import org.apache.lucene.document.TextField;
35 import org.scilab.modules.commons.ScilabConstants;
36 import org.scilab.modules.xcos.palette.model.PaletteBlock;
39 * Index the help pages of all blocks.
40 * @author Marcos Cardinot <mcardinot@gmail.com>
42 public final class PaletteIndexer {
44 private PaletteSearchManager mgr;
45 private List<String> roots;
49 * @param psm PaletteSearchManager
51 public PaletteIndexer(PaletteSearchManager psm) {
53 // javaHelp directories
54 roots = new ArrayList<String>();
55 roots.add(ScilabConstants.SCI.getAbsolutePath() + "/modules/helptools/javaHelp");
61 public void createIndex(Hashtable<String, List<PaletteBlock>> ht) {
63 mgr.getIndexWriter().deleteAll();
64 Set< Entry<String, List<PaletteBlock>> > treePaths = ht.entrySet();
65 for (Entry<String, List<PaletteBlock>> entry : treePaths) {
66 String treePath = entry.getKey();
67 List<PaletteBlock> blocks = entry.getValue();
68 for (PaletteBlock block : blocks) {
69 indexBlock(treePath, block.getName());
72 mgr.getIndexWriter().commit();
73 } catch (IOException e) {
79 * @param treePath tree path
80 * @param blockName block name
81 * @throws IOException If there is a low-level I/O error
83 private void indexBlock(String treePath, String blockName) throws IOException {
84 Document doc = new Document();
85 doc.add(new StringField("blockName", blockName, Field.Store.YES));
86 doc.add(new StringField("treePath", treePath, Field.Store.YES));
88 List<File> helpPages = findHelpPages(blockName);
89 if (helpPages.isEmpty()) {
90 doc.add(new TextField("helpPage", blockName, Field.Store.YES));
92 for (File helpPage : helpPages) {
93 InputStream stream = new FileInputStream(helpPage);
94 doc.add(new StringField("filePath", helpPage.getAbsolutePath(), Field.Store.YES));
95 doc.add(new TextField("helpPage", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
99 mgr.getIndexWriter().addDocument(doc);
103 * @param blockName block name
106 private List<File> findHelpPages(final String blockName) {
107 List<String> subdirs = new ArrayList<String>();
108 for (String root : roots) {
109 File r = new File(root);
114 String[] ss = r.list(new FilenameFilter() {
116 public boolean accept(File current, String name) {
117 return new File(current, name).isDirectory();
120 for (int i = 0; i < ss.length; ++i) {
121 ss[i] = root + File.separator + ss[i];
123 subdirs.addAll(Arrays.asList(ss));
126 List<File> helpPages = new ArrayList<File>();
127 for (String dir : subdirs) {
128 File file = new File(dir + File.separator + blockName + ".html");