/*
    <ObjXml.js est une librairie javascript servant a gerer les les fichiers XML en sql.>
    Copyright (C) 2008  Michael Bruneau

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
function ObjXml(){
	this.xmlDoc=null;
	this.nodepos=0;
}
ObjXml.prototype.loadXML = function (fichierxml, request) {
//load xml file
	if(this.xmlDoc == null){
		try{
			this.xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			this.nodepos=1;
		}
		catch(e){
			try{
				this.xmlDoc=document.implementation.createDocument("","",null);
				this.nodepos=0;
				}
			catch(e) {alert(e.message);}
		}
		try{
			this.xmlDoc.async=false;
			this.xmlDoc.load(fichierxml);
		}
		catch(e) {alert(e.message);}
	}

	request.replace(/\r/g, " ");
	request.replace(/\n/g, " ");
	request.replace(/\t/g, " ");
	request.replace(/[ ]{2,}/g, " ");
	var regExpSql = /^select (.*) from (.*) where (.*)$/i;
	regExpSql.exec(request);
	var sqlSelect = RegExp.$1;
	var sqlFrom = RegExp.$2;
	var sqlWhere = RegExp.$3;
	var listeSqlSelect = sqlSelect.split(/ {0,}, {0,}/);
	var listeSqlWhere = sqlWhere.split(" and ");

//document.write(sqlSelect+"<br>\n"+sqlFrom+"<br>\n"+listeSqlWhere.join(";"));

var debstr="";


	var xmlDocSelect = this.xmlDoc.getElementsByTagName(sqlFrom);
	var valeurRetour="";
	var tabValeurRetour=new Array();
	for(var i=0; i<xmlDocSelect.length; i++){
		for(var j=0; j<listeSqlWhere.length; j++){
//document.write(listeSqlWhere.length+"<br>");
			var tmp_listeSqlWhere=listeSqlWhere[j];
			while(tmp_listeSqlWhere.match(/([a-zA-Z0-9_]{1,}) {0,}([=<>]{1,2}| like ) {0,}('[%a-zA-Z0-9_éèà&; ]{1,}')/)){
				tmp_listeSqlWhere=tmp_listeSqlWhere.replace(/([a-zA-Z0-9_]{1,}) {0,}([=<>]{1,}| like ) {0,}('[%a-zA-Z0-9_éèà&; ]{1,}')/, "");
				var nomChamps = RegExp.$1;
				var ope = RegExp.$2;
				var valeurChamps = RegExp.$3;
				valeurChamps=valeurChamps.replace(/'([%a-zA-Z0-9_éèà&; ]{1,})'/, '$1');
				valeurChamps=valeurChamps.replace(/%/g,".*");
				for(var k=(1-this.nodepos);k<((xmlDocSelect[i].childNodes.length));k=k+(2-this.nodepos)){
					if(xmlDocSelect[i].childNodes[k].tagName == nomChamps){
						valeurChamps=valeurChamps.replace(/[^a-zA-Z0-9]/g,".");
						switch(ope){
							case "=":
								ope="==valeurChamps";
							break;
							case "<=":
								ope="<=valeurChamps";
							break;
							case "<":
								ope="<valeurChamps";
							break;
							case ">=":
								ope=">=valeurChamps";
							break;
							case ">":
								ope=">valeurChamps";
							break;
							case "<>":
								ope="!=valeurChamps";
							break;
							case " like ":
								ope=".match(valeurChamps)";
							break;
						}
						//xmlDocSelect[i].childNodes[k].childNodes[0].nodeValue.replace(/[^a-zA-Z0-9]/g,".")
//debstr+=xmlDocSelect[i].childNodes[k].childNodes[0].nodeValue.replace(/[^a-zA-Z0-9]/g,".")+"\n";
//debstr+=ope+"\n";
						if(eval("xmlDocSelect["+i+"].childNodes["+k+"].childNodes[0].nodeValue.replace(/[^a-zA-Z0-9]/g,\".\")"+ope)){
							for(var m=0;m<(listeSqlSelect.length);m++){
								for(var l=(1-this.nodepos);l<((xmlDocSelect[i].childNodes.length));l=l+(2-this.nodepos)){
									if(xmlDocSelect[i].childNodes[l].tagName == listeSqlSelect[m]){
										valeurRetour=valeurRetour+"--++--"+xmlDocSelect[i].childNodes[l].childNodes[0].nodeValue;
									}
								}
							}
						}
						tabValeurRetour[i] = new Array();
						tabValeurRetour[i] = valeurRetour;
						valeurRetour="";
					}
				}
			}
		}
	}
	var tabOutValeurRetour=new Array();
	for(var h in tabValeurRetour){
		double=0;
		for(var i in tabValeurRetour){
			if((tabValeurRetour[i]==tabValeurRetour[h]) && (double==0)){
				double=1;
				tabOutValeurRetour[tabValeurRetour[h]]=tabValeurRetour[h];
			}
		}
	}
	for(elem in tabOutValeurRetour){
		if(elem.length>0){
//if(elem.match(/^--\+\+--/)){elem=elem.replace(/^--\+\+--/g, "");}
			valeurRetour+=elem+"--**--";
		}
	}
	if(valeurRetour.match(/--\*\*--$/)){valeurRetour=valeurRetour.replace(/--\*\*--$/, "");}
	if(valeurRetour.match(/^--\*\*--/)){valeurRetour=valeurRetour.replace(/^--\*\*--/, "");}
//	if(valeurRetour.match(/^--\+\+--/)){valeurRetour=valeurRetour.replace(/^--\+\+--/, "");}
//alert(debstr);
	return valeurRetour;
}



