// Path: src/org/owasp/webscarab/model/Preferences.java
// public class Preferences {
//     
//     static Properties _props = new Properties();
//     private static String _location = null;
//     
//     /** Creates a new instance of Preferences */
//     private Preferences() {
//     }
//     
//     public static Properties getPreferences() {
//         return _props;
//     }
//     
//     public static void loadPreferences(String file) throws IOException {
//         // If we are given a filename to load, use it, otherwise
//         // look for a props file in the user's home directory
//         // if the file does not exist, use the standard defaults
//         
//         if (file == null) {
//             String sep = System.getProperty("file.separator");
//             String home = System.getProperty("user.home");
//             _location = home + sep + "WebScarab.properties";
//         } else {
//             _location = file;
//         }
//         
//         try {
//             Properties props = new Properties();
//             InputStream is = new FileInputStream(_location);
//             props.load(is);
//             _props = props;
//         } catch (FileNotFoundException fnfe) {
//             // we'll just use the defaults
//         }
//     }
//     
//     public static void savePreferences() throws IOException {
//         FileOutputStream fos = new FileOutputStream(_location);
//         _props.store(fos,"WebScarab Properties");
//         fos.close();
//     }
//     
//     public static void setPreference(String key, String value) {
//         _props.setProperty(key, value);
//     }
//     
//     public static String getPreference(String key) {
//         String result = System.getProperty(key);
//         if (result == null)
//             result = _props.getProperty(key);
//         return result;
//     }
//     
//     public static String getPreference(String key, String defaultValue) {
//         String result = getPreference(key);
//         if (result == null)
//             result = defaultValue;
//         return result;
//     }
//     
//     public static String remove(String key) {
//         return (String) _props.remove(key);
//     }
//     
// }


import java.io.File;
import java.io.IOException;
import org.apache.bsf.BSFManager;
import org.apache.bsf.BSFException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;
import javax.swing.event.EventListenerList;
import org.owasp.webscarab.model.Preferences;
/*
 * ScriptManager.java
 *
 * Created on 07 January 2005, 04:42
 */

package org.owasp.webscarab.plugin;



/**
 *
 * @author  rogan
 */
public class ScriptManager {
    
    private BSFManager _bsfManager;
    private TreeMap<String, Hook[]> _hooks = new TreeMap<String, Hook[]>();
    private EventListenerList _listeners = new EventListenerList();
    
    private Logger _logger = Logger.getLogger(getClass().getName());
    
    /** Creates a new instance of ScriptManager */
    public ScriptManager(Framework framework) {
        try {
            _bsfManager = new BSFManager();
            _bsfManager.declareBean("framework", framework, framework.getClass());
            _bsfManager.declareBean("out", System.out, System.out.getClass());
            _bsfManager.declareBean("err", System.err, System.out.getClass());
        } catch (BSFException bsfe) {
            _logger.severe("Declaring a bean should not throw an exception! " + bsfe);
        }
    }
    
    public void addScriptListener(ScriptListener listener) {
        synchronized(_listeners) {
            _listeners.add(ScriptListener.class, listener);
        }
    }
    
    public void removeScriptListener(ScriptListener listener) {
        synchronized(_listeners) {
            _listeners.remove(ScriptListener.class, listener);
        }
    }
    
    public void registerHooks(String pluginName, Hook[] hooks) {
        if (hooks != null && hooks.length > 0) {
            _hooks.put(pluginName, hooks);
            for (int i=0; i<hooks.length; i++) {
                hooks[i].setBSFManager(_bsfManager);
            }
            fireHooksChanged();
        }
    }
    
    public int getPluginCount() {
        return _hooks.size();
    }
    
    public String getPlugin(int i) {
        String[] plugins = _hooks.keySet().toArray(new String[0]);
        return plugins[i];
    }
    
    public int getHookCount(String plugin) {
        Hook[] hooks = _hooks.get(plugin);
        if (hooks == null) return 0;
        return hooks.length;
    }
    
    public Hook getHook(String plugin, int i) {
        Hook[] hooks = _hooks.get(plugin);
        if (hooks == null) return null;
        return hooks[i];
    }
    
    public void addScript(String plugin, Hook hook, Script script, int position) throws BSFException {
        String language = BSFManager.getLangFromFilename(script.getFile().getName());
        if (language != null) {
            script.setLanguage(language);
            script.setEnabled(true);
            hook.addScript(script, position);
            fireScriptAdded(plugin, hook, script);
        }
    }
    
    public void addScript(String plugin, Hook hook, Script script) throws BSFException {
        addScript(plugin, hook, script, hook.getScriptCount());
    }
    
    public void setEnabled(String plugin, Hook hook, Script script, boolean enabled) {
        script.setEnabled(enabled);
        fireScriptChanged(plugin, hook, script);
    }
    
    public void removeScript(String plugin, Hook hook, Script script) {
        int count = hook.getScriptCount();
        for (int i=0; i<count; i++) {
            Script s = hook.getScript(i);
            if (s == script) {
                hook.removeScript(i);
                fireScriptRemoved(plugin, hook, script);
                return;
            }
        }
    }
    
    public void loadScripts() {
        Iterator<Map.Entry<String, Hook[]>> hookIt = _hooks.entrySet().iterator();
        while (hookIt.hasNext()) {
            Map.Entry<String, Hook[]> entry = hookIt.next();
            String plugin =  entry.getKey();
            Hook[] hooks = entry.getValue();
            if (hooks != null) {
                for (int i=0; i<hooks.length; i++) {
                    for (int j=0; j<hooks[i].getScriptCount(); j++)
                        hooks[i].removeScript(j);
                    int j=0;
// Path: src/org/owasp/webscarab/model/Preferences.java
// public class Preferences {
//     
//     static Properties _props = new Properties();
//     private static String _location = null;
//     
//     /** Creates a new instance of Preferences */
//     private Preferences() {
//     }
//     
//     public static Properties getPreferences() {
//         return _props;
//     }
//     
//     public static void loadPreferences(String file) throws IOException {
//         // If we are given a filename to load, use it, otherwise
//         // look for a props file in the user's home directory
//         // if the file does not exist, use the standard defaults
//         
//         if (file == null) {
//             String sep = System.getProperty("file.separator");
//             String home = System.getProperty("user.home");
//             _location = home + sep + "WebScarab.properties";
//         } else {
//             _location = file;
//         }
//         
//         try {
//             Properties props = new Properties();
//             InputStream is = new FileInputStream(_location);
//             props.load(is);
//             _props = props;
//         } catch (FileNotFoundException fnfe) {
//             // we'll just use the defaults
//         }
//     }
//     
//     public static void savePreferences() throws IOException {
//         FileOutputStream fos = new FileOutputStream(_location);
//         _props.store(fos,"WebScarab Properties");
//         fos.close();
//     }
//     
//     public static void setPreference(String key, String value) {
//         _props.setProperty(key, value);
//     }
//     
//     public static String getPreference(String key) {
//         String result = System.getProperty(key);
//         if (result == null)
//             result = _props.getProperty(key);
//         return result;
//     }
//     
//     public static String getPreference(String key, String defaultValue) {
//         String result = getPreference(key);
//         if (result == null)
//             result = defaultValue;
//         return result;
//     }
//     
//     public static String remove(String key) {
//         return (String) _props.remove(key);
//     }
//     
// }


// Path: src/org/owasp/webscarab/plugin/ScriptManager.java
import java.io.File;
import java.io.IOException;
import org.apache.bsf.BSFManager;
import org.apache.bsf.BSFException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;
import javax.swing.event.EventListenerList;
import org.owasp.webscarab.model.Preferences;

/*
 * ScriptManager.java
 *
 * Created on 07 January 2005, 04:42
 */

package org.owasp.webscarab.plugin;



/**
 *
 * @author  rogan
 */
public class ScriptManager {
    
    private BSFManager _bsfManager;
    private TreeMap<String, Hook[]> _hooks = new TreeMap<String, Hook[]>();
    private EventListenerList _listeners = new EventListenerList();
    
    private Logger _logger = Logger.getLogger(getClass().getName());
    
    /** Creates a new instance of ScriptManager */
    public ScriptManager(Framework framework) {
        try {
            _bsfManager = new BSFManager();
            _bsfManager.declareBean("framework", framework, framework.getClass());
            _bsfManager.declareBean("out", System.out, System.out.getClass());
            _bsfManager.declareBean("err", System.err, System.out.getClass());
        } catch (BSFException bsfe) {
            _logger.severe("Declaring a bean should not throw an exception! " + bsfe);
        }
    }
    
    public void addScriptListener(ScriptListener listener) {
        synchronized(_listeners) {
            _listeners.add(ScriptListener.class, listener);
        }
    }
    
    public void removeScriptListener(ScriptListener listener) {
        synchronized(_listeners) {
            _listeners.remove(ScriptListener.class, listener);
        }
    }
    
    public void registerHooks(String pluginName, Hook[] hooks) {
        if (hooks != null && hooks.length > 0) {
            _hooks.put(pluginName, hooks);
            for (int i=0; i<hooks.length; i++) {
                hooks[i].setBSFManager(_bsfManager);
            }
            fireHooksChanged();
        }
    }
    
    public int getPluginCount() {
        return _hooks.size();
    }
    
    public String getPlugin(int i) {
        String[] plugins = _hooks.keySet().toArray(new String[0]);
        return plugins[i];
    }
    
    public int getHookCount(String plugin) {
        Hook[] hooks = _hooks.get(plugin);
        if (hooks == null) return 0;
        return hooks.length;
    }
    
    public Hook getHook(String plugin, int i) {
        Hook[] hooks = _hooks.get(plugin);
        if (hooks == null) return null;
        return hooks[i];
    }
    
    public void addScript(String plugin, Hook hook, Script script, int position) throws BSFException {
        String language = BSFManager.getLangFromFilename(script.getFile().getName());
        if (language != null) {
            script.setLanguage(language);
            script.setEnabled(true);
            hook.addScript(script, position);
            fireScriptAdded(plugin, hook, script);
        }
    }
    
    public void addScript(String plugin, Hook hook, Script script) throws BSFException {
        addScript(plugin, hook, script, hook.getScriptCount());
    }
    
    public void setEnabled(String plugin, Hook hook, Script script, boolean enabled) {
        script.setEnabled(enabled);
        fireScriptChanged(plugin, hook, script);
    }
    
    public void removeScript(String plugin, Hook hook, Script script) {
        int count = hook.getScriptCount();
        for (int i=0; i<count; i++) {
            Script s = hook.getScript(i);
            if (s == script) {
                hook.removeScript(i);
                fireScriptRemoved(plugin, hook, script);
                return;
            }
        }
    }
    
    public void loadScripts() {
        Iterator<Map.Entry<String, Hook[]>> hookIt = _hooks.entrySet().iterator();
        while (hookIt.hasNext()) {
            Map.Entry<String, Hook[]> entry = hookIt.next();
            String plugin =  entry.getKey();
            Hook[] hooks = entry.getValue();
            if (hooks != null) {
                for (int i=0; i<hooks.length; i++) {
                    for (int j=0; j<hooks[i].getScriptCount(); j++)
                        hooks[i].removeScript(j);
                    int j=0;
                    String scriptName = Preferences.getPreference(hooks[i].getName()+"."+j+".name");