Source Code Cross Referenced for JavaScriptEvaluator.java in  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » loader » transformation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » octopus » org.webdocwf.util.loader.transformation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.