001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.commonplugin.help;
005    
006    import graphlab.platform.core.BlackBoard;
007    import graphlab.platform.StaticUtils;
008    import graphlab.platform.plugin.Plugger;
009    
010    import java.io.*;
011    import java.net.URL;
012    import java.util.Enumeration;
013    import java.util.Hashtable;
014    import java.util.jar.JarEntry;
015    import java.util.jar.JarFile;
016    
017    public class Utils {
018    
019        public static String helpPreDestDir = "Doc/";
020    
021        public static Hashtable<String, URL> pluginHelps = new Hashtable<String, URL>();
022    
023        /**
024         * Register a help entry on help menu.
025         *
026         * @param blackboard blackboard!
027         * @param pluginName name of plugin (= jar file) containing help files.
028         * @param title      Title that will be shown to user for help entry
029         * @param filter     Prefix path of help content in jar file
030         * @return <code>URL</code> of index of registered help.
031         *         This url will be opened when user selects the registered
032         *         title of help.
033         */
034        public static URL registerHelpPlugin(BlackBoard blackboard,
035                                             String pluginName, String title, String filter) {
036            try {
037                Plugger plugger = blackboard.getData(Plugger.PLUGGER_INSTANCE);
038                if (plugger == null)
039                    return null;
040                String index = "index" + plugger.versions.get(pluginName) + ".html";
041                String dest = Utils.helpPreDestDir + pluginName;
042                File f = new File(helpPreDestDir);
043                if (!f.isDirectory())
044                    f.mkdir();
045                f = new File(dest);
046                if (!f.isDirectory())
047                    f.mkdir();
048                f = new File(dest + "/" + index);
049                if (!f.isFile()) {
050                    JarFile jarFile = new JarFile(plugger.files.get(pluginName));
051                    Enumeration<JarEntry> entries = jarFile.entries();
052                    while (entries.hasMoreElements()) {
053                        JarEntry je = entries.nextElement();
054    
055                        if (!je.getName().startsWith(filter))
056                            continue;
057    
058                        System.out.println("Extracting " + je.getName());
059                        String fname = je.getName().substring(filter.length());
060    
061                        if (je.isDirectory())
062                            (new File(dest + "/" + fname)).mkdir();
063                        else {
064                            File efile = new File(dest, fname);
065    
066                            InputStream in = new BufferedInputStream(jarFile
067                                    .getInputStream(je));
068                            OutputStream out = new BufferedOutputStream(
069                                    new FileOutputStream(efile));
070    
071                            StaticUtils.copyStream(in, out);
072    
073                            out.flush();
074                            out.close();
075                            in.close();
076                        }
077                    }
078                }
079                URL url = f.toURL();
080                pluginHelps.put(title, url);
081                System.out.println("Help of plugin '" + pluginName
082                        + "' with name '" + title + "' Registered.");
083                return url;
084            } catch (Exception e) {
085                StaticUtils.addExceptiontoLog(e, blackboard);
086            }
087            return null;
088        }
089    
090    }