View Javadoc
1 /*** 2 JavaScriptEvaluator - Class used to evaluate javaScript expresions used 3 for transformations 4 5 Copyright (C) 2002-2003 Together 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public 9 License as published by the Free Software Foundation; either 10 version 2.1 of the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public 18 License along with this library; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 21 JavaScriptEvaluator.java 22 Date:29.6.2004 23 @version 1.0 24 @author: Zoran Milakovic zoran@prozone.co.yu 25 @author: Zeljko Kovacevic zeljko@prozone.co.yu 26 */ 27 package org.webdocwf.util.loader.transformation; 28 29 import java.util.HashMap; 30 import java.util.Iterator; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.Vector; 34 35 import org.mozilla.javascript.Context; 36 import org.mozilla.javascript.NativeArray; 37 import org.mozilla.javascript.Scriptable; 38 import org.webdocwf.util.loader.LoaderException; 39 import org.webdocwf.util.loader.logging.Logger; 40 /*** 41 * This class is used to evaluate javaScript expresions used to transform data in transformations. 42 * As base for this class is used rhino1.5 (Rhino is an open-source implementation of JavaScript 43 * written entirely in Java) 44 * @author Zeljko Kovacevic 45 * @version 1.0 46 */ 47 48 public class JavaScriptEvaluator implements Transformer { 49 List retValue = new Vector(); 50 private String expression = ""; 51 private Vector variableNames = new Vector(); 52 HashMap hmValues = new HashMap(); 53 public static String CONFIG_STRING = "configString"; 54 Logger logger; 55 56 public void configure(String s) { 57 hmValues.put(CONFIG_STRING, s); 58 } 59 60 public void release() { 61 } 62 63 /*** 64 * This method will transform data from input List using javaScript 65 * and return List with transformed values 66 * @param valueToTransform input values for transformation 67 * @return List with transformed values 68 */ 69 public List transformValue(List valueToTransform) throws Exception { 70 Context cx = Context.enter(); 71 72 Scriptable scope = cx.initStandardObjects(null); 73 NativeArray result = null; 74 List retValue = new Vector(); 75 76 try { 77 int length = valueToTransform.size(); 78 for (int i = 0; i < length; i++) { 79 80 String value = (String) valueToTransform.get(i); 81 String key = (String) this.variableNames.elementAt(i); 82 hmValues.put(key, value); 83 84 } 85 86 try { 87 88 result = evaluateExpression(scope, this.expression, hmValues); 89 90 } catch (Exception e) { 91 LoaderException le = new LoaderException("Error while transforming data using javaScript for transformation.", e); 92 logger.write("full", le.getStackTraceAsString()); 93 logger.write("normal", e.getMessage() + "Java script is not valid!"); 94 throw le; 95 } 96 if (result != null) { 97 98 for (int i = 0; i < result.getLength(); i++) { 99 retValue.add(result.get(i, scope)); 100 } 101 } 102 } catch (Exception e) { 103 LoaderException le = new LoaderException("Exception:Error while transform data with javaScript. ", e); 104 logger.write("full", le.getStackTraceAsString()); 105 throw le; 106 } finally { 107 // Exit from the context. 108 Context.exit(); 109 } 110 return retValue; 111 } 112 /***This method will do evaluation of javaScript. 113 * @param expr contains javaScript code. 114 * @param variables contains all variables thath will be replaced and used in this context 115 */ 116 private NativeArray evaluateExpression(Scriptable scope, String expr, HashMap variables) throws LoaderException { 117 Context cx = Context.enter(); 118 try { 119 prepareContext(scope, variables); 120 NativeArray pomEval = (NativeArray) cx.evaluateString(scope, expr, "", 1, null); 121 return pomEval; 122 } catch (Exception e) { 123 LoaderException le = new LoaderException("Exception:Error while evaluating javaScript for transformation.", e); 124 throw le; 125 } finally { 126 Context.exit(); 127 } 128 } 129 /***This method will do prepare context.It will replace variables from 130 * javaScript source with real values from variables. 131 * @param scope contains javaScript code. 132 * @param variables contains all variables that will be replaced and used in this context 133 */ 134 private void prepareContext(Scriptable scope, HashMap variables) throws Exception { 135 Iterator iter = variables.entrySet().iterator(); 136 while (iter.hasNext()) { 137 Map.Entry me = (Map.Entry) iter.next(); 138 String key = me.getKey().toString(); 139 Object value = me.getValue(); 140 scope.put(key, scope, value); 141 } 142 } 143 144 /*** 145 * This method will return javaScript expression used for transformation. 146 * @return String 147 */ 148 public String getExpression() { 149 return this.expression; 150 } 151 152 /*** 153 * This method set javaScript expression 154 * @param exppression String which is javaScript expression 155 */ 156 public void setExpression(String exppression) { 157 this.expression = exppression; 158 } 159 160 /*** 161 * This method returns vector with variable names from java script. 162 * @return vector with variable names 163 */ 164 public Vector getVariableNames() { 165 return variableNames; 166 } 167 168 /*** 169 * This method set variable names from java script. 170 * @param vector Vector with variable names 171 */ 172 public void setVariableNames(Vector vector) { 173 this.variableNames = vector; 174 } 175 /*** 176 * This method set logger 177 * @param logger 178 */ 179 public void setLogger(Logger logger) { 180 this.logger = logger; 181 } 182 183 }

This page was automatically generated by Maven