⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javascriptevaluator.java

📁 数据仓库工具
💻 JAVA
字号:
/** 
 JavaScriptEvaluator - Class used to evaluate javaScript expresions used 
 for transformations

	Copyright (C) 2002-2003  Together

	This library 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.1 of the License, or (at your option) any later version.

	This library 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

 JavaScriptEvaluator.java
 Date:29.6.2004
 @version 1.0
 @author:Zeljko Kovacevic  zeljko@prozone.co.yu
 */
package org.webdocwf.util.loader.transformation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.Scriptable;
import org.webdocwf.util.loader.LoaderException;
import org.webdocwf.util.loader.logging.Logger;
/**
 * This class is used to evaluate javaScript expresions used to transform data in transformations.
 * As base for this class is used rhino1.5 (Rhino is an open-source implementation of JavaScript 
 * written entirely in Java)
 * @author Zeljko Kovacevic
 * @version 1.0
 */

public class JavaScriptEvaluator implements Transformer {
    List retValue = new Vector();
    private String expression = "";
    private Vector variableNames = new Vector();
    HashMap hmValues = new HashMap();
    public static String CONFIG_STRING = "configString";
    Logger logger;

    public void configure(String s) {
        hmValues.put(CONFIG_STRING, s);
    }
		
    public void release() {
    }

    /**
     * This method will transform data from input List using javaScript 
     * and return List with transformed values
     * @param valueToTransform input values for transformation
     * @return List with transformed values
    */
    public List transformValue(List valueToTransform) throws Exception {
        Context cx = Context.enter();

        Scriptable scope = cx.initStandardObjects(null);
        NativeArray result = null;
        List retValue = new Vector();

        try {
            int length = valueToTransform.size();
            for (int i = 0; i < length; i++) {

                String value = (String) valueToTransform.get(i);
                String key = (String) this.variableNames.elementAt(i);
                hmValues.put(key, value);

            }

            try {

                result = evaluateExpression(scope, this.expression, hmValues);

            } catch (Exception e) {
                LoaderException le = new LoaderException("Error while transforming data using javaScript for transformation.", e);
                logger.write("full", le.getStackTraceAsString());
                logger.write("normal", e.getMessage() + "Java script is not valid!");
                throw le;
            }
            if (result != null) {

                for (int i = 0; i < result.getLength(); i++) {
                    retValue.add(result.get(i, scope));
                }
            }
        } catch (Exception e) {
            LoaderException le = new LoaderException("Exception:Error while transform data with javaScript. ", e);
            logger.write("full", le.getStackTraceAsString());
            throw le;
        } finally {
            // Exit from the context.
            Context.exit();
        }
        return retValue;
    }
    /**This method will do evaluation of javaScript.
     * @param expr contains javaScript code.
     * @param variables contains all variables thath will be replaced and used in this context 
     */
    private NativeArray evaluateExpression(Scriptable scope, String expr, HashMap variables) throws LoaderException {
        Context cx = Context.enter();
        try {
            prepareContext(scope, variables);
            NativeArray pomEval = (NativeArray) cx.evaluateString(scope, expr, "", 1, null);
            return pomEval;
        } catch (Exception e) {
            LoaderException le = new LoaderException("Exception:Error while evaluating javaScript for transformation.", e);
            throw le;
        } finally {
            Context.exit();
        }
    }
    /**This method will do prepare context.It will replace variables from 
    	* javaScript source with real values from variables.
    	* @param scope contains javaScript code.
    	* @param variables contains all variables that will be replaced and used in this context 
    	*/
    private void prepareContext(Scriptable scope, HashMap variables) throws Exception {
        Iterator iter = variables.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry me = (Map.Entry) iter.next();
            String key = me.getKey().toString();
            Object value = me.getValue();
            scope.put(key, scope, value);
        }
    }

    /**
     * This method will return javaScript expression used for transformation. 
     * @return String
     */
    public String getExpression() {
        return this.expression;
    }

    /**
     * This method set javaScript expression 
     * @param exppression String which is javaScript expression
     */
    public void setExpression(String exppression) {
        this.expression = exppression;
    }

    /**
     * This method returns vector with variable names from java script.
     * @return vector with variable names
     */
    public Vector getVariableNames() {
        return variableNames;
    }

    /**
     * This method set variable names from java script.
     * @param vector Vector with variable names
     */
    public void setVariableNames(Vector vector) {
        this.variableNames = vector;
    }
    /**
     * This method set logger
     * @param logger 
     */
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -