From bfc1b8914de01d197e73527c8ce8a13235c3ac3e Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 7 Aug 2003 16:11:31 +0000 Subject: [PATCH] Remove Fesi scripting engine from Trunk --- src/helma/scripting/fesi/ESBeanWrapper.java | 98 -- src/helma/scripting/fesi/ESGenericObject.java | 236 ---- src/helma/scripting/fesi/ESMapWrapper.java | 189 --- src/helma/scripting/fesi/ESNode.java | 795 ----------- .../scripting/fesi/FesiActionAdapter.java | 260 ---- src/helma/scripting/fesi/FesiEngine.java | 1038 --------------- src/helma/scripting/fesi/HopExtension.java | 1174 ----------------- src/helma/scripting/fesi/NodeConstructor.java | 164 --- src/helma/scripting/fesi/PhantomEngine.java | 32 - .../scripting/fesi/extensions/Database.java | 1076 --------------- .../fesi/extensions/DomExtension.java | 307 ----- .../scripting/fesi/extensions/ESMail.java | 353 ----- .../fesi/extensions/FtpExtension.java | 525 -------- .../fesi/extensions/ImageExtension.java | 124 -- .../fesi/extensions/MailExtension.java | 324 ----- .../fesi/extensions/XmlRpcExtension.java | 182 --- 16 files changed, 6877 deletions(-) delete mode 100644 src/helma/scripting/fesi/ESBeanWrapper.java delete mode 100644 src/helma/scripting/fesi/ESGenericObject.java delete mode 100644 src/helma/scripting/fesi/ESMapWrapper.java delete mode 100644 src/helma/scripting/fesi/ESNode.java delete mode 100644 src/helma/scripting/fesi/FesiActionAdapter.java delete mode 100644 src/helma/scripting/fesi/FesiEngine.java delete mode 100644 src/helma/scripting/fesi/HopExtension.java delete mode 100644 src/helma/scripting/fesi/NodeConstructor.java delete mode 100644 src/helma/scripting/fesi/PhantomEngine.java delete mode 100644 src/helma/scripting/fesi/extensions/Database.java delete mode 100644 src/helma/scripting/fesi/extensions/DomExtension.java delete mode 100644 src/helma/scripting/fesi/extensions/ESMail.java delete mode 100644 src/helma/scripting/fesi/extensions/FtpExtension.java delete mode 100644 src/helma/scripting/fesi/extensions/ImageExtension.java delete mode 100644 src/helma/scripting/fesi/extensions/MailExtension.java delete mode 100644 src/helma/scripting/fesi/extensions/XmlRpcExtension.java diff --git a/src/helma/scripting/fesi/ESBeanWrapper.java b/src/helma/scripting/fesi/ESBeanWrapper.java deleted file mode 100644 index f97daee2..00000000 --- a/src/helma/scripting/fesi/ESBeanWrapper.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.ESNull; -import FESI.Data.ESValue; -import FESI.Data.ESWrapper; -import FESI.Exceptions.EcmaScriptException; -import FESI.Interpreter.Evaluator; -import helma.framework.core.ApplicationBean; -import helma.objectmodel.INode; -import helma.util.SystemProperties; -import java.util.Map; - -/** - * Wrap a Java Bean for use in EcmaScript. - */ -public class ESBeanWrapper extends ESWrapper { - FesiEngine engine; - - /** - * Creates a new ESBeanWrapper object. - * - * @param object ... - * @param engine ... - */ - public ESBeanWrapper(Object object, FesiEngine engine) { - super(object, engine.getEvaluator(), true); - this.engine = engine; - } - - /** - * Wrap getProperty, return ESNode if INode would be returned, - * ESMapWrapper if Map would be returned. - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - try { - ESValue val = super.getProperty(propertyName, hash); - - if (val instanceof ESWrapper) { - Object theObject = ((ESWrapper) val).getJavaObject(); - - if (val instanceof ESWrapper && theObject instanceof INode) { - return engine.getNodeWrapper((INode) theObject); - } else if (val instanceof ESWrapper && theObject instanceof Map) { - ESMapWrapper wrapper = new ESMapWrapper(engine, (Map) theObject); - - if (theObject instanceof SystemProperties && - super.getJavaObject() instanceof ApplicationBean) { - wrapper.setReadonly(true); - } - - return wrapper; - } - } - - return val; - } catch (Exception rte) { - return ESNull.theNull; - } - } - - /** - * - * - * @param propertyName ... - * @param propertyValue ... - * @param hash ... - * - * @throws EcmaScriptException ... - */ - public void putProperty(String propertyName, ESValue propertyValue, int hash) - throws EcmaScriptException { - try { - super.putProperty(propertyName, propertyValue, hash); - } catch (Exception rte) { - // create a nice error message - throw new EcmaScriptException("can't set property " + propertyName + - " to this value on " + - getJavaObject().toString()); - } - } -} diff --git a/src/helma/scripting/fesi/ESGenericObject.java b/src/helma/scripting/fesi/ESGenericObject.java deleted file mode 100644 index 6af94430..00000000 --- a/src/helma/scripting/fesi/ESGenericObject.java +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import helma.framework.IPathElement; -import helma.framework.core.*; -import java.util.*; - -/** - * A wrapper for a Java object that may or may not implement the IPathElement interface. - */ -public class ESGenericObject extends ObjectPrototype { - ESWrapper wrapper; - Object wrappedObject; - - /** - * Creates a new ESGenericObject object. - * - * @param prototype ... - * @param evaluator ... - * @param obj ... - */ - public ESGenericObject(ESObject prototype, Evaluator evaluator, Object obj) { - super(prototype, evaluator); - wrappedObject = obj; - wrapper = new ESWrapper(obj, evaluator); - } - - /** - * - * - * @return ... - */ - public String getESClassName() { - return "GenericObject"; - } - - /** - * - * - * @return ... - */ - public String toString() { - return wrappedObject.toString(); - } - - /** - * - * - * @return ... - */ - public String toDetailString() { - return wrapper.toDetailString(); - } - - /** - * - * - * @param propertyName ... - * @param propertyValue ... - * @param hash ... - * - * @throws EcmaScriptException ... - */ - public void putProperty(String propertyName, ESValue propertyValue, int hash) - throws EcmaScriptException { - wrapper.putProperty(propertyName, propertyValue, hash); - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public boolean hasProperty(String propertyName, int hash) - throws EcmaScriptException { - return super.hasProperty(propertyName, hash) || - wrapper.hasProperty(propertyName, hash); - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public boolean deleteProperty(String propertyName, int hash) - throws EcmaScriptException { - return wrapper.deleteProperty(propertyName, hash); - } - - /** - * - * - * @param i ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(int i) throws EcmaScriptException { - return wrapper.getProperty(i); - } - - /** - * - * - * @param index ... - * @param propertyValue ... - * - * @throws EcmaScriptException ... - */ - public void putProperty(int index, ESValue propertyValue) - throws EcmaScriptException { - wrapper.putProperty(index, propertyValue); - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - ESValue val = super.getProperty(propertyName, hash); - - if ((val == null) || (val == ESUndefined.theUndefined)) { - val = wrapper.getProperty(propertyName, hash); - } - - return val; - } - - /** - * - * - * @param evaluator ... - * @param thisObject ... - * @param functionName ... - * @param arguments ... - * - * @return ... - * - * @throws EcmaScriptException ... - * @throws NoSuchMethodException ... - */ - public ESValue doIndirectCall(Evaluator evaluator, ESObject thisObject, - String functionName, ESValue[] arguments) - throws EcmaScriptException, NoSuchMethodException { - if (super.hasProperty(functionName, functionName.hashCode())) { - return super.doIndirectCall(evaluator, thisObject, functionName, arguments); - } - - return wrapper.doIndirectCall(evaluator, thisObject, functionName, arguments); - } - - /** - * - * - * @return ... - */ - public Enumeration getAllProperties() { - return wrapper.getProperties(); - } - - /** - * - * - * @return ... - */ - public Enumeration getProperties() { - return wrapper.getProperties(); - } - - /** - * - * - * @return ... - */ - public Object toJavaObject() { - return wrappedObject; - } - - /** - * An ESNode equals another object if it is an ESNode that wraps the same INode - * or the wrapped INode itself. FIXME: doesen't check dbmapping/type! - */ - public boolean equals(Object what) { - if (what == null) { - return false; - } - - if (what == this) { - return true; - } - - if (what instanceof ESGenericObject) { - ESGenericObject other = (ESGenericObject) what; - - return (wrappedObject.equals(other.wrappedObject)); - } - - return false; - } -} diff --git a/src/helma/scripting/fesi/ESMapWrapper.java b/src/helma/scripting/fesi/ESMapWrapper.java deleted file mode 100644 index ec7c6967..00000000 --- a/src/helma/scripting/fesi/ESMapWrapper.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.Evaluator; -import helma.framework.core.*; -import helma.objectmodel.INode; -import java.util.*; - -/** - * An EcmaScript object that makes stuff in a hashtable accessible as its properties - */ -public class ESMapWrapper extends ESWrapper { - private Map data; - private FesiEngine engine; - private boolean readonly = false; - - /** - * Creates a new ESMapWrapper object. - * - * @param engine ... - */ - public ESMapWrapper(FesiEngine engine) { - super(new Object(), engine.getEvaluator()); - this.engine = engine; - } - - /** - * Creates a new ESMapWrapper object. - * - * @param engine ... - * @param data ... - */ - public ESMapWrapper(FesiEngine engine, Map data) { - super(new Object(), engine.getEvaluator()); - this.engine = engine; - this.data = data; - } - - /** - * - * - * @param readonly ... - */ - public void setReadonly(boolean readonly) { - this.readonly = readonly; - } - - /** - * - * - * @param data ... - */ - public void setData(Map data) { - this.data = data; - } - - /** - * Overridden to make the object read-only - */ - public void putProperty(String propertyName, ESValue propertyValue, int hash) - throws EcmaScriptException { - if (data == null) { - data = new HashMap(); - } - - if (propertyValue == ESNull.theNull) { - deleteProperty(propertyName, hash); - } else if (readonly == false) { - data.put(propertyName, propertyValue.toJavaObject()); - } else { - throw new EcmaScriptException("object is readonly"); - } - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public boolean deleteProperty(String propertyName, int hash) - throws EcmaScriptException { - if (readonly == false) { - data.remove(propertyName); - - return true; - } else { - return false; - } - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - if (data == null) { - return ESNull.theNull; - } - - Object val = data.get(propertyName); - - if (val == null) { - return ESNull.theNull; - } - - if (val instanceof String) { - return new ESString((String) val); - } else if (val instanceof INode) { - return engine.getNodeWrapper((INode) val); - } else if (val instanceof Map) { - return new ESMapWrapper(engine, (Map) val); - } else if (val instanceof ESValue) { - return (ESValue) val; - } - - return ESLoader.normalizeValue(val, evaluator); - } - - /** - * - * - * @return ... - */ - public Enumeration getAllProperties() { - return getProperties(); - } - - /** - * - * - * @return ... - */ - public Enumeration getProperties() { - Object[] keys = (data == null) ? null : data.keySet().toArray(); - - return new Enum(keys); - } - - class Enum implements Enumeration { - Object[] elements; - int pos; - - Enum(Object[] elements) { - this.elements = elements; - pos = 0; - } - - public boolean hasMoreElements() { - return (elements != null) && (pos < elements.length); - } - - public Object nextElement() { - if ((elements == null) || (pos >= elements.length)) { - throw new NoSuchElementException(); - } - - return elements[pos++]; - } - } -} diff --git a/src/helma/scripting/fesi/ESNode.java b/src/helma/scripting/fesi/ESNode.java deleted file mode 100644 index 3ce462c3..00000000 --- a/src/helma/scripting/fesi/ESNode.java +++ /dev/null @@ -1,795 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import helma.framework.core.*; -import helma.objectmodel.*; -import helma.objectmodel.db.*; -import helma.util.*; -import java.io.*; -import java.util.*; - -/** - * An EcmaScript wrapper around a Node object. This is the basic - * HopObject that can be stored in the internal or external databases. - */ -public class ESNode extends ObjectPrototype { - // the INode object wrapped by this ESObject - INode node; - - // the cache node - persistent nodes have a transient property as a commodity to - // store temporary stuff. - INode cache; - - // the ecmascript wrapper for the cache. - ESNode cacheWrapper; - - // The handle of the wrapped Node. Makes ESNodes comparable without accessing the wrapped node. - NodeHandle handle; - DbMapping dbmap; - Throwable lastError = null; - FesiEngine engine; - - /** - * Constructor used to create transient cache nodes - */ - public ESNode(INode node, FesiEngine engine) { - super(engine.getPrototype("hopobject"), engine.getEvaluator()); - this.engine = engine; - this.node = node; - cache = null; - cacheWrapper = null; - - // this is a transient node, set node handle to null - handle = null; - } - - /** - * Creates a new ESNode object. - * - * @param prototype ... - * @param evaluator ... - * @param obj ... - * @param engine ... - */ - public ESNode(ESObject prototype, Evaluator evaluator, Object obj, FesiEngine engine) { - super(prototype, evaluator); - - // eval.app.logEvent ("in ESNode constructor: "+o.getClass ()); - this.engine = engine; - - if (obj == null) { - node = new TransientNode(null); - } else if (obj instanceof ESWrapper) { - node = (INode) ((ESWrapper) obj).getJavaObject(); - } else if (obj instanceof INode) { - node = (INode) obj; - } else { - node = new TransientNode(obj.toString()); - } - - // set node handle to wrapped node - if (node instanceof Node) { - handle = ((Node) node).getHandle(); - } else { - handle = null; - } - - // cache Node is initialized on demend when it is needed - cache = null; - cacheWrapper = null; - } - - /** - * Check if the node has been invalidated. If so, it has to be re-fetched - * from the db via the app's node manager. - */ - protected void checkNode() { - if (node.getState() == INode.INVALID) { - try { - node = handle.getNode(engine.app.getWrappedNodeManager()); - } catch (Exception nx) { - } - } - } - - /** - * - * - * @return ... - */ - public INode getNode() { - checkNode(); - - return node; - } - - /** - * - * - * @param protoName ... - */ - public void setPrototype(String protoName) { - checkNode(); - node.setPrototype(protoName); - } - - /** - * - * - * @return ... - */ - public String getPrototypeName() { - return node.getPrototype(); - } - - /** - * - * - * @return ... - */ - public String getESClassName() { - return "HopObject"; - } - - /** - * - * - * @return ... - */ - public String toString() { - if (node == null) { - return ""; - } - - return node.toString(); - } - - /** - * - * - * @return ... - */ - public String toDetailString() { - return "ES:[Object: builtin " + this.getClass().getName() + ":" + - ((node == null) ? "null" : node.toString()) + "]"; - } - - protected void setError(Throwable e) { - lastError = e; - } - - /** - * - * - * @param what ... - * - * @return ... - */ - public boolean add(ESValue[] what) { - checkNode(); - - for (int i = 0; i < what.length; i++) - if (what[i] instanceof ESNode) { - ESNode esn = (ESNode) what[i]; - INode added = node.addNode(esn.getNode()); - } - - return true; - } - - /** - * - * - * @return ... - */ - public ESValue list() { - checkNode(); - - int l = node.numberOfNodes(); - Enumeration e = node.getSubnodes(); - ESObject ap = evaluator.getArrayPrototype(); - ArrayPrototype theArray = new ArrayPrototype(ap, evaluator); - - if (e != null) { - theArray.setSize(l); - - for (int i = 0; i < l; i++) { - theArray.setElementAt(engine.getNodeWrapper((INode) e.nextElement()), i); - } - } else { - theArray.setSize(0); - } - - return theArray; - } - - /** - * - * - * @param what ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public boolean addAt(ESValue[] what) throws EcmaScriptException { - checkNode(); - - if (what.length < 2) { - throw new EcmaScriptException("Wrong number of arguments"); - } - - if (!(what[1] instanceof ESNode)) { - throw new EcmaScriptException("Can only add Node objects as subnodes"); - } - - ESNode esn = (ESNode) what[1]; - INode added = node.addNode(esn.getNode(), (int) what[0].toInt32()); - - return true; - } - - /** - * Remove node itself or one or more subnodes. - */ - public boolean remove(ESValue[] args) { - checkNode(); - - // semantics: if called without arguments, remove self. - // otherwise, remove given subnodes. - if (args.length == 0) { - return node.remove(); - } else { - for (int i = 0; i < args.length; i++) { - if (args[i] instanceof ESNode) { - ESNode esn = (ESNode) args[i]; - - node.removeNode(esn.getNode()); - } - } - } - - return true; - } - - /** - * Invalidate the node itself or a subnode - */ - public boolean invalidate(ESValue[] args) { - if (node instanceof helma.objectmodel.db.Node) { - if (args.length == 0) { - ((helma.objectmodel.db.Node) node).invalidate(); - } else { - ((helma.objectmodel.db.Node) node).invalidateNode(args[0].toString()); - } - } - - return true; - } - - /** - * Check if node is contained in subnodes - */ - public int contains(ESValue[] args) { - checkNode(); - - if ((args.length == 1) && args[0] instanceof ESNode) { - ESNode esn = (ESNode) args[0]; - - return node.contains(esn.getNode()); - } - - return -1; - } - - /** - * Prefetch child objects from (relational) database. - */ - public void prefetchChildren(ESValue[] args) throws Exception { - checkNode(); - - if (!(node instanceof Node)) { - return; - } - - int start = 0; - int length = 0; - - try { - if (args.length == 0) { - start = 0; - length = 1000; - } else if (args.length == 2) { - if (args[0].isNumberValue()) { - start = args[0].toInt32(); - } else { - throw new RuntimeException("Illegal argument in prefetchChildren: " + - args[0]); - } - - if (args[1].isNumberValue()) { - length = args[1].toInt32(); - } else { - throw new RuntimeException("Illegal argument in prefetchChildren: " + - args[1]); - } - } else { - throw new RuntimeException("Wrong number of arguments in prefetchChildren"); - } - } catch (Exception x) { - throw new IllegalArgumentException(x.getMessage()); - } - - ((Node) node).prefetchChildren(start, length); - } - - - /** - * - * - * @param pval ... - * - * @return ... - */ - public boolean setParent(ESValue[] pval) { - // do a couple of checks: both nodes need to be persistent in order for setParent to make sense. - if (!(node instanceof helma.objectmodel.db.Node)) { - return false; - } - - if ((pval == null) || (pval.length < 1) || (pval.length > 2)) { - return false; - } - - if (!(pval[0] instanceof ESNode)) { - return false; - } - - ESNode esn = (ESNode) pval[0]; - INode pn = esn.getNode(); - - if (!(pn instanceof helma.objectmodel.db.Node)) { - return false; - } - - // check if there is an additional string element - if so, it's the property name by which the node is - // accessed, otherwise it will be accessed as anonymous subnode via its id - String propname = null; - - if ((pval.length == 2) && (pval[1] != null) && !(pval[1] instanceof ESNull)) { - propname = pval[1].toString(); - } - - helma.objectmodel.db.Node n = (helma.objectmodel.db.Node) node; - - n.setParent((helma.objectmodel.db.Node) pn, propname); - - return true; - } - - /** - * - * - * @param propertyName ... - * @param propertyValue ... - * @param hash ... - * - * @throws EcmaScriptException ... - */ - public void putProperty(String propertyName, ESValue propertyValue, int hash) - throws EcmaScriptException { - checkNode(); - - // engine.app.logEvent ("put property called: "+propertyName+", "+propertyValue.getClass()); - if ("cache".equalsIgnoreCase(propertyName)) { - throw new EcmaScriptException("Can't modify read-only property \"" + - propertyName + "\"."); - } - - if ("subnodeRelation".equalsIgnoreCase(propertyName)) { - node.setSubnodeRelation((propertyValue instanceof ESNull) ? null - : propertyValue.toString()); - - return; - } - - // check if the property is write protected, i.e. the type.property file describes it as [readonly] - DbMapping dbm = node.getDbMapping(); - - if (dbm != null) { - Relation rel = dbm.getPropertyRelation(propertyName); - - if ((rel != null) && rel.isReadonly()) { - return; - } - } - - if (propertyValue instanceof ESNull || propertyValue instanceof ESUndefined) { - node.unset(propertyName); - } else if (propertyValue instanceof ESString) { - node.setString(propertyName, propertyValue.toString()); - } else if (propertyValue instanceof ESBoolean) { - node.setBoolean(propertyName, propertyValue.booleanValue()); - } else if (propertyValue instanceof ESNumber) { - node.setFloat(propertyName, propertyValue.doubleValue()); - } else if (propertyValue instanceof DatePrototype) { - node.setDate(propertyName, (Date) propertyValue.toJavaObject()); - } else if (propertyValue instanceof ESNode) { - // long now = System.currentTimeMillis (); - ESNode esn = (ESNode) propertyValue; - - node.setNode(propertyName, esn.getNode()); - - // engine.app.logEvent ("*** spent "+(System.currentTimeMillis () - now)+" ms to set property "+propertyName); - } else { - // engine.app.logEvent ("got "+propertyValue.getClass ()); - // A persistent node can't store anything other than the types above, so throw an exception - // throw new EcmaScriptException ("Can't set a JavaScript Object or Array as property of "+node); - node.setJavaObject(propertyName, propertyValue.toJavaObject()); - } - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public boolean deleteProperty(String propertyName, int hash) - throws EcmaScriptException { - checkNode(); - - // engine.app.logEvent ("delete property called: "+propertyName); - if (node.get(propertyName) != null) { - node.unset(propertyName); - - return true; - } - - return super.deleteProperty(propertyName, hash); - } - - /** - * - * - * @param i ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(int i) throws EcmaScriptException { - checkNode(); - - INode n = node.getSubnodeAt(i); - - if (n == null) { - return ESNull.theNull; - } - - return engine.getNodeWrapper(n); - } - - /** - * - * - * @param index ... - * @param propertyValue ... - * - * @throws EcmaScriptException ... - */ - public void putProperty(int index, ESValue propertyValue) - throws EcmaScriptException { - checkNode(); - - if (propertyValue instanceof ESNode) { - ESNode n = (ESNode) propertyValue; - - node.addNode(n.getNode(), index); - } else { - throw new EcmaScriptException("Can only add Nodes to Node arrays"); - } - } - - /** - * Retrieve a property from the node object or the underlying EcmaScript prototype. - * Normally we would first check the node object and then the prototype, but since - * node properties are potentially expensive to look up because they require database - * queries, we do the prototype lookup first. This usually doesn't cause any confusion - * because generally things are divided cleanly between prototype and object - the - * first holds the functions, the latter the mapped data properties. - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - checkNode(); - - // engine.app.logEvent ("get property called: "+propertyName); - ESValue retval = super.getProperty(propertyName, hash); - - if (!(retval instanceof ESUndefined)) { - return retval; - } - - return getNodeProperty(propertyName); - } - - /** - * Retrieve a property only from the node itself, not the underlying EcmaScript prototype object. - * This is called directly when we call get(x) on a hopobject, since we don't want to return - * the prototype functions in that case. - */ - public ESValue getNodeProperty(String propertyName) - throws EcmaScriptException { - // check for null - if (propertyName == null) { - return ESNull.theNull; - } - - // persistent or persistent capable nodes have a cache property that's a transient node. - // it it hasn't requested before, initialize it now - if ("cache".equalsIgnoreCase(propertyName) && node instanceof Node) { - cache = node.getCacheNode(); - - if (cacheWrapper == null) { - cacheWrapper = new ESNode(cache, engine); - } else { - cacheWrapper.node = cache; - } - - return cacheWrapper; - } - - if ("subnodeRelation".equalsIgnoreCase(propertyName)) { - String rel = node.getSubnodeRelation(); - - return (rel == null) ? (ESValue) ESNull.theNull : new ESString(rel); - } - - // Everything starting with an underscore is interpreted as internal property - if (propertyName.startsWith("_")) { - return getInternalProperty(propertyName); - } - - // this _may_ do a relational query if properties are mapped to a relational type. - IProperty p = node.get(propertyName); - - if (p != null) { - if (p.getType() == IProperty.STRING) { - String str = p.getStringValue(); - - if (str == null) { - return ESNull.theNull; - } else { - return new ESString(str); - } - } - - if (p.getType() == IProperty.BOOLEAN) { - return ESBoolean.makeBoolean(p.getBooleanValue()); - } - - if (p.getType() == IProperty.DATE) { - return new DatePrototype(evaluator, p.getDateValue()); - } - - if (p.getType() == IProperty.INTEGER) { - return new ESNumber((double) p.getIntegerValue()); - } - - if (p.getType() == IProperty.FLOAT) { - return new ESNumber(p.getFloatValue()); - } - - if (p.getType() == IProperty.NODE) { - INode nd = p.getNodeValue(); - - if (nd == null) { - return ESNull.theNull; - } else { - return engine.getNodeWrapper(nd); - } - } - - if (p.getType() == IProperty.JAVAOBJECT) { - return ESLoader.normalizeObject(p.getJavaObjectValue(), evaluator); - } - } - - // as last resort, try to get property as anonymous subnode - INode anon = (INode) node.getChildElement(propertyName); - - if (anon != null) { - return engine.getNodeWrapper(anon); - } - - return ESNull.theNull; - } - - /** - * Some internal properties defined for every Node object. These are most commonly - * used for debugging Helma applications. - */ - private ESValue getInternalProperty(String propertyName) - throws EcmaScriptException { - if ("__id__".equals(propertyName) || "_id".equals(propertyName)) { - return new ESString(node.getID()); - } - - if ("__prototype__".equals(propertyName) || "_prototype".equals(propertyName)) { - String p = node.getPrototype(); - - if (p == null) { - return ESNull.theNull; - } else { - return new ESString(node.getPrototype()); - } - } - - if ("__parent__".equals(propertyName) || "_parent".equals(propertyName)) { - INode n = node.getParent(); - - if (n == null) { - return ESNull.theNull; - } else { - return engine.getNodeWrapper(n); - } - } - - // some more internal properties - if ("__name__".equals(propertyName)) { - return new ESString(node.getName()); - } - - if ("__fullname__".equals(propertyName)) { - return new ESString(node.getFullName()); - } - - if ("__hash__".equals(propertyName)) { - return new ESString("" + node.hashCode()); - } - - if ("__node__".equals(propertyName)) { - return new ESWrapper(node, evaluator); - } - - if ("__created__".equalsIgnoreCase(propertyName)) { - return new DatePrototype(evaluator, node.created()); - } - - if ("__lastmodified__".equalsIgnoreCase(propertyName)) { - return new DatePrototype(evaluator, node.lastModified()); - } - - return ESNull.theNull; - } - - /** - * - * - * @return ... - */ - public boolean clearCache() { - checkNode(); - node.clearCacheNode(); - - return true; - } - - /** - * - * - * @return ... - */ - public Enumeration getAllProperties() { - return getProperties(); - } - - /** - * - * - * @return ... - */ - public Enumeration getProperties() { - checkNode(); - - return node.properties(); - } - - /** - * - * - * @return ... - */ - public String error() { - if (lastError == null) { - return ""; - } else { - String exceptionName = lastError.getClass().getName(); - int l = exceptionName.lastIndexOf("."); - - if (l > 0) { - exceptionName = exceptionName.substring(l + 1); - } - - return exceptionName + ": " + lastError.getMessage(); - } - } - - /** - * - */ - public void clearError() { - lastError = null; - } - - /** - * - * - * @return ... - */ - public Object toJavaObject() { - return getNode(); - } - - /** - * An ESNode equals another object if it is an ESNode that wraps the same INode - * or the wrapped INode itself. FIXME: doesen't check dbmapping/type! - */ - public boolean equals(Object what) { - if (what == null) { - return false; - } - - if (what == this) { - return true; - } - - if (what instanceof ESNode) { - ESNode other = (ESNode) what; - - if (handle != null) { - return handle.equals(other.handle); - } else { - return (node == other.node); - } - } - - return false; - } - - /** - * - * - * @param hint ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getDefaultValue(int hint) throws EcmaScriptException { - return new ESString(this.toString()); - } -} - // class ESNode diff --git a/src/helma/scripting/fesi/FesiActionAdapter.java b/src/helma/scripting/fesi/FesiActionAdapter.java deleted file mode 100644 index 6051a4be..00000000 --- a/src/helma/scripting/fesi/FesiActionAdapter.java +++ /dev/null @@ -1,260 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.AST.ASTFormalParameterList; -import FESI.AST.ASTStatementList; -import FESI.AST.EcmaScriptTreeConstants; -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import FESI.Parser.*; -import helma.framework.*; -import helma.framework.core.*; -import helma.scripting.*; -import helma.util.Updatable; -import java.io.*; -import java.util.Iterator; -import java.util.Vector; - -/** - * An class that updates fesi interpreters with actionfiles and templates. - */ -public class FesiActionAdapter { - Prototype prototype; - Application app; - String functionName; - String sourceName; - - // this is the parsed function which can be easily applied to FesiEngine objects - TypeUpdater pfunc; - - // this is the parsed function which can be easily applied to FesiEngine objects - TypeUpdater pfuncAsString; - - /** - * Creates a new FesiActionAdapter object. - * - * @param action ... - */ - public FesiActionAdapter(ActionFile action) { - prototype = action.getPrototype(); - app = action.getApplication(); - - Reader reader = null; - - functionName = action.getFunctionName(); - sourceName = action.getSourceName(); - - try { - reader = action.getReader(); - pfunc = parseFunction(functionName, - "arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10", - reader, sourceName); - } catch (Throwable x) { - String message = x.getMessage(); - - pfunc = new ErrorFeedback(functionName, message); - } finally { - try { - reader.close(); - } catch (Exception ignore) { - } - } - - // check if this is a template and we need to generate an "_as_string" variant - if (action instanceof Template) { - String content = "res.pushStringBuffer(); " + action.getContent() + - "\r\nreturn res.popStringBuffer();\r\n"; - - try { - pfuncAsString = parseFunction(functionName + "_as_string", - "arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10", - new StringReader(content), sourceName); - } catch (Throwable x) { - String message = x.getMessage(); - - pfunc = new ErrorFeedback(functionName + "_as_string", message); - } - } else { - pfuncAsString = null; - } - } - - /** - * - * - * @param engine ... - * - * @throws EcmaScriptException ... - */ - public synchronized void updateEvaluator(FesiEngine engine) - throws EcmaScriptException { - if (pfunc != null) { - pfunc.updateEvaluator(engine); - } - - if (pfuncAsString != null) { - pfuncAsString.updateEvaluator(engine); - } - } - - protected TypeUpdater parseFunction(String funcName, String params, Reader body, - String sourceName) - throws EcmaScriptException { - // ESObject fp = app.eval.evaluator.getFunctionPrototype(); - // ConstructedFunctionObject function = null; - ASTFormalParameterList fpl = null; - ASTStatementList sl = null; - FunctionEvaluationSource fes = null; - - /* if (body == null || "".equals (body.trim())) - body = ";\r\n"; - else - body = body + "\r\n"; */ - if (params == null) { - params = ""; - } else { - params = params.trim(); - } - - EcmaScript parser; - StringReader is; - - // Special case for empty parameters - if (params.length() == 0) { - fpl = new ASTFormalParameterList(EcmaScriptTreeConstants.JJTFORMALPARAMETERLIST); - } else { - is = new java.io.StringReader(params); - parser = new EcmaScript(is); - - try { - fpl = (ASTFormalParameterList) parser.FormalParameterList(); - is.close(); - } catch (ParseException x) { - throw new EcmaScriptParseException(x, - new FileEvaluationSource(sourceName, - null)); - } - } - - // this is very very very strange: without the toString, lots of obscure exceptions - // deep inside the parser... - // is = new java.io.StringReader(body.toString ()); - try { - parser = new EcmaScript(body); - sl = (ASTStatementList) parser.StatementList(); - body.close(); - } catch (ParseException x) { - app.logEvent("Error parsing file " + app.getName() + ":" + sourceName + ": " + - x); - throw new EcmaScriptParseException(x, - new FileEvaluationSource(sourceName, null)); - } catch (Exception x) { - app.logEvent("Error parsing file " + app.getName() + ":" + sourceName + ": " + - x); - throw new RuntimeException(x.getMessage()); - } - - fes = new FunctionEvaluationSource(new FileEvaluationSource(sourceName, null), - funcName); - - return new ParsedFunction(fpl, sl, fes, null, funcName); - } - - interface TypeUpdater { - public void updateEvaluator(FesiEngine engine) - throws EcmaScriptException; - } - - class ParsedFunction implements TypeUpdater { - ASTFormalParameterList fpl = null; - ASTStatementList sl = null; - FunctionEvaluationSource fes = null; - String fullFunctionText = null; - String functionName; - - public ParsedFunction(ASTFormalParameterList fpl, ASTStatementList sl, - FunctionEvaluationSource fes, String fullFunctionText, - String functionName) { - this.fpl = fpl; - this.sl = sl; - this.fes = fes; - this.fullFunctionText = fullFunctionText; - this.functionName = functionName; - } - - public void updateEvaluator(FesiEngine engine) - throws EcmaScriptException { - ObjectPrototype op = engine.getPrototype(prototype.getName()); - - EcmaScriptVariableVisitor vdvisitor = engine.evaluator.getVarDeclarationVisitor(); - Vector vnames = vdvisitor.processVariableDeclarations(sl, fes); - - FunctionPrototype fp = ConstructedFunctionObject.makeNewConstructedFunction(engine.evaluator, - functionName, - fes, - fullFunctionText, - fpl.getArguments(), - vnames, - sl); - - op.putHiddenProperty(functionName, fp); - } - } - - class ErrorFeedback implements TypeUpdater { - String functionName; - String errorMessage; - - public ErrorFeedback(String fname, String msg) { - functionName = fname; - errorMessage = msg; - } - - public void updateEvaluator(FesiEngine engine) - throws EcmaScriptException { - ObjectPrototype op = engine.getPrototype(prototype.getName()); - - FunctionPrototype fp = (FunctionPrototype) engine.evaluator.getFunctionPrototype(); - FunctionPrototype func = new ThrowException(functionName, engine.evaluator, - fp, errorMessage); - - op.putHiddenProperty(functionName, func); - } - } - - class ThrowException extends BuiltinFunctionObject { - String message; - - ThrowException(String name, Evaluator evaluator, FunctionPrototype fp, - String message) { - super(fp, evaluator, name, 1); - this.message = (message == null) ? "No error message available" : message; - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw new EcmaScriptException(message); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw new EcmaScriptException(message); - } - } -} diff --git a/src/helma/scripting/fesi/FesiEngine.java b/src/helma/scripting/fesi/FesiEngine.java deleted file mode 100644 index f1fd7524..00000000 --- a/src/helma/scripting/fesi/FesiEngine.java +++ /dev/null @@ -1,1038 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import helma.doc.DocApplication; -import helma.extensions.ConfigurationException; -import helma.extensions.HelmaExtension; -import helma.framework.*; -import helma.framework.core.*; -import helma.main.Server; -import helma.objectmodel.*; -import helma.objectmodel.db.DbMapping; -import helma.objectmodel.db.Relation; -import helma.scripting.*; -import helma.scripting.fesi.extensions.*; -import helma.util.CacheMap; -import helma.util.Updatable; -import java.io.*; -import java.util.*; - -/** - * This is the implementation of ScriptingEnvironment for the FESI EcmaScript interpreter. - */ -public class FesiEngine implements ScriptingEngine { - // extensions loaded by this evaluator - static final String[] extensions = new String[] { - "FESI.Extensions.BasicIO", - "FESI.Extensions.FileIO", - "helma.scripting.fesi.extensions.XmlRpcExtension", - "helma.scripting.fesi.extensions.ImageExtension", - "helma.scripting.fesi.extensions.FtpExtension", - "FESI.Extensions.JavaAccess", - "helma.scripting.fesi.extensions.DomExtension", - "FESI.Extensions.OptionalRegExp" - }; - - // the application we're running in - Application app; - - // The FESI evaluator - Evaluator evaluator; - - // the global object - GlobalObject global; - - // caching table for JavaScript object wrappers - CacheMap wrappercache; - - // table containing JavaScript prototypes - Hashtable prototypes; - - // the request evaluator instance owning this fesi evaluator - RequestEvaluator reval; - - // remember global variables from last invokation to be able to - // do lazy cleanup - Map lastGlobals = null; - - // the global vars set by extensions - HashMap extensionGlobals; - - // the introspector that provides documentation for this application - DocApplication doc = null; - - /** - * Zero argument constructor. - */ - public FesiEngine() { - } - - /** - * Initialize a FESI evaluator for the given application and request evaluator. - */ - public void init(Application app, RequestEvaluator reval) { - this.app = app; - this.reval = reval; - wrappercache = new CacheMap(200, .75f); - prototypes = new Hashtable(); - - try { - evaluator = new Evaluator(this); - global = evaluator.getGlobalObject(); - - for (int i = 0; i < extensions.length; i++) - evaluator.addExtension(extensions[i]); - - HopExtension hopx = new HopExtension(app); - - hopx.initializeExtension(this); - - MailExtension mailx = (MailExtension) evaluator.addExtension("helma.scripting.fesi.extensions.MailExtension"); - - mailx.setProperties(app.getProperties()); - - Database dbx = (Database) evaluator.addExtension("helma.scripting.fesi.extensions.Database"); - - dbx.setApplication(app); - - // load extensions defined in server.properties: - extensionGlobals = new HashMap(); - - Vector extVec = Server.getServer().getExtensions(); - - for (int i = 0; i < extVec.size(); i++) { - HelmaExtension ext = (HelmaExtension) extVec.get(i); - - try { - HashMap tmpGlobals = ext.initScripting(app, this); - - if (tmpGlobals != null) { - extensionGlobals.putAll(tmpGlobals); - } - } catch (ConfigurationException e) { - app.logEvent("Couldn't initialize extension " + ext.getName() + ": " + - e.getMessage()); - } - } - - // fake a cache member like the one found in ESNodes - global.putHiddenProperty("cache", new ESNode(new TransientNode("cache"), this)); - global.putHiddenProperty("undefined", ESUndefined.theUndefined); - - ESBeanWrapper appnode = new ESBeanWrapper(new ApplicationBean(app), this); - - global.putHiddenProperty("app", appnode); - initialize(); - } catch (Exception e) { - System.err.println("Cannot initialize interpreter"); - System.err.println("Error: " + e); - e.printStackTrace(); - throw new RuntimeException(e.getMessage()); - } - } - - /** - * Initialize the evaluator, making sure the minimum type information - * necessary to bootstrap the rest is parsed. - */ - private void initialize() { - // set the thread filed in the FESI evaluator - evaluator.thread = Thread.currentThread(); - - Collection protos = app.getPrototypes(); - - for (Iterator i = protos.iterator(); i.hasNext();) { - Prototype proto = (Prototype) i.next(); - - initPrototype(proto); - } - - // always fully initialize global prototype, because - // we always need it and there's no chance to trigger - // creation on demand. - getPrototype("global"); - evaluator.thread = null; - } - - /** - * Initialize a prototype without fully parsing its script files. - */ - void initPrototype(Prototype prototype) { - // System.err.println ("FESI INIT PROTO "+prototype); - ObjectPrototype op = null; - - // get the prototype's prototype if possible and necessary - ObjectPrototype opp = null; - Prototype parent = prototype.getParentPrototype(); - - if (parent != null) { - // see if parent prototype is already registered. if not, register it - opp = getRawPrototype(parent.getName()); - - if (opp == null) { - initPrototype(parent); - opp = getRawPrototype(parent.getName()); - } - } - - String name = prototype.getName(); - - if (!"global".equalsIgnoreCase(name) && !"hopobject".equalsIgnoreCase(name) && - (opp == null)) { - if (app.isJavaPrototype(name)) { - opp = getRawPrototype("__javaobject__"); - } else { - opp = getRawPrototype("hopobject"); - } - } - - // if prototype doesn't exist (i.e. is a standard prototype built by HopExtension), create it. - op = getRawPrototype(name); - - if (op == null) { - op = new ObjectPrototype(opp, evaluator); - - try { - op.putProperty("prototypename", new ESString(name), - "prototypename".hashCode()); - } catch (EcmaScriptException ignore) { - } - - putPrototype(name, op); - } else { - // set parent prototype just in case it has been changed - op.setPrototype(opp); - } - - // Register a constructor for all types except global. - // This will first create a new prototyped hopobject and then calls - // the actual (scripted) constructor on it. - if (!"global".equalsIgnoreCase(name) && !"root".equalsIgnoreCase(name)) { - try { - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - global.putHiddenProperty(name, new NodeConstructor(name, fp, this)); - } catch (EcmaScriptException ignore) { - } - } - - // app.typemgr.updatePrototype (prototype.getName()); - // evaluatePrototype (prototype); - } - - /** - * Set up a prototype, parsing and compiling all its script files. - */ - void evaluatePrototype(Prototype prototype) { - // System.err.println ("FESI EVALUATE PROTO "+prototype+" FOR "+this); - ObjectPrototype op = null; - - // get the prototype's prototype if possible and necessary - ObjectPrototype opp = null; - Prototype parent = prototype.getParentPrototype(); - - if (parent != null) { - // see if parent prototype is already registered. if not, register it - opp = getPrototype(parent.getName()); - - if (opp == null) { - evaluatePrototype(parent); - opp = getPrototype(parent.getName()); - } - } - - String name = prototype.getName(); - - if (!"global".equalsIgnoreCase(name) && !"hopobject".equalsIgnoreCase(name) && - (opp == null)) { - if (app.isJavaPrototype(name)) { - opp = getPrototype("__javaobject__"); - } else { - opp = getPrototype("hopobject"); - } - } - - // if prototype doesn't exist (i.e. is a standard prototype built by HopExtension), create it. - op = getPrototype(name); - - if (op == null) { - op = new ObjectPrototype(opp, evaluator); - - try { - op.putProperty("prototypename", new ESString(name), - "prototypename".hashCode()); - } catch (EcmaScriptException ignore) { - } - - putPrototype(name, op); - } else { - // reset prototype to original state - resetPrototype(op); - - // set parent prototype just in case it has been changed - op.setPrototype(opp); - } - - // Register a constructor for all types except global. - // This will first create a new prototyped hopobject and then calls - // the actual (scripted) constructor on it. - if (!"global".equalsIgnoreCase(name) && !"root".equalsIgnoreCase(name)) { - try { - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - global.putHiddenProperty(name, new NodeConstructor(name, fp, this)); - } catch (EcmaScriptException ignore) { - } - } - - for (Iterator it = prototype.getZippedCode().values().iterator(); it.hasNext();) { - Object code = it.next(); - - evaluate(prototype, code); - } - - for (Iterator it = prototype.getCode().values().iterator(); it.hasNext();) { - Object code = it.next(); - - evaluate(prototype, code); - } - } - - /** - * Return an object prototype to its initial state, removing all application specific - * functions. - */ - void resetPrototype(ObjectPrototype op) { - for (Enumeration en = op.getAllProperties(); en.hasMoreElements();) { - String prop = en.nextElement().toString(); - - try { - ESValue esv = op.getProperty(prop, prop.hashCode()); - - if (esv instanceof ConstructedFunctionObject || - esv instanceof FesiActionAdapter.ThrowException) { - op.deleteProperty(prop, prop.hashCode()); - } - } catch (Exception x) { - } - } - } - - /** - * This method is called before an execution context is entered to let the - * engine know it should update its prototype information. - */ - public void updatePrototypes() { - // set the thread filed in the FESI evaluator - evaluator.thread = Thread.currentThread(); - - Collection protos = app.getPrototypes(); - - for (Iterator i = protos.iterator(); i.hasNext();) { - Prototype proto = (Prototype) i.next(); - TypeInfo info = (TypeInfo) prototypes.get(proto.getName()); - - if (info == null) { - // a prototype we don't know anything about yet. Init local update info. - initPrototype(proto); - info = (TypeInfo) prototypes.get(proto.getName()); - } - - // only update prototype if it has already been initialized. - // otherwise, this will be done on demand - // System.err.println ("CHECKING PROTO: "+info); - if (info.lastUpdate > 0) { - Prototype p = app.typemgr.getPrototype(info.protoName); - - if (p != null) { - // System.err.println ("UPDATING PROTO: "+p); - app.typemgr.updatePrototype(p); - - if (p.getLastUpdate() > info.lastUpdate) { - evaluatePrototype(p); - info.lastUpdate = p.getLastUpdate(); - } - } - } - } - } - - /** - * This method is called when an execution context for a request - * evaluation is entered. The globals parameter contains the global values - * to be applied during this execution context. - */ - public void enterContext(HashMap globals) throws ScriptingException { - // set the thread filed in the FESI evaluator - evaluator.thread = Thread.currentThread(); - - // set globals on the global object - globals.putAll(extensionGlobals); - - if ((globals != null) && (globals != lastGlobals)) { - // loop through global vars and set them - for (Iterator i = globals.keySet().iterator(); i.hasNext();) { - String k = (String) i.next(); - Object v = globals.get(k); - ESValue sv = null; - - try { - // we do a lot of extra work to make access to global variables - // comfortable to EcmaScript coders, i.e. we use a lot of custom wrappers - // that expose properties and functions in a special way instead of just going - // with the standard java object wrappers. - if (v instanceof Map) { - sv = new ESMapWrapper(this, (Map) v); - } else if ("path".equals(k)) { - ArrayPrototype parr = new ArrayPrototype(evaluator.getArrayPrototype(), - evaluator); - List path = (List) v; - - // register path elements with their prototype - for (int j = 0; j < path.size(); j++) { - Object pathElem = path.get(j); - ESValue wrappedElement = getElementWrapper(pathElem); - - parr.putProperty(j, wrappedElement); - - String protoname = app.getPrototypeName(pathElem); - - if (protoname != null) { - parr.putHiddenProperty(protoname, wrappedElement); - } - } - - sv = parr; - } else if ("req".equals(k)) { - sv = new ESBeanWrapper(v, this); - } else if ("res".equals(k)) { - sv = new ESBeanWrapper(v, this); - } else if ("session".equals(k)) { - sv = new ESBeanWrapper(v, this); - } else if ("app".equals(k)) { - sv = new ESBeanWrapper(v, this); - } else if (v instanceof ESValue) { - sv = (ESValue) v; - } else { - sv = ESLoader.normalizeValue(v, evaluator); - } - - global.putHiddenProperty(k, sv); - } catch (Exception x) { - app.logEvent("Error setting global variable " + k + ": " + x); - } - } - } - - // remember the globals set on this evaluator - lastGlobals = globals; - } - - /** - * This method is called to let the scripting engine know that the current - * execution context has terminated. - */ - public void exitContext() { - // unset the thread filed in the FESI evaluator - evaluator.thread = null; - - // loop through previous globals and unset them, if necessary. - if (lastGlobals != null) { - for (Iterator i = lastGlobals.keySet().iterator(); i.hasNext();) { - String g = (String) i.next(); - - try { - global.deleteProperty(g, g.hashCode()); - } catch (Exception x) { - System.err.println("Error resetting global property: " + g); - } - } - - lastGlobals = null; - } - } - - /** - * Invoke a function on some object, using the given arguments and global vars. - */ - public Object invoke(Object thisObject, String functionName, Object[] args, - boolean xmlrpc) throws ScriptingException { - ESObject eso = null; - - if (thisObject == null) { - eso = global; - } else { - eso = getElementWrapper(thisObject); - } - - // Workaround to let the evaluator know about the current thread - // even if enterContext() has not been called yet. Otherwise, - // a TimeoutException would be thrown. - evaluator.thread = Thread.currentThread(); - - try { - ESValue[] esv = (args == null) ? new ESValue[0] : new ESValue[args.length]; - - for (int i = 0; i < esv.length; i++) { - // XML-RPC requires special argument conversion - if (xmlrpc) { - esv[i] = processXmlRpcArgument(args[i], evaluator); - } - // for java.util.Map objects, we use the special "tight" wrapper - // that makes the Map look like a native object - else if (args[i] instanceof Map) { - esv[i] = new ESMapWrapper(this, (Map) args[i]); - } else { - esv[i] = ESLoader.normalizeValue(args[i], evaluator); - } - } - - ESValue retval = eso.doIndirectCall(evaluator, eso, functionName, esv); - - if (xmlrpc) { - return processXmlRpcResponse(retval); - } else if (retval == null) { - return null; - } else { - return retval.toJavaObject(); - } - } catch (RedirectException redirect) { - throw redirect; - } catch (TimeoutException timeout) { - throw timeout; - } catch (ConcurrencyException concur) { - throw concur; - } catch (Exception x) { - // check if this is a redirect exception, which has been converted by fesi - // into an EcmaScript exception, which is why we can't explicitly catch it - if (reval.res.getRedirect() != null) { - throw new RedirectException(reval.res.getRedirect()); - } - - // do the same for not-modified responses - if (reval.res.getNotModified()) { - throw new RedirectException(null); - } - - // has the request timed out? If so, throw TimeoutException - if (evaluator.thread != Thread.currentThread()) { - throw new TimeoutException(); - } - - if (app.debug()) { - x.printStackTrace(); - } - - // create and throw a ScriptingException wrapping the original exception - throw new ScriptingException(x); - } - } - - /** - * Let the evaluator know that the current evaluation has been - * aborted. This is done by setting the thread ref in the evaluator - * object to null. - */ - public void abort() { - // unset the thread filed in the FESI evaluator - evaluator.thread = null; - } - - /** - * Check if an object has a function property (public method if it - * is a java object) with that name. - */ - public boolean hasFunction(Object obj, String fname) { - // System.err.println ("HAS_FUNC: "+fname); - String protoname = app.getPrototypeName(obj); - - try { - ObjectPrototype op = getPrototype(protoname); - - // if this is an untyped object return false - if (op == null) { - return false; - } - - ESValue func = op.getProperty(fname, fname.hashCode()); - - if ((func != null) && func instanceof FunctionPrototype) { - return true; - } - } catch (EcmaScriptException esx) { - // System.err.println ("Error in hasFunction: "+esx); - return false; - } - - return false; - } - - /** - * Check if an object has a defined property (public field if it - * is a java object) with that name. - */ - public Object get(Object obj, String propname) { - if ((obj == null) || (propname == null)) { - return null; - } - - String prototypeName = app.getPrototypeName(obj); - - if ("user".equalsIgnoreCase(prototypeName) && - "password".equalsIgnoreCase(propname)) { - throw new RuntimeException("access to password property not allowed"); - } - - // if this is a HopObject, check if the property is defined - // in the type.properties db-mapping. - if (obj instanceof INode) { - DbMapping dbm = app.getDbMapping(prototypeName); - - if (dbm != null) { - Relation rel = dbm.propertyToRelation(propname); - - if ((rel == null) || !rel.isPrimitive()) { - throw new RuntimeException("\"" + propname + "\" is not defined in " + - prototypeName); - } - } - } - - ESObject eso = getElementWrapper(obj); - - try { - ESValue prop = eso.getProperty(propname, propname.hashCode()); - - if ((prop != null) && !(prop instanceof ESNull) && - !(prop instanceof ESUndefined)) { - return prop.toJavaObject(); - } - } catch (EcmaScriptException esx) { - // System.err.println ("Error in getProperty: "+esx); - return null; - } - - return null; - } - - /** - * Get a DocApplication object for a specific application. - */ - public IPathElement getIntrospector() { - if (doc == null) { - doc = new DocApplication(app.getName(), app.getAppDir().toString()); - doc.readApplication(); - } - - return doc; - } - - /** - * Convert an input argument from Java to the scripting runtime - * representation. - */ - public static ESValue processXmlRpcArgument(Object what, Evaluator evaluator) - throws Exception { - if (what == null) { - return ESNull.theNull; - } - - if (what instanceof Vector) { - Vector v = (Vector) what; - ArrayPrototype retval = new ArrayPrototype(evaluator.getArrayPrototype(), - evaluator); - int l = v.size(); - - for (int i = 0; i < l; i++) - retval.putProperty(i, processXmlRpcArgument(v.elementAt(i), evaluator)); - - return retval; - } - - if (what instanceof Hashtable) { - Hashtable t = (Hashtable) what; - ESObject retval = new ObjectPrototype(evaluator.getObjectPrototype(), - evaluator); - - for (Enumeration e = t.keys(); e.hasMoreElements();) { - String next = (String) e.nextElement(); - - retval.putProperty(next, processXmlRpcArgument(t.get(next), evaluator), - next.hashCode()); - } - - return retval; - } - - if (what instanceof String) { - return new ESString(what.toString()); - } - - if (what instanceof Number) { - return new ESNumber(new Double(what.toString()).doubleValue()); - } - - if (what instanceof Boolean) { - return ESBoolean.makeBoolean(((Boolean) what).booleanValue()); - } - - if (what instanceof Date) { - return new DatePrototype(evaluator, (Date) what); - } - - return ESLoader.normalizeValue(what, evaluator); - } - - /** - * convert a JavaScript Object object to a generic Java object stucture. - */ - public static Object processXmlRpcResponse(ESValue what) - throws EcmaScriptException { - if ((what == null) || what instanceof ESNull) { - return null; - } - - if (what instanceof ArrayPrototype) { - ArrayPrototype a = (ArrayPrototype) what; - int l = a.size(); - Vector v = new Vector(); - - for (int i = 0; i < l; i++) { - Object nj = processXmlRpcResponse(a.getProperty(i)); - - v.addElement(nj); - } - - return v; - } - - if (what instanceof ObjectPrototype) { - ObjectPrototype o = (ObjectPrototype) what; - Hashtable t = new Hashtable(); - - for (Enumeration e = o.getProperties(); e.hasMoreElements();) { - String next = (String) e.nextElement(); - - // We don't do deep serialization of HopObjects to avoid - // that the whole web site structure is sucked out with one - // object. Instead we return some kind of "proxy" objects - // that only contain the prototype and id of the HopObject property. - Object nj = null; - ESValue esv = o.getProperty(next, next.hashCode()); - - if (esv instanceof ESNode) { - INode node = ((ESNode) esv).getNode(); - - if (node != null) { - Hashtable nt = new Hashtable(); - - nt.put("id", node.getID()); - - if (node.getPrototype() != null) { - nt.put("prototype", node.getPrototype()); - } - - nj = nt; - } - } else { - nj = processXmlRpcResponse(esv); - } - - if (nj != null) { // can't put null as value in hashtable - t.put(next, nj); - } - } - - return t; - } - - if (what instanceof ESUndefined || what instanceof ESNull) { - return null; - } - - Object jval = what.toJavaObject(); - - if (jval instanceof Byte || jval instanceof Short) { - jval = new Integer(jval.toString()); - } - - return jval; - } - - /** - * Return the FESI Evaluator object wrapped by this object. - */ - public Evaluator getEvaluator() { - return evaluator; - } - - /** - * Return the application we're running in - */ - public Application getApplication() { - return app; - } - - /** - * Get a raw prototype, i.e. in potentially unfinished state - * without checking if it needs to be updated. - */ - private ObjectPrototype getRawPrototype(String protoName) { - if (protoName == null) { - return null; - } - - TypeInfo info = (TypeInfo) prototypes.get(protoName); - - return (info == null) ? null : info.objectPrototype; - } - - /** - * Get the object prototype for a prototype name and initialize/update it - * if necessary. - */ - public ObjectPrototype getPrototype(String protoName) { - if (protoName == null) { - return null; - } - - TypeInfo info = (TypeInfo) prototypes.get(protoName); - - if ((info != null) && (info.lastUpdate == 0)) { - Prototype p = app.typemgr.getPrototype(protoName); - - if (p != null) { - app.typemgr.updatePrototype(p); - - if (p.getLastUpdate() > info.lastUpdate) { - info.lastUpdate = p.getLastUpdate(); - evaluatePrototype(p); - } - - // set info.lastUpdate to 1 if it is 0 so we know we - // have initialized this prototype already, even if - // it is empty (i.e. doesn't contain any scripts/skins/actoins - if (info.lastUpdate == 0) { - info.lastUpdate = 1; - } - } - } - - return (info == null) ? null : info.objectPrototype; - } - - /** - * Register an object prototype for a certain prototype name. - */ - public void putPrototype(String protoName, ObjectPrototype op) { - if ((protoName != null) && (op != null)) { - prototypes.put(protoName, new TypeInfo(op, protoName)); - } - } - - /** - * Get a Script wrapper for an object. In contrast to getElementWrapper, this is called for - * any Java object, not just the ones in the request path which we know are scripted. - * So what we do is check if the object belongs to a scripted class. If so, we call getElementWrapper() - * with the object, otherwise we return a generic unscripted object wrapper. - */ - public ESValue getObjectWrapper(Object e) { - if (app.getPrototypeName(e) != null) { - return getElementWrapper(e); - } - /* else if (e instanceof Map) - return new ESMapWrapper (this, (Map) e); */ - /* else if (e instanceof INode) - return new ESNode ((INode) e, this); */ - else { - return new ESWrapper(e, evaluator); - } - } - - /** - * Get a Script wrapper for any given object. If the object implements the IPathElement - * interface, the getPrototype method will be used to retrieve the name of the prototype - * to use. Otherwise, a Java-Class-to-Script-Prototype mapping is consulted. - */ - public ESObject getElementWrapper(Object e) { - // check if e is an instance of a helma objectmodel node. - if (e instanceof INode) { - return getNodeWrapper((INode) e); - } - - // Gotta find out the prototype name to use for this object... - String prototypeName = app.getPrototypeName(e); - - ObjectPrototype op = getPrototype(prototypeName); - - if (op == null) { - op = getPrototype("hopobject"); - } - - return new ESGenericObject(op, evaluator, e); - } - - /** - * Get a script wrapper for an instance of helma.objectmodel.INode - */ - public ESNode getNodeWrapper(INode n) { - // FIXME: should this return ESNull.theNull? - if (n == null) { - return null; - } - - ESNode esn = (ESNode) wrappercache.get(n); - - if ((esn == null) || (esn.getNode() != n)) { - String protoname = n.getPrototype(); - - ObjectPrototype op = null; - - // set the DbMapping of the node according to its prototype. - // this *should* be done on the objectmodel level, but isn't currently - // for embedded nodes since there's not enough type info at the objectmodel level - // for those nodes. - if ((protoname != null) && (protoname.length() > 0) && - (n.getDbMapping() == null)) { - n.setDbMapping(app.getDbMapping(protoname)); - } - - op = getPrototype(protoname); - - // no prototype found for this node? - if (op == null) { - op = getPrototype("hopobject"); - } - - esn = new ESNode(op, evaluator, n, this); - - wrappercache.put(n, esn); - - // app.logEvent ("Wrapper for "+n+" created"); - } - - return esn; - } - - /** - * Register a new Node wrapper with the wrapper cache. This is used by the - * Node constructor. - */ - public void putNodeWrapper(INode n, ESNode esn) { - wrappercache.put(n, esn); - } - - /** - * Return the application's classloader - */ - public ClassLoader getClassLoader() { - return app.getClassLoader(); - } - - /** - * Return the RequestEvaluator owning and driving this FESI evaluator. - */ - public RequestEvaluator getRequestEvaluator() { - return reval; - } - - /** - * Return the Response object of the current evaluation context. Proxy method to RequestEvaluator. - */ - public ResponseTrans getResponse() { - return reval.res; - } - - /** - * Return the Request object of the current evaluation context. Proxy method to RequestEvaluator. - */ - public RequestTrans getRequest() { - return reval.req; - } - - private synchronized void evaluate(Prototype prototype, Object code) { - if (code instanceof FunctionFile) { - FunctionFile funcfile = (FunctionFile) code; - File file = funcfile.getFile(); - - if (file != null) { - try { - FileReader fr = new FileReader(file); - EvaluationSource es = new FileEvaluationSource(funcfile.getSourceName(), - null); - - updateEvaluator(prototype, fr, es); - } catch (IOException iox) { - app.logEvent("Error updating function file: " + iox); - } - } else { - StringReader reader = new StringReader(funcfile.getContent()); - EvaluationSource es = new FileEvaluationSource(funcfile.getSourceName(), - null); - - updateEvaluator(prototype, reader, es); - } - } else if (code instanceof ActionFile) { - ActionFile action = (ActionFile) code; - FesiActionAdapter fa = new FesiActionAdapter(action); - - try { - fa.updateEvaluator(this); - } catch (EcmaScriptException esx) { - app.logEvent("Error parsing " + action + ": " + esx); - } - } - } - - private synchronized void updateEvaluator(Prototype prototype, Reader reader, - EvaluationSource source) { - try { - ObjectPrototype op = getPrototype(prototype.getName()); - - // do the update, evaluating the file - evaluator.evaluate(reader, op, source, false); - } catch (Throwable e) { - app.logEvent("Error parsing function file " + source + ": " + e); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ignore) { - } - } - } - } - - class TypeInfo { - ObjectPrototype objectPrototype; - long lastUpdate = 0; - String protoName; - - public TypeInfo(ObjectPrototype op, String name) { - objectPrototype = op; - protoName = name; - } - - public String toString() { - return ("TypeInfo[" + protoName + "," + new Date(lastUpdate) + "]"); - } - } -} diff --git a/src/helma/scripting/fesi/HopExtension.java b/src/helma/scripting/fesi/HopExtension.java deleted file mode 100644 index aa6309f5..00000000 --- a/src/helma/scripting/fesi/HopExtension.java +++ /dev/null @@ -1,1174 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import helma.framework.*; -import helma.framework.IPathElement; -import helma.framework.core.*; -import helma.objectmodel.*; -import helma.scripting.ScriptingException; -import helma.util.*; -import org.xml.sax.InputSource; -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.text.*; -import java.text.SimpleDateFormat; -import java.util.*; - -/** - * This is the basic Extension for FESI interpreters used in Helma. It sets up - * varios constructors, global functions and properties on the HopObject prototype - * (Node objects), the global prototype, the session object etc. - */ -public final class HopExtension { - protected Application app; - protected FesiEngine engine; - - // previously in FESI.Data.NumberObject - private Hashtable formatTable = new Hashtable(); - - /** - * Creates a new HopExtension object. - * - * @param app ... - */ - public HopExtension(Application app) { - this.app = app; - } - - /** - * Called by the evaluator after the extension is loaded. - */ - public void initializeExtension(FesiEngine engine) - throws EcmaScriptException { - this.engine = engine; - - Evaluator evaluator = engine.getEvaluator(); - GlobalObject go = evaluator.getGlobalObject(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - ESObject op = evaluator.getObjectPrototype(); - - // The editor functions for String, Boolean and Number are deprecated! - ESObject sp = evaluator.getStringPrototype(); - - sp.putHiddenProperty("editor", new StringEditor("editor", evaluator, fp)); - - ESObject np = evaluator.getNumberPrototype(); - - np.putHiddenProperty("editor", new NumberEditor("editor", evaluator, fp)); - np.putHiddenProperty("format", new NumberPrototypeFormat("format", evaluator, fp)); - - ESObject bp = evaluator.getBooleanPrototype(); - - bp.putHiddenProperty("editor", new BooleanEditor("editor", evaluator, fp)); - - ESObject dp = evaluator.getDatePrototype(); - - dp.putHiddenProperty("format", new DatePrototypeFormat("format", evaluator, fp)); - - sp.putHiddenProperty("trim", new StringTrim("trim", evaluator, fp)); - - // generic (Java wrapper) object prototype - ObjectPrototype esObjectPrototype = new ObjectPrototype(op, evaluator); - - // the Node prototype - ObjectPrototype esNodePrototype = new ObjectPrototype(op, evaluator); - - // the Session prototype - ObjectPrototype esSessionPrototype = new ObjectPrototype(esNodePrototype, - evaluator); - - // the Node constructor - ESObject node = new NodeConstructor("Node", fp, engine); - - // register the default methods of Node objects in the Node prototype - esNodePrototype.putHiddenProperty("add", new NodeAdd("add", evaluator, fp)); - esNodePrototype.putHiddenProperty("addAt", new NodeAddAt("addAt", evaluator, fp)); - esNodePrototype.putHiddenProperty("remove", - new NodeRemove("remove", evaluator, fp)); - esNodePrototype.putHiddenProperty("list", new NodeList("list", evaluator, fp)); - esNodePrototype.putHiddenProperty("set", new NodeSet("set", evaluator, fp)); - esNodePrototype.putHiddenProperty("get", new NodeGet("get", evaluator, fp)); - esNodePrototype.putHiddenProperty("count", new NodeCount("count", evaluator, fp)); - esNodePrototype.putHiddenProperty("contains", - new NodeContains("contains", evaluator, fp)); - esNodePrototype.putHiddenProperty("size", new NodeCount("size", evaluator, fp)); - esNodePrototype.putHiddenProperty("editor", - new NodeEditor("editor", evaluator, fp)); - esNodePrototype.putHiddenProperty("path", new NodeHref("path", evaluator, fp)); - esNodePrototype.putHiddenProperty("href", new NodeHref("href", evaluator, fp)); - esNodePrototype.putHiddenProperty("setParent", - new NodeSetParent("setParent", evaluator, fp)); - esNodePrototype.putHiddenProperty("invalidate", - new NodeInvalidate("invalidate", evaluator, fp)); - esNodePrototype.putHiddenProperty("prefetchChildren", - new NodePrefetch("prefetchChildren", evaluator, - fp)); - esNodePrototype.putHiddenProperty("renderSkin", - new RenderSkin("renderSkin", evaluator, fp, - false, false)); - esNodePrototype.putHiddenProperty("renderSkinAsString", - new RenderSkin("renderSkinAsString", evaluator, - fp, false, true)); - esNodePrototype.putHiddenProperty("clearCache", - new NodeClearCache("clearCache", evaluator, fp)); - - // default methods for generic Java wrapper object prototype. - // This is a small subset of the methods in esNodePrototype. - esObjectPrototype.putHiddenProperty("href", new NodeHref("href", evaluator, fp)); - esObjectPrototype.putHiddenProperty("renderSkin", - new RenderSkin("renderSkin", evaluator, fp, - false, false)); - esObjectPrototype.putHiddenProperty("renderSkinAsString", - new RenderSkin("renderSkinAsString", - evaluator, fp, false, true)); - - // methods that give access to properties and global user lists - go.putHiddenProperty("Node", node); // register the constructor for a plain Node object. - go.putHiddenProperty("HopObject", node); // HopObject is the new name for node. - go.putHiddenProperty("getProperty", - new GlobalGetProperty("getProperty", evaluator, fp)); - go.putHiddenProperty("token", new GlobalGetProperty("token", evaluator, fp)); - go.putHiddenProperty("getAge", new GlobalGetAge("getAge", evaluator, fp)); - go.putHiddenProperty("getURL", new GlobalGetURL("getURL", evaluator, fp)); - go.putHiddenProperty("encode", new GlobalEncode("encode", evaluator, fp)); - go.putHiddenProperty("encodeXml", new GlobalEncodeXml("encodeXml", evaluator, fp)); - go.putHiddenProperty("encodeForm", - new GlobalEncodeForm("encodeForm", evaluator, fp)); - go.putHiddenProperty("format", new GlobalFormat("format", evaluator, fp)); - go.putHiddenProperty("stripTags", new GlobalStripTags("stripTags", evaluator, fp)); - go.putHiddenProperty("getXmlDocument", - new GlobalGetXmlDocument("getXmlDocument", evaluator, fp)); - go.putHiddenProperty("getHtmlDocument", - new GlobalGetHtmlDocument("getHtmlDocument", evaluator, fp)); - go.putHiddenProperty("jdomize", new GlobalJDOM("jdomize", evaluator, fp)); - go.putHiddenProperty("getSkin", new GlobalCreateSkin("getSkin", evaluator, fp)); - go.putHiddenProperty("createSkin", - new GlobalCreateSkin("createSkin", evaluator, fp)); - go.putHiddenProperty("renderSkin", - new RenderSkin("renderSkin", evaluator, fp, true, false)); - go.putHiddenProperty("renderSkinAsString", - new RenderSkin("renderSkinAsString", evaluator, fp, true, - true)); - go.putHiddenProperty("authenticate", - new GlobalAuthenticate("authenticate", evaluator, fp)); - go.deleteProperty("exit", "exit".hashCode()); - - // register object prototypes with FesiEngine - engine.putPrototype("global", go); - engine.putPrototype("hopobject", esNodePrototype); - engine.putPrototype("__javaobject__", esObjectPrototype); - - // engine.putPrototype ("session", esSessionPrototype); - } - - private String getNumberChoice(String name, double from, double to, double step, - double value) { - double l = 0.000001; - StringBuffer b = new StringBuffer(""); - - return b.toString(); - } - - class NodeAdd extends BuiltinFunctionObject { - NodeAdd(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return ESBoolean.makeBoolean(node.add(arguments)); - } - } - - class NodeAddAt extends BuiltinFunctionObject { - NodeAddAt(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return ESBoolean.makeBoolean(node.addAt(arguments)); - } - } - - class NodeRemove extends BuiltinFunctionObject { - NodeRemove(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return ESBoolean.makeBoolean(node.remove(arguments)); - } - } - - class NodeList extends BuiltinFunctionObject { - NodeList(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 0); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return node.list(); - } - } - - class NodeGet extends BuiltinFunctionObject { - NodeGet(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESValue esv = null; - - if (arguments[0].isNumberValue()) { - int i = arguments[0].toInt32(); - - esv = thisObject.getProperty(i); - } else { - String name = arguments[0].toString(); - - // call esNodeProperty() method special to ESNode because we want to avoid - // retrieving prototype functions when calling hopobject.get(). - ESNode esn = (ESNode) thisObject; - - esv = esn.getNodeProperty(name); - } - - return (esv); - } - } - - class NodeSet extends BuiltinFunctionObject { - NodeSet(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode esn = (ESNode) thisObject; - - if (arguments[0].isNumberValue()) { - return ESBoolean.makeBoolean(esn.addAt(arguments)); - } else { - String propname = arguments[0].toString(); - - esn.putProperty(propname, arguments[1], propname.hashCode()); - } - - return ESBoolean.makeBoolean(true); - } - } - - class NodeCount extends BuiltinFunctionObject { - NodeCount(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - INode node = ((ESNode) thisObject).getNode(); - - return new ESNumber((double) node.numberOfNodes()); - } - } - - class NodeContains extends BuiltinFunctionObject { - NodeContains(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return new ESNumber(node.contains(arguments)); - } - } - - class NodeInvalidate extends BuiltinFunctionObject { - NodeInvalidate(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 0); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode esn = (ESNode) thisObject; - - return ESBoolean.makeBoolean(esn.invalidate(arguments)); - } - } - - class NodePrefetch extends BuiltinFunctionObject { - NodePrefetch(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 0); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - try { - ESNode esn = (ESNode) thisObject; - - esn.prefetchChildren(arguments); - } catch (IllegalArgumentException illarg) { - throw illarg; - } catch (Exception x) { - // swallow exceptions in prefetchChildren - } - - return ESNull.theNull; - } - } - - class NodeEditor extends BuiltinFunctionObject { - NodeEditor(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - throw new EcmaScriptException("Missing argument for Node.editor(): Name of property to be edited."); - } - - String propName = arguments[0].toString(); - ESValue propValue = thisObject.getProperty(propName, propName.hashCode()); - - if (propValue.isBooleanValue()) { - return (booleanEditor(thisObject, propName, propValue, arguments)); - } - - if (propValue.isNumberValue()) { - return (numberEditor(thisObject, propName, propValue, arguments)); - } - - if (propValue instanceof DatePrototype) { - return (dateEditor(thisObject, propName, propValue, arguments)); - } - - return (stringEditor(thisObject, propName, propValue, arguments)); - } - - public ESValue stringEditor(ESObject thisObject, String propName, - ESValue propValue, ESValue[] arguments) - throws EcmaScriptException { - String value = null; - - if ((propValue == null) || (ESNull.theNull == propValue) || - (ESUndefined.theUndefined == propValue)) { - value = ""; - } else { - value = HtmlEncoder.encodeFormValue(propValue.toString()); - } - - if ((arguments.length == 2) && arguments[1].isNumberValue()) { - return new ESString(""); - } else if ((arguments.length == 3) && arguments[1].isNumberValue() && - arguments[2].isNumberValue()) { - return new ESString(""); - } - - return new ESString(""); - } - - public ESValue dateEditor(ESObject thisObject, String propName, - ESValue propValue, ESValue[] arguments) - throws EcmaScriptException { - Date date = (Date) propValue.toJavaObject(); - DateFormat fmt = (arguments.length > 1) - ? new SimpleDateFormat(arguments[1].toString()) - : new SimpleDateFormat(); - - return new ESString(""); - } - - public ESValue numberEditor(ESObject thisObject, String propName, - ESValue propValue, ESValue[] arguments) - throws EcmaScriptException { - ESNumber esn = (ESNumber) propValue.toESNumber(); - double value = esn.doubleValue(); - - if ((arguments.length == 3) && arguments[1].isNumberValue() && - arguments[2].isNumberValue()) { - return new ESString(getNumberChoice(propName, arguments[1].toInt32(), - arguments[2].toInt32(), 1, value)); - } else if ((arguments.length == 4) && arguments[1].isNumberValue() && - arguments[2].isNumberValue() && arguments[3].isNumberValue()) { - return new ESString(getNumberChoice(propName, arguments[1].doubleValue(), - arguments[2].doubleValue(), - arguments[3].doubleValue(), value)); - } - - DecimalFormat fmt = new DecimalFormat(); - - if ((arguments.length == 2) && arguments[1].isNumberValue()) { - return new ESString(""); - } else { - return new ESString(""); - } - } - - public ESValue booleanEditor(ESObject thisObject, String propName, - ESValue propValue, ESValue[] arguments) - throws EcmaScriptException { - ESBoolean esb = (ESBoolean) propValue.toESBoolean(); - String value = esb.toString(); - - if (arguments.length < 2) { - String retval = ""); - - return new ESString(retval.toString()); - } - } - - class StringEditor extends BuiltinFunctionObject { - StringEditor(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw (new EcmaScriptException("String.editor() wird nicht mehr unterstuetzt. Statt node.strvar.editor(...) kann node.editor(\"strvar\", ...) verwendet werden.")); - } - } - - class NumberEditor extends BuiltinFunctionObject { - NumberEditor(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw (new EcmaScriptException("Number.editor() wird nicht mehr unterstuetzt. Statt node.nvar.editor(...) kann node.editor(\"nvar\", ...) verwendet werden.")); - } - } - - class BooleanEditor extends BuiltinFunctionObject { - BooleanEditor(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw (new EcmaScriptException("Boolean.editor() wird nicht mehr unterstuetzt. Statt node.boolvar.editor(...) kann node.editor(\"boolvar\", ...) verwendet werden.")); - } - } - - // previously in FESI.Data.DateObject - class DatePrototypeFormat extends BuiltinFunctionObject { - DatePrototypeFormat(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 0); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - DatePrototype aDate = (DatePrototype) thisObject; - - DateFormat df = (arguments.length > 0) - ? new SimpleDateFormat(arguments[0].toString()) - : new SimpleDateFormat(); - - df.setTimeZone(TimeZone.getDefault()); - - return (aDate.toJavaObject() == null) ? new ESString("") - : new ESString(df.format((Date) aDate.toJavaObject())); - } - } - - class NumberPrototypeFormat extends BuiltinFunctionObject { - NumberPrototypeFormat(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 0); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESObject aNumber = (ESObject) thisObject; - String fstr = "#,##0.00"; - - if (arguments.length >= 1) { - fstr = arguments[0].toString(); - } - - DecimalFormat df = (DecimalFormat) formatTable.get(fstr); - - if (df == null) { - df = new DecimalFormat(fstr); - formatTable.put(fstr, df); - } - - return new ESString(df.format(aNumber.doubleValue())); - } - } - - class StringTrim extends BuiltinFunctionObject { - StringTrim(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return new ESString(thisObject.toString().trim()); - } - } - - class GlobalGetProperty extends BuiltinFunctionObject { - GlobalGetProperty(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length == 0) { - return new ESString(""); - } - - String defval = (arguments.length > 1) ? arguments[1].toString() : ""; - - return new ESString(app.getProperty(arguments[0].toString(), defval)); - } - } - - class GlobalAuthenticate extends BuiltinFunctionObject { - GlobalAuthenticate(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length != 2) { - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(app.authenticate(arguments[0].toString(), - arguments[1].toString())); - } - } - - /** - * Get a parsed Skin from an app-managed skin text - */ - class GlobalCreateSkin extends BuiltinFunctionObject { - GlobalCreateSkin(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments.length != 1) || ESNull.theNull.equals(arguments[0])) { - throw new EcmaScriptException("createSkin must be called with one String argument"); - } - - String str = arguments[0].toString(); - Skin skin = new Skin(str, app); - - return new ESWrapper(skin, evaluator); - } - } - - /** - * Render a skin - */ - class RenderSkin extends BuiltinFunctionObject { - boolean global; - boolean asString; - - RenderSkin(String name, Evaluator evaluator, FunctionPrototype fp, - boolean global, boolean asString) { - super(fp, evaluator, name, 1); - this.global = global; - this.asString = asString; - } - - public ESValue callFunction(ESObject thisObj, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments.length < 1) || (arguments.length > 2) || - (arguments[0] == null) || (arguments[0] == ESNull.theNull)) { - throw new EcmaScriptException("renderSkin requires one argument containing the skin name and an optional parameter object argument"); - } - - try { - Skin skin = null; - ESObject thisObject = global ? null : thisObj; - HashMap params = null; - - if ((arguments.length > 1) && arguments[1] instanceof ESObject) { - // create an parameter object to pass to the skin - ESObject paramObject = (ESObject) arguments[1]; - - params = new HashMap(); - - for (Enumeration en = paramObject.getProperties(); - en.hasMoreElements();) { - String propname = (String) en.nextElement(); - - params.put(propname, - paramObject.getProperty(propname, propname.hashCode()) - .toJavaObject()); - } - } - - // first, see if the first argument already is a skin object. If not, it's the name of the skin to be called - if (arguments[0] instanceof ESWrapper) { - Object obj = ((ESWrapper) arguments[0]).toJavaObject(); - - if (obj instanceof Skin) { - skin = (Skin) obj; - } - } - - // retrieve res.skinpath, an array of objects that tell us where to look for skins - // (strings for directory names and INodes for internal, db-stored skinsets) - ResponseTrans res = engine.getResponse(); - Object[] skinpath = res.getSkinpath(); - - // ready... retrieve the skin and render it. - Object javaObject = (thisObject == null) ? null : thisObject.toJavaObject(); - - if (skin == null) { - String skinid = app.getPrototypeName(javaObject) + "/" + - arguments[0].toString(); - - skin = res.getCachedSkin(skinid); - - if (skin == null) { - skin = app.getSkin(app.getPrototypeName(javaObject), arguments[0].toString(), skinpath); - res.cacheSkin(skinid, skin); - } - } - - if (asString) { - res.pushStringBuffer(); - } - - if (skin != null) { - skin.render(engine.getRequestEvaluator(), javaObject, params); - } else { - res.write("[Skin not found: " + arguments[0] + "]"); - } - - if (asString) { - return new ESString(res.popStringBuffer()); - } - } catch (RedirectException redir) { - // let redirect pass through - throw redir; - } catch (Exception x) { - x.printStackTrace(); - throw new EcmaScriptException("renderSkin: " + x); - } - - return ESNull.theNull; - } - } - - class GlobalGetAge extends BuiltinFunctionObject { - GlobalGetAge(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments.length != 1) || !(arguments[0] instanceof DatePrototype)) { - throw new EcmaScriptException("Invalid arguments for function getAge(Date)"); - } - - Date d = (Date) arguments[0].toJavaObject(); - - try { - long then = d.getTime() / 60000L; - long now = System.currentTimeMillis() / 60000L; - StringBuffer age = new StringBuffer(); - String divider = "vor "; - long diff = now - then; - long days = diff / 1440; - - if (days > 0) { - age.append((days > 1) ? (divider + days + " Tagen") : (divider + - "1 Tag")); - divider = ", "; - } - - long hours = (diff % 1440) / 60; - - if (hours > 0) { - age.append((hours > 1) ? (divider + hours + " Stunden") - : (divider + "1 Stunde")); - divider = ", "; - } - - long minutes = diff % 60; - - if (minutes > 0) { - age.append((minutes > 1) ? (divider + minutes + " Minuten") - : (divider + "1 Minute")); - } - - return new ESString(age.toString()); - } catch (Exception e) { - app.logEvent("Error formatting date: " + e); - e.printStackTrace(); - - return new ESString(""); - } - } - } - - class GlobalGetURL extends BuiltinFunctionObject { - GlobalGetURL(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - try { - URL url = new URL(arguments[0].toString()); - URLConnection con = url.openConnection(); - - // do we have if-modified-since or etag headers to set? - if (arguments.length > 1) { - if (arguments[1] instanceof DatePrototype) { - Date date = (Date) arguments[1].toJavaObject(); - - con.setIfModifiedSince(date.getTime()); - - SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", - Locale.UK); - - format.setTimeZone(TimeZone.getTimeZone("GMT")); - con.setRequestProperty("If-Modified-Since", format.format(date)); - } else if (arguments[1] != null) { - con.setRequestProperty("If-None-Match", arguments[1].toString()); - } - } - - String httpUserAgent = app.getProperty("httpUserAgent"); - - if (httpUserAgent != null) { - con.setRequestProperty("User-Agent", httpUserAgent); - } - - con.setAllowUserInteraction(false); - - String filename = url.getFile(); - String contentType = con.getContentType(); - long lastmod = con.getLastModified(); - String etag = con.getHeaderField("ETag"); - int length = con.getContentLength(); - int resCode = 0; - - if (con instanceof HttpURLConnection) { - resCode = ((HttpURLConnection) con).getResponseCode(); - } - - ByteArrayOutputStream body = new ByteArrayOutputStream(); - - if ((length != 0) && (resCode != 304)) { - InputStream in = new BufferedInputStream(con.getInputStream()); - byte[] b = new byte[1024]; - int read; - - while ((read = in.read(b)) > -1) - body.write(b, 0, read); - - in.close(); - } - - MimePart mime = new MimePart(filename, body.toByteArray(), contentType); - - if (lastmod > 0) { - mime.lastModified = new Date(lastmod); - } - - mime.eTag = etag; - - return ESLoader.normalizeObject(mime, evaluator); - } catch (Exception ignore) { - } - - return ESNull.theNull; - } - } - - class GlobalEncode extends BuiltinFunctionObject { - GlobalEncode(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - return new ESString(HtmlEncoder.encodeAll(arguments[0].toString())); - } - } - - class GlobalEncodeXml extends BuiltinFunctionObject { - GlobalEncodeXml(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - return new ESString(HtmlEncoder.encodeXml(arguments[0].toString())); - } - } - - class GlobalEncodeForm extends BuiltinFunctionObject { - GlobalEncodeForm(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - return new ESString(HtmlEncoder.encodeFormValue(arguments[0].toString())); - } - } - - // strip tags from XML/HTML text - class GlobalStripTags extends BuiltinFunctionObject { - GlobalStripTags(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - StringBuffer b = new StringBuffer(); - char[] c = arguments[0].toString().toCharArray(); - boolean inTag = false; - - for (int i = 0; i < c.length; i++) { - if (c[i] == '<') { - inTag = true; - } - - if (!inTag) { - b.append(c[i]); - } - - if (c[i] == '>') { - inTag = false; - } - } - - return new ESString(b.toString()); - } - } - - class GlobalFormat extends BuiltinFunctionObject { - GlobalFormat(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length < 1) { - return ESNull.theNull; - } - - return new ESString(HtmlEncoder.encode(arguments[0].toString())); - } - } - - class GlobalGetXmlDocument extends BuiltinFunctionObject { - GlobalGetXmlDocument(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - try { - Object p = arguments[0].toJavaObject(); - Object doc = helma.util.XmlUtils.parseXml(p); - - return ESLoader.normalizeObject(doc, evaluator); - } catch (Exception noluck) { - app.logEvent("Error creating XML document: " + noluck); - } - - return ESNull.theNull; - } - } - - class GlobalGetHtmlDocument extends BuiltinFunctionObject { - GlobalGetHtmlDocument(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - try { - Object p = arguments[0].toJavaObject(); - Object doc = helma.util.XmlUtils.parseHtml(p); - - return ESLoader.normalizeObject(doc, evaluator); - } catch (Exception noluck) { - app.logEvent("Error creating HTML document: " + noluck); - } - - return ESNull.theNull; - } - } - - class GlobalJDOM extends BuiltinFunctionObject { - GlobalJDOM(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - try { - Class.forName("org.w3c.dom.Document"); - - org.w3c.dom.Document doc = (org.w3c.dom.Document) arguments[0].toJavaObject(); - - Class.forName("org.jdom.input.DOMBuilder"); - - org.jdom.input.DOMBuilder builder = new org.jdom.input.DOMBuilder(); - - return ESLoader.normalizeObject(builder.build(doc), evaluator); - } catch (Exception noluck) { - app.logEvent("Error building JDOM document: " + noluck); - } - - return ESNull.theNull; - } - } - - class NodeSetParent extends BuiltinFunctionObject { - NodeSetParent(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 2); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return ESBoolean.makeBoolean(node.setParent(arguments)); - } - } - - class NodeClearCache extends BuiltinFunctionObject { - NodeClearCache(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 2); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = (ESNode) thisObject; - - return ESBoolean.makeBoolean(node.clearCache()); - } - } - - class NodeHref extends BuiltinFunctionObject { - NodeHref(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - Object elem = thisObject.toJavaObject(); - String tmpname = (arguments.length == 0) ? "" : arguments[0].toString(); - String basicHref = app.getNodeHref(elem, tmpname); - - // check if the app.properties specify a href-function to post-process the - // basic href. - String hrefFunction = app.getProperty("hrefFunction", null); - - if (hrefFunction != null) { - Object funcElem = elem; - - while (funcElem != null) { - if (engine.hasFunction(funcElem, hrefFunction)) { - Object obj; - - try { - obj = engine.invoke(funcElem, hrefFunction, - new Object[] { basicHref }, false); - } catch (ScriptingException x) { - throw new RuntimeException("Error in hrefFunction: " + x); - } - - if (obj == null) { - throw new RuntimeException("hrefFunction " + hrefFunction + - " returned null"); - } - - basicHref = obj.toString(); - - break; - } - - funcElem = app.getParentElement(funcElem); - } - } - - // check if the app.properties specify a href-skin to post-process the - // basic href. - String hrefSkin = app.getProperty("hrefSkin", null); - - if (hrefSkin != null) { - // we need to post-process the href with a skin for this application - // first, look in the object href was called on. - Object skinElem = elem; - Skin skin = null; - - // ResponseTrans res = engine.getResponse(); - // Object[] skinpath = res.getSkinpath (); - while ((skin == null) && (skinElem != null)) { - Prototype proto = app.getPrototype(skinElem); - - if (proto != null) { - skin = proto.getSkin(hrefSkin); - } - - if (skin == null) { - skinElem = app.getParentElement(skinElem); - } - } - - if (skin != null) { - basicHref = renderSkin(skin, basicHref, skinElem); - } - } - - return new ESString(basicHref); - } - - private String renderSkin(Skin skin, String path, Object skinElem) - throws EcmaScriptException { - engine.getResponse().pushStringBuffer(); - - HashMap param = new HashMap(); - - param.put("path", path); - skin.render(engine.getRequestEvaluator(), skinElem, param); - - return engine.getResponse().popStringBuffer().trim(); - } - } -} diff --git a/src/helma/scripting/fesi/NodeConstructor.java b/src/helma/scripting/fesi/NodeConstructor.java deleted file mode 100644 index c4172e05..00000000 --- a/src/helma/scripting/fesi/NodeConstructor.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import helma.framework.core.*; -import helma.objectmodel.db.Node; - -/** - * A constructor for user defined data types. This first constructs a node, sets its prototype - * and invokes the scripted constructor function on it. - */ -public class NodeConstructor extends BuiltinFunctionObject { - FesiEngine engine; - String typename; - - /** - * Creates a new NodeConstructor object. - * - * @param name ... - * @param fp ... - * @param engine ... - */ - public NodeConstructor(String name, FunctionPrototype fp, FesiEngine engine) { - super(fp, engine.getEvaluator(), name, 1); - typename = name; - this.engine = engine; - } - - /** - * - * - * @param thisObject ... - * @param arguments ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - /** - * - * - * @param thisObject ... - * @param arguments ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESNode node = null; - Application app = engine.getApplication(); - - if ("Node".equals(typename) || "hopobject".equalsIgnoreCase(typename)) { - String nodeName = null; - - if ((arguments.length > 0) && (arguments[0] != null)) { - nodeName = arguments[0].toString(); - } - - Node n = new Node(nodeName, (String) null, app.getWrappedNodeManager()); - - node = new ESNode(engine.getPrototype("hopobject"), this.evaluator, n, engine); - engine.putNodeWrapper(node.getNode(), node); - } else { - // Typed nodes are instantiated as helma.objectmodel.db.Node from the beginning - // even if we don't know yet if they are going to be stored in a database. The reason - // is that we want to be able to use the specail features like subnode relations even for - // transient nodes. - ObjectPrototype op = engine.getPrototype(typename); - Node n = new Node(typename, typename, app.getWrappedNodeManager()); - - node = new ESNode(op, engine.getEvaluator(), n, engine); - node.setPrototype(typename); - node.getNode().setDbMapping(app.getDbMapping(typename)); - - try { - // first try calling "constructor", if that doesn't work, try calling a function - // with the name of the type. - // HACK: There is an incompatibility problem here, because the property - // constructor is defined as the constructor of the object by EcmaScript. - if (op.getProperty("constructor", "constructor".hashCode()) instanceof ConstructedFunctionObject) { - node.doIndirectCall(engine.getEvaluator(), node, "constructor", - arguments); - } else if (op.getProperty(typename, typename.hashCode()) instanceof ConstructedFunctionObject) { - node.doIndirectCall(engine.getEvaluator(), node, typename, arguments); - } - } catch (Exception x) { - throw new EcmaScriptException(x.toString()); - } - } - - return node; - } - - /** - * - * - * @param propertyName ... - * @param previousScope ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getPropertyInScope(String propertyName, ScopeChain previousScope, - int hash) throws EcmaScriptException { - return super.getPropertyInScope(propertyName, previousScope, hash); - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - if ("prototype".equals(propertyName)) { - return engine.getPrototype(typename); - } - - return super.getProperty(propertyName, hash); - } - - /** - * - * - * @return ... - */ - public String[] getSpecialPropertyNames() { - String[] ns = { }; - - return ns; - } -} - // class NodeConstructor diff --git a/src/helma/scripting/fesi/PhantomEngine.java b/src/helma/scripting/fesi/PhantomEngine.java deleted file mode 100644 index 3a0100b9..00000000 --- a/src/helma/scripting/fesi/PhantomEngine.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi; - -import helma.scripting.ScriptingException; - -/** - * - */ -public final class PhantomEngine extends FesiEngine { - /** - * - */ - public Object invoke(Object thisObject, String functionName, Object[] args, - boolean xmlrpc) throws ScriptingException { - return super.invoke(thisObject, functionName, args, xmlrpc); - } -} diff --git a/src/helma/scripting/fesi/extensions/Database.java b/src/helma/scripting/fesi/extensions/Database.java deleted file mode 100644 index b30f2077..00000000 --- a/src/helma/scripting/fesi/extensions/Database.java +++ /dev/null @@ -1,1076 +0,0 @@ -// Database.java -// FESI Copyright (c) Jean-Marc Lugrin, 1999 -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. - -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - -// Modified to use Helma database connections, Hannes Wallnöfer 2000 - -package helma.scripting.fesi.extensions; - -import helma.framework.core.Application; -import helma.objectmodel.db.DbSource; -import FESI.Parser.*; -import FESI.AST.*; -import FESI.Interpreter.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Data.*; - -import java.util.Date; -import java.util.Enumeration; -import java.util.Vector; -import java.sql.*; - - -/** - * A Database object, representing a connection to a JDBC Driver - */ -class ESDatabase extends ESObject { - - private transient Connection connection = null; // Null if not connected - private transient DatabaseMetaData databaseMetaData = null; - private transient String driverName = null; - private transient ClassLoader driverLoader = null; - private transient Exception lastError = null; - private transient ESObject esRowSetPrototype = null; - private transient boolean driverOK = false; - - /** - * Create a new database object based on a hop data source. - * - * @param prototype The prototype object for all database object - * @param evaluator The current evaluator - * @param esRowSetPrototype The prototype to use to create row set - * @param dbsource The name of the DB source - */ - - ESDatabase(ESObject prototype, - Evaluator evaluator, - ESObject esRowSetPrototype, - DbSource dbsource, int flag) { - super(prototype, evaluator); - this.esRowSetPrototype = esRowSetPrototype; // specific to an evaluator - try { - connection = dbsource.getConnection (); - driverName = dbsource.getDriverName (); - } catch (Exception e) { - // System.err.println("##Cannot find driver class: " + e); - // e.printStackTrace(); - lastError = e; - } - driverOK = true; - } - - /** - * Create a new database object based on a driver name, with driver on the classpath - * - * @param prototype The prototype object for all database object - * @param evaluator The current evaluator - * @param esRowSetPrototype The prototype to use to create row set - * @param driverName The class name of the JDBC driver - */ - - ESDatabase(ESObject prototype, - Evaluator evaluator, - ESObject esRowSetPrototype, - String driverName) { - super(prototype, evaluator); - this.driverName = driverName; - this.esRowSetPrototype = esRowSetPrototype; // specific to an evaluator - try { - Class driverClass = Class.forName(driverName); - if (!Driver.class.isAssignableFrom(driverClass)) { - - // System.err.println("##Bad class " + driverClass); - lastError = new EcmaScriptException("Class " + driverClass + " is not a JDBC driver"); - } - driverClass.newInstance(); // may be needed by some drivers, harmless for others - } catch (ClassNotFoundException e) { - // System.err.println("##Cannot find driver class: " + e); - // e.printStackTrace(); - lastError = e; - } catch (InstantiationException e) { - - // System.err.println("##Cannot instantiate driver class: " + e); - // e.printStackTrace(); - lastError = e; - } catch (IllegalAccessException e) { - // ignore as this may happen depending on driver, it may not be severe - // for example because the instance was created at class initialization time - } - driverOK = true; - } - - /** - * Create a new database object based on a driver name and its classpath - * - * @param prototype The prototype object for all database object - * @param evaluator The current evaluator - * @param esRowSetPrototype The prototype to use to create row set - * @param driverName The class name of the JDBC driver - * @param pathName The path to the classes of the JDBC driver - */ - ESDatabase(ESObject prototype, - Evaluator evaluator, - ESObject esRowSetPrototype, - String driverName, - String pathName) { - super(prototype, evaluator); - this.driverName = driverName; - this.esRowSetPrototype = esRowSetPrototype; - try { - this.driverLoader = LocalClassLoader.makeLocalClassLoader(pathName); - } catch (EcmaScriptException e) { - // System.err.println("##Cannot find driver class: " + e); - // e.printStackTrace(); - lastError = e; - return; - } - try { - Class driverClass = driverLoader.loadClass(driverName); - if (!Driver.class.isAssignableFrom(driverClass)) { - // System.err.println("##Bad class " + driverClass); - // e.printStackTrace(); - lastError = new EcmaScriptException("Class " + driverClass + " is not a JDBC driver"); - return; - } - driverClass.newInstance(); - } catch (ClassNotFoundException e) { - // System.err.println("##Cannot find driver class: " + e); - // e.printStackTrace(); - lastError = e; - } catch (InstantiationException e) { - // System.err.println("##Cannot instantiate driver class: " + e); - // e.printStackTrace(); - lastError = e; - } catch (IllegalAccessException e) { - // System.err.println("##Cannot access driver class: " + e); - // e.printStackTrace(); - lastError = e; - } - driverOK = true; - } - - /** - * Create the database prototype object which cannot be connected - * - * @param prototype The prototype object for the database prototype object - * @param evaluator The current evaluator - */ - - ESDatabase(ESObject prototype, - Evaluator evaluator) { - super(prototype, evaluator); - this.driverName = null; - this.esRowSetPrototype = null; - driverOK = false; // Avoid usage of this object - } - - public String getESClassName() { - return "Database"; - } - - public String toString() { - if (driverName==null) return "[database protoype]"; - return "[Database: '" + driverName + - (driverOK ? - (connection==null ? "' - disconnected] " : " - connected]") - : " - in error]"); - } - - public String toDetailString() { - return "ES:[Object: builtin " + this.getClass().getName() + ":" + - this.toString() + "]"; - } - - ESValue getLastError() throws EcmaScriptException { - if (lastError == null) { - return ESNull.theNull; - } else { - return ESLoader.normalizeValue(lastError, evaluator); - } - } - - - /** - * Connect to the database, using the specific url, optional user name and password - * - * @param arguments The argument list - * @return true if successful, false otherwise - */ - ESValue connect(ESValue arguments[]) throws EcmaScriptException { - if (!driverOK) { - throw new EcmaScriptException("Driver not initialized properly - cannot connect"); - } - lastError = null; - String url = (arguments.length>0) ? arguments[0].toString() : null; - String userName = (arguments.length>1) ? arguments[1].toString() : null; - String password = (arguments.length>2) ? arguments[2].toString() : null; - try { - if (userName == null) { - connection = DriverManager.getConnection(url); - } else { - connection = DriverManager.getConnection(url,userName,password); - } - } catch(Exception e) { - // System.err.println("##Cannot connect: " + e); - // e.printStackTrace(); - lastError = e; - return ESBoolean.makeBoolean(false); - } - return ESBoolean.makeBoolean(true); - } - - - /** - * Disconnect from the database, nop if not conected - * - * @return true if successful, false if error during idsconnect - */ - ESValue disconnect() throws EcmaScriptException { - if (!driverOK) { - throw new EcmaScriptException("Driver not initialized properly - cannot disconnect"); - } - lastError = null; - if (connection != null) { - try { - connection.close(); - connection = null; - lastError = null; - } catch (SQLException e) { - // System.err.println("##Cannot disonnect: " + e); - // e.printStackTrace(); - lastError = e; - return ESBoolean.makeBoolean(false); - } - } - return ESBoolean.makeBoolean(true); - } - - ESValue release() { - if (driverOK) { - try { - disconnect(); - } catch (EcmaScriptException e) { - // ignored - } - } - return ESUndefined.theUndefined; - } - - ESValue executeRetrieval(ESValue arguments[]) throws EcmaScriptException { - String sql = (arguments.length>0) ? arguments[0].toString() : null; - - if (connection==null) { - throw new EcmaScriptException("JDBC driver not connected"); - } - Statement statement = null; - ResultSet resultSet = null; - - try { - statement = connection.createStatement(); - resultSet = statement.executeQuery(sql); // will return true if first result is a result set - } catch(SQLException e) { - // System.err.println("##Cannot retrieve: " + e); - // e.printStackTrace(); - lastError = e; - try { - if (statement!=null) statement.close(); - } catch (Exception ignored) { - } - statement = null; - return ESBoolean.makeBoolean(false); - } - ESRowSet rowSet = new ESRowSet(esRowSetPrototype, evaluator, sql, this, statement, resultSet); - return rowSet; - //return ESBoolean.makeBoolean(true); - } - - ESValue executeCommand(ESValue arguments[]) throws EcmaScriptException { - String sql = (arguments.length>0) ? arguments[0].toString() : null; - int count = 0; - - if (connection==null) { - throw new EcmaScriptException("JDBC driver not connected"); - } - - Statement statement = null; - try { - - statement = connection.createStatement(); - count = statement.executeUpdate(sql); // will return true if first result is a result set - } catch(SQLException e) { - // System.err.println("##Cannot retrieve: " + e); - // e.printStackTrace(); - lastError = e; - try { - if (statement != null) statement.close(); - } catch (Exception ignored) { - } - statement = null; - return ESBoolean.makeBoolean(false); - } - if (statement!=null) try { - statement.close(); - } catch (SQLException e) { - // ignored - } - return new ESNumber(count); - //return ESBoolean.makeBoolean(true); - } - - public Object getMetaData() - { - if (databaseMetaData == null) - try { - databaseMetaData = connection.getMetaData(); - } catch (SQLException e) { - // ignored - } - return databaseMetaData; - } -} - - -/** - * A RowSet object - */ -class ESRowSet extends ESObject { - - private transient ESDatabase database = null; - private transient String sql = null; - private transient Statement statement = null; - private transient ResultSet resultSet = null; - private transient ResultSetMetaData resultSetMetaData = null; - private transient Vector colNames = null; - private transient boolean lastRowSeen = false; - private transient boolean firstRowSeen = false; - private transient Exception lastError = null; - - ESRowSet(ESObject prototype, - Evaluator evaluator, - String sql, - ESDatabase database, - Statement statement, - ResultSet resultSet) throws EcmaScriptException { - super(prototype, evaluator); - this.sql = sql; - this.database = database; - this.statement = statement; - this.resultSet = resultSet; - - if (sql==null) throw new NullPointerException(); - if (resultSet==null) throw new NullPointerException(); - if (statement==null) throw new NullPointerException(); - if (database==null) throw new NullPointerException(); - - try { - - this.resultSetMetaData = resultSet.getMetaData(); - int numcols = resultSetMetaData.getColumnCount(); - //IServer.getLogger().log("$$NEXT : " + numcols); - colNames = new Vector(numcols); - for (int i=0; i0 && idx <=colNames.size()) { - return (String) colNames.elementAt(idx-1); // to base 0 - } else { - throw new EcmaScriptException("Column index (base 1) " + idx + - " out of range, max: " +colNames.size()); - } - } - - - public int getColumnDatatypeNumber(int idx) throws EcmaScriptException { - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - if (idx>0 && idx <=colNames.size()) { - try { - return resultSetMetaData.getColumnType(idx); - } catch (SQLException e) { - lastError = e; - return -1; - } - } else { - throw new EcmaScriptException("Column index (base 1) " + idx + - " out of range, max: " +colNames.size()); - } - } - - - public String getColumnDatatypeName(int idx) throws EcmaScriptException { - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - if (idx>0 && idx <=colNames.size()) { - try { - return resultSetMetaData.getColumnTypeName(idx); - } catch (SQLException e) { - lastError = e; - return null; - } - } else { - throw new EcmaScriptException("Column index (base 1) " + idx + - " out of range, max: " +colNames.size()); - } - } - - - public ESValue getColumnItem(String propertyName) throws EcmaScriptException { - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - if (!firstRowSeen) { - throw new EcmaScriptException("Attempt to access data before the first row is read"); - } - int hash = propertyName.hashCode(); - try { - int index = -1; // indicates not a valid index value - try { - char c = propertyName.charAt(0); - if ('0' <= c && c <= '9') { - index = Integer.parseInt(propertyName); - } - } catch (NumberFormatException e) { - } catch (StringIndexOutOfBoundsException e) { // for charAt - } - if (index>=0) { - return getProperty(index); - } - Object o = resultSet.getObject(propertyName); - ESValue value = ESLoader.normalizeValue(o, evaluator); - // IServer.getLogger().log("&& @VALUE : " + value); - lastError = null; - return value; - } catch (SQLException e) { - //System.err.println("##Cannot get property '" + propertyName + "' " + e); - //e.printStackTrace(); - lastError = e; - } - return ESUndefined.theUndefined; - } - - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - //System.err.println(" &&& Getting property '" + propertyName + "'"); - - // Length property is firsy checked - - // First return system or or prototype properties - if (propertyName.equals("length")) { - return new ESNumber((double) colNames.size()); - } else if (super.hasProperty(propertyName, hash)) { - return super.getProperty(propertyName, hash); - } else { - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - if (!firstRowSeen) { - throw new EcmaScriptException("Attempt to access data before the first row is read"); - } - try { - int index = -1; // indicates not a valid index value - try { - char c = propertyName.charAt(0); - if ('0' <= c && c <= '9') { - index = Integer.parseInt(propertyName); - } - } catch (NumberFormatException e) { - } catch (StringIndexOutOfBoundsException e) { // for charAt - } - if (index>=0) { - return getProperty(index); - } - Object o = resultSet.getObject(propertyName); - ESValue value = ESLoader.normalizeValue(o, evaluator); - // IServer.getLogger().log("&& @VALUE : " + value); - lastError = null; - return value; - } catch (SQLException e) { - // System.err.println("##Cannot get property '" + propertyName + "' " + e); - // e.printStackTrace(); - lastError = e; - } - } - return ESUndefined.theUndefined; - } - - public ESValue getProperty(int index) - throws EcmaScriptException { - if (!firstRowSeen) { - throw new EcmaScriptException("Attempt to access data before the first row is read"); - } - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - - try { - Object o = resultSet.getObject(index); - ESValue value = ESLoader.normalizeValue(o, evaluator); - lastError = null; - return value; - } catch (SQLException e) { - // System.err.println("##Cannot get property: " + e); - // e.printStackTrace(); - lastError = e; - } - return ESUndefined.theUndefined; - } - - /* - * Returns an enumerator for the key elements of this object. - * - * @return the enumerator - may have 0 length of coulmn names where not found - */ - public Enumeration getProperties() { - if (resultSet == null) { - return (new Vector()).elements(); - } - return colNames.elements(); - } - - /** - * Get all properties (including hidden ones), for the command - * @listall of the interpreter. Include the visible properties of the - * prototype (that is the one added by the user) but not the - * hidden ones of the prototype (otherwise this would list - * all functions for any object). - * - * @return An enumeration of all properties (visible and hidden). - */ - public Enumeration getAllProperties() { - return new Enumeration() { - String [] specialProperties = getSpecialPropertyNames(); - int specialEnumerator = 0; - Enumeration props = getProperties(); // all of object properties - String currentKey = null; - int currentHash = 0; - boolean inside = false; // true when examing prototypes properties - public boolean hasMoreElements() { - // OK if we already checked for a property and one exists - if (currentKey != null) return true; - // Loop on special properties first - if (specialEnumerator < specialProperties.length) { - currentKey = specialProperties[specialEnumerator]; - currentHash = currentKey.hashCode(); - specialEnumerator++; - return true; - } - // loop on standard or prototype properties - while (props.hasMoreElements()) { - currentKey = (String) props.nextElement(); - currentHash = currentKey.hashCode(); - if (inside) { - try { - if (hasProperty(currentKey, currentHash)) continue; - } catch (EcmaScriptException ignore) { - } - // SHOULD CHECK IF NOT IN SPECIAL - } - return true; - } - // If prototype properties have not yet been examined, look for them - if (!inside && getPrototype() != null) { - inside = true; - props = getPrototype().getProperties(); - while (props.hasMoreElements()) { - currentKey = (String) props.nextElement(); - currentHash = currentKey.hashCode(); - try { - if (hasProperty(currentKey, currentHash)) continue; - } catch (EcmaScriptException ignore) { - } - return true; - } - } - return false; - } - public Object nextElement() { - if (hasMoreElements()) { - String key = currentKey; - currentKey = null; - return key; - } else { - throw new java.util.NoSuchElementException(); - } - } - }; - } - - public String[] getSpecialPropertyNames() { - String [] ns = {"length"}; - return ns; - } - - - ESValue next() throws EcmaScriptException { - boolean status = false; - if (lastRowSeen) { - throw new EcmaScriptException("Attempt to access a next row after last row has been returned"); - } - if (resultSet == null) { - throw new EcmaScriptException("Attempt to access a released result set"); - } - try { - status = resultSet.next(); - lastError = null; - } catch (SQLException e) { - // System.err.println("##Cannot do next:" + e); - // e.printStackTrace(); - lastError = e; - } - if (status) firstRowSeen = true; - else lastRowSeen = true; - return ESBoolean.makeBoolean(status); - } - - public String toString() { - return "[RowSet: '"+sql+"'" + - (resultSet==null ? " - released]" : - (lastRowSeen ? " - at end]" : - (firstRowSeen ? "]" : " - at start]"))); - } - -} - - - - -public class Database extends Extension { - - - private transient Evaluator evaluator = null; - private ESObject esDatabasePrototype = null; - private ESObject esRowSetPrototype = null; - Application app; - - public Database () { - super(); - } - - - public void setApplication (Application app) { - this.app = app; - } - - ////////////////// Added by Hannes Wallnoefer - class GlobalGetDBConnection extends BuiltinFunctionObject { - GlobalGetDBConnection(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if (arguments.length != 1) - throw new EcmaScriptException ("Wrong number of arguments in getDBConnection(dbsource)"); - String srcname = arguments[0].toString (); - DbSource dbsrc = app.getDbSource (srcname.toLowerCase ()); - if (dbsrc == null) - throw new EcmaScriptException ("DbSource "+srcname+" does not exist"); - ESDatabase db = new ESDatabase (esDatabasePrototype, this.evaluator, - esRowSetPrototype, dbsrc, 0); - return db; - } - } - - - class GlobalObjectDatabase extends BuiltinFunctionObject { - GlobalObjectDatabase(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = null; - //IServer.getLogger().log("esDatabasePrototype="+esDatabasePrototype); - if (arguments.length==0) { - throw new EcmaScriptException("Database requires 1 or 2 arguments"); - } else if (arguments.length==1) { - database = new ESDatabase(esDatabasePrototype, - this.evaluator, - esRowSetPrototype, - arguments[0].toString()); - } else if (arguments.length>1) { - database = new ESDatabase(esDatabasePrototype, - this.evaluator, - esRowSetPrototype, - arguments[0].toString(), - arguments[1].toString()); - } - return database; - } - - - } - - - class DatabaseGetLastError extends BuiltinFunctionObject { - DatabaseGetLastError(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.getLastError(); - } - } - - class DatabaseRelease extends BuiltinFunctionObject { - DatabaseRelease(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.release(); - } - } - - - class DatabaseConnect extends BuiltinFunctionObject { - DatabaseConnect(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.connect(arguments); - } - } - - - class DatabaseDisconnect extends BuiltinFunctionObject { - DatabaseDisconnect(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.disconnect(); - } - } - - class DatabaseExecuteRetrieval extends BuiltinFunctionObject { - DatabaseExecuteRetrieval(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.executeRetrieval(arguments); - } - } - - - class DatabaseExecuteCommand extends BuiltinFunctionObject { - DatabaseExecuteCommand(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return database.executeCommand(arguments); - } - } - - - class DatabaseGetMetaData extends BuiltinFunctionObject { - DatabaseGetMetaData(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESDatabase database = (ESDatabase) thisObject; - return new ESWrapper(database.getMetaData(), this.evaluator); - } - } - - - - - - class RowSetRelease extends BuiltinFunctionObject { - RowSetRelease(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - return rowSet.release(); - } - } - - class RowSetNext extends BuiltinFunctionObject { - RowSetNext(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - return rowSet.next(); - } - } - - class RowSetGetColumnCount extends BuiltinFunctionObject { - RowSetGetColumnCount(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - return new ESNumber((double) rowSet.getColumnCount()); - } - } - - class RowSetGetColumnName extends BuiltinFunctionObject { - RowSetGetColumnName(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - if (arguments.length<1) { - throw new EcmaScriptException("Missing parameter in function " + this); - } - int idx = arguments[0].toUInt32(); - String name = rowSet.getColumnName(idx); // base 1 - if (name==null) { - return ESUndefined.theUndefined; - } else { - return new ESString(name); - } - } - } - - - class RowSetGetColumnItem extends BuiltinFunctionObject { - RowSetGetColumnItem(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - if (arguments.length<1) { - throw new EcmaScriptException("Missing parameter in function " + this); - } - String name = arguments[0].toString(); - return rowSet.getColumnItem(name); - } - } - - - - class RowSetGetColumnDatatypeNumber extends BuiltinFunctionObject { - RowSetGetColumnDatatypeNumber(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - if (arguments.length<1) { - throw new EcmaScriptException("Missing parameter in function " + this); - } - int idx = arguments[0].toUInt32(); - return new ESNumber((double)rowSet.getColumnDatatypeNumber(idx)); - } - } - - - class RowSetGetColumnDatatypeName extends BuiltinFunctionObject { - RowSetGetColumnDatatypeName(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - if (arguments.length<1) { - throw new EcmaScriptException("Missing parameter in function " + this); - } - int idx = arguments[0].toUInt32(); - String name = rowSet.getColumnDatatypeName(idx); - //IServer.getLogger().log("Datat type name for col " + idx + " is " +name); - if (name==null) { - return ESUndefined.theUndefined; - } else { - return new ESString(name); - } - } - } - - - class RowSetHasMoreRows extends BuiltinFunctionObject { - RowSetHasMoreRows(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - return ESBoolean.makeBoolean(rowSet.hasMoreRows()); - } - } - - - class RowSetGetMetaData extends BuiltinFunctionObject { - RowSetGetMetaData(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - public ESValue callFunction(ESObject thisObject, - ESValue[] arguments) - throws EcmaScriptException { - ESRowSet rowSet = (ESRowSet) thisObject; - return new ESWrapper(rowSet.getMetaData(), this.evaluator); - } - } - - - public void initializeExtension(Evaluator evaluator) throws EcmaScriptException { - - this.evaluator = evaluator; - GlobalObject go = evaluator.getGlobalObject(); - ObjectPrototype op = (ObjectPrototype) evaluator.getObjectPrototype(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - esRowSetPrototype = new ObjectPrototype(op, evaluator); - esDatabasePrototype = new ESDatabase(op, evaluator); // No driver - - ESObject globalDatabaseObject = new GlobalObjectDatabase("Database", evaluator, fp); - globalDatabaseObject.putHiddenProperty("prototype",esDatabasePrototype); - globalDatabaseObject.putHiddenProperty("length",new ESNumber(2)); - - - esDatabasePrototype.putHiddenProperty("constructor",globalDatabaseObject); - esDatabasePrototype.putHiddenProperty("getLastError", - new DatabaseGetLastError("getLastError", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("release", - new DatabaseRelease("release", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("connect", - new DatabaseConnect("connect", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("disconnect", - new DatabaseDisconnect("disconnect", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("executeRetrieval", - new DatabaseExecuteRetrieval("executeRetrieval", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("executeCommand", - new DatabaseExecuteCommand("executeCommand", evaluator, fp)); - esDatabasePrototype.putHiddenProperty("getMetaData", - new DatabaseGetMetaData("getMetaData", evaluator, fp)); - - esRowSetPrototype.putHiddenProperty("release", - new RowSetRelease("release", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("next", - new RowSetNext("next", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getColumnCount", - new RowSetGetColumnCount("getColumnCount", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getColumnName", - new RowSetGetColumnName("getColumnName", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getColumnItem", - new RowSetGetColumnItem("getColumnItem", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getColumnDatatypeNumber", - new RowSetGetColumnDatatypeNumber("getColumnDatatypeNumber", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getColumnDatatypeName", - new RowSetGetColumnDatatypeName("getColumnDatatypeName", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("hasMoreRows", - new RowSetHasMoreRows("hasMoreRows", evaluator, fp)); - esRowSetPrototype.putHiddenProperty("getMetaData", - new RowSetGetMetaData("getMetaData", evaluator, fp)); - - go.putHiddenProperty("Database", globalDatabaseObject); - // added by Hannes Wallnoefer - go.putHiddenProperty ("getDBConnection", new GlobalGetDBConnection ("getDBConnection", evaluator, fp)); - } - } diff --git a/src/helma/scripting/fesi/extensions/DomExtension.java b/src/helma/scripting/fesi/extensions/DomExtension.java deleted file mode 100644 index d3cb491e..00000000 --- a/src/helma/scripting/fesi/extensions/DomExtension.java +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import helma.framework.core.Application; -import helma.framework.core.RequestEvaluator; -import helma.objectmodel.INode; -import helma.objectmodel.db.Node; -import helma.objectmodel.dom.*; -import helma.scripting.fesi.ESNode; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.StringReader; - -/** - * - */ -public class DomExtension extends Extension { - private transient Evaluator evaluator = null; - - /** - * Creates a new DomExtension object. - */ - public DomExtension() { - super(); - } - - /** - * - * - * @param evaluator ... - * - * @throws EcmaScriptException ... - */ - public void initializeExtension(Evaluator evaluator) - throws EcmaScriptException { - this.evaluator = evaluator; - - GlobalObject go = evaluator.getGlobalObject(); - ObjectPrototype op = (ObjectPrototype) evaluator.getObjectPrototype(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - ESObject globalXml = new GlobalObjectXml("Xml", evaluator, fp); - - globalXml.putHiddenProperty("length", new ESNumber(1)); - globalXml.putHiddenProperty("read", new XmlRead("read", evaluator, fp, false)); - globalXml.putHiddenProperty("readFromString", - new XmlRead("readFromString", evaluator, fp, true)); - globalXml.putHiddenProperty("write", new XmlWrite("write", evaluator, fp)); - globalXml.putHiddenProperty("writeToString", - new XmlWriteToString("writeToString", evaluator, fp)); - globalXml.putHiddenProperty("get", new XmlGet("get", evaluator, fp)); - globalXml.putHiddenProperty("getFromString", - new XmlGetFromString("getFromString", evaluator, fp)); - go.putHiddenProperty("Xml", globalXml); - } - - class GlobalObjectXml extends BuiltinFunctionObject { - GlobalObjectXml(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - throw new EcmaScriptException("Xml can't be instanced"); - } - - public String toString() { - try { - String parser = javax.xml.parsers.DocumentBuilderFactory.newInstance() - .getClass() - .getPackage() - .getName(); - - return "[Xml " + parser + "]"; - } catch (NullPointerException zeero) { - return "[Xml - no parser available]"; - } - } - } - - class XmlWrite extends BuiltinFunctionObject { - XmlWrite(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments == null) || (arguments.length != 2)) { - throw new EcmaScriptException("Wrong number of arguments in Xml.write()"); - } - - INode node = null; - - try { - node = ((ESNode) arguments[0]).getNode(); - } catch (Exception e) { - // we definitly need a node - throw new EcmaScriptException("First argument in Xml.write() is not an hopobject"); - } - - try { - File tmpFile = new File(arguments[1].toString() + ".tmp." + - XmlWriter.generateID()); - XmlWriter writer = new XmlWriter(tmpFile, "UTF-8"); - - writer.setDatabaseMode(false); - - boolean result = writer.write(node); - - writer.close(); - - File finalFile = new File(arguments[1].toString()); - - tmpFile.renameTo(finalFile); - this.evaluator.engine.getApplication().logEvent("wrote xml to " + - finalFile.getAbsolutePath()); - } catch (IOException io) { - throw new EcmaScriptException(io.toString()); - } - - return ESBoolean.makeBoolean(true); - } - } - - /** - * Xml.create() is used to get a string containing the xml-content. - * Useful if Xml-content should be made public through the web. - */ - class XmlWriteToString extends BuiltinFunctionObject { - XmlWriteToString(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments == null) || (arguments.length == 0)) { - throw new EcmaScriptException("Not enough arguments in Xml.writeToString()"); - } - - INode node = null; - - try { - node = ((ESNode) arguments[0]).getNode(); - } catch (Exception e) { - // we definitly need a node - throw new EcmaScriptException("argument is not an hopobject"); - } - - try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - XmlWriter writer = new XmlWriter(out, "UTF-8"); - - // in case we ever want to limit serialization depth... - // if (arguments.length > 1 && arguments[1] instanceof ESNumber) - // writer.setMaxLevels(arguments[1].toInt32()); - writer.setDatabaseMode(false); - - boolean result = writer.write(node); - - writer.flush(); - - return new ESString(out.toString("UTF-8")); - } catch (IOException io) { - throw new EcmaScriptException(io.toString()); - } - } - } - - class XmlRead extends BuiltinFunctionObject { - boolean fromstring; - - XmlRead(String name, Evaluator evaluator, FunctionPrototype fp, boolean fromstring) { - super(fp, evaluator, name, 1); - this.fromstring = fromstring; - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments == null) || (arguments.length == 0)) { - throw new EcmaScriptException("Missing arguments in Xml.read()"); - } - - INode node = null; - - try { - node = ((ESNode) arguments[1]).getNode(); - } catch (Exception e) { //classcast, arrayindex etc - - // make sure we have a node, even if 2nd arg doesn't exist or is not a node - node = new Node((String) null, (String) null, - this.evaluator.engine.getApplication() - .getWrappedNodeManager()); - } - - try { - XmlReader reader = new XmlReader(); - INode result = null; - - if (fromstring) { - result = reader.read(new StringReader(arguments[0].toString()), node); - } else { - result = reader.read(new File(arguments[0].toString()), node); - } - - return this.evaluator.engine.getNodeWrapper(result); - } catch (NoClassDefFoundError e) { - throw new EcmaScriptException("Can't load XML parser:" + e); - } catch (Exception f) { - throw new EcmaScriptException(f.toString()); - } - } - } - - class XmlGet extends BuiltinFunctionObject { - XmlGet(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments == null) || (arguments.length == 0)) { - throw new EcmaScriptException("Xml.get() needs a location as an argument"); - } - - try { - XmlConverter converter; - - if (arguments.length > 1) { - converter = new XmlConverter(arguments[1].toString()); - } else { - converter = new XmlConverter(); - } - - INode node = new helma.objectmodel.db.Node((String) null, (String) null, - this.evaluator.engine.getApplication() - .getWrappedNodeManager()); - INode result = converter.convert(arguments[0].toString(), node); - - return this.evaluator.engine.getNodeWrapper(result); - } catch (NoClassDefFoundError e) { - throw new EcmaScriptException("Can't load dom-capable xml parser."); - } catch (RuntimeException f) { - throw new EcmaScriptException(f.toString()); - } - } - } - - class XmlGetFromString extends BuiltinFunctionObject { - XmlGetFromString(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - if ((arguments == null) || (arguments.length == 0)) { - throw new EcmaScriptException("Xml.getFromString() needs an XML string as parameter"); - } - - try { - XmlConverter converter; - - if (arguments.length > 1) { - converter = new XmlConverter(arguments[1].toString()); - } else { - converter = new XmlConverter(); - } - - INode node = new helma.objectmodel.db.Node((String) null, (String) null, - this.evaluator.engine.getApplication() - .getWrappedNodeManager()); - INode result = converter.convertFromString(arguments[0].toString(), node); - - return this.evaluator.engine.getNodeWrapper(result); - } catch (NoClassDefFoundError e) { - throw new EcmaScriptException("Can't load dom-capable xml parser."); - } catch (RuntimeException f) { - throw new EcmaScriptException(f.toString()); - } - } - } -} diff --git a/src/helma/scripting/fesi/extensions/ESMail.java b/src/helma/scripting/fesi/extensions/ESMail.java deleted file mode 100644 index 13aedd08..00000000 --- a/src/helma/scripting/fesi/extensions/ESMail.java +++ /dev/null @@ -1,353 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Interpreter.*; -import helma.util.*; -import java.io.*; -import java.util.*; -import javax.activation.*; -import javax.mail.Address; -import javax.mail.Message; -import javax.mail.Multipart; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; -import javax.mail.internet.MimeUtility; - -/** - * A JavaScript wrapper around a JavaMail message class to send - * mail via SMTP from Helma - */ -public class ESMail extends ESObject implements Serializable { - public static final int OK = 0; - public static final int SUBJECT = 10; - public static final int TEXT = 11; - public static final int MIMEPART = 12; - public static final int TO = 20; - public static final int CC = 21; - public static final int BCC = 22; - public static final int FROM = 23; - public static final int REPLYTO = 24; - public static final int SEND = 30; - MailExtension mailx; - Properties mprops; - MimeMessage message; - Multipart multipart; - StringBuffer buffer; - int status; - - /** - * Creates a new ESMail object. - * - * @param mailx ... - */ - public ESMail(MailExtension mailx) { - super(mailx.esMailPrototype, mailx.eval); - this.status = OK; - this.mailx = mailx; - this.mprops = mailx.mprops; - - // create some properties and get the default Session - try { - Properties props = new Properties(); - - props.put("mail.smtp.host", mprops.getProperty("smtp", "mail")); - - Session session = Session.getDefaultInstance(props, null); - - message = new MimeMessage(session); - } catch (Throwable t) { - this.evaluator.engine.getApplication().logEvent("Error in mail constructor: " + - t); - } - } - - /** - * - * - * @param status ... - */ - public void setStatus(int status) { - // Only register the first error that occurrs - if (this.status == 0) { - this.status = status; - } - } - - /** - * - * - * @return ... - */ - public int getStatus() { - return status; - } - - /** - * - * - * @param propertyName ... - * @param hash ... - * - * @return ... - * - * @throws EcmaScriptException ... - */ - public ESValue getProperty(String propertyName, int hash) - throws EcmaScriptException { - if ("status".equalsIgnoreCase(propertyName)) { - return new ESNumber(status); - } - - return super.getProperty(propertyName, hash); - } - - /** - * - */ - public void setText(ESValue val) throws Exception { - if (buffer == null) { - buffer = new StringBuffer(); - } - - if (val != null) { - buffer.append(val.toString()); - } - } - - /** - * - * - * @param val ... - * - * @throws Exception ... - * @throws IOException ... - */ - public void addPart(ESValue[] val) throws Exception { - if ((val == null) || (val.length == 0) || (val.length > 2)) { - throw new IOException("mail.addPart called with wrong number of arguments."); - } - - if (multipart == null) { - multipart = new MimeMultipart(); - } - - MimeBodyPart part = new MimeBodyPart(); - Object obj = val[0].toJavaObject(); - - if (obj instanceof String) { - part.setContent(obj.toString(), "text/plain"); - } else if (obj instanceof File) { - FileDataSource source = new FileDataSource((File) obj); - - part.setDataHandler(new DataHandler(source)); - } else if (obj instanceof MimePart) { - MimePartDataSource source = new MimePartDataSource((MimePart) obj); - - part.setDataHandler(new DataHandler(source)); - } - - // check if an explicit file name was given for this part - if (val.length == 2) { - try { - part.setFileName(val[1].toString()); - } catch (Exception x) { - // FIXME: error setting file name ... should we ignore this or throw an exception? - } - } - - multipart.addBodyPart(part); - } - - /** - * - * - * @param val ... - * - * @throws Exception ... - */ - public void setSubject(ESValue val) throws Exception { - if (val == null) { - return; - } - - message.setSubject(MimeUtility.encodeWord(val.toString())); - } - - /** - * - * - * @param add ... - * - * @throws Exception ... - * @throws AddressException ... - */ - public void setReplyTo(ESValue add) throws Exception { - String addstring = add.toString(); - - if (addstring.indexOf("@") < 0) { - throw new AddressException(); - } - - Address[] replyTo = new Address[1]; - - replyTo[0] = new InternetAddress(addstring); - message.setReplyTo(replyTo); - } - - /** - * - * - * @param add ... - * - * @throws Exception ... - * @throws AddressException ... - */ - public void setFrom(ESValue[] add) throws Exception { - String addstring = add[0].toString(); - - if (addstring.indexOf("@") < 0) { - throw new AddressException(); - } - - Address address = null; - - if (add.length > 1) { - address = new InternetAddress(addstring, - MimeUtility.encodeWord(add[1].toString())); - } else { - address = new InternetAddress(addstring); - } - - message.setFrom(address); - } - - /** - * - * - * @param add ... - * - * @throws Exception ... - * @throws AddressException ... - */ - public void addTo(ESValue[] add) throws Exception { - String addstring = add[0].toString(); - - if (addstring.indexOf("@") < 0) { - throw new AddressException(); - } - - Address address = null; - - if (add.length > 1) { - address = new InternetAddress(addstring, - MimeUtility.encodeWord(add[1].toString())); - } else { - address = new InternetAddress(addstring); - } - - message.addRecipient(Message.RecipientType.TO, address); - } - - /** - * - * - * @param add ... - * - * @throws Exception ... - * @throws AddressException ... - */ - public void addCC(ESValue[] add) throws Exception { - String addstring = add[0].toString(); - - if (addstring.indexOf("@") < 0) { - throw new AddressException(); - } - - Address address = null; - - if (add.length > 1) { - address = new InternetAddress(addstring, - MimeUtility.encodeWord(add[1].toString())); - } else { - address = new InternetAddress(addstring); - } - - message.addRecipient(Message.RecipientType.CC, address); - } - - /** - * - * - * @param add ... - * - * @throws Exception ... - * @throws AddressException ... - */ - public void addBCC(ESValue[] add) throws Exception { - String addstring = add[0].toString(); - - if (addstring.indexOf("@") < 0) { - throw new AddressException(); - } - - Address address = null; - - if (add.length > 1) { - address = new InternetAddress(addstring, - MimeUtility.encodeWord(add[1].toString())); - } else { - address = new InternetAddress(addstring); - } - - message.addRecipient(Message.RecipientType.BCC, address); - } - - /** - * - * - * @throws Exception ... - */ - public void send() throws Exception { - if (buffer != null) { - // if we also have a multipart body, add - // plain string as first part to it. - if (multipart != null) { - MimeBodyPart part = new MimeBodyPart(); - - part.setContent(buffer.toString(), "text/plain"); - multipart.addBodyPart(part, 0); - message.setContent(multipart); - } else { - message.setText(buffer.toString()); - } - } else if (multipart != null) { - message.setContent(multipart); - } else { - message.setText(""); - } - - Transport.send(message); - } -} diff --git a/src/helma/scripting/fesi/extensions/FtpExtension.java b/src/helma/scripting/fesi/extensions/FtpExtension.java deleted file mode 100644 index 0c772170..00000000 --- a/src/helma/scripting/fesi/extensions/FtpExtension.java +++ /dev/null @@ -1,525 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.AST.*; -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import FESI.Parser.*; -import com.oroinc.net.ftp.*; -import helma.objectmodel.*; -import java.io.*; - -/** - * A FTP-client object that allows to do some FTP from HOP applications. - * FTP support is far from complete but can easily be extended if more - * functionality is needed. - * This uses the NetComponent classes from savarese.org (ex oroinc.com). - */ -class ESFtpClient extends ESObject { - private FTPClient ftpclient; - private String server; - private Exception lastError = null; - private File localDir = null; - - /** - * Create a new FTP Client - * - * @param prototype The prototype object for the FTP object - * @param evaluator The current evaluator - */ - ESFtpClient(ESObject prototype, Evaluator evaluator, ESValue srvstr) { - super(prototype, evaluator); - this.server = srvstr.toString(); - } - - ESFtpClient(ESObject prototype, Evaluator evaluator) { - super(prototype, evaluator); - } - - /** - * - * - * @return ... - */ - public String getESClassName() { - return "FtpClient"; - } - - /** - * - * - * @return ... - */ - public String toString() { - return "[FtpClient]"; - } - - /** - * - * - * @return ... - */ - public String toDetailString() { - return "ES:[Object: builtin " + this.getClass().getName() + ":" + - this.toString() + "]"; - } - - ESValue getLastError() throws EcmaScriptException { - if (lastError == null) { - return ESNull.theNull; - } else { - return ESLoader.normalizeValue(lastError, evaluator); - } - } - - /** - * Login to the FTP server - * - * @param arguments The argument list - * @return true if successful, false otherwise - */ - ESValue login(ESValue[] arguments) throws EcmaScriptException { - if (server == null) { - return ESBoolean.makeBoolean(false); - } - - try { - ftpclient = new FTPClient(); - ftpclient.connect(server); - - boolean b = ftpclient.login(arguments[0].toString(), arguments[1].toString()); - - return ESBoolean.makeBoolean(b); - } catch (Exception x) { - return ESBoolean.makeBoolean(false); - } catch (NoClassDefFoundError x) { - return ESBoolean.makeBoolean(false); - } - } - - ESValue cd(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESBoolean.makeBoolean(false); - } - - try { - ftpclient.changeWorkingDirectory(arguments[0].toString()); - - return ESBoolean.makeBoolean(true); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue mkdir(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESBoolean.makeBoolean(false); - } - - try { - return ESBoolean.makeBoolean(ftpclient.makeDirectory(arguments[0].toString())); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue lcd(ESValue[] arguments) throws EcmaScriptException { - try { - localDir = new File(arguments[0].toString()); - - if (!localDir.exists()) { - localDir.mkdirs(); - } - - return ESBoolean.makeBoolean(true); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue putFile(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESBoolean.makeBoolean(false); - } - - try { - String fn = arguments[0].toString(); - File f = (localDir == null) ? new File(fn) : new File(localDir, fn); - InputStream fin = new BufferedInputStream(new FileInputStream(f)); - - ftpclient.storeFile(arguments[1].toString(), fin); - fin.close(); - - return ESBoolean.makeBoolean(true); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue putString(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESBoolean.makeBoolean(false); - } - - try { - byte[] bytes = null; - - // check if this already is a byte array - if (arguments[0] instanceof ESArrayWrapper) { - Object o = ((ESArrayWrapper) arguments[0]).toJavaObject(); - - if (o instanceof byte[]) { - bytes = (byte[]) o; - } - } - - if (bytes == null) { - bytes = arguments[0].toString().getBytes(); - } - - ByteArrayInputStream bin = new ByteArrayInputStream(bytes); - - ftpclient.storeFile(arguments[1].toString(), bin); - - return ESBoolean.makeBoolean(true); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue getFile(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESBoolean.makeBoolean(false); - } - - try { - String fn = arguments[0].toString(); - File f = (localDir == null) ? new File(fn) : new File(localDir, fn); - OutputStream out = new BufferedOutputStream(new FileOutputStream(f)); - - ftpclient.retrieveFile(arguments[0].toString(), out); - out.close(); - - return ESBoolean.makeBoolean(true); - } catch (Exception wrong) { - } - - return ESBoolean.makeBoolean(false); - } - - ESValue getString(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient == null) { - return ESNull.theNull; - } - - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - - ftpclient.retrieveFile(arguments[0].toString(), bout); - - return new ESString(bout.toString()); - } catch (Exception wrong) { - } - - return ESNull.theNull; - } - - /** - * Disconnect from FTP server - * - * @param arguments The argument list - * @return true if successful, false otherwise - */ - ESValue logout(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient != null) { - try { - ftpclient.logout(); - } catch (IOException ignore) { - } - - try { - ftpclient.disconnect(); - } catch (IOException ignore) { - } - } - - return ESBoolean.makeBoolean(true); - } - - ESValue binary(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient != null) { - try { - ftpclient.setFileType(FTP.BINARY_FILE_TYPE); - - return ESBoolean.makeBoolean(true); - } catch (IOException ignore) { - } - } - - return ESBoolean.makeBoolean(false); - } - - ESValue ascii(ESValue[] arguments) throws EcmaScriptException { - if (ftpclient != null) { - try { - ftpclient.setFileType(FTP.ASCII_FILE_TYPE); - - return ESBoolean.makeBoolean(true); - } catch (IOException ignore) { - } - } - - return ESBoolean.makeBoolean(false); - } -} - - -/** - * - */ -public class FtpExtension extends Extension { - private transient Evaluator evaluator = null; - private ESObject esFtpPrototype = null; - - /** - * Creates a new FtpExtension object. - */ - public FtpExtension() { - super(); - } - - /** - * - * - * @param evaluator ... - * - * @throws EcmaScriptException ... - */ - public void initializeExtension(Evaluator evaluator) - throws EcmaScriptException { - this.evaluator = evaluator; - - GlobalObject go = evaluator.getGlobalObject(); - ObjectPrototype op = (ObjectPrototype) evaluator.getObjectPrototype(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - esFtpPrototype = new ESFtpClient(op, evaluator); - - ESObject globalFtpObject = new GlobalObjectFtpClient("FtpClient", evaluator, fp); - - globalFtpObject.putHiddenProperty("prototype", esFtpPrototype); - globalFtpObject.putHiddenProperty("length", new ESNumber(1)); - - esFtpPrototype.putHiddenProperty("login", - new FtpClientLogin("login", evaluator, fp)); - esFtpPrototype.putHiddenProperty("cd", new FtpClientCD("cd", evaluator, fp)); - esFtpPrototype.putHiddenProperty("mkdir", - new FtpClientMKDIR("mkdir", evaluator, fp)); - esFtpPrototype.putHiddenProperty("lcd", new FtpClientLCD("lcd", evaluator, fp)); - esFtpPrototype.putHiddenProperty("putFile", - new FtpClientPutFile("putFile", evaluator, fp)); - esFtpPrototype.putHiddenProperty("putString", - new FtpClientPutString("putString", evaluator, fp)); - esFtpPrototype.putHiddenProperty("getFile", - new FtpClientGetFile("getFile", evaluator, fp)); - esFtpPrototype.putHiddenProperty("getString", - new FtpClientGetString("getString", evaluator, fp)); - esFtpPrototype.putHiddenProperty("logout", - new FtpClientLogout("logout", evaluator, fp)); - esFtpPrototype.putHiddenProperty("binary", - new FtpClientBinary("binary", evaluator, fp)); - esFtpPrototype.putHiddenProperty("ascii", - new FtpClientAscii("ascii", evaluator, fp)); - - go.putHiddenProperty("FtpClient", globalFtpObject); - } - - class GlobalObjectFtpClient extends BuiltinFunctionObject { - GlobalObjectFtpClient(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = null; - - if (arguments.length != 1) { - throw new EcmaScriptException("FtpClient requires 1 argument"); - } - - ftp = new ESFtpClient(esFtpPrototype, this.evaluator, arguments[0]); - - return ftp; - } - } - - class FtpClientLogin extends BuiltinFunctionObject { - FtpClientLogin(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.login(arguments); - } - } - - class FtpClientCD extends BuiltinFunctionObject { - FtpClientCD(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.cd(arguments); - } - } - - class FtpClientMKDIR extends BuiltinFunctionObject { - FtpClientMKDIR(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.mkdir(arguments); - } - } - - class FtpClientLCD extends BuiltinFunctionObject { - FtpClientLCD(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.lcd(arguments); - } - } - - class FtpClientPutFile extends BuiltinFunctionObject { - FtpClientPutFile(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.putFile(arguments); - } - } - - class FtpClientPutString extends BuiltinFunctionObject { - FtpClientPutString(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.putString(arguments); - } - } - - class FtpClientGetFile extends BuiltinFunctionObject { - FtpClientGetFile(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.getFile(arguments); - } - } - - class FtpClientGetString extends BuiltinFunctionObject { - FtpClientGetString(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.getString(arguments); - } - } - - class FtpClientLogout extends BuiltinFunctionObject { - FtpClientLogout(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.logout(arguments); - } - } - - class FtpClientBinary extends BuiltinFunctionObject { - FtpClientBinary(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.binary(arguments); - } - } - - class FtpClientAscii extends BuiltinFunctionObject { - FtpClientAscii(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESFtpClient ftp = (ESFtpClient) thisObject; - - return ftp.ascii(arguments); - } - } -} diff --git a/src/helma/scripting/fesi/extensions/ImageExtension.java b/src/helma/scripting/fesi/extensions/ImageExtension.java deleted file mode 100644 index 822e38a4..00000000 --- a/src/helma/scripting/fesi/extensions/ImageExtension.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import helma.image.*; -import helma.objectmodel.*; -import helma.util.*; -import java.awt.image.*; -import java.io.*; -import java.rmi.Naming; -import java.util.*; - -/** - * Extension to do Image manipulation from HOP. - */ -public class ImageExtension extends Extension { - static ImageGenerator imggen; - protected Evaluator evaluator = null; - - /** - * Creates a new ImageExtension object. - */ - public ImageExtension() { - super(); - } - - /** - * Called by the evaluator after the extension is loaded. - */ - public void initializeExtension(Evaluator evaluator) - throws EcmaScriptException { - this.evaluator = evaluator; - - GlobalObject go = evaluator.getGlobalObject(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - ESObject image = new GlobalObjectImage("Image", evaluator, fp, this); // the Image constructor - - go.putHiddenProperty("Image", image); // register the constructor for a Image object. - } - - class GlobalObjectImage extends BuiltinFunctionObject { - ImageExtension imagex; - - GlobalObjectImage(String name, Evaluator evaluator, FunctionPrototype fp, - ImageExtension imagex) { - super(fp, evaluator, name, 1); - this.imagex = imagex; - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - Object img = null; - - try { - if (imggen == null) { - try { - imggen = new ImageGenerator(); - } catch (UnsatisfiedLinkError noawt) { - System.err.println("Error creating Image: " + noawt); - } - } - - if (arguments.length == 1) { - if (arguments[0] instanceof ESArrayWrapper) { - Object obj = ((ESArrayWrapper) arguments[0]).toJavaObject(); - - if (obj instanceof byte[]) { - img = imggen.createImage((byte[]) obj); - } - } else if (arguments[0] instanceof ESString) { - String imgurl = arguments[0].toString(); - - img = imggen.createPaintableImage(imgurl); - } - } else if (arguments.length == 2) { - if (arguments[0] instanceof ESWrapper && - arguments[1] instanceof ESWrapper) { - // create a new image from an existing one and an image filter - Object image = arguments[0].toJavaObject(); - Object filter = arguments[1].toJavaObject(); - - img = imggen.createPaintableImage((ImageWrapper) image, - (ImageFilter) filter); - } else if (arguments[0].isNumberValue() && - arguments[1].isNumberValue()) { - img = imggen.createPaintableImage(arguments[0].toInt32(), - arguments[1].toInt32()); - } - } - } catch (Exception error) { - System.err.println("Error creating Image: " + error); - } - - if (img == null) { - throw new EcmaScriptException("Error creating image: Bad parameters or setup problem."); - } - - return new ESWrapper(img, this.evaluator); - } - } -} diff --git a/src/helma/scripting/fesi/extensions/MailExtension.java b/src/helma/scripting/fesi/extensions/MailExtension.java deleted file mode 100644 index 8b69f841..00000000 --- a/src/helma/scripting/fesi/extensions/MailExtension.java +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import helma.util.*; -import java.io.*; -import java.util.*; - -/** - * Extension to create and send mail messages via SMTP from HOP applications - */ -public class MailExtension extends Extension { - protected Evaluator eval = null; - protected ObjectPrototype esMailPrototype = null; - protected Properties mprops; - - /** - * Creates a new MailExtension object. - */ - public MailExtension() { - super(); - } - - /** - * - * - * @param props ... - */ - public void setProperties(Properties props) { - this.mprops = props; - } - - /** - * Called by the evaluator after the extension is loaded. - */ - public void initializeExtension(Evaluator evaluator) - throws EcmaScriptException { - this.eval = evaluator; - - GlobalObject go = evaluator.getGlobalObject(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - ESObject op = evaluator.getObjectPrototype(); - - esMailPrototype = new ObjectPrototype(op, evaluator); // the Node prototype - - ESObject mail = new GlobalObjectMail("Mail", evaluator, fp, this); // the Mail constructor - - go.putHiddenProperty("Mail", mail); // register the constructor for a Mail object. - - // methods for sending mail from JS... - ESObject p = new MailSetText("setText", evaluator, fp); - - esMailPrototype.putHiddenProperty("setText", p); - esMailPrototype.putHiddenProperty("addText", p); - - esMailPrototype.putHiddenProperty("addPart", - new MailAddPart("addPart", evaluator, fp)); - esMailPrototype.putHiddenProperty("setSubject", - new MailSetSubject("setSubject", evaluator, fp)); - esMailPrototype.putHiddenProperty("setReplyTo", - new MailSetReplyTo("setReplyTo", evaluator, fp)); - esMailPrototype.putHiddenProperty("setFrom", - new MailSetFrom("setFrom", evaluator, fp)); - - p = new MailAddTo("addTo", evaluator, fp); - esMailPrototype.putHiddenProperty("addTo", p); - esMailPrototype.putHiddenProperty("setTo", p); - - p = new MailAddCC("addCC", evaluator, fp); - esMailPrototype.putHiddenProperty("addCC", p); - esMailPrototype.putHiddenProperty("setCC", p); - - p = new MailAddBCC("addBCC", evaluator, fp); - esMailPrototype.putHiddenProperty("addBCC", p); - esMailPrototype.putHiddenProperty("setBCC", p); - - esMailPrototype.putHiddenProperty("send", new MailSend("send", evaluator, fp)); - } - - class GlobalObjectMail extends BuiltinFunctionObject { - MailExtension mailx; - - GlobalObjectMail(String name, Evaluator evaluator, FunctionPrototype fp, - MailExtension mailx) { - super(fp, evaluator, name, 1); - this.mailx = mailx; - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = null; - - if (arguments.length == 0) { - mail = new ESMail(mailx); - } else { - mail = new ESMail(mailx); - - // should/could do something with extra arguments... - } - - return mail; - } - } - - class MailSetText extends BuiltinFunctionObject { - MailSetText(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - if (arguments.length == 1) { - try { - mail.setText(arguments[0]); - } catch (Exception x) { - mail.setStatus(ESMail.TEXT); - - return ESBoolean.makeBoolean(false); - } - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailAddPart extends BuiltinFunctionObject { - MailAddPart(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.addPart(arguments); - } catch (Exception x) { - mail.setStatus(ESMail.MIMEPART); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailSetSubject extends BuiltinFunctionObject { - MailSetSubject(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - if (arguments.length == 1) { - try { - mail.setSubject(arguments[0]); - } catch (Exception x) { - mail.setStatus(ESMail.SUBJECT); - - return ESBoolean.makeBoolean(false); - } - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailSetReplyTo extends BuiltinFunctionObject { - MailSetReplyTo(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - if (arguments.length == 1) { - try { - mail.setReplyTo(arguments[0]); - } catch (Exception x) { - mail.setStatus(ESMail.REPLYTO); - - return ESBoolean.makeBoolean(false); - } - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailSetFrom extends BuiltinFunctionObject { - MailSetFrom(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.setFrom(arguments); - } catch (Exception x) { - mail.setStatus(ESMail.FROM); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailAddTo extends BuiltinFunctionObject { - MailAddTo(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.addTo(arguments); - } catch (Exception x) { - mail.setStatus(ESMail.TO); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailAddCC extends BuiltinFunctionObject { - MailAddCC(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.addCC(arguments); - } catch (Exception x) { - mail.setStatus(ESMail.CC); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailAddBCC extends BuiltinFunctionObject { - MailAddBCC(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.addBCC(arguments); - } catch (Exception x) { - mail.setStatus(ESMail.BCC); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } - - class MailSend extends BuiltinFunctionObject { - MailSend(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESMail mail = (ESMail) thisObject; - - try { - mail.send(); - } catch (Exception x) { - evaluator.engine.getApplication().logEvent("Error sending mail: " + x); - mail.setStatus(ESMail.SEND); - - return ESBoolean.makeBoolean(false); - } - - return ESBoolean.makeBoolean(true); - } - } -} diff --git a/src/helma/scripting/fesi/extensions/XmlRpcExtension.java b/src/helma/scripting/fesi/extensions/XmlRpcExtension.java deleted file mode 100644 index dbc9bfe5..00000000 --- a/src/helma/scripting/fesi/extensions/XmlRpcExtension.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.scripting.fesi.extensions; - -import FESI.Data.*; -import FESI.Exceptions.*; -import FESI.Extensions.*; -import FESI.Interpreter.*; -import helma.scripting.fesi.FesiEngine; -import org.apache.xmlrpc.*; -import java.io.*; -import java.net.*; -import java.util.*; - -/** - * An extension to transparently call and serve XML-RPC from the - * FESI EcmaScript interpreter. - * The extension adds constructors for XML-RPC clients and servers to the Global Object. - * For more information on how to use this please look at the files server.es and - * client.es in the src/fesi directory of the distribution. - * - * All argument conversion is done automatically. Currently the following argument and return - * types are supported: - *
    - *
  • plain objects (with all properties returned by ESObject.getProperties ()) - *
  • arrays - *
  • strings - *
  • date objects - *
  • booleans - *
  • integer and float numbers (long values are not supported!) - *
- * - */ -public class XmlRpcExtension extends Extension { - Evaluator evaluator; - ESObject op; - - /** - * - * - * @param evaluator ... - * - * @throws EcmaScriptException ... - */ - public void initializeExtension(Evaluator evaluator) - throws EcmaScriptException { - // XmlRpc.setDebug (true); - this.evaluator = evaluator; - - GlobalObject go = evaluator.getGlobalObject(); - FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype(); - - op = evaluator.getObjectPrototype(); - - go.putHiddenProperty("Remote", new GlobalObjectRemote("Remote", evaluator, fp)); // the Remote constructor - } - - class GlobalObjectRemote extends BuiltinFunctionObject { - GlobalObjectRemote(String name, Evaluator evaluator, FunctionPrototype fp) { - super(fp, evaluator, name, 1); - } - - public ESValue callFunction(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - return doConstruct(thisObject, arguments); - } - - public ESObject doConstruct(ESObject thisObject, ESValue[] arguments) - throws EcmaScriptException { - ESObject remote = null; - String url = null; - String robj = null; - - if (arguments.length >= 1) { - url = arguments[0].toString(); - } - - if (arguments.length >= 2) { - robj = arguments[1].toString(); - } - - try { - remote = new ESRemote(op, this.evaluator, url, robj); - } catch (MalformedURLException x) { - throw new EcmaScriptException(x.toString()); - } - - return remote; - } - } - - class ESRemote extends ObjectPrototype { - URL url; - String remoteObject; - - public ESRemote(ESObject prototype, Evaluator evaluator, String urlstring, - String robj) throws MalformedURLException { - super(prototype, evaluator); - this.url = new URL(urlstring); - remoteObject = robj; - } - - public ESRemote(ESObject prototype, Evaluator evaluator, URL url, String robj) { - super(prototype, evaluator); - this.url = url; - remoteObject = robj; - } - - public ESValue doIndirectCall(Evaluator evaluator, ESObject target, - String functionName, ESValue[] arguments) - throws EcmaScriptException, NoSuchMethodException { - // System.out.println ("doIndirectCall called with "+remoteObject+"."+functionName); - XmlRpcClient client = new XmlRpcClient(url); - - // long now = System.currentTimeMillis (); - Object retval = null; - int l = arguments.length; - Vector v = new Vector(); - - for (int i = 0; i < l; i++) { - Object arg = FesiEngine.processXmlRpcResponse(arguments[i]); - - // System.out.println ("converted to J: "+arg.getClass ()); - v.addElement(arg); - } - - // System.out.println ("spent "+(System.currentTimeMillis ()-now)+" millis in argument conversion"); - ESObject esretval = ObjectObject.createObject(evaluator); - - try { - String method = (remoteObject == null) ? functionName - : (remoteObject + "." + - functionName); - - retval = client.execute(method, v); - esretval.putProperty("error", ESNull.theNull, "error".hashCode()); - esretval.putProperty("result", - FesiEngine.processXmlRpcArgument(retval, evaluator), - "result".hashCode()); - } catch (Exception x) { - String msg = x.getMessage(); - - if ((msg == null) || (msg.length() == 0)) { - msg = x.toString(); - } - - esretval.putProperty("error", new ESString(msg), "error".hashCode()); - esretval.putProperty("result", ESNull.theNull, "result".hashCode()); - } - - return esretval; - } - - public ESValue getProperty(String name, int hash) - throws EcmaScriptException { - ESValue sprop = super.getProperty(name, hash); - - if ((sprop != ESUndefined.theUndefined) && (sprop != ESNull.theNull)) { - return sprop; - } - - String newRemoteObject = (remoteObject == null) ? name - : (remoteObject + "." + name); - - return new ESRemote(op, this.evaluator, url, newRemoteObject); - } - } -}