Source Code Cross Referenced for NativeGlobal.java in  » Scripting » rhino » org » mozilla » javascript » 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 » rhino » org.mozilla.javascript 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002:         *
003:         * ***** BEGIN LICENSE BLOCK *****
004:         * Version: MPL 1.1/GPL 2.0
005:         *
006:         * The contents of this file are subject to the Mozilla Public License Version
007:         * 1.1 (the "License"); you may not use this file except in compliance with
008:         * the License. You may obtain a copy of the License at
009:         * http://www.mozilla.org/MPL/
010:         *
011:         * Software distributed under the License is distributed on an "AS IS" basis,
012:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013:         * for the specific language governing rights and limitations under the
014:         * License.
015:         *
016:         * The Original Code is Rhino code, released
017:         * May 6, 1999.
018:         *
019:         * The Initial Developer of the Original Code is
020:         * Netscape Communications Corporation.
021:         * Portions created by the Initial Developer are Copyright (C) 1997-1999
022:         * the Initial Developer. All Rights Reserved.
023:         *
024:         * Contributor(s):
025:         *   Norris Boyd
026:         *   Igor Bukanov
027:         *   Mike McCabe
028:         *
029:         * Alternatively, the contents of this file may be used under the terms of
030:         * the GNU General Public License Version 2 or later (the "GPL"), in which
031:         * case the provisions of the GPL are applicable instead of those above. If
032:         * you wish to allow use of your version of this file only under the terms of
033:         * the GPL and not to allow others to use your version of this file under the
034:         * MPL, indicate your decision by deleting the provisions above and replacing
035:         * them with the notice and other provisions required by the GPL. If you do
036:         * not delete the provisions above, a recipient may use your version of this
037:         * file under either the MPL or the GPL.
038:         *
039:         * ***** END LICENSE BLOCK ***** */
040:
041:        package org.mozilla.javascript;
042:
043:        import java.io.Serializable;
044:
045:        import org.mozilla.javascript.xml.XMLLib;
046:
047:        /**
048:         * This class implements the global native object (function and value
049:         * properties only).
050:         *
051:         * See ECMA 15.1.[12].
052:         *
053:         * @author Mike Shaver
054:         */
055:
056:        public class NativeGlobal implements  Serializable, IdFunctionCall {
057:            static final long serialVersionUID = 6080442165748707530L;
058:
059:            public static void init(Context cx, Scriptable scope, boolean sealed) {
060:                NativeGlobal obj = new NativeGlobal();
061:
062:                for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id) {
063:                    String name;
064:                    int arity = 1;
065:                    switch (id) {
066:                    case Id_decodeURI:
067:                        name = "decodeURI";
068:                        break;
069:                    case Id_decodeURIComponent:
070:                        name = "decodeURIComponent";
071:                        break;
072:                    case Id_encodeURI:
073:                        name = "encodeURI";
074:                        break;
075:                    case Id_encodeURIComponent:
076:                        name = "encodeURIComponent";
077:                        break;
078:                    case Id_escape:
079:                        name = "escape";
080:                        break;
081:                    case Id_eval:
082:                        name = "eval";
083:                        break;
084:                    case Id_isFinite:
085:                        name = "isFinite";
086:                        break;
087:                    case Id_isNaN:
088:                        name = "isNaN";
089:                        break;
090:                    case Id_isXMLName:
091:                        name = "isXMLName";
092:                        break;
093:                    case Id_parseFloat:
094:                        name = "parseFloat";
095:                        break;
096:                    case Id_parseInt:
097:                        name = "parseInt";
098:                        arity = 2;
099:                        break;
100:                    case Id_unescape:
101:                        name = "unescape";
102:                        break;
103:                    case Id_uneval:
104:                        name = "uneval";
105:                        break;
106:                    default:
107:                        throw Kit.codeBug();
108:                    }
109:                    IdFunctionObject f = new IdFunctionObject(obj, FTAG, id,
110:                            name, arity, scope);
111:                    if (sealed) {
112:                        f.sealObject();
113:                    }
114:                    f.exportAsScopeProperty();
115:                }
116:
117:                ScriptableObject.defineProperty(scope, "NaN",
118:                        ScriptRuntime.NaNobj, ScriptableObject.DONTENUM);
119:                ScriptableObject.defineProperty(scope, "Infinity",
120:                        ScriptRuntime.wrapNumber(Double.POSITIVE_INFINITY),
121:                        ScriptableObject.DONTENUM);
122:                ScriptableObject.defineProperty(scope, "undefined",
123:                        Undefined.instance, ScriptableObject.DONTENUM);
124:
125:                String[] errorMethods = Kit.semicolonSplit(""
126:                        + "ConversionError;" + "EvalError;" + "RangeError;"
127:                        + "ReferenceError;" + "SyntaxError;" + "TypeError;"
128:                        + "URIError;" + "InternalError;" + "JavaException;");
129:
130:                /*
131:                    Each error constructor gets its own Error object as a prototype,
132:                    with the 'name' property set to the name of the error.
133:                 */
134:                for (int i = 0; i < errorMethods.length; i++) {
135:                    String name = errorMethods[i];
136:                    Scriptable errorProto = ScriptRuntime.newObject(cx, scope,
137:                            "Error", ScriptRuntime.emptyArgs);
138:                    errorProto.put("name", errorProto, name);
139:                    if (sealed) {
140:                        if (errorProto instanceof  ScriptableObject) {
141:                            ((ScriptableObject) errorProto).sealObject();
142:                        }
143:                    }
144:                    IdFunctionObject ctor = new IdFunctionObject(obj, FTAG,
145:                            Id_new_CommonError, name, 1, scope);
146:                    ctor.markAsConstructor(errorProto);
147:                    if (sealed) {
148:                        ctor.sealObject();
149:                    }
150:                    ctor.exportAsScopeProperty();
151:                }
152:            }
153:
154:            public Object execIdCall(IdFunctionObject f, Context cx,
155:                    Scriptable scope, Scriptable this Obj, Object[] args) {
156:                if (f.hasTag(FTAG)) {
157:                    int methodId = f.methodId();
158:                    switch (methodId) {
159:                    case Id_decodeURI:
160:                    case Id_decodeURIComponent: {
161:                        String str = ScriptRuntime.toString(args, 0);
162:                        return decode(str, methodId == Id_decodeURI);
163:                    }
164:
165:                    case Id_encodeURI:
166:                    case Id_encodeURIComponent: {
167:                        String str = ScriptRuntime.toString(args, 0);
168:                        return encode(str, methodId == Id_encodeURI);
169:                    }
170:
171:                    case Id_escape:
172:                        return js_escape(args);
173:
174:                    case Id_eval:
175:                        return js_eval(cx, scope, args);
176:
177:                    case Id_isFinite: {
178:                        boolean result;
179:                        if (args.length < 1) {
180:                            result = false;
181:                        } else {
182:                            double d = ScriptRuntime.toNumber(args[0]);
183:                            result = (d == d && d != Double.POSITIVE_INFINITY && d != Double.NEGATIVE_INFINITY);
184:                        }
185:                        return ScriptRuntime.wrapBoolean(result);
186:                    }
187:
188:                    case Id_isNaN: {
189:                        // The global method isNaN, as per ECMA-262 15.1.2.6.
190:                        boolean result;
191:                        if (args.length < 1) {
192:                            result = true;
193:                        } else {
194:                            double d = ScriptRuntime.toNumber(args[0]);
195:                            result = (d != d);
196:                        }
197:                        return ScriptRuntime.wrapBoolean(result);
198:                    }
199:
200:                    case Id_isXMLName: {
201:                        Object name = (args.length == 0) ? Undefined.instance
202:                                : args[0];
203:                        XMLLib xmlLib = XMLLib.extractFromScope(scope);
204:                        return ScriptRuntime.wrapBoolean(xmlLib.isXMLName(cx,
205:                                name));
206:                    }
207:
208:                    case Id_parseFloat:
209:                        return js_parseFloat(args);
210:
211:                    case Id_parseInt:
212:                        return js_parseInt(args);
213:
214:                    case Id_unescape:
215:                        return js_unescape(args);
216:
217:                    case Id_uneval: {
218:                        Object value = (args.length != 0) ? args[0]
219:                                : Undefined.instance;
220:                        return ScriptRuntime.uneval(cx, scope, value);
221:                    }
222:
223:                    case Id_new_CommonError:
224:                        // The implementation of all the ECMA error constructors
225:                        // (SyntaxError, TypeError, etc.)
226:                        return NativeError.make(cx, scope, f, args);
227:                    }
228:                }
229:                throw f.unknown();
230:            }
231:
232:            /**
233:             * The global method parseInt, as per ECMA-262 15.1.2.2.
234:             */
235:            private Object js_parseInt(Object[] args) {
236:                String s = ScriptRuntime.toString(args, 0);
237:                int radix = ScriptRuntime.toInt32(args, 1);
238:
239:                int len = s.length();
240:                if (len == 0)
241:                    return ScriptRuntime.NaNobj;
242:
243:                boolean negative = false;
244:                int start = 0;
245:                char c;
246:                do {
247:                    c = s.charAt(start);
248:                    if (!Character.isWhitespace(c))
249:                        break;
250:                    start++;
251:                } while (start < len);
252:
253:                if (c == '+' || (negative = (c == '-')))
254:                    start++;
255:
256:                final int NO_RADIX = -1;
257:                if (radix == 0) {
258:                    radix = NO_RADIX;
259:                } else if (radix < 2 || radix > 36) {
260:                    return ScriptRuntime.NaNobj;
261:                } else if (radix == 16 && len - start > 1
262:                        && s.charAt(start) == '0') {
263:                    c = s.charAt(start + 1);
264:                    if (c == 'x' || c == 'X')
265:                        start += 2;
266:                }
267:
268:                if (radix == NO_RADIX) {
269:                    radix = 10;
270:                    if (len - start > 1 && s.charAt(start) == '0') {
271:                        c = s.charAt(start + 1);
272:                        if (c == 'x' || c == 'X') {
273:                            radix = 16;
274:                            start += 2;
275:                        } else if ('0' <= c && c <= '9') {
276:                            radix = 8;
277:                            start++;
278:                        }
279:                    }
280:                }
281:
282:                double d = ScriptRuntime.stringToNumber(s, start, radix);
283:                return ScriptRuntime.wrapNumber(negative ? -d : d);
284:            }
285:
286:            /**
287:             * The global method parseFloat, as per ECMA-262 15.1.2.3.
288:             *
289:             * @param args the arguments to parseFloat, ignoring args[>=1]
290:             */
291:            private Object js_parseFloat(Object[] args) {
292:                if (args.length < 1)
293:                    return ScriptRuntime.NaNobj;
294:
295:                String s = ScriptRuntime.toString(args[0]);
296:                int len = s.length();
297:                int start = 0;
298:                // Scan forward to skip whitespace
299:                char c;
300:                for (;;) {
301:                    if (start == len) {
302:                        return ScriptRuntime.NaNobj;
303:                    }
304:                    c = s.charAt(start);
305:                    if (!TokenStream.isJSSpace(c)) {
306:                        break;
307:                    }
308:                    ++start;
309:                }
310:
311:                int i = start;
312:                if (c == '+' || c == '-') {
313:                    ++i;
314:                    if (i == len) {
315:                        return ScriptRuntime.NaNobj;
316:                    }
317:                    c = s.charAt(i);
318:                }
319:
320:                if (c == 'I') {
321:                    // check for "Infinity"
322:                    if (i + 8 <= len && s.regionMatches(i, "Infinity", 0, 8)) {
323:                        double d;
324:                        if (s.charAt(start) == '-') {
325:                            d = Double.NEGATIVE_INFINITY;
326:                        } else {
327:                            d = Double.POSITIVE_INFINITY;
328:                        }
329:                        return ScriptRuntime.wrapNumber(d);
330:                    }
331:                    return ScriptRuntime.NaNobj;
332:                }
333:
334:                // Find the end of the legal bit
335:                int decimal = -1;
336:                int exponent = -1;
337:                for (; i < len; i++) {
338:                    switch (s.charAt(i)) {
339:                    case '.':
340:                        if (decimal != -1) // Only allow a single decimal point.
341:                            break;
342:                        decimal = i;
343:                        continue;
344:
345:                    case 'e':
346:                    case 'E':
347:                        if (exponent != -1)
348:                            break;
349:                        exponent = i;
350:                        continue;
351:
352:                    case '+':
353:                    case '-':
354:                        // Only allow '+' or '-' after 'e' or 'E'
355:                        if (exponent != i - 1)
356:                            break;
357:                        continue;
358:
359:                    case '0':
360:                    case '1':
361:                    case '2':
362:                    case '3':
363:                    case '4':
364:                    case '5':
365:                    case '6':
366:                    case '7':
367:                    case '8':
368:                    case '9':
369:                        continue;
370:
371:                    default:
372:                        break;
373:                    }
374:                    break;
375:                }
376:                s = s.substring(start, i);
377:                try {
378:                    return Double.valueOf(s);
379:                } catch (NumberFormatException ex) {
380:                    return ScriptRuntime.NaNobj;
381:                }
382:            }
383:
384:            /**
385:             * The global method escape, as per ECMA-262 15.1.2.4.
386:
387:             * Includes code for the 'mask' argument supported by the C escape
388:             * method, which used to be part of the browser imbedding.  Blame
389:             * for the strange constant names should be directed there.
390:             */
391:
392:            private Object js_escape(Object[] args) {
393:                final int URL_XALPHAS = 1, URL_XPALPHAS = 2, URL_PATH = 4;
394:
395:                String s = ScriptRuntime.toString(args, 0);
396:
397:                int mask = URL_XALPHAS | URL_XPALPHAS | URL_PATH;
398:                if (args.length > 1) { // the 'mask' argument.  Non-ECMA.
399:                    double d = ScriptRuntime.toNumber(args[1]);
400:                    if (d != d
401:                            || ((mask = (int) d) != d)
402:                            || 0 != (mask & ~(URL_XALPHAS | URL_XPALPHAS | URL_PATH))) {
403:                        throw Context.reportRuntimeError0("msg.bad.esc.mask");
404:                    }
405:                }
406:
407:                StringBuffer sb = null;
408:                for (int k = 0, L = s.length(); k != L; ++k) {
409:                    int c = s.charAt(k);
410:                    if (mask != 0
411:                            && ((c >= '0' && c <= '9')
412:                                    || (c >= 'A' && c <= 'Z')
413:                                    || (c >= 'a' && c <= 'z') || c == '@'
414:                                    || c == '*' || c == '_' || c == '-'
415:                                    || c == '.' || (0 != (mask & URL_PATH) && (c == '/' || c == '+')))) {
416:                        if (sb != null) {
417:                            sb.append((char) c);
418:                        }
419:                    } else {
420:                        if (sb == null) {
421:                            sb = new StringBuffer(L + 3);
422:                            sb.append(s);
423:                            sb.setLength(k);
424:                        }
425:
426:                        int hexSize;
427:                        if (c < 256) {
428:                            if (c == ' ' && mask == URL_XPALPHAS) {
429:                                sb.append('+');
430:                                continue;
431:                            }
432:                            sb.append('%');
433:                            hexSize = 2;
434:                        } else {
435:                            sb.append('%');
436:                            sb.append('u');
437:                            hexSize = 4;
438:                        }
439:
440:                        // append hexadecimal form of c left-padded with 0
441:                        for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) {
442:                            int digit = 0xf & (c >> shift);
443:                            int hc = (digit < 10) ? '0' + digit
444:                                    : 'A' - 10 + digit;
445:                            sb.append((char) hc);
446:                        }
447:                    }
448:                }
449:
450:                return (sb == null) ? s : sb.toString();
451:            }
452:
453:            /**
454:             * The global unescape method, as per ECMA-262 15.1.2.5.
455:             */
456:
457:            private Object js_unescape(Object[] args) {
458:                String s = ScriptRuntime.toString(args, 0);
459:                int firstEscapePos = s.indexOf('%');
460:                if (firstEscapePos >= 0) {
461:                    int L = s.length();
462:                    char[] buf = s.toCharArray();
463:                    int destination = firstEscapePos;
464:                    for (int k = firstEscapePos; k != L;) {
465:                        char c = buf[k];
466:                        ++k;
467:                        if (c == '%' && k != L) {
468:                            int end, start;
469:                            if (buf[k] == 'u') {
470:                                start = k + 1;
471:                                end = k + 5;
472:                            } else {
473:                                start = k;
474:                                end = k + 2;
475:                            }
476:                            if (end <= L) {
477:                                int x = 0;
478:                                for (int i = start; i != end; ++i) {
479:                                    x = Kit.xDigitToInt(buf[i], x);
480:                                }
481:                                if (x >= 0) {
482:                                    c = (char) x;
483:                                    k = end;
484:                                }
485:                            }
486:                        }
487:                        buf[destination] = c;
488:                        ++destination;
489:                    }
490:                    s = new String(buf, 0, destination);
491:                }
492:                return s;
493:            }
494:
495:            private Object js_eval(Context cx, Scriptable scope, Object[] args) {
496:                String m = ScriptRuntime.getMessage1("msg.cant.call.indirect",
497:                        "eval");
498:                throw NativeGlobal.constructError(cx, "EvalError", m, scope);
499:            }
500:
501:            static boolean isEvalFunction(Object functionObj) {
502:                if (functionObj instanceof  IdFunctionObject) {
503:                    IdFunctionObject function = (IdFunctionObject) functionObj;
504:                    if (function.hasTag(FTAG) && function.methodId() == Id_eval) {
505:                        return true;
506:                    }
507:                }
508:                return false;
509:            }
510:
511:            /**
512:             * @deprecated Use {@link ScriptRuntime#constructError(String,String)}
513:             * instead.
514:             */
515:            public static EcmaError constructError(Context cx, String error,
516:                    String message, Scriptable scope) {
517:                return ScriptRuntime.constructError(error, message);
518:            }
519:
520:            /**
521:             * @deprecated Use
522:             * {@link ScriptRuntime#constructError(String,String,String,int,String,int)}
523:             * instead.
524:             */
525:            public static EcmaError constructError(Context cx, String error,
526:                    String message, Scriptable scope, String sourceName,
527:                    int lineNumber, int columnNumber, String lineSource) {
528:                return ScriptRuntime.constructError(error, message, sourceName,
529:                        lineNumber, lineSource, columnNumber);
530:            }
531:
532:            /*
533:             *   ECMA 3, 15.1.3 URI Handling Function Properties
534:             *
535:             *   The following are implementations of the algorithms
536:             *   given in the ECMA specification for the hidden functions
537:             *   'Encode' and 'Decode'.
538:             */
539:            private static String encode(String str, boolean fullUri) {
540:                byte[] utf8buf = null;
541:                StringBuffer sb = null;
542:
543:                for (int k = 0, length = str.length(); k != length; ++k) {
544:                    char C = str.charAt(k);
545:                    if (encodeUnescaped(C, fullUri)) {
546:                        if (sb != null) {
547:                            sb.append(C);
548:                        }
549:                    } else {
550:                        if (sb == null) {
551:                            sb = new StringBuffer(length + 3);
552:                            sb.append(str);
553:                            sb.setLength(k);
554:                            utf8buf = new byte[6];
555:                        }
556:                        if (0xDC00 <= C && C <= 0xDFFF) {
557:                            throw Context.reportRuntimeError0("msg.bad.uri");
558:                        }
559:                        int V;
560:                        if (C < 0xD800 || 0xDBFF < C) {
561:                            V = C;
562:                        } else {
563:                            k++;
564:                            if (k == length) {
565:                                throw Context
566:                                        .reportRuntimeError0("msg.bad.uri");
567:                            }
568:                            char C2 = str.charAt(k);
569:                            if (!(0xDC00 <= C2 && C2 <= 0xDFFF)) {
570:                                throw Context
571:                                        .reportRuntimeError0("msg.bad.uri");
572:                            }
573:                            V = ((C - 0xD800) << 10) + (C2 - 0xDC00) + 0x10000;
574:                        }
575:                        int L = oneUcs4ToUtf8Char(utf8buf, V);
576:                        for (int j = 0; j < L; j++) {
577:                            int d = 0xff & utf8buf[j];
578:                            sb.append('%');
579:                            sb.append(toHexChar(d >>> 4));
580:                            sb.append(toHexChar(d & 0xf));
581:                        }
582:                    }
583:                }
584:                return (sb == null) ? str : sb.toString();
585:            }
586:
587:            private static char toHexChar(int i) {
588:                if (i >> 4 != 0)
589:                    Kit.codeBug();
590:                return (char) ((i < 10) ? i + '0' : i - 10 + 'a');
591:            }
592:
593:            private static int unHex(char c) {
594:                if ('A' <= c && c <= 'F') {
595:                    return c - 'A' + 10;
596:                } else if ('a' <= c && c <= 'f') {
597:                    return c - 'a' + 10;
598:                } else if ('0' <= c && c <= '9') {
599:                    return c - '0';
600:                } else {
601:                    return -1;
602:                }
603:            }
604:
605:            private static int unHex(char c1, char c2) {
606:                int i1 = unHex(c1);
607:                int i2 = unHex(c2);
608:                if (i1 >= 0 && i2 >= 0) {
609:                    return (i1 << 4) | i2;
610:                }
611:                return -1;
612:            }
613:
614:            private static String decode(String str, boolean fullUri) {
615:                char[] buf = null;
616:                int bufTop = 0;
617:
618:                for (int k = 0, length = str.length(); k != length;) {
619:                    char C = str.charAt(k);
620:                    if (C != '%') {
621:                        if (buf != null) {
622:                            buf[bufTop++] = C;
623:                        }
624:                        ++k;
625:                    } else {
626:                        if (buf == null) {
627:                            // decode always compress so result can not be bigger then
628:                            // str.length()
629:                            buf = new char[length];
630:                            str.getChars(0, k, buf, 0);
631:                            bufTop = k;
632:                        }
633:                        int start = k;
634:                        if (k + 3 > length)
635:                            throw Context.reportRuntimeError0("msg.bad.uri");
636:                        int B = unHex(str.charAt(k + 1), str.charAt(k + 2));
637:                        if (B < 0)
638:                            throw Context.reportRuntimeError0("msg.bad.uri");
639:                        k += 3;
640:                        if ((B & 0x80) == 0) {
641:                            C = (char) B;
642:                        } else {
643:                            // Decode UTF-8 sequence into ucs4Char and encode it into
644:                            // UTF-16
645:                            int utf8Tail, ucs4Char, minUcs4Char;
646:                            if ((B & 0xC0) == 0x80) {
647:                                // First  UTF-8 should be ouside 0x80..0xBF
648:                                throw Context
649:                                        .reportRuntimeError0("msg.bad.uri");
650:                            } else if ((B & 0x20) == 0) {
651:                                utf8Tail = 1;
652:                                ucs4Char = B & 0x1F;
653:                                minUcs4Char = 0x80;
654:                            } else if ((B & 0x10) == 0) {
655:                                utf8Tail = 2;
656:                                ucs4Char = B & 0x0F;
657:                                minUcs4Char = 0x800;
658:                            } else if ((B & 0x08) == 0) {
659:                                utf8Tail = 3;
660:                                ucs4Char = B & 0x07;
661:                                minUcs4Char = 0x10000;
662:                            } else if ((B & 0x04) == 0) {
663:                                utf8Tail = 4;
664:                                ucs4Char = B & 0x03;
665:                                minUcs4Char = 0x200000;
666:                            } else if ((B & 0x02) == 0) {
667:                                utf8Tail = 5;
668:                                ucs4Char = B & 0x01;
669:                                minUcs4Char = 0x4000000;
670:                            } else {
671:                                // First UTF-8 can not be 0xFF or 0xFE
672:                                throw Context
673:                                        .reportRuntimeError0("msg.bad.uri");
674:                            }
675:                            if (k + 3 * utf8Tail > length)
676:                                throw Context
677:                                        .reportRuntimeError0("msg.bad.uri");
678:                            for (int j = 0; j != utf8Tail; j++) {
679:                                if (str.charAt(k) != '%')
680:                                    throw Context
681:                                            .reportRuntimeError0("msg.bad.uri");
682:                                B = unHex(str.charAt(k + 1), str.charAt(k + 2));
683:                                if (B < 0 || (B & 0xC0) != 0x80)
684:                                    throw Context
685:                                            .reportRuntimeError0("msg.bad.uri");
686:                                ucs4Char = (ucs4Char << 6) | (B & 0x3F);
687:                                k += 3;
688:                            }
689:                            // Check for overlongs and other should-not-present codes
690:                            if (ucs4Char < minUcs4Char || ucs4Char == 0xFFFE
691:                                    || ucs4Char == 0xFFFF) {
692:                                ucs4Char = 0xFFFD;
693:                            }
694:                            if (ucs4Char >= 0x10000) {
695:                                ucs4Char -= 0x10000;
696:                                if (ucs4Char > 0xFFFFF)
697:                                    throw Context
698:                                            .reportRuntimeError0("msg.bad.uri");
699:                                char H = (char) ((ucs4Char >>> 10) + 0xD800);
700:                                C = (char) ((ucs4Char & 0x3FF) + 0xDC00);
701:                                buf[bufTop++] = H;
702:                            } else {
703:                                C = (char) ucs4Char;
704:                            }
705:                        }
706:                        if (fullUri && URI_DECODE_RESERVED.indexOf(C) >= 0) {
707:                            for (int x = start; x != k; x++) {
708:                                buf[bufTop++] = str.charAt(x);
709:                            }
710:                        } else {
711:                            buf[bufTop++] = C;
712:                        }
713:                    }
714:                }
715:                return (buf == null) ? str : new String(buf, 0, bufTop);
716:            }
717:
718:            private static boolean encodeUnescaped(char c, boolean fullUri) {
719:                if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')
720:                        || ('0' <= c && c <= '9')) {
721:                    return true;
722:                }
723:                if ("-_.!~*'()".indexOf(c) >= 0)
724:                    return true;
725:                if (fullUri) {
726:                    return URI_DECODE_RESERVED.indexOf(c) >= 0;
727:                }
728:                return false;
729:            }
730:
731:            private static final String URI_DECODE_RESERVED = ";/?:@&=+$,#";
732:
733:            /* Convert one UCS-4 char and write it into a UTF-8 buffer, which must be
734:             * at least 6 bytes long.  Return the number of UTF-8 bytes of data written.
735:             */
736:            private static int oneUcs4ToUtf8Char(byte[] utf8Buffer, int ucs4Char) {
737:                int utf8Length = 1;
738:
739:                //JS_ASSERT(ucs4Char <= 0x7FFFFFFF);
740:                if ((ucs4Char & ~0x7F) == 0)
741:                    utf8Buffer[0] = (byte) ucs4Char;
742:                else {
743:                    int i;
744:                    int a = ucs4Char >>> 11;
745:                    utf8Length = 2;
746:                    while (a != 0) {
747:                        a >>>= 5;
748:                        utf8Length++;
749:                    }
750:                    i = utf8Length;
751:                    while (--i > 0) {
752:                        utf8Buffer[i] = (byte) ((ucs4Char & 0x3F) | 0x80);
753:                        ucs4Char >>>= 6;
754:                    }
755:                    utf8Buffer[0] = (byte) (0x100 - (1 << (8 - utf8Length)) + ucs4Char);
756:                }
757:                return utf8Length;
758:            }
759:
760:            private static final Object FTAG = new Object();
761:
762:            private static final int Id_decodeURI = 1,
763:                    Id_decodeURIComponent = 2, Id_encodeURI = 3,
764:                    Id_encodeURIComponent = 4, Id_escape = 5, Id_eval = 6,
765:                    Id_isFinite = 7, Id_isNaN = 8, Id_isXMLName = 9,
766:                    Id_parseFloat = 10, Id_parseInt = 11, Id_unescape = 12,
767:                    Id_uneval = 13,
768:
769:                    LAST_SCOPE_FUNCTION_ID = 13,
770:
771:                    Id_new_CommonError = 14;
772:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.