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;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.document.Field;
31 import org.apache.lucene.document.StringField;
32 import org.apache.lucene.document.TextField;
33 import org.scilab.modules.xcos.palette.model.PaletteBlock;
36 * Index the help pages of all blocks.
37 * @author Marcos Cardinot <mcardinot@gmail.com>
39 public final class PaletteIndexer {
41 private PaletteSearchManager mgr;
42 private List<String> roots;
46 * @param psm PaletteSearchManager
48 public PaletteIndexer(PaletteSearchManager psm) {
50 // javaHelp directories
51 roots = new ArrayList<String>();
52 roots.add("/home/cardinot/scilab/scilab/modules/helptools/javaHelp"); // TODO fix path
58 public void createIndex(Hashtable<String, List<PaletteBlock>> ht) {
60 mgr.getIndexWriter().deleteAll();
61 Set<String> treePaths = ht.keySet();
62 for (String treePath : treePaths) {
63 List<PaletteBlock> blocks = ht.get(treePath);
64 for (PaletteBlock block : blocks) {
65 indexBlock(treePath, block.getName());
68 mgr.getIndexWriter().commit();
69 } catch (IOException e) {
75 * @param treePath tree path
76 * @param blockName block name
77 * @throws IOException If there is a low-level I/O error
79 private void indexBlock(String treePath, String blockName) throws IOException {
80 Document doc = new Document();
81 doc.add(new StringField("blockName", blockName, Field.Store.YES));
82 doc.add(new StringField("treePath", treePath, Field.Store.YES));
84 List<File> helpPages = findHelpPages(blockName);
85 if (helpPages.isEmpty()) {
86 doc.add(new TextField("helpPage", blockName, Field.Store.YES));
88 for (File helpPage : helpPages) {
89 InputStream stream = new FileInputStream(helpPage);
90 doc.add(new StringField("filePath", helpPage.getAbsolutePath(), Field.Store.YES));
91 doc.add(new TextField("helpPage", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
95 mgr.getIndexWriter().addDocument(doc);
99 * @param blockName block name
102 private List<File> findHelpPages(final String blockName) {
103 List<String> subdirs = new ArrayList<String>();
104 for (String root : roots) {
105 File r = new File(root);
110 String[] ss = r.list(new FilenameFilter() {
111 public boolean accept(File current, String name) {
112 return new File(current, name).isDirectory();
115 for (int i = 0; i < ss.length; ++i) {
116 ss[i] = root + File.separator + ss[i];
118 subdirs.addAll(Arrays.asList(ss));
121 List<File> helpPages = new ArrayList<File>();
122 for (String dir : subdirs) {
123 File file = new File(dir + File.separator + blockName + ".html");