﻿/**
*
*  uno.xml.present.js 
*  by Hideki Yamamoto eu-gsm[+393453332248]
*
**/
//if(!uno.xml){alert('uno.xml.present.js requires uno.xml.js');}
////////////////////////////CROSSWBROWSER XML DATA ACCESS//////////////////////////
uno.xml.attributevalue = function(attribute){
    if(attribute.value){return attribute.value;}
    if(attribute.nodeValue){return attribute.nodeValue;}};
uno.xml.nodetext = function(node){
    if(node.text){return node.text;}
    if(node.textContent){return node.textContent;}};
uno.xml.nodexml = function (xmlobject){
    var test;
    if(xmlobject.xml){
        test = xmlobject.xml;
    }else if(XMLSerializer){
        test = (new XMLSerializer()).serializeToString(xmlobject);
    } else{
        test = uno.xml.tostringformat(xmlobject,'','<','>','','');
    }     
    return test;
};
uno.xml.nodexmltohtml = function (xmlobject){
    var test;
    test = uno.xml.tostringformat(xmlobject,'','&lt;','&gt;','<br />','&nbsp;&nbsp;');
    test = test.replace(/à/g,"&agrave;").replace(/è/g,"&egrave;");
    test = test.replace(/ì/g,"&igrave;").replace(/ò/g,"&ograve;");
    test = test.replace(/ù/g,"&ugrave;");
    return test;
};
uno.xml._tnodes = new Array();
uno.xml.transform = function(node){
    var transformable = false;
    var docwrite = false;
    var usefunc = false;
    var funcname = false;
    var teval = false;    
    for (var ai = 0 ; ai < node.attributes.length ; ai++){
        if((node.attributes[ai].name == 'twrite')&&(uno.xml.attributevalue(node.attributes[ai]).toLowerCase != 'false')){
            docwrite = true;transformable=true;
        }else if(node.attributes[ai].name == 'tfunction'){
           funcname = uno.xml.attributevalue(node.attributes[ai]);
           usefunc=true;transformable=true;
        }else if((node.attributes[ai].name == 'teval')&&(uno.xml.attributevalue(node.attributes[ai]).toLowerCase != 'false')){
           transformable=true;teval=true;
    }   }   
    if (transformable){
        if (docwrite){document.write(uno.xml.nodetext(node));}
        else if (usefunc){
             var idx = uno.xml._tnodes.length;
             uno.xml._tnodes[idx] = node;
             var code = funcname+'(uno.xml._tnodes['+idx+'])';
             eval(code);}
        else if (teval){
             var s = uno.xml.nodetext(node);
             eval(s);
        }
    }else{
        for(var i = 0; i<node.childNodes.length; i++){
            if(node.childNodes[i].nodeType == 1){
                uno.xml.transform(node.childNodes[i]);
}   }   }   };
//////////////////////////////////////////////////////////////////////////////////////////
uno.xml.tostringformat = function (node,tabulation,opentag,closetag,newline,tabincrement){
    var test = tabulation + opentag + node.nodeName;
    if(node.hasAttributes){
        for (var ai = 0 ; ai < node.attributes.length ; ai++){
            test += ' ' + node.attributes[ai].name + '=' + uno.xml.attributevalue(node.attributes[ai]);
    }   }
    test += closetag + newline;      
    if (node.hasChildNodes){
        for(var i = 0; i<node.childNodes.length; i++){
            if (node.childNodes[i].nodeName=='#text'){
                var data = uno.xml.nodetext(node.childNodes[i]);
                data = data.replace(/\n/g,'');
                for (var axe = 0; axe < 1000 ; axe++){
                     if (data.indexOf(' ') == 0){data = data.replace(' ','');}
                    else if (data.indexOf('\n') == 0){data = data.replace('\n','');}
                    else{axe=2000;}
                }
                if(data != ''){
                test += tabulation + uno.xml.nodetext(node.childNodes[i]) + newline;}
            }else if(node.childNodes[i].nodeName=='#cdata-section'){
                test += tabulation + '  ' + '<![CDATA[' + uno.xml.nodetext(node.childNodes[i]) + ']]>'+newline;
            }else if(node.childNodes[i].nodeType == 1){
                if(tabincrement){
                    test += uno.xml.tostringformat(node.childNodes[i],tabulation+tabincrement,opentag,closetag,newline);
                }else{
                    test += uno.xml.tostringformat(node.childNodes[i],tabulation,opentag,closetag,newline);                
                }                
    }   }   }
    return test + tabulation + opentag + '/' + node.nodeName + closetag + newline;
};