Source Code Cross Referenced for Builtin.java in  » Scripting » Pnuts » pnuts » lang » 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 » Pnuts » pnuts.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Builtin.java 1.4 05/05/25
003:         *
004:         * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005:         *
006:         * See the file "LICENSE.txt" for information on usage and redistribution
007:         * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008:         */
009:        package pnuts.lang;
010:
011:        import java.io.File;
012:        import java.io.FileNotFoundException;
013:        import java.io.InputStream;
014:        import java.io.Reader;
015:        import java.net.URL;
016:        import org.pnuts.util.*;
017:
018:        /**
019:         * Builtin functions
020:         */
021:        final class Builtin extends PnutsFunction {
022:
023:            private final static boolean requireLatestScript = "true"
024:                    .equalsIgnoreCase(Runtime
025:                            .getProperty("pnuts.lang.requireLatestScript"));
026:
027:            protected Builtin(String name) {
028:                super (name);
029:            }
030:
031:            protected Object exec(Object[] args, Context context) {
032:                if (name == _getContext) {
033:                    if (args.length == 0) {
034:                        return context;
035:                    }
036:                } else if (name == _package) {
037:                    if (args.length == 1) {
038:                        Package pkg;
039:                        if (args[0] instanceof  Package) {
040:                            pkg = (Package) args[0];
041:                        } else if (args[0] != null) {
042:                            pkg = Package.getPackage((String) args[0], context);
043:                        } else {
044:                            pkg = Package.getInstance(null,
045:                                    context.currentPackage.root, context);
046:                        }
047:                        context.setCurrentPackage(pkg);
048:                        return null;
049:                    } else if (args.length == 0) {
050:                        return context.currentPackage;
051:                    }
052:                } else if (name == _import) {
053:                    if (args.length == 1) {
054:                        if (args[0] == null) {
055:                            context.importEnv = new ImportEnv();
056:                            return null;
057:                        } else {
058:                            String s = (String) args[0];
059:                            if (!s.endsWith("*")) {
060:                                context.addClassToImport(s);
061:                            } else {
062:                                int idx = s.lastIndexOf('.');
063:                                if (idx > 0) {
064:                                    context.addPackageToImport(s.substring(0,
065:                                            idx));
066:                                } else {
067:                                    context.addPackageToImport("");
068:                                }
069:                            }
070:                            return null;
071:                        }
072:                    } else if (args.length == 0) {
073:                        return context.importEnv.list();
074:                    }
075:                } else if (name == _throw) {
076:                    if (args.length == 1) {
077:                        Object arg = args[0];
078:                        if (arg instanceof  PnutsException) {
079:                            throw (PnutsException) arg;
080:                        } else if (arg instanceof  Throwable) {
081:                            throw new PnutsException((Throwable) arg, context);
082:                        } else {
083:                            throw new PnutsException(arg.toString(), context);
084:                        }
085:                    }
086:                } else if (name == _defined) {
087:                    if (args.length == 1) {
088:                        return Boolean.valueOf(context
089:                                .defined((String) args[0]));
090:                    }
091:                } else if (name == _quit) {
092:                    if (args.length == 0) {
093:                        throw new Escape();
094:                    } else if (args.length == 1) {
095:                        throw new Escape(args[0]);
096:                    }
097:                } else if (name == _eval) {
098:                    Context c = null;
099:                    if (args.length == 1) {
100:                        //				c = (Context) ((Context) context).clone(false, true);
101:                        c = context;
102:                        //				c.eval = true;
103:                        if (c.stackFrame != null) {
104:                            Cell prev = c.evalFrameStack;
105:                            Cell cell = new Cell();
106:                            c.evalFrameStack = cell;
107:                            cell.object = c.stackFrame;
108:                            cell.next = prev;
109:                        }
110:                        c.stackFrame = new StackFrame();
111:                        return Pnuts.eval((String) args[0], c);
112:                    } else if (args.length == 2) {
113:                        if (args[1] instanceof  Context) {
114:                            c = (Context) ((Context) args[1])
115:                                    .clone(false, true);
116:                        } else if (args[1] instanceof  Package) {
117:                            c = (Context) context.clone(false, true);
118:                            c.currentPackage = (Package) args[1];
119:                        } else {
120:                            c = (Context) context.clone(false, true);
121:                            c.currentPackage = Package.getPackage(
122:                                    (String) args[1], context);
123:                        }
124:                        //				c.eval = true;
125:                        return c.pnutsImpl.eval((String) args[0], c);
126:                    }
127:                } else if (name == _loadFile) {
128:                    try {
129:                        Context c = null;
130:                        Object arg0 = args[0];
131:                        if (args.length > 0) {
132:                            if (args.length > 1) {
133:                                c = (Context) ((Context) args[1]).clone(false,
134:                                        true);
135:                            } else {
136:                                c = (Context) context.clone();
137:                            }
138:                            if (arg0 instanceof  File) {
139:                                return Pnuts.loadFile(((File) arg0).getPath(),
140:                                        c);
141:                            } else {
142:                                return Pnuts.loadFile((String) arg0, c);
143:                            }
144:                        }
145:                    } catch (FileNotFoundException e) {
146:                        throw new PnutsException(e, context);
147:                    }
148:                } else if (name == _load) {
149:                    try {
150:                        Context c = null;
151:                        if (args.length > 0) {
152:                            if (args.length > 1) {
153:                                c = (Context) args[1];
154:                                c = (Context) c.clone(false, true);
155:                            } else {
156:                                c = (Context) context.clone();
157:                            }
158:                            if (args[0] instanceof  InputStream) {
159:                                return Pnuts.load((InputStream) args[0], c);
160:                            } else if (args[0] instanceof  Reader) {
161:                                return Pnuts.load((Reader) args[0], c);
162:                            } else if (args[0] instanceof  URL) {
163:                                return Pnuts.load((URL) args[0], c);
164:                            } else {
165:                                return Pnuts.load((String) args[0], c);
166:                            }
167:                        }
168:                    } catch (FileNotFoundException e) {
169:                        throw new PnutsException(e, context);
170:                    }
171:                } else if (name == _autoload) {
172:                    if (args.length == 2) {
173:                        context.currentPackage.autoload((String) args[0],
174:                                (String) args[1], context);
175:                        return null;
176:                    }
177:                } else if (name == _require) {
178:                    try {
179:                        if (args.length == 1) {
180:                            Context c = (Context) context.clone();
181:                            Pnuts.require((String) args[0], c,
182:                                    requireLatestScript);
183:                            return null;
184:                        }
185:                    } catch (FileNotFoundException e) {
186:                        throw new PnutsException(e, context);
187:                    }
188:                } else if (name == _class) {
189:                    if (args.length == 1) {
190:                        Object arg = args[0];
191:                        if (arg instanceof  Class) {
192:                            return arg;
193:                        } else {
194:                            try {
195:                                Class cls = Pnuts.loadClass((String) arg,
196:                                        context);
197:                                cls.getClass(); // initialize the class
198:                                return cls;
199:                            } catch (ClassNotFoundException e) {
200:                                return null;
201:                            }
202:                        }
203:                    }
204:                } else if (name == _use) {
205:                    if (args.length == 0) {
206:                        return context.usedPackages();
207:                    } else if (args.length == 1) {
208:                        Object arg0 = args[0];
209:                        if (arg0 == null) {
210:                            context.clearPackages();
211:                            return null;
212:                        } else if (arg0 instanceof  Package) {
213:                            boolean ret = context.usePackage((Package) arg0,
214:                                    false);
215:                            return (ret ? Boolean.TRUE : Boolean.FALSE);
216:                        } else if (arg0 instanceof  String) {
217:                            boolean ret = context.usePackage((String) arg0,
218:                                    false);
219:                            return (ret ? Boolean.TRUE : Boolean.FALSE);
220:                        }
221:                    }
222:                } else if (name == _unuse) {
223:                    if (args.length == 1) {
224:                        Object arg0 = args[0];
225:                        Package pkg = null;
226:                        if (arg0 instanceof  String) {
227:                            pkg = Package.getPackage((String) arg0, context);
228:                        } else if (arg0 instanceof  Package) {
229:                            pkg = (Package) arg0;
230:                        }
231:                        if (pkg != null) {
232:                            boolean ret = context.unusePackage(pkg);
233:                            return (ret ? Boolean.TRUE : Boolean.FALSE);
234:                        }
235:                    }
236:                }
237:                throw new PnutsException("function.notDefined", new Object[] {
238:                        name, new Integer(args.length) }, context);
239:            }
240:
241:            public String toString() {
242:                return "<builtin " + name + ">";
243:            }
244:
245:            public String unparse(int narg) {
246:                return null;
247:            }
248:
249:            public String[] getImportEnv(int narg) {
250:                return null;
251:            }
252:
253:            public boolean isBuiltin() {
254:                return true;
255:            }
256:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.