1+ /*
2+ * OWASP Enterprise Security API (ESAPI)
3+ *
4+ * This file is part of the Open Web Application Security Project (OWASP)
5+ * Enterprise Security API (ESAPI) project. For details, please see
6+ * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
7+ *
8+ * Copyright (c) 2008 - The OWASP Foundation
9+ *
10+ * The ESAPI is published by OWASP under the BSD license. You should read and accept the
11+ * LICENSE before you use, modify, and/or redistribute this software.
12+ */
13+
14+ // Utility and Core API Methods
15+ var $namespace = function ( name , separator , container ) {
16+ var ns = name . split ( separator || '.' ) ,
17+ o = container || window ,
18+ i ,
19+ len ;
20+ for ( i = 0 , len = ns . length ; i < len ; i ++ ) {
21+ o = o [ ns [ i ] ] = o [ ns [ i ] ] || { } ;
22+ }
23+ return o ;
24+ } ;
25+
26+ var $require = function ( sPackage ) {
27+ if ( ! eval ( sPackage ) ) {
28+ throw new RuntimeException ( sPackage + " required, but does not exist in the document." ) ;
29+ }
30+ } ;
31+
32+ if ( ! $ ) {
33+ var $ = function ( sElementID ) {
34+ return document . getElementById ( sElementID ) ;
35+ } ;
36+ }
37+
38+ if ( ! Array . prototype . each ) {
39+ Array . prototype . each = function ( fIterator ) {
40+ if ( typeof fIterator != 'function' ) {
41+ throw 'Illegal Argument for Array.each' ;
42+ }
43+
44+ for ( var i = 0 ; i < this . length ; i ++ ) {
45+ fIterator ( this [ i ] ) ;
46+ }
47+ } ;
48+ }
49+
50+ if ( ! Array . prototype . contains ) {
51+ Array . prototype . contains = function ( srch ) {
52+ var found = false ;
53+ this . each ( function ( e ) {
54+ if ( ( srch . equals && srch . equals ( e ) ) || e == srch ) {
55+ found = true ;
56+ return ;
57+ }
58+ } ) ;
59+ return found ;
60+ } ;
61+ }
62+
63+ if ( ! Array . prototype . containsKey ) {
64+ Array . prototype . containsKey = function ( srch ) {
65+ for ( var key in this ) {
66+ if ( key . toLowerCase ( ) == srch . toLowerCase ( ) ) {
67+ return true ;
68+ }
69+ }
70+ return false ;
71+ } ;
72+ }
73+
74+ if ( ! Array . prototype . getCaseInsensitive ) {
75+ Array . prototype . getCaseInsensitive = function ( key ) {
76+ for ( var k in this ) {
77+ if ( k . toLowerCase ( ) == key . toLowerCase ( ) ) {
78+ return this [ k ] ;
79+ }
80+ }
81+ return null ;
82+ } ;
83+ }
84+
85+ if ( ! String . prototype . charCodeAt ) {
86+ String . prototype . charCodeAt = function ( idx ) {
87+ var c = this . charAt ( idx ) ;
88+ for ( var i = 0 ; i < 65536 ; i ++ ) {
89+ var s = String . fromCharCode ( i ) ;
90+ if ( s == c ) { return i ; }
91+ }
92+ return 0 ;
93+ } ;
94+ }
95+
96+ if ( ! String . prototype . endsWith ) {
97+ String . prototype . endsWith = function ( test ) {
98+ return this . substr ( ( this . length - test . length ) , test . length ) == test ;
99+ } ;
100+ }
101+
102+ // Declare Core Exceptions
103+ if ( ! Exception ) {
104+ var Exception = function ( sMsg , oException ) {
105+ this . cause = oException ;
106+ this . errorMessage = sMsg ;
107+ } ;
108+
109+ Exception . prototype = Error . prototype ;
110+
111+ Exception . prototype . getCause = function ( ) { return this . cause ; } ;
112+
113+ Exception . prototype . getMessage = function ( ) { return this . message ; } ;
114+
115+ /**
116+ * This method creates the stacktrace for the Exception only when it is called the first time and
117+ * caches it for access after that. Since building a stacktrace is a fairly expensive process, we
118+ * only want to do it if it is called.
119+ */
120+ Exception . prototype . getStackTrace = function ( ) {
121+ if ( this . callstack ) {
122+ return this . callstack ;
123+ }
124+
125+ if ( this . stack ) { // Mozilla
126+ var lines = stack . split ( "\n" ) ;
127+ for ( var i = 0 , len = lines . length ; i < len ; i ++ ) {
128+ if ( lines [ i ] . match ( / ^ \s * [ A - Z a - z 0 - 9 \= + \$ ] + \( / ) ) {
129+ this . callstack . push ( lines [ i ] ) ;
130+ }
131+ }
132+ this . callstack . shift ( ) ;
133+ return this . callstack ;
134+ }
135+ else if ( window . opera && this . message ) { // Opera
136+ var lines = this . message . split ( '\n' ) ;
137+ for ( var i = 0 , len = lines . length ; i < len ; i ++ ) {
138+ if ( lines [ i ] . match ( / ^ \s * [ A - Z a - z 0 - 9 \= + \$ ] + \( / ) ) {
139+ var entry = lines [ i ] ;
140+ if ( lines [ i + 1 ] ) {
141+ entry += " at " + lines [ i + 1 ] ;
142+ i ++ ;
143+ }
144+ this . callstack . push ( entry ) ;
145+ }
146+ }
147+ this . callstack . shift ( ) ;
148+ return this . callstack ;
149+ }
150+ else { // IE and Safari
151+ var currentFunction = arguments . callee . caller ;
152+ while ( currentFunction ) {
153+ var fn = currentFunction . toString ( ) ;
154+ var fname = fn . substring ( fn . indexOf ( "function" ) + 8 , fn . indexOf ( "(" ) ) || "anonymous" ;
155+ this . callstack . push ( fname ) ;
156+ currentFunction = currentFunction . caller ;
157+ }
158+ return this . callstack ;
159+ }
160+ } ;
161+
162+ Exception . prototype . printStackTrace = function ( writer ) {
163+ var out = this . constructor . toString ( ) + ": " + this . getMessage ( ) + "|||" + this . getStackTrace ( ) . join ( "|||" ) ;
164+
165+ if ( this . cause ) {
166+ if ( this . cause . printStackTrace ) {
167+ out += "||||||Caused by " + this . cause . printStackTrace ( ) . replace ( "\n" , "|||" ) ;
168+ }
169+ }
170+
171+ if ( ! writer ) {
172+ return writer . replace ( "|||" , "\n" ) ;
173+ } else if ( writer . value ) {
174+ writer . value = out . replace ( "|||" , "\n" ) ;
175+ } else if ( writer . writeln ) {
176+ writer . writeln ( out . replace ( "|||" , "\n" ) ) ;
177+ } else if ( writer . innerHTML ) {
178+ writer . innerHTML = out . replace ( "|||" , "<br/>" ) ;
179+ } else if ( writer . innerText ) {
180+ writer . innerText = out . replace ( "|||" , "<br/>" ) ;
181+ } else if ( writer . append ) {
182+ writer . append ( out . replace ( "|||" , "\n" ) ) ;
183+ } else if ( writer instanceof Function ) {
184+ writer ( out . replace ( "|||" , "\n" ) ) ;
185+ }
186+ } ;
187+ }
188+
189+ if ( ! RuntimeException ) {
190+ var RuntimeException = { } ;
191+ RuntimeException . prototype = Exception . prototype ;
192+ }
193+
194+ if ( ! IllegalArgumentException ) {
195+ var IllegalArgumentException = { } ;
196+ IllegalArgumentException . prototype = Exception . prototype ;
197+ }
0 commit comments