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;
34 import org.scilab.modules.commons.ScilabConstants;
35 import org.scilab.modules.xcos.palette.model.PaletteBlock;
38 * Index the help pages of all blocks.
39 * @author Marcos Cardinot <mcardinot@gmail.com>
41 public final class PaletteIndexer {
43 private PaletteSearchManager mgr;
44 private List<String> roots;
48 * @param psm PaletteSearchManager
50 public PaletteIndexer(PaletteSearchManager psm) {
52 // javaHelp directories
53 roots = new ArrayList<String>();
54 roots.add(ScilabConstants.SCI.getAbsolutePath() + "/modules/helptools/javaHelp");
60 public void createIndex(Hashtable<String, List<PaletteBlock>> ht) {
62 mgr.getIndexWriter().deleteAll();
63 Set<String> treePaths = ht.keySet();
64 for (String treePath : treePaths) {
65 List<PaletteBlock> blocks = ht.get(treePath);
66 for (PaletteBlock block : blocks) {
67 indexBlock(treePath, block.getName());
70 mgr.getIndexWriter().commit();
71 } catch (IOException e) {
77 * @param treePath tree path
78 * @param blockName block name
79 * @throws IOException If there is a low-level I/O error
81 private void indexBlock(String treePath, String blockName) throws IOException {
82 Document doc = new Document();
83 doc.add(new StringField("blockName", blockName, Field.Store.YES));
84 doc.add(new StringField("treePath", treePath, Field.Store.YES));
86 List<File> helpPages = findHelpPages(blockName);
87 if (helpPages.isEmpty()) {
88 doc.add(new TextField("helpPage", blockName, Field.Store.YES));
90 for (File helpPage : helpPages) {
91 InputStream stream = new FileInputStream(helpPage);
92 doc.add(new StringField("filePath", helpPage.getAbsolutePath(), Field.Store.YES));
93 doc.add(new TextField("helpPage", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
97 mgr.getIndexWriter().addDocument(doc);
101 * @param blockName block name
104 private List<File> findHelpPages(final String blockName) {
105 List<String> subdirs = new ArrayList<String>();
106 for (String root : roots) {
107 File r = new File(root);
112 String[] ss = r.list(new FilenameFilter() {
113 public boolean accept(File current, String name) {
114 return new File(current, name).isDirectory();
117 for (int i = 0; i < ss.length; ++i) {
118 ss[i] = root + File.separator + ss[i];
120 subdirs.addAll(Arrays.asList(ss));
123 List<File> helpPages = new ArrayList<File>();
124 for (String dir : subdirs) {
125 File file = new File(dir + File.separator + blockName + ".html");