webglade

JavaScript library to dynamically create XUL GUI from Glade XML files
git clone https://logand.com/git/webglade.git/
Log | Files | Refs | README | LICENSE

AmazonParser.js (3972B)


      1 /**
      2  *
      3  * Used to merge more concurrent response from Amazon.
      4  *
      5  * @fileName AmazonParser.js
      6  * @$LastChangedDate: 2004-05-07 16:58:00 +0200 (Fri, 07 May 2004) $
      7  * @author Fabio Serra <faser@faser.net>
      8  * @copyright Fabio Serra (The Initial Developer of the Original Code)
      9  * @license Mozilla Public License Version 1.1
     10  *
     11 */
     12 
     13 /**
     14  * Construct a new AmazonParser object
     15  * @class This class is used to manage all the XML Amazon response arrived from
     16  * one or multiple request in the same Connection
     17  * @constructor
     18  * @return A new AmazonParser object
     19  *
     20 */
     21 function AmazonParser(xmlDoc) {
     22 	if(xmlDoc) {this.xmlDoc = xmlDoc;}
     23 }
     24 
     25 /**
     26  * Get the total pages available for the search
     27  * @type int
     28 */
     29 AmazonParser.prototype.getTotalPages = function() {
     30 	if(!this.xmlDoc) {return false;}
     31 	var path = "//ProductInfo/TotalPages";
     32 	var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
     33 	if(xpathResult != null) {
     34 		return xpathResult.textContent;
     35 	} else {
     36 		throw("AmazonParser Exception: TotalPages not found");
     37 	}
     38 }
     39 
     40 /**
     41  * Get the total items for the search
     42  * @type int
     43 */
     44 AmazonParser.prototype.getTotalResults = function() {
     45 	if(!this.xmlDoc) {return false;}
     46 	var path = "//ProductInfo/TotalResults";
     47 	var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
     48 	if(xpathResult != null) {
     49 		return xpathResult.textContent;
     50 	} else {
     51 		throw("AmazonParser Exception: TotalResult not found");
     52 	}
     53 }
     54 
     55 /**
     56  * Check if the valid XML has the error tag
     57  * @type bool
     58 */
     59 AmazonParser.prototype.hasError = function() {
     60 	var path = "//ErrorMsg";
     61 	var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
     62 	if(xpathResult != null) {
     63 		return true;
     64 	} else {
     65 		return false;
     66 	}
     67 }
     68 
     69 /**
     70  * Get the error message from the valid xml Amazon response
     71  * @throw ErrorMsg not found
     72  * @type string
     73 */
     74 AmazonParser.prototype.getErrorMsg = function() {
     75 	var path = "//ErrorMsg";
     76 	var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
     77 	if(xpathResult != null) {
     78 		return xpathResult.textContent;
     79 	} else {
     80 		throw("AmazonParser Exception: ErrorMsg not found");
     81 	}
     82 }
     83 
     84 /**
     85  * Count how many Details node are present in the xml response
     86  * type int
     87 */
     88 AmazonParser.prototype.getNrDetails = function() {
     89 	var el = this.xmlDoc.getElementsByTagName("Details");
     90 	if(el.length) {
     91 		return el.length;
     92 	}else{
     93 		return 0;
     94 	}
     95 }
     96 
     97 /**
     98  * Merge the new Amazon document with the current document
     99  * Once the documents are merged TotalPages, TotalResults are not more valid
    100  * because they referer to the first original amazon page.
    101  * @param {XML Document} docToMerge
    102  * @return true if the document has been merged or created
    103  * @type bool
    104 */
    105 AmazonParser.prototype.mergeDocument = function(docToMerge) {
    106 	if(!this.xmlDoc) {
    107 		this.xmlDoc = docToMerge;
    108 		return true;
    109 	}
    110 	//I can merge an amazon response only if there are not error
    111 	if(this.hasError()) {return false;}
    112 
    113 	var reference = this.xmlDoc.getElementsByTagName('ProductInfo').item(0);
    114 	var details = docToMerge.getElementsByTagName('Details');
    115 	if(details.length > 0) {
    116 		for (var i=0;i< details.length ;i++) {
    117 			var node = details.item(i).cloneNode(true);
    118 			reference.appendChild(node);
    119 		}
    120 	}
    121 
    122 	return true;
    123 }
    124 
    125 /**
    126  * Add an empty review tag if it's missing from heavy search. In this way the life
    127  * is more easy for the DisplayController.
    128  * @return void
    129 */
    130 AmazonParser.prototype.setMissingReviews = function() {
    131 	var parser = new DOMParser();
    132 	var strTag = "<Reviews/>"
    133 	var reviews;
    134 	var nodes = this.xmlDoc.getElementsByTagName("Details");
    135 	for(var i= 0; i < nodes.length; i++) {
    136 		reviews = nodes[i].getElementsByTagName("Reviews").item(0);
    137 		if(reviews == null) {
    138 			innerXML(nodes.item(i),strTag);
    139 		 }
    140 
    141 	}
    142 }