1 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 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 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 Context.exit(); 109 } 110 return retValue; 111 } 112 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 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 148 public String getExpression() { 149 return this.expression; 150 } 151 152 156 public void setExpression(String exppression) { 157 this.expression = exppression; 158 } 159 160 164 public Vector getVariableNames() { 165 return variableNames; 166 } 167 168 172 public void setVariableNames(Vector vector) { 173 this.variableNames = vector; 174 } 175 179 public void setLogger(Logger logger) { 180 this.logger = logger; 181 } 182 183 } 184 | Popular Tags |