/** XmlWrapper3.js
 *    3'rd implementation of iPOV's XML/XSLT Object Wrapper.
 *    @requires XmlWrapper3.js
 */  
//

function xw_getXsltProcessor(src) {
    return xw_newXsltProcessor(src);
}

function xw_newXsltProcessor(src) {
    //alert('xw_newXsltProcessor : ' + src);
    var doc = xw_getXmlObject(src, true, false);    //first we have to load an XML Wrapper.
    if (doc instanceof XwIEXmlDocument) {
        return new XwIEXslProcessor(doc);
    } else if (doc instanceof  XwDomXmlDocument) {
        return new XwDomXslProcessor(doc);
    } else {
        return null;
    }
}

/**  Wrapper for IE's XSLT Processor: internally holds a XwIEXmlDocument.
 *  @param xwdoc The XwIEXmlDocument that contains the source XML for the XSLT.
 */
function XwIEXslProcessor(xwdoc) {
    this.type = 'ie';
    this.xwdoc = xwdoc;
    if (xw_isEmpty(this.xwdoc)) {
        throw new Error('Null XML Object');
    }
    this.xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
    this.xslTemplate.stylesheet = this.xwdoc.document;
    this.processor = this.xslTemplate.createProcessor();
}
XwIEXslProcessor.prototype.addParameter = function (name, value) {
    this.processor.addParameter(name, value);
    return this;
};
XwIEXslProcessor.prototype.addParameters = function (params) {
    for (key in params) {
        this.addParameter(key, params[key]);
    }
};
XwIEXslProcessor.prototype.clearParameters = function () {
    this.processor = this.xslTemplate.createProcessor();
};
/** Returns new XW XML Document which is the result of the transform */
XwIEXslProcessor.prototype.transform = function (xdoc) {
    var xmlText = this.transformToText(xdoc);
    //alert('IeXslProcessor.prototype.transform 4: ' + xmlText);
    var xmlDocument = new XwIEXmlDocument();
    xmlDocument.loadXML(xmlText);
    return xmlDocument;
};
XwIEXslProcessor.prototype.transformToText = function (xdoc) {
    if (xdoc.document) {
        this.processor.input = xdoc.document;
    } else {
        // We hope the object is an instance of an MS Xml Object:
        this.processor.input = xdoc;
    }
    this.processor.transform();
    return this.processor.output;
}

/**  Wrapper for Mozilla/Firefox/Dom XSLT Processor: internally holds a XwDomXmlDocument.
 *  @param xwdoc The XwDomXmlDocument that contains the source XML for the XSLT.
 */
function XwDomXslProcessor(xwdoc) {
    this.type = 'dom';
    this.xwdoc = xwdoc;
    this.processor = new XSLTProcessor();
    if (this.xwdoc instanceof  XwDomXmlDocument) {
        this.processor.importStylesheet(this.xwdoc.document);
    } else {
        this.processor.importStylesheet(this.xwdoc);
    }
}
XwDomXslProcessor.prototype.addParameter = function (name, value) {
    this.processor.setParameter(null, name, value);
    return this;
};
XwDomXslProcessor.prototype.addParameters = function (params) {
    for (key in params) {
        this.addParameter(key, params[key]);
    }
};
XwDomXslProcessor.prototype.clearParameters = function () {
    this.processor.clearParameters();
};
XwDomXslProcessor.prototype.transform = function (xdoc) {
    var xmlDocument = new XwDomXmlDocument();
    try {
        if (xdoc.document) {
            xmlDocument.document = this.processor.transformToDocument(xdoc.document);
        } else {
            xmlDocument.document = this.processor.transformToDocument(xdoc);
        }
    } catch (e) {
        xmlDocument.error = e;
        alert('XSLT Error.\r\n[error #' + e.number + ']\r\n' + e.description);
        //xwrapper.handleError(e, 'XSLT Error.');       
    }
    return xmlDocument;
};
/** Rather than returning the result as an XML Document, return it as a text string. */
XwDomXslProcessor.prototype.transformToText = function (xdoc) {
    // In DOM/Mozilla model XSLT results in a document object:
    var result = this.transform(xdoc);
    if (xw_isEmpty(result) || xw_isEmpty(result.document)) {
        return '';
    } else {
        return result.toString();
    }
};
