Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / IndexerHelper.java @ 478

History | View | Annotate | Download (3.61 KB)

1
package org.gvsig.scripting.impl;
2

    
3
import com.sun.java.help.search.Indexer;
4
import java.io.File;
5
import java.io.IOException;
6
import java.lang.reflect.Field;
7
import java.util.Hashtable;
8
import java.util.logging.Level;
9
import org.apache.commons.io.FileUtils;
10
import org.apache.commons.lang3.StringUtils;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

    
14
public class IndexerHelper {
15

    
16
    protected static final Logger logger = LoggerFactory.getLogger(IndexerHelper.class);
17

    
18
    protected static Indexer indexer;
19

    
20
    public IndexerHelper() {
21

    
22
    }
23

    
24
    /**
25
     * Repeated invokation of the javahelp indexer (possibly via multiple
26
     * classloaders) is causing trouble, residue from previous invokations seems
27
     * to cause errors this is a nasty workaround for the problem. alternatively
28
     * we could try invoking the indexer from a separate jvm i guess, ut that's
29
     * more work.
30
     *
31
     */
32
    public void ClearStaticFieldsInJavaHelpIndexer() {
33
        if (indexer == null) {
34
            return;
35
        }
36
        try {
37
            Class<?> clazz = Class.forName("com.sun.java.help.search.Indexer");
38
            Field fld = clazz.getDeclaredField("kitRegistry");
39
            fld.setAccessible(true);
40
            Hashtable<?, ?> hash = (Hashtable<?, ?>) fld.get(null);
41
            hash.clear();
42

    
43
            clazz = Class.forName("com.sun.java.help.search.HTMLIndexerKit");
44
            fld = clazz.getDeclaredField("defaultParser");
45
            fld.setAccessible(true);
46
            fld.set(null, null);
47

    
48
            fld = clazz.getDeclaredField("defaultCallback");
49
            fld.setAccessible(true);
50
            fld.set(null, null);
51

    
52
        } catch (Exception ex) {
53
            logger.warn("Can't clear indexer", ex);
54
        }
55
    }
56

    
57
    private Indexer getIndexer() {
58
        if (indexer == null) {
59
            indexer = new Indexer();
60
        }
61
        return indexer;
62
    }
63

    
64
    public void index(File config, File searchdb, File docs) {
65
            /*
66
            https://docs.oracle.com/cd/E19253-01/819-0913/author/jhindexer.html
67
            
68
            
69
            Usage:   java JavaHelp.Index options file ...
70
            
71
            Options: -c file   config file
72
            -db file  generated database file name
73
            -verbose  verbose documentation
74
            -nostopwords ignore stop words
75
            -locale language_country_variant
76
            -logfile log file name
77
            Note: config file composition:
78
            IndexRemove /public_html/JavaHelp/demo
79
            IndexPrepend ..
80
            StopWords word1 ... wordN
81
            StopWordsFile stopWordFileName
82
            File /public_html/JavaHelp/demo/first.html
83
            File=/public_html/JavaHelp/demo/second.html
84
            ...
85
            */
86
            
87
        try {
88
            FileUtils.write(config,
89
                    "IndexRemove " + docs.getAbsolutePath() + "\n"
90
            );
91
        } catch (IOException ex) {
92
            logger.warn("Can't create indexer config file in '"+config.getAbsolutePath()+"'.",ex);
93
        }
94
        String[] args = new String[]{
95
            //"-verbose",
96
            "-c",
97
            config.getAbsolutePath(),
98
            "-db",
99
            searchdb.getAbsolutePath() + "/",
100
            docs.getAbsolutePath() + "/"
101
        };
102
        ClearStaticFieldsInJavaHelpIndexer();
103
        try {
104
            logger.info("Start indexing help, args: '"+StringUtils.join(args, ", ")+"'.");
105
            this.getIndexer().compile(args);
106
            logger.info("End indexing help, args: '"+StringUtils.join(args, ", ")+"'.");
107
        } catch (Exception e) {
108
            logger.info("Can't index help ('" + args + "').", e);
109
        }
110
    }
111
}