001: /*=============================================================================
002: * Copyright Texas Instruments 2003. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package oscript.bsf;
020:
021: import oscript.OscriptInterpreter;
022: import oscript.fs.*;
023: import oscript.data.*;
024: import oscript.exceptions.*;
025:
026: import org.apache.bsf.*;
027: import org.apache.bsf.util.BSFEngineImpl;
028: import org.apache.bsf.util.BSFFunctions;
029:
030: import java.util.Vector;
031: import java.util.Hashtable;
032: import java.io.*;
033:
034: /**
035: * BSF support for <a href="http://objectscript.sourceforge.net">ObjectScript</a>.
036: *
037: * @author Rob Clark (rob@ti.com)
038: */
039: public class ObjectScriptEngine extends BSFEngineImpl {
040: private static final Value[] EMPTY_ARGS = new Value[0];
041: private static final Scope GS = OscriptInterpreter.getGlobalScope();
042:
043: /**
044: * Initialize the engine.
045: */
046: public void initialize(BSFManager mgr, String lang,
047: Vector declaredBeans) throws BSFException {
048: super .initialize(mgr, lang, declaredBeans);
049:
050: GS.createMember("bsf", Reference.ATTR_CONST).opAssign(
051: JavaBridge.convertToScriptObject(new BSFFunctions(mgr,
052: this )));
053:
054: int sz = declaredBeans.size();
055: for (int i = 0; i < sz; i++)
056: declareBean((BSFDeclaredBean) (declaredBeans.elementAt(i)));
057: }
058:
059: /**
060: * Declare a bean
061: */
062: public void declareBean(BSFDeclaredBean bean) throws BSFException {
063: GS.createMember(bean.name, 0).opAssign(
064: JavaBridge.convertToScriptObject(bean.bean));
065: }
066:
067: /**
068: * Undeclare a previously declared bean.
069: */
070: public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
071: try {
072: GS.getMember(bean.name).opAssign(Value.NULL);
073: } catch (PackagedScriptObjectException e) {
074: // ignore... perhaps no such bean in the first place?
075: e.printStackTrace(); // XXX for now
076: }
077: }
078:
079: /**
080: * call the named method of the given object.
081: */
082: public Object call(Object object, String method, Object[] args)
083: throws BSFException {
084: Value[] oargs = (args == null) ? EMPTY_ARGS
085: : new Value[args.length];
086:
087: if (args != null)
088: for (int i = 0; i < args.length; i++)
089: oargs[i] = JavaBridge.convertToScriptObject(args[i]);
090:
091: Value oobject = (object == null) ? GS : JavaBridge
092: .convertToScriptObject(object);
093:
094: return oobject.getMember(method).callAsFunction(oargs)
095: .castToJavaObject();
096: }
097:
098: /**
099: * Evaluate an expression.
100: */
101: public Object eval(String source, int lineNo, int columnNo,
102: Object script) throws BSFException {
103: try {
104: return OscriptInterpreter.eval(
105: resolve(source, lineNo, columnNo, script))
106: .castToJavaObject();
107: } catch (Throwable e) {
108: // XXX deal with BSFException, wrapped exceptions, etc...
109: throw new BSFException(BSFException.REASON_OTHER_ERROR,
110: "ObjectScript Error: " + e.getMessage(), e);
111: }
112: }
113:
114: private static synchronized AbstractFile resolve(String source,
115: int lineNo, int columnNo, Object script) {
116: final String scriptStr = script.toString();
117:
118: /* note: encoding lineNo and columnNo into path should make things
119: * function properly of source is not itself a .os file, but is src
120: * code embedded in some other sort of document which potentially
121: * includes multiple sections of src code
122: */
123: String path = source + "." + lineNo + "." + columnNo + ".os";
124:
125: /* normalize path:
126: */
127: path = AbstractFileSystem.normalize(path);
128:
129: return new AbstractAbstractFile(path, false) {
130:
131: public InputStream getInputStream() throws IOException {
132: return new StringBufferInputStream(scriptStr);
133: }
134:
135: };
136: }
137: }
138:
139: /*
140: * Local Variables:
141: * tab-width: 2
142: * indent-tabs-mode: nil
143: * mode: java
144: * c-indentation-style: java
145: * c-basic-offset: 2
146: * eval: (c-set-offset 'substatement-open '0)
147: * eval: (c-set-offset 'case-label '+)
148: * eval: (c-set-offset 'inclass '+)
149: * eval: (c-set-offset 'inline-open '0)
150: * End:
151: */
|