Source Code Cross Referenced for SimpleScriptContext.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.util.*;
011:        import java.io.*;
012:
013:        /**
014:         * Simple implementation of ScriptContext.
015:         *
016:         * @author Mike Grogan
017:         * @version 1.0
018:         * @since 1.6
019:         */
020:        public class SimpleScriptContext implements  ScriptContext {
021:
022:            /**
023:             * By default, a <code>PrintWriter</code> based on <code>System.out</code>
024:             * is used.
025:             */
026:            protected Writer writer;
027:
028:            /**
029:             * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
030:             * used.
031:             */
032:            protected Writer errorWriter;
033:
034:            /**
035:             * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
036:             * is used.
037:             */
038:            protected Reader reader;
039:
040:            /**
041:             * By default, a <code>SimpleBindings</code> is used.
042:             */
043:            protected Bindings engineScope;
044:
045:            /**
046:             * By default, a <code>SimpleBindings</code> is used.
047:             */
048:            protected Bindings globalScope;
049:
050:            public SimpleScriptContext() {
051:
052:                engineScope = new SimpleBindings();
053:                globalScope = null;
054:                reader = new InputStreamReader(System.in);
055:                writer = new PrintWriter(System.out, true);
056:                errorWriter = new PrintWriter(System.err, true);
057:
058:            }
059:
060:            /**
061:             * Sets a <code>Bindings</code> of attributes for the given scope.  If the value
062:             * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
063:             * <code>engineScope</code> field.  If the value
064:             * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
065:             * <code>globalScope</code> field.
066:             *
067:             * @param bindings The <code>Bindings</code> of attributes to set.
068:             * @param scope The value of the scope in which the attributes are set.
069:             *
070:             * @throws <code>IllegalArgumentException</code> if scope is invalid.
071:             * @throws <code>NullPointerException</code> is the value of scope is <code>ENGINE_SCOPE</code> and
072:             * the specified <code>Bindings</code> is null.
073:             */
074:            public void setBindings(Bindings bindings, int scope) {
075:
076:                switch (scope) {
077:
078:                case ENGINE_SCOPE:
079:                    if (bindings == null) {
080:                        throw new NullPointerException(
081:                                "Engine scope cannot be null.");
082:                    }
083:                    engineScope = bindings;
084:                    break;
085:                case GLOBAL_SCOPE:
086:                    globalScope = bindings;
087:                    break;
088:                default:
089:                    throw new IllegalArgumentException("Invalid scope value.");
090:                }
091:            }
092:
093:            public Object getAttribute(String name) {
094:
095:                Object ret;
096:                if (null != (ret = getAttribute(name, ENGINE_SCOPE))) {
097:                    return ret;
098:                } else if (null != (ret = getAttribute(name, GLOBAL_SCOPE))) {
099:                    return ret;
100:                }
101:
102:                return null;
103:            }
104:
105:            public Object getAttribute(String name, int scope) {
106:
107:                switch (scope) {
108:
109:                case ENGINE_SCOPE:
110:                    return engineScope.get(name);
111:
112:                case GLOBAL_SCOPE:
113:                    if (globalScope != null) {
114:                        return globalScope.get(name);
115:                    }
116:                    return null;
117:
118:                default:
119:                    throw new IllegalArgumentException("Illegal scope value.");
120:                }
121:            }
122:
123:            public Object removeAttribute(String name, int scope) {
124:
125:                switch (scope) {
126:
127:                case ENGINE_SCOPE:
128:                    if (getBindings(ENGINE_SCOPE) != null) {
129:                        return getBindings(ENGINE_SCOPE).remove(name);
130:                    }
131:                    return null;
132:
133:                case GLOBAL_SCOPE:
134:                    if (getBindings(GLOBAL_SCOPE) != null) {
135:                        return getBindings(GLOBAL_SCOPE).remove(name);
136:                    }
137:                    return null;
138:
139:                default:
140:                    throw new IllegalArgumentException("Illegal scope value.");
141:                }
142:            }
143:
144:            public void setAttribute(String name, Object value, int scope) {
145:
146:                switch (scope) {
147:
148:                case ENGINE_SCOPE:
149:                    engineScope.put(name, value);
150:                    return;
151:
152:                case GLOBAL_SCOPE:
153:                    if (globalScope != null) {
154:                        globalScope.put(name, value);
155:                    }
156:                    return;
157:
158:                default:
159:                    throw new IllegalArgumentException("Illegal scope value.");
160:                }
161:            }
162:
163:            public Writer getWriter() {
164:                return writer;
165:            }
166:
167:            public Reader getReader() {
168:                return reader;
169:            }
170:
171:            public void setReader(Reader reader) {
172:                this .reader = reader;
173:            }
174:
175:            public void setWriter(Writer writer) {
176:                this .writer = writer;
177:            }
178:
179:            public Writer getErrorWriter() {
180:                return errorWriter;
181:            }
182:
183:            public void setErrorWriter(Writer writer) {
184:                this .errorWriter = writer;
185:            }
186:
187:            public int getAttributesScope(String name) {
188:
189:                if (getAttribute(name, ENGINE_SCOPE) != null) {
190:                    return ENGINE_SCOPE;
191:                } else if (globalScope != null
192:                        && getAttribute(name, GLOBAL_SCOPE) != null) {
193:                    return GLOBAL_SCOPE;
194:                } else {
195:                    return 0;
196:                }
197:            }
198:
199:            /**
200:             * Returns the value of the <code>engineScope</code> field if specified scope is
201:             * <code>ENGINE_SCOPE</code>.  Returns the value of the <code>globalScope</code> field if the specified scope is
202:             * <code>GLOBAL_SCOPE</code>.
203:             *
204:             * @param scope The specified scope
205:             * @return The value of either the  <code>engineScope</code> or <code>globalScope</code> field.
206:             * @throws <code>IllegalArgumentException</code> if the value of scope is invalid.
207:             */
208:            public Bindings getBindings(int scope) {
209:                if (scope == ENGINE_SCOPE) {
210:                    return engineScope;
211:                } else if (scope == GLOBAL_SCOPE) {
212:                    return globalScope;
213:                } else {
214:                    throw new IllegalArgumentException("Illegal scope value.");
215:                }
216:
217:            }
218:
219:            public List<Integer> getScopes() {
220:                return scopes;
221:            }
222:
223:            private static List<Integer> scopes;
224:            static {
225:                scopes = new ArrayList<Integer>(2);
226:                scopes.add(ENGINE_SCOPE);
227:                scopes.add(GLOBAL_SCOPE);
228:                scopes = Collections.unmodifiableList(scopes);
229:            }
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.