PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/edgeSNS/edgeSNSProto/war/js/thirdparty/AjaxPages.js

https://code.google.com/p/edge2cc/
JavaScript | 314 lines | 187 code | 61 blank | 66 comment | 95 complexity | 9e48cff93fd136cfc01a3dad50d8792c MD5 | raw file
  1. /**
  2. * Ajax Pages v0.5
  3. *
  4. * This software is licensed under the MIT License.
  5. *
  6. * The MIT License
  7. *
  8. * Copyright (c) 2005 Gustavo Ribeiro Amigo
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  11. * this software and associated documentation files (the "Software"), to deal in
  12. * the Software without restriction, including without limitation the rights to use,
  13. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  14. * the Software, and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  21. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  24. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  25. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. /**
  28. * Class responsable for processing Ajax Pages.
  29. * @constructor
  30. * @author Gustavo Amigo
  31. */
  32. var AjaxPages = function() {};
  33. /**
  34. * Loads an Ajax Pages template from a file
  35. * @param url The file to be loaded
  36. * @returns The template loaded
  37. */
  38. AjaxPages.prototype.load = function ( url ) {
  39. var req;
  40. // branch for native XMLHttpRequest object
  41. if (window.XMLHttpRequest) {
  42. req = new XMLHttpRequest();
  43. req.open("GET", url, false);
  44. req.send(null);
  45. // branch for IE/Windows ActiveX version
  46. } else if (window.ActiveXObject) {
  47. req = new ActiveXObject("Microsoft.XMLHTTP");
  48. if (req) {
  49. req.open("GET", url, false);
  50. req.send();
  51. }
  52. }
  53. this.template = req.responseText;
  54. return req.responseText;
  55. }
  56. /**
  57. * Parses the template
  58. * @param value (Optional) The template code
  59. * @returns The javascript code generated from the parsing
  60. */
  61. AjaxPages.prototype.parse = function( value ) {
  62. if ( value == undefined ) {
  63. value = this.template;
  64. }
  65. var out = "";
  66. var lineNumber = 1;
  67. try {
  68. var betweenPerc = false;
  69. out = "function(context) { \n";
  70. out += "var __ajp = new AjaxPages();\n";
  71. out += "var __include;\n";
  72. out += "try {\n"
  73. out += " if ( context == undefined ) { \n";
  74. out += " context = '';\n";
  75. out += " }\n";
  76. out += "var out= unescape('";
  77. var line = "";
  78. for (i = 0; i < value.length; i++ )
  79. {
  80. var nextTwo = "";
  81. if ( i <= value.length - 2 ) {
  82. nextTwo = value.charAt(i) + value.charAt( i + 1 );
  83. }
  84. var nextThree = "";
  85. if ( i <= value.length - 3 ) {
  86. nextThree = value.charAt(i) + value.charAt( i + 1 ) + value.charAt( i + 2 );
  87. }
  88. if ( nextTwo == "<%" && nextThree != "<%=" && nextThree != "<%@") {
  89. out += "');\n";
  90. betweenPerc = true;
  91. i += 1;
  92. } else if ( nextTwo == "<%" && nextThree == "<%=" && nextThree != "<%@") {
  93. out += escape(line) + "');\n";
  94. line = "";
  95. out += " out+= ";
  96. betweenPerc = true;
  97. i += 2;
  98. } else if ( nextTwo == "<%" && nextThree != "<%=" && nextThree == "<%@" ) {
  99. i += 3;
  100. var directive = "";
  101. while ( nextTwo != "%>" ) {
  102. directive += value.charAt(i);
  103. i++;
  104. if ( i <= value.length - 2 ) {
  105. nextTwo = value.charAt(i) + value.charAt( i + 1 );
  106. }
  107. }
  108. out += escape(line) + "');\n";
  109. line = "";
  110. out += this._processDirective( directive );
  111. out += " out+= unescape('";
  112. i++;
  113. } else if ( nextTwo == "%>" ) {
  114. out += ";\n" + " out+= unescape('";
  115. // TODO Throw error if it is off, wrong sintax
  116. betweenPerc = false;
  117. i += 1;
  118. } else if ( value.charAt(i) == String.fromCharCode(10) ) {
  119. if ( !betweenPerc ) {
  120. out += escape(line) + "\\n');\n" + " out+= unescape('";
  121. line = "";
  122. lineNumber ++;
  123. }
  124. } else if ( value.charAt(i) == String.fromCharCode(13) ) {
  125. } else {
  126. if ( betweenPerc ) {
  127. out += value.charAt(i) ;
  128. } else {
  129. line += value.charAt(i);
  130. }
  131. }
  132. }
  133. out += escape(line) + "');\n";
  134. out += "} catch (e) {"
  135. out += "return '"+"An exception occurred while excuting template. Error type: ' + e.name"
  136. + "+ '. Error message: ' + e.message;";
  137. out += "}"
  138. out += " return out;\n";
  139. out += "}\n";
  140. } catch (e) {
  141. out = "function(context) { \n";
  142. out += "return '"+"An exception occurred while parsing on line "+ lineNumber +". Error type: " + e.name
  143. + ". Error message: " + e.message+"';";
  144. out += "}"
  145. }
  146. return out;
  147. }
  148. /**
  149. * Private method. Should not be used externally.
  150. * @private
  151. */
  152. AjaxPages.prototype._processDirective = function(directive) {
  153. var i = 0;
  154. var tolkenIndex = 0;
  155. var tolken = new Array();
  156. //Skip first spaces;
  157. while ( directive.charAt(i) == ' ' ) {
  158. i++;
  159. }
  160. tolken[tolkenIndex] = "";
  161. while ( directive.charAt(i) != ' ' && i <= directive.length ) {
  162. tolken[tolkenIndex] += directive.charAt(i);
  163. i++;
  164. }
  165. tolkenIndex++;
  166. //Skip first spaces;
  167. while ( directive.charAt(i) == ' ' ) {
  168. i++;
  169. }
  170. tolken[tolkenIndex] = "";
  171. while ( directive.charAt(i) != ' ' && directive.charAt(i) != '=' && i <= directive.length ) {
  172. tolken[tolkenIndex] += directive.charAt(i);
  173. i++;
  174. }
  175. tolkenIndex++;
  176. //Skip first spaces;
  177. while ( directive.charAt(i) == ' ' ) {
  178. i++;
  179. }
  180. if( directive.charAt(i) != '=' )
  181. throw new AjaxPagesException("Sintax error", "Tolken = expected attribute");
  182. i++
  183. //Skip first spaces;
  184. while ( directive.charAt(i) == ' ' ) {
  185. i++;
  186. }
  187. tolken[tolkenIndex] = "";
  188. while ( directive.charAt(i) != ' ' && i <= directive.length ) {
  189. tolken[tolkenIndex] += directive.charAt(i);
  190. i++;
  191. }
  192. tolkenIndex++;
  193. //Skip first spaces;
  194. while ( directive.charAt(i) == ' ' && i <= directive.length ) {
  195. i++;
  196. }
  197. tolken[tolkenIndex] = "";
  198. while ( directive.charAt(i) != ' ' && directive.charAt(i) != '=' && i <= directive.length && i <= directive.length ) {
  199. tolken[tolkenIndex] += directive.charAt(i);
  200. i++;
  201. }
  202. tolkenIndex++;
  203. if( directive.charAt(i) != '=' && i <= directive.length )
  204. throw new AjaxPagesException("Sintax error", "Tolken = expected after attribute" );
  205. i++
  206. tolken[tolkenIndex] = "";
  207. while ( directive.charAt(i) != ' ' && i <= directive.length && i <= directive.length ) {
  208. tolken[tolkenIndex] += directive.charAt(i);
  209. i++;
  210. }
  211. var file = "";
  212. var context = "";
  213. if ( tolken[0] != "include" )
  214. throw new AjaxPagesException("Sintax error","Directive " + tolken[0] + " unknown.") ;
  215. if ( tolken[1] != "file" )
  216. throw new AjaxPagesException("Sintax error", "Attribute file expected after include." );
  217. else file = tolken[2];
  218. if ( tolken[3] != "context" && tolken[3] != "" )
  219. throw new AjaxPagesException( "Sintax error", "Attribute context expected after file.");
  220. else if ( tolken[3] == "context" )
  221. context = tolken[4]
  222. else
  223. context = "context";
  224. var out = " __ajp.load("+ file +");\n";
  225. out += " __include = __ajp.getProcessor();\n";
  226. out += " out+= __include(" + context + ");\n";
  227. return out;
  228. }
  229. /**
  230. * Processes the template
  231. * @param value (Optional) The template code.
  232. * @returns The output from processing the template
  233. */
  234. AjaxPages.prototype.process = function( value ) {
  235. eval ( "var processor =" + this.parse( value ) );
  236. return processor();
  237. }
  238. /**
  239. * Get the function that processes the template
  240. * @param value (Optional) The template code
  241. * @returns The function that process the template
  242. */
  243. AjaxPages.prototype.getProcessor = function( value ) {
  244. eval ( "var processor =" + this.parse( value ) );
  245. return processor;
  246. }
  247. /**
  248. * Exception throwed by AjaxPages
  249. */
  250. AjaxPagesException = function( name, message ) {
  251. this.name = name;
  252. this.message = message;
  253. }