2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Marcos CARDINOT
4 * Copyright (C) 2017 - Scilab Enterprises - Clement DAVID
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * 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.IOException;
18 import java.io.InputStreamReader;
19 import java.net.MalformedURLException;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.SimpleFileVisitor;
25 import java.nio.file.attribute.BasicFileAttributes;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.Optional;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import org.apache.lucene.document.Document;
36 import org.apache.lucene.document.Field;
37 import org.apache.lucene.document.StringField;
38 import org.apache.lucene.document.TextField;
40 import org.scilab.modules.commons.ScilabConstants;
41 import org.scilab.modules.xcos.palette.model.PaletteBlock;
44 * Index the help pages of all blocks.
46 * @author Marcos Cardinot <mcardinot@gmail.com>
48 public final class PaletteIndexer {
50 private PaletteSearchManager mgr;
51 private List<File> roots;
56 * @param psm PaletteSearchManager
58 public PaletteIndexer(PaletteSearchManager psm) {
60 // javaHelp directories
61 roots = new ArrayList<>();
64 roots.add(new File(ScilabConstants.SCI.getAbsolutePath() + "/modules/helptools/javaHelp"));
67 roots.add(new File(ScilabConstants.SCI.getAbsolutePath() + "/modules/helptools/jar"));
70 public void createIndex(Map<String, PaletteBlock> blockNameToPalette) {
72 mgr.getIndexWriter().deleteAll();
74 // insert all block names
75 for (String blk : blockNameToPalette.keySet()) {
79 // insert all help pages
80 for (File r : roots) {
85 Files.walkFileTree(r.toPath(), new SimpleFileVisitor<Path>() {
87 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
88 String fname = file.getFileName().toString();
89 int dot = fname.lastIndexOf('.');
91 return FileVisitResult.CONTINUE;
94 String basename = fname.substring(0, dot);
95 if (fname.endsWith(".jar")) {
96 try (JarFile jf = new JarFile(file.toFile())) {
97 index(file.toAbsolutePath().toUri().toString(), basename, jf, blockNameToPalette);
99 } else if (fname.endsWith(".html")) {
100 // this is a regular file
101 if (blockNameToPalette.containsKey(basename)) {
102 index(basename, file.toUri().toURL());
106 return FileVisitResult.CONTINUE;
111 mgr.getIndexWriter().commit();
112 } catch (IOException ex) {
113 Logger.getLogger(PaletteIndexer.class.getName()).log(Level.SEVERE, null, ex);
117 private void index(String fileURI, String basename, JarFile jf, Map<String, PaletteBlock> blockNameToTreePath) {
120 Entry(JarEntry jarEntry) {
129 int slash = je.getName().lastIndexOf('/') + 1;
130 int dot = je.getName().lastIndexOf('.');
131 if (slash < 0 || dot < 0) {
135 blk = je.getName().substring(slash, dot);
141 // we have to generate a url in the form before indexing
142 // jar:file:/modules/helptools/jar/scilab_en_US_help.jar!/scilab_en_US_help/blockName.html
143 url = new URL("jar:" + fileURI + "!/" + basename + '/' + blk + ".html");
144 } catch (MalformedURLException ex) {
145 Logger.getLogger(PaletteIndexer.class.getName()).log(Level.SEVERE, null, ex);
152 .map(je -> new Entry(je))
153 .filter(e -> e.block())
154 .filter(e -> blockNameToTreePath.containsKey(e.blk))
155 .filter(e -> e.url())
156 .forEach(e -> index(e.blk, e.url));
159 private void index(String basename, URL url) {
161 Document doc = new Document();
163 // add the block name
164 Field refname = new TextField("refname", basename, Field.Store.YES);
165 refname.setBoost(100f);
168 // add the refpurpose
169 try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()))) {
170 Optional<String> found = r.lines().filter(l -> l.contains("refpurpose")).findFirst();
173 if (found.isPresent()) {
174 refpurpose = new TextField("refpurpose", found.get(), Field.Store.YES);
176 refpurpose = new TextField("refpurpose", "", Field.Store.YES);
179 refpurpose.setBoost(10f);
183 // add the html content
184 try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()))) {
185 doc.add(new TextField("content", r));
188 mgr.getIndexWriter().addDocument(doc);
189 } catch (IOException e) {
190 Logger.getLogger(PaletteIndexer.class.getName()).log(Level.SEVERE, null, e);
194 private void index(String block) {
196 Document doc = new Document();
197 doc.add(new StringField("refname", block, Field.Store.YES));
198 doc.add(new StringField("refpurpose", block, Field.Store.YES));
199 doc.add(new TextField("content", block, Field.Store.YES));
201 mgr.getIndexWriter().addDocument(doc);
202 } catch (IOException e) {
203 Logger.getLogger(PaletteIndexer.class.getName()).log(Level.SEVERE, null, e);