001: package jimm.datavision;
002:
003: import jimm.util.XMLWriter;
004: import java.util.*;
005: import org.apache.bsf.BSFManager;
006: import org.apache.bsf.BSFException;
007:
008: /**
009: * Bean Scripting Framework management.
010: *
011: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
012: */
013: public class Scripting implements Writeable {
014:
015: protected static final String DEFAULT_LANGUAGE = "Ruby";
016: protected static final String DEFAULT_CLASS = "org.jruby.javasupport.bsf.JRubyEngine";
017:
018: protected Report report;
019: protected String defaultLanguage;
020: protected Map languages;
021: protected Map managers;
022:
023: public Scripting(Report report) {
024: this .report = report;
025: defaultLanguage = DEFAULT_LANGUAGE;
026: languages = new HashMap();
027: languages.put(DEFAULT_LANGUAGE, DEFAULT_CLASS);
028: managers = new HashMap();
029: }
030:
031: /** Returns an immutable copy of the map of languages. */
032: public Map getLanguages() {
033: return Collections.unmodifiableMap(languages);
034: }
035:
036: /** Adds a language to our list. */
037: public void addLanguage(String language, String className) {
038: languages.put(language, className);
039: }
040:
041: /**
042: * Replace the contents of our language map with the contents of
043: * <var>langs</var>.
044: *
045: * @param langs maps language names to class names
046: */
047: public void replaceLanguages(Map langs) {
048: languages.clear();
049: languages.putAll(langs);
050: }
051:
052: /** Returns the default language name. */
053: public String getDefaultLanguage() {
054: return defaultLanguage;
055: }
056:
057: /** Sets the default language name. */
058: public void setDefaultLanguage(String language) {
059: if (language == null)
060: throw new IllegalArgumentException(
061: "default language may not be null");
062: defaultLanguage = language;
063: }
064:
065: /**
066: * Returns true if we can load class <var>klass</var>.
067: */
068: public boolean canFind(String klass) {
069: BSFManager manager = new BSFManager();
070: boolean found = false;
071: try {
072: manager.getClassLoader().loadClass(klass);
073: found = true;
074: } catch (ClassNotFoundException e) {
075: }
076: return found;
077: }
078:
079: /**
080: * Evaluates an <var>evalString</var> using <var>language</var> and returns
081: * the results.
082: *
083: * @param language the language to use
084: * @param evalString the string to evaluate
085: * @param name a name (for example, a formula name) to display with error
086: * messages
087: * @return the result
088: */
089: public Object eval(String language, String evalString, String name)
090: throws BSFException {
091: return getBsfManager(language).eval(language, name, 1, 1,
092: evalString);
093: }
094:
095: /** Returns BSFManager for the default language. */
096: public BSFManager getBsfManager() throws BSFException {
097: return getBsfManager(defaultLanguage);
098: }
099:
100: /** Returns BSFManager for <var>language</var>. */
101: public BSFManager getBsfManager(String language)
102: throws BSFException {
103: BSFManager manager = (BSFManager) managers.get(language);
104: if (manager == null) {
105: manager = new BSFManager();
106: manager.declareBean("report", report, Report.class);
107: BSFManager.registerScriptingEngine(language,
108: (String) languages.get(language), null);
109: managers.put(language, manager);
110: }
111: return manager;
112: }
113:
114: public void writeXML(XMLWriter out) {
115: out.startElement("bean-scripting-framework");
116: out.attr("default-language", defaultLanguage);
117:
118: for (Iterator iter = languages.keySet().iterator(); iter
119: .hasNext();) {
120: String language = (String) iter.next();
121: out.startElement("language");
122: out.attr("name", language);
123: out.attr("class", languages.get(language));
124: out.endElement();
125: }
126:
127: out.endElement();
128: }
129:
130: }
|