Index: trunk/instant_messenger/js/xtools.js
===================================================================
--- trunk/instant_messenger/js/xtools.js	(revision 260)
+++ trunk/instant_messenger/js/xtools.js	(revision 305)
@@ -1,115 +1,151 @@
-function XTools()
+(function()
 {
-}
+	var _FILES = [];
 
-XTools.prototype = {
-	'convert' : function(pString)
+	function _convert(pString)
 	{
 		if ( typeof pString != 'string' )
 			return false;
 
-		if (window.ActiveXObject)
+		if ( window.ActiveXObject )
 		{
-			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
-			xmlDoc.async = "false";
-			xmlDoc.loadXML(pString);
+			var _xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
+			_xmlDoc.async = "false";
+			_xmlDoc.loadXML(pString);
 		}
 		else
 		{
 			var parser = new DOMParser();
-			var xmlDoc = parser.parseFromString(pString, "text/xml");
+			var _xmlDoc = parser.parseFromString(pString, "text/xml");
 		}
 
-		return xmlDoc;
-	},
-	'load' : function(pFile)
+		return _xmlDoc;
+	}
+
+	function _load(pFile)
 	{
-		var data = null;
-		if ( document.implementation.createDocument )
-			data = document.implementation.createDocument("", "", null);
+		pFile = this._PATH + 'xsl/' + pFile;
+		if ( !(_FILES[pFile]) )
+		{
+			var _data = null;
+			if ( document.implementation.createDocument )
+				_data = document.implementation.createDocument("", "", null);
+			else
+				_data = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
+
+			_data.async = false;
+			_data.load(pFile + '?' + Date.parse(new Date));
+			_FILES[pFile] = _data;
+		}
+		return _FILES[pFile];
+	}
+
+	function _parse()
+	{
+		if ( arguments.length == 1 )
+		{
+			pXML = _xml('root');
+			pXSL = arguments[0];
+		}
 		else
-			data = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
+		{
+			pXML = arguments[0];
+			pXSL = arguments[1];
+		}
+		switch ( typeof pXML )
+		{
+			case 'object' :
+			break;
+			case 'string' :
+				if ( pXML.indexOf('<') == 0 )
+					pXML = _convert(pXML);
+				else
+					pXML = _load.call(this, pXML);
+			break;
+			default :
+				return {'error':'invalid xml'}
+		}
+		switch ( typeof pXSL )
+		{
+			case 'object' :
+			break;
+			case 'string' :
+				pXSL = _load.call(this, pXSL);
+			break;
+			default :
+				return {'error':'invalid xsl'}
+		}
 
-		data.async = false;
-		data.load( path_im + 'xsl/' + pFile + '?' + Date.parse(new Date));
-		return data;
-	},
-	'parse' : function(pXML, pXSL)
-	{
-		try
+		var fragment = null;
+		if ( window.XSLTProcessor )
 		{
-			switch ( typeof pXML )
+			var xslProc = new XSLTProcessor();
+			xslProc.importStylesheet(pXSL);
+
+			if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
 			{
-				case 'object' :
-				break;
-				case 'string' :
-					if ( pXML.indexOf('<') == 0 )
-						pXML = this.convert(pXML);
-					else
-						pXML = this.load(pXML);
-				break;
-				default :
-					return {'error':'invalid xml'}
-			}
-			switch ( typeof pXSL )
-			{
-				case 'object' :
-				break;
-				case 'string' :
-					pXSL = this.load(pXSL);
-				break;
-				default :
-					return {'error':'invalid xsl'}
+				var params = arguments[2];
+				for (var i in params )
+					 xslProc.setParameter(null, i, params[i]);
 			}
 
-			var fragment = null;
-			if ( window.XSLTProcessor )
+			fragment = xslProc.transformToFragment(pXML, document);
+
+			// para retornar valor igual ao ie.
+			var aux = document.createElement("div");
+			aux.appendChild(fragment);
+			fragment = aux.innerHTML;
+		}
+		else
+		{
+			var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
+			xslTemplate.stylesheet = pXSL;
+
+			var xslProc = xslTemplate.createProcessor();
+			xslProc.input = pXML;
+
+			if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
 			{
-				var xslProc = new XSLTProcessor();
-				xslProc.importStylesheet(pXSL);
-				fragment = xslProc.transformToFragment(pXML, document);
+				var params = arguments[2];
+				for (var i in params )
+					 xslProc.addParameter(i, params[i], '');
+			}
 
-				// para retornar valor igual ao ie.
-				var aux = document.createElement("div");
-				aux.appendChild(fragment);
-				fragment = aux.innerHTML;
-			}
-			else
-			{
-				var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
-				xslTemplate.stylesheet = pXSL;
+			xslProc.transform();
+			fragment = xslProc.output;
+		}
+		return fragment;
+	}
 
-				var myXslProc = xslTemplate.createProcessor();
-				myXslProc.input = pXML;
+	function _xml()
+	{
+		var a = false;
+		if ( document.implementation.createDocument )
+			a = document.implementation.createDocument("", "", null);
+		else if ( ActiveXObject )
+			a = new ActiveXObject("Msxml2.DOMDocument");
 
-				myXslProc.transform();
-				fragment = myXslProc.output;
-			}
-			return fragment;
-		}
-		catch(e)
-		{
-			alert("transform\n\n" + e + "\n\n" + e.description);
-		}
-	},
-	'xml' : function()
+		if ( arguments.length == 1 && typeof arguments[0] == 'string' )
+			a.appendChild(a.createElement(arguments[0]));
+		//with ( a )
+		//	appendChild(createProcessingInstruction("xml", "version='1.0'"));
+
+		return a;
+	}
+
+	function XTools()
 	{
-		try
-		{
-			var a = false;
-			if ( document.implementation.createDocument )
-				a = document.implementation.createDocument("", "", null);
-			else if ( ActiveXObject )
-				a = new ActiveXObject("Msxml2.DOMDocument");
+		var _argv = arguments;
+		this._PATH = ( _argv.length > 0 ) ?
+			_argv[0] : '';
+		if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) )
+			this._PATH += '/';
+	}
 
-			//with ( a )
-			//	appendChild(createProcessingInstruction("xml", "version='1.0'"));
-
-			return a;
-		}
-		catch(e)
-		{
-			alert("create\n\n" + e + "\n\n" + e.description);
-		}
-	}
-};
+	XTools.prototype.convert = _convert;
+	XTools.prototype.load = _load;
+	XTools.prototype.parse = _parse;
+	XTools.prototype.xml = _xml;
+	window.XTools = XTools;
+}
+)();
