001: /**
002: JavaScriptEvaluator - Class used to evaluate javaScript expresions used
003: for transformations
004:
005: Copyright (C) 2002-2003 Together
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020:
021: JavaScriptEvaluator.java
022: Date:29.6.2004
023: @version 1.0
024: @author: Zoran Milakovic zoran@prozone.co.yu
025: @author: Zeljko Kovacevic zeljko@prozone.co.yu
026: */package org.webdocwf.util.loader.transformation;
027:
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Vector;
033:
034: import org.mozilla.javascript.Context;
035: import org.mozilla.javascript.NativeArray;
036: import org.mozilla.javascript.Scriptable;
037: import org.webdocwf.util.loader.LoaderException;
038: import org.webdocwf.util.loader.logging.Logger;
039:
040: /**
041: * This class is used to evaluate javaScript expresions used to transform data in transformations.
042: * As base for this class is used rhino1.5 (Rhino is an open-source implementation of JavaScript
043: * written entirely in Java)
044: * @author Zeljko Kovacevic
045: * @version 1.0
046: */
047:
048: public class JavaScriptEvaluator implements Transformer {
049: List retValue = new Vector();
050: private String expression = "";
051: private Vector variableNames = new Vector();
052: HashMap hmValues = new HashMap();
053: public static String CONFIG_STRING = "configString";
054: Logger logger;
055:
056: public void configure(String s) {
057: hmValues.put(CONFIG_STRING, s);
058: }
059:
060: public void release() {
061: }
062:
063: /**
064: * This method will transform data from input List using javaScript
065: * and return List with transformed values
066: * @param valueToTransform input values for transformation
067: * @return List with transformed values
068: */
069: public List transformValue(List valueToTransform) throws Exception {
070: Context cx = Context.enter();
071:
072: Scriptable scope = cx.initStandardObjects(null);
073: NativeArray result = null;
074: List retValue = new Vector();
075:
076: try {
077: int length = valueToTransform.size();
078: for (int i = 0; i < length; i++) {
079:
080: String value = (String) valueToTransform.get(i);
081: String key = (String) this .variableNames.elementAt(i);
082: hmValues.put(key, value);
083:
084: }
085:
086: try {
087:
088: result = evaluateExpression(scope, this .expression,
089: hmValues);
090:
091: } catch (Exception e) {
092: LoaderException le = new LoaderException(
093: "Error while transforming data using javaScript for transformation.",
094: e);
095: logger.write("full", le.getStackTraceAsString());
096: logger.write("normal", e.getMessage()
097: + "Java script is not valid!");
098: throw le;
099: }
100: if (result != null) {
101:
102: for (int i = 0; i < result.getLength(); i++) {
103: retValue.add(result.get(i, scope));
104: }
105: }
106: } catch (Exception e) {
107: LoaderException le = new LoaderException(
108: "Exception:Error while transform data with javaScript. ",
109: e);
110: logger.write("full", le.getStackTraceAsString());
111: throw le;
112: } finally {
113: // Exit from the context.
114: Context.exit();
115: }
116: return retValue;
117: }
118:
119: /**This method will do evaluation of javaScript.
120: * @param expr contains javaScript code.
121: * @param variables contains all variables thath will be replaced and used in this context
122: */
123: private NativeArray evaluateExpression(Scriptable scope,
124: String expr, HashMap variables) throws LoaderException {
125: Context cx = Context.enter();
126: try {
127: prepareContext(scope, variables);
128: NativeArray pomEval = (NativeArray) cx.evaluateString(
129: scope, expr, "", 1, null);
130: return pomEval;
131: } catch (Exception e) {
132: LoaderException le = new LoaderException(
133: "Exception:Error while evaluating javaScript for transformation.",
134: e);
135: throw le;
136: } finally {
137: Context.exit();
138: }
139: }
140:
141: /**This method will do prepare context.It will replace variables from
142: * javaScript source with real values from variables.
143: * @param scope contains javaScript code.
144: * @param variables contains all variables that will be replaced and used in this context
145: */
146: private void prepareContext(Scriptable scope, HashMap variables)
147: throws Exception {
148: Iterator iter = variables.entrySet().iterator();
149: while (iter.hasNext()) {
150: Map.Entry me = (Map.Entry) iter.next();
151: String key = me.getKey().toString();
152: Object value = me.getValue();
153: scope.put(key, scope, value);
154: }
155: }
156:
157: /**
158: * This method will return javaScript expression used for transformation.
159: * @return String
160: */
161: public String getExpression() {
162: return this .expression;
163: }
164:
165: /**
166: * This method set javaScript expression
167: * @param exppression String which is javaScript expression
168: */
169: public void setExpression(String exppression) {
170: this .expression = exppression;
171: }
172:
173: /**
174: * This method returns vector with variable names from java script.
175: * @return vector with variable names
176: */
177: public Vector getVariableNames() {
178: return variableNames;
179: }
180:
181: /**
182: * This method set variable names from java script.
183: * @param vector Vector with variable names
184: */
185: public void setVariableNames(Vector vector) {
186: this .variableNames = vector;
187: }
188:
189: /**
190: * This method set logger
191: * @param logger
192: */
193: public void setLogger(Logger logger) {
194: this.logger = logger;
195: }
196:
197: }
|