Source Code Cross Referenced for AbstractScriptEngine.java in  » Scripting » beanshell » javax » script » 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 » Scripting » beanshell » javax.script 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * %W% %E% %U%
003:         *
004:         * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005:         * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006:         */
007:
008:        package javax.script;
009:
010:        import java.io.Reader;
011:        import java.util.Map;
012:        import java.util.Iterator;
013:
014:        /**
015:         * Provides a standard implementation for several of the variants of the <code>eval</code>
016:         * method.
017:         * <br><br>
018:         * <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>
019:         * <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>
020:         * <br><br> are implemented using the abstract methods
021:         * <br><br>
022:         * <code><b>eval(Reader,ScriptContext)</b></code> or
023:         * <code><b>eval(String, ScriptContext)</b></code>
024:         * <br><br>
025:         * with a <code>SimpleScriptContext</code>.
026:         * <br><br>
027:         * A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>
028:         * of the <code>AbstractScriptEngine</code>..
029:         *
030:         * @author Mike Grogan
031:         * @version 1.0
032:         * @since 1.6
033:         */
034:        public abstract class AbstractScriptEngine implements  ScriptEngine {
035:
036:            /**
037:             * The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.
038:             */
039:
040:            protected ScriptContext context;
041:
042:            /**
043:             * Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>
044:             * as its default <code>ScriptContext</code>.
045:             */
046:            public AbstractScriptEngine() {
047:
048:                context = new SimpleScriptContext();
049:
050:            }
051:
052:            /**
053:             * Creates a new instance using the specified <code>Bindings</code> as the
054:             * <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>ScriptContext</code> field.
055:             *
056:             * @param n The specified <code>Bindings</code>.
057:             */
058:            public AbstractScriptEngine(Bindings n) {
059:
060:                this ();
061:                context.setBindings(n, ScriptContext.ENGINE_SCOPE);
062:            }
063:
064:            /**
065:             * Sets the value of the protected <code>ScriptContext</code> field to the specified
066:             * <code>ScriptContext</code>.
067:             *
068:             * @param ctxt The specified <code>ScriptContext</code>.
069:             */
070:            public void setContext(ScriptContext ctxt) {
071:                context = ctxt;
072:            }
073:
074:            /**
075:             * Returns the value of the protected <code>ScriptContext</code>> field.
076:             *
077:             * @return The value of the protected <code>ScriptContext</code> field.
078:             */
079:            public ScriptContext getContext() {
080:                return context;
081:            }
082:
083:            /**
084:             * Returns the <code>Bindings</code> with the specified scope value in
085:             * the protected <code>ScriptContext</code> field.
086:             *
087:             * @param scope The specified scope
088:             *
089:             * @return The corresponding <code>Bindings</code>.
090:             *
091:             **@throws <code>IllegalArgumentException</code> if the value of scope is
092:             * invalid for the type the protected <code>ScriptContext</code> field.
093:             */
094:
095:            public Bindings getBindings(int scope) {
096:
097:                if (scope == ScriptContext.GLOBAL_SCOPE) {
098:                    return context.getBindings(ScriptContext.GLOBAL_SCOPE);
099:                } else if (scope == ScriptContext.ENGINE_SCOPE) {
100:                    return context.getBindings(ScriptContext.ENGINE_SCOPE);
101:                } else {
102:                    throw new IllegalArgumentException("Invalid scope value.");
103:                }
104:            }
105:
106:            /**
107:             * Sets the <code>Bindings</code> with the corresponding scope value in the
108:             * <code>context</code> field.
109:             *
110:             * @param bindings The specified <code>Bindings</code>.
111:             * @param scope The specified scope.
112:             *
113:             * @throws <code>IllegalArgumentException</code> if the value of scope is
114:             * invalid for the type the <code>context</code> field.
115:             */
116:            public void setBindings(Bindings bindings, int scope) {
117:
118:                if (scope == ScriptContext.GLOBAL_SCOPE) {
119:                    context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
120:                    ;
121:                } else if (scope == ScriptContext.ENGINE_SCOPE) {
122:                    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
123:                    ;
124:                } else {
125:                    throw new IllegalArgumentException("Invalid scope value.");
126:                }
127:            }
128:
129:            /**
130:             * Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>
131:             * <code>Bindings</code> of the protected <code>ScriptContext</code> field.
132:             *
133:             * @param key The specified key.
134:             * @param value The specified value.
135:             */
136:            public void put(String key, Object value) {
137:
138:                Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
139:                if (nn != null) {
140:                    nn.put(key, value);
141:                }
142:
143:            }
144:
145:            /**
146:             * Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the
147:             * protected <code>ScriptContext</code> field.
148:             *
149:             * @return The value for the specified key.
150:             */
151:            public Object get(String key) {
152:
153:                Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
154:                if (nn != null) {
155:                    return nn.get(key);
156:                }
157:
158:                return null;
159:            }
160:
161:            /**
162:             * <code>eval(Reader, Bindings)</code> calls the abstract
163:             * <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>
164:             * whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>
165:             * are identical to those members of the protected <code>ScriptContext</code> field.  The specified
166:             * <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>
167:             *
168:             * <code>Bindings</code> of the <code>context</code> field.
169:             *
170:             * @param reader A <code>Reader</code> containing the source of the script.
171:             * @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>
172:             * while the script executes.
173:             *
174:             * @return The return value from <code>eval(Reader, ScriptContext)</code>
175:             */
176:            public Object eval(Reader reader, Bindings bindings)
177:                    throws ScriptException {
178:
179:                ScriptContext ctxt = getScriptContext(bindings);
180:
181:                return eval(reader, ctxt);
182:            }
183:
184:            /**
185:             * Same as <code>eval(Reader, Bindings)</code> except that the abstract
186:             * <code>eval(String, ScriptContext)</code> is used.
187:             *
188:             * @param script A <code>String</code> containing the source of the script.
189:             *
190:             * @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
191:             * while the script executes.
192:             *
193:             * @return The return value from <code>eval(String, ScriptContext)</code>
194:             */
195:            public Object eval(String script, Bindings bindings)
196:                    throws ScriptException {
197:
198:                ScriptContext ctxt = getScriptContext(bindings);
199:
200:                return eval(script, ctxt);
201:            }
202:
203:            /**
204:             * <code>eval(Reader)</code> calls the abstract
205:             * <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
206:             * field.
207:             *
208:             * @param reader A <code>Reader</code> containing the source of the script.
209:             * @return The return value from <code>eval(Reader, ScriptContext)</code>
210:             */
211:            public Object eval(Reader reader) throws ScriptException {
212:
213:                return eval(reader, context);
214:            }
215:
216:            /**
217:             * Same as <code>eval(Reader)</code> except that the abstract
218:             * <code>eval(String, ScriptContext)</code> is used.
219:             *
220:             * @param script A <code>String</code> containing the source of the script.
221:             * @return The return value from <code>eval(String, ScriptContext)</code>
222:             */
223:            public Object eval(String script) throws ScriptException {
224:
225:                return eval(script, context);
226:            }
227:
228:            /**
229:             * Returns a <code>SimpleScriptContext</code>.  The <code>SimpleScriptContext</code>:
230:             *<br><br>
231:             * <ul>
232:             * <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
233:             * </li>
234:             * <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
235:             * method as its <code>GLOBAL_SCOPE</code>
236:             * </li>
237:             * <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
238:             * <code>ScriptEngine</code>
239:             * </li>
240:             * </ul>
241:             * <br><br>
242:             * A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
243:             * using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
244:             * versions.
245:             *
246:             * @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
247:             * @return The <code>SimpleScriptContext</code>
248:             */
249:            protected ScriptContext getScriptContext(Bindings nn) {
250:
251:                SimpleScriptContext ctxt = new SimpleScriptContext();
252:                Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
253:
254:                if (gs != null) {
255:                    ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
256:                }
257:
258:                if (nn != null) {
259:                    ctxt.setBindings(nn, ScriptContext.ENGINE_SCOPE);
260:                } else {
261:                    throw new NullPointerException(
262:                            "Engine scope Bindings may not be null.");
263:                }
264:
265:                ctxt.setReader(context.getReader());
266:                ctxt.setWriter(context.getWriter());
267:                ctxt.setErrorWriter(context.getErrorWriter());
268:
269:                return ctxt;
270:
271:            }
272:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.