AjaxPages.js
Summary
No overview generated for 'AjaxPages.js'
var AjaxPages = function() {};
AjaxPages.prototype.load = function ( url ) {
var req;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.open("GET", url, true);
req.send();
}
}
this.template = req.responseText;
return req.responseText;
}
AjaxPages.prototype.parse = function( value ) {
if ( value == undefined ) {
value = this.template;
}
var out = "";
var lineNumber = 1;
try {
var betweenPerc = false;
out = "function(context) { \n";
out += "var __ajp = new AjaxPages();\n";
out += "var __include;\n";
out += "try {\n"
out += " if ( context == undefined ) { \n";
out += " context = '';\n";
out += " }\n";
out += "var out= unescape('";
var line = "";
for (i = 0; i < value.length; i++ )
{
var nextTwo = "";
if ( i <= value.length - 2 ) {
nextTwo = value.charAt(i) + value.charAt( i + 1 );
}
var nextThree = "";
if ( i <= value.length - 3 ) {
nextThree = value.charAt(i) + value.charAt( i + 1 ) + value.charAt( i + 2 );
}
if ( nextTwo == "<%" && nextThree != "<%=" && nextThree != "<%@") {
out += "');\n";
betweenPerc = true;
i += 1;
} else if ( nextTwo == "<%" && nextThree == "<%=" && nextThree != "<%@") {
out += escape(line) + "');\n";
line = "";
out += " out+= ";
betweenPerc = true;
i += 2;
} else if ( nextTwo == "<%" && nextThree != "<%=" && nextThree == "<%@" ) {
i += 3;
var directive = "";
while ( nextTwo != "%>" ) {
directive += value.charAt(i);
i++;
if ( i <= value.length - 2 ) {
nextTwo = value.charAt(i) + value.charAt( i + 1 );
}
}
out += escape(line) + "');\n";
line = "";
out += this._processDirective( directive );
out += " out+= unescape('";
i++;
} else if ( nextTwo == "%>" ) {
out += ";\n" + " out+= unescape('";
betweenPerc = false;
i += 1;
} else if ( value.charAt(i) == String.fromCharCode(10) ) {
if ( !betweenPerc ) {
out += escape(line) + "\\n');\n" + " out+= unescape('";
line = "";
lineNumber ++;
}
} else if ( value.charAt(i) == String.fromCharCode(13) ) {
} else {
if ( betweenPerc ) {
out += value.charAt(i) ;
} else {
line += value.charAt(i);
}
}
}
out += escape(line) + "');\n";
out += "} catch (e) {"
out += "return '"+"An exception occurred while excuting template. Error type: ' + e.name"
+ "+ '. Error message: ' + e.message;";
out += "}"
out += " return out;\n";
out += "}\n";
} catch (e) {
out = "function(context) { \n";
out += "return '"+"An exception occurred while parsing on line "+ lineNumber +". Error type: " + e.name
+ ". Error message: " + e.message+"';";
out += "}"
}
return out;
}
AjaxPages.prototype._processDirective = function(directive) {
var i = 0;
var tolkenIndex = 0;
var tolken = new Array();
while ( directive.charAt(i) == ' ' ) {
i++;
}
tolken[tolkenIndex] = "";
while ( directive.charAt(i) != ' ' && i <= directive.length ) {
tolken[tolkenIndex] += directive.charAt(i);
i++;
}
tolkenIndex++;
while ( directive.charAt(i) == ' ' ) {
i++;
}
tolken[tolkenIndex] = "";
while ( directive.charAt(i) != ' ' && directive.charAt(i) != '=' && i <= directive.length ) {
tolken[tolkenIndex] += directive.charAt(i);
i++;
}
tolkenIndex++;
while ( directive.charAt(i) == ' ' ) {
i++;
}
if( directive.charAt(i) != '=' )
throw new AjaxPagesException("Sintax error", "Tolken = expected attribute");
i++
while ( directive.charAt(i) == ' ' ) {
i++;
}
tolken[tolkenIndex] = "";
while ( directive.charAt(i) != ' ' && i <= directive.length ) {
tolken[tolkenIndex] += directive.charAt(i);
i++;
}
tolkenIndex++;
while ( directive.charAt(i) == ' ' && i <= directive.length ) {
i++;
}
tolken[tolkenIndex] = "";
while ( directive.charAt(i) != ' ' && directive.charAt(i) != '=' && i <= directive.length && i <= directive.length ) {
tolken[tolkenIndex] += directive.charAt(i);
i++;
}
tolkenIndex++;
if( directive.charAt(i) != '=' && i <= directive.length )
throw new AjaxPagesException("Sintax error", "Tolken = expected after attribute" );
i++
tolken[tolkenIndex] = "";
while ( directive.charAt(i) != ' ' && i <= directive.length && i <= directive.length ) {
tolken[tolkenIndex] += directive.charAt(i);
i++;
}
var file = "";
var context = "";
if ( tolken[0] != "include" )
throw new AjaxPagesException("Sintax error","Directive " + tolken[0] + " unknown.") ;
if ( tolken[1] != "file" )
throw new AjaxPagesException("Sintax error", "Attribute file expected after include." );
else file = tolken[2];
if ( tolken[3] != "context" && tolken[3] != "" )
throw new AjaxPagesException( "Sintax error", "Attribute context expected after file.");
else if ( tolken[3] == "context" )
context = tolken[4]
else
context = "context";
var out = " __ajp.load("+ file +");\n";
out += " __include = __ajp.getProcessor();\n";
out += " out+= __include(" + context + ");\n";
return out;
}
AjaxPages.prototype.process = function( value ) {
eval ( "var processor =" + this.parse( value ) );
return processor();
}
AjaxPages.prototype.getProcessor = function( value ) {
eval ( "var processor =" + this.parse( value ) );
return processor;
}
AjaxPagesException = function( name, message ) {
this.name = name;
this.message = message;
}
Documentation generated by
JSDoc on Mon Jul 25 23:41:31 2005