Source Code Cross Referenced for NativeRegexp.java in  » EJB-Server-resin-3.1.5 » ecmascript » com » caucho » es » 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 » EJB Server resin 3.1.5 » ecmascript » com.caucho.es 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003:         *
004:         * This file is part of Resin(R) Open Source
005:         *
006:         * Each copy or derived work must preserve the copyright notice and this
007:         * notice unmodified.
008:         *
009:         * Resin Open Source is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU General Public License as published by
011:         * the Free Software Foundation; either version 2 of the License, or
012:         * (at your option) any later version.
013:         *
014:         * Resin Open Source is distributed in the hope that it will be useful,
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017:         * of NON-INFRINGEMENT.  See the GNU General Public License for more
018:         * details.
019:         *
020:         * You should have received a copy of the GNU General Public License
021:         * along with Resin Open Source; if not, write to the
022:         *   Free SoftwareFoundation, Inc.
023:         *   59 Temple Place, Suite 330
024:         *   Boston, MA 02111-1307  USA
025:         *
026:         * @author Scott Ferguson
027:         */
028:
029:        package com.caucho.es;
030:
031:        /**
032:         * JavaScript object
033:         */
034:        class NativeRegexp extends Native {
035:            static ESId INDEX = ESId.intern("index");
036:            static ESId INPUT = ESId.intern("input");
037:
038:            static final int NEW = 1;
039:            static final int COMPILE = NEW + 1;
040:            static final int EXEC = COMPILE + 1;
041:            static final int TEST = EXEC + 1;
042:            static final int TO_STRING = TEST + 1;
043:
044:            /**
045:             * Create a new object based on a prototype
046:             */
047:            private NativeRegexp(String name, int n, int len) {
048:                super (name, len);
049:
050:                this .n = n;
051:            }
052:
053:            /**
054:             * Creates the native Regexp object
055:             */
056:            static ESRegexpWrapper create(Global resin) {
057:                NativeRegexp nativeRegexp = new NativeRegexp("Regexp", NEW, 1);
058:                ESRegexp proto;
059:
060:                try {
061:                    proto = new ESRegexp("", "");
062:                } catch (Exception e) {
063:                    throw new RuntimeException();
064:                }
065:                proto.prototype = resin.objProto;
066:                resin.regexpProto = proto;
067:                ESRegexpWrapper regexp = new ESRegexpWrapper(resin,
068:                        nativeRegexp, proto);
069:
070:                put(proto, "exec", EXEC, 1);
071:                put(proto, "compile", COMPILE, 2);
072:                put(proto, "test", TEST, 1);
073:                put(proto, "toString", TO_STRING, 0);
074:
075:                proto.setClean();
076:                regexp.setClean();
077:
078:                return regexp;
079:            }
080:
081:            static private void put(ESObject proto, String name, int n, int len) {
082:                ESId id = ESId.intern(name);
083:                NativeRegexp fun = new NativeRegexp(name, n, len);
084:
085:                proto.put(id, fun, DONT_ENUM);
086:            }
087:
088:            public ESBase call(Call eval, int length) throws Throwable {
089:                switch (n) {
090:                case NEW:
091:                    return create(eval, length);
092:
093:                case TO_STRING:
094:                    try {
095:                        ESRegexp regexp = (ESRegexp) eval.getThis();
096:                        String s = regexp.pattern.toString();
097:                        String f = regexp.flags.toString();
098:
099:                        return ESString.create("/" + s + "/" + f);
100:                    } catch (ClassCastException e) {
101:                        throw new ESException("toString expected regexp object");
102:                    }
103:
104:                case EXEC:
105:                    return exec(eval, length);
106:
107:                case COMPILE:
108:                    return compile(eval, length);
109:
110:                case TEST:
111:                    return test(eval, length);
112:
113:                default:
114:                    throw new ESException("Unknown object function");
115:                }
116:            }
117:
118:            private ESBase create(Call eval, int length) throws Throwable {
119:                ESString pattern;
120:                ESString flags = null;
121:
122:                if (length == 0)
123:                    pattern = ESString.NULL;
124:                else
125:                    pattern = eval.getArg(0).toStr();
126:
127:                if (length > 1)
128:                    flags = eval.getArg(1).toStr();
129:                else
130:                    flags = ESString.NULL;
131:
132:                ESObject obj;
133:                obj = new ESRegexp(pattern, flags);
134:
135:                return obj;
136:            }
137:
138:            private ESBase compile(Call eval, int length) throws Throwable {
139:                ESString pattern;
140:                ESString flags = null;
141:                ESBase arg = eval.getArg(-1);
142:
143:                if (arg instanceof  ESThunk)
144:                    arg = ((ESThunk) arg).toObject();
145:
146:                if (!(arg instanceof  ESRegexp))
147:                    throw new ESException("compile must be bound to regexp");
148:                ESRegexp regexp = (ESRegexp) arg;
149:
150:                if (length == 0)
151:                    return esUndefined;
152:                else
153:                    pattern = eval.getArg(0).toStr();
154:
155:                if (length > 1)
156:                    flags = eval.getArg(1).toStr();
157:                else
158:                    flags = ESString.NULL;
159:
160:                regexp.compile(pattern, flags);
161:
162:                return regexp;
163:            }
164:
165:            static ESBase exec(Call eval, int length) throws Throwable {
166:                ESBase reg = eval.getArg(-1);
167:                ESRegexp regexp;
168:
169:                if (reg instanceof  ESThunk)
170:                    reg = ((ESThunk) reg).toObject();
171:
172:                if (reg instanceof  ESRegexp)
173:                    regexp = (ESRegexp) reg;
174:                else
175:                    regexp = new ESRegexp(reg.toStr(), ESString.NULL);
176:                if (regexp.prototype == null)
177:                    throw new RuntimeException();
178:
179:                ESString string;
180:
181:                Global global = Global.getGlobalProto();
182:                if (length == 0)
183:                    string = global.getRegexp().getProperty(
184:                            global.getRegexp().INPUT).toStr();
185:                else
186:                    string = eval.getArg(0).toStr();
187:
188:                global.getRegexp().setRegexp(regexp);
189:                if (!regexp.exec(string))
190:                    return esNull;
191:
192:                return esNull;
193:                /* java.util.regex
194:                  
195:                ESArray array = global.createArray();
196:                for (int i = 0; i < regexp.regexp.length(); i++) {
197:                  int begin = regexp.regexp.getBegin(i);
198:                  int end = regexp.regexp.getEnd(i);
199:                  if (begin < end && begin >= 0)
200:                array.setProperty(i, string.substring(begin, end));
201:                  else
202:                array.setProperty(i, ESString.create("")); // XXX: possible?
203:                }
204:
205:                // java.util.regex
206:                // array.setProperty(INDEX, ESNumber.create(regexp.regexp.getBegin(0)));
207:                array.setProperty(INPUT, string);
208:
209:                return array;
210:                 */
211:            }
212:
213:            private ESBase test(Call eval, int length) throws Throwable {
214:                ESBase reg = eval.getArg(-1);
215:                ESRegexp regexp;
216:
217:                if (reg instanceof  ESThunk)
218:                    reg = ((ESThunk) reg).toObject();
219:
220:                if (reg instanceof  ESRegexp)
221:                    regexp = (ESRegexp) reg;
222:                else
223:                    regexp = new ESRegexp(reg.toStr(), ESString.NULL);
224:                if (regexp.prototype == null)
225:                    throw new RuntimeException();
226:
227:                ESString string;
228:                ESRegexpWrapper globalRegexp = Global.getGlobalProto()
229:                        .getRegexp();
230:                if (length == 0)
231:                    string = globalRegexp.getProperty(globalRegexp.INPUT)
232:                            .toStr();
233:                else
234:                    string = eval.getArg(0).toStr();
235:
236:                globalRegexp.setRegexp(regexp);
237:                return ESBoolean.create(regexp.exec(string));
238:            }
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.