Symbian.org/FeedUpdateBroker.js
changeset 10 07ac2f6a36a9
parent 6 5e0dece09f96
child 13 3a1db8573f1e
equal deleted inserted replaced
6:5e0dece09f96 10:07ac2f6a36a9
     2 // The FeedUpdateBroker class implements a simple RSS fetcher and parser.
     2 // The FeedUpdateBroker class implements a simple RSS fetcher and parser.
     3 // Adapted from WRTKit RssReader example
     3 // Adapted from WRTKit RssReader example
     4 
     4 
     5 // Constructor.
     5 // Constructor.
     6 function FeedUpdateBroker() {
     6 function FeedUpdateBroker() {
       
     7 	this.escapeLtGt=true;
     7     this.httpReq = null;
     8     this.httpReq = null;
     8     this.feedAddress = null;
     9     this.feedAddress = null;
     9     this.callback = null;
    10     this.callback = null;
    10 	this.ignoreContent = false;
    11 	this.ignoreContent = false;
    11 	this.cancelled = false;
    12 	this.cancelled = false;
    93             while (node != null) {
    94             while (node != null) {
    94                 if (node.nodeType == Node.ELEMENT_NODE) {
    95                 if (node.nodeType == Node.ELEMENT_NODE) {
    95                     if (node.nodeName == "pubDate" ||
    96                     if (node.nodeName == "pubDate" ||
    96                             node.nodeName == "lastBuildDate" ||
    97                             node.nodeName == "lastBuildDate" ||
    97                             node.nodeName == "dc:date") {
    98                             node.nodeName == "dc:date") {
    98                         lastModified = getTextOfNode(node);
    99                         lastModified = getTextOfNode(node, this.escapeLtGt);
    99                         break;
   100                         break;
   100                     }
   101                     }
   101                 }
   102                 }
   102                 node = node.nextSibling;
   103                 node = node.nextSibling;
   103             }
   104             }
   123             node = itemElements[i].firstChild;
   124             node = itemElements[i].firstChild;
   124             while (node != null) {
   125             while (node != null) {
   125                 if (node.nodeType == Node.ELEMENT_NODE) {
   126                 if (node.nodeType == Node.ELEMENT_NODE) {
   126                     if (node.nodeName == "title") {
   127                     if (node.nodeName == "title") {
   127                         // item title
   128                         // item title
   128                         title = getTextOfNode(node);
   129                         title = getTextOfNode(node, this.escapeLtGt);
   129                     } else if (node.nodeName == "pubDate" || node.nodeName == "dc:date") {
   130                     } else if (node.nodeName == "pubDate" || node.nodeName == "dc:date") {
   130                         // item publishing date
   131                         // item publishing date
   131                         date = getTextOfNode(node);
   132                         date = getTextOfNode(node, this.escapeLtGt);
   132                     } else if (node.nodeName == "description" && !this.ignoreContent ) {
   133                     } else if (node.nodeName == "description" && !this.ignoreContent ) {
   133                         // item description
   134                         // item description
   134                         description = getTextOfNode(node);
   135                         description = getTextOfNode(node, this.escapeLtGt);
   135                     } else if (node.nodeName == "link") {
   136                     } else if (node.nodeName == "link") {
   136                         // link URL
   137                         // link URL
   137                         url = getTextOfNode(node);
   138                         url = getTextOfNode(node, this.escapeLtGt);
   138                     } else if (node.nodeName == "dc:creator" ) {
   139                     } else if (node.nodeName == "dc:creator" ) {
   139 						author = getTextOfNode(node);
   140 						author = getTextOfNode(node, this.escapeLtGt);
   140 					}
   141 					}
   141                 }
   142                 }
   142                 node = node.nextSibling;
   143                 node = node.nextSibling;
   143             }
   144             }
   144             
   145             
   153         return { status: "error" };
   154         return { status: "error" };
   154     }
   155     }
   155 }
   156 }
   156 
   157 
   157 // Returns the text of a node.
   158 // Returns the text of a node.
   158 function getTextOfNode(node) {
   159 function getTextOfNode(node, escapeLtGt) {
   159     var buf = "";
   160     var buf = "";
   160 	// iterate through all child elements and collect all text to the buffer
   161 	// iterate through all child elements and collect all text to the buffer
   161 	var child = node.firstChild;
   162 	var child = node.firstChild;
   162 	while (child != null) {
   163 	while (child != null) {
   163 		if (child.nodeType == Node.TEXT_NODE || child.nodeType == Node.CDATA_SECTION_NODE) {
   164 		if (child.nodeType == Node.TEXT_NODE || child.nodeType == Node.CDATA_SECTION_NODE) {
   164 			// append text to buffer
   165 			// append text to buffer
   165 			if (buf != "") {
   166 			if (buf != "") {
   166 				buf += " ";
   167 				buf += " ";
   167 			}
   168 			}
   168 			buf += escapeLtGt(child.nodeValue);
   169 			buf += child.textContent;
       
   170 //			if (escapeLtGt) {
       
   171 //				buf += doEscapeLtGt(child.nodeValue);
       
   172 //			} else {
       
   173 //				buf += child.nodeValue;
       
   174 //			}
   169 		}
   175 		}
   170 		child = child.nextSibling;
   176 		child = child.nextSibling;
   171 	}
   177 	}
   172     // strip all tags from the buffer
       
   173     var strippedBuf = "";
       
   174     var textStartPos = -1;
       
   175     var tagBalance = 0;
       
   176 	
   178 	
   177     var pos;
   179 	return buf;
   178     // iterate through the text and append all text to the stripped buffer
   180 //    // strip all tags from the buffer
   179     // that is at a tag balance of 0
   181 //    var strippedBuf = "";
   180     for (pos = 0; pos < buf.length; pos++) {
   182 //    var textStartPos = -1;
   181         var c = buf.charAt(pos);
   183 //    var tagBalance = 0;
   182         if (c == '<') {
   184 //	
   183             // entering a tag
   185 //    var pos;
   184             if (tagBalance == 0 && textStartPos != -1) {
   186 //    // iterate through the text and append all text to the stripped buffer
   185                 // everything up to here was valid text
   187 //    // that is at a tag balance of 0
   186                 strippedBuf += buf.substring(textStartPos, pos);
   188 //    for (pos = 0; pos < buf.length; pos++) {
   187                 textStartPos = -1;
   189 //        var c = buf.charAt(pos);
   188             }
   190 //        if (c == '<') {
   189             tagBalance++;
   191 //            // entering a tag
   190         } else if (c == '>') {
   192 //            if (tagBalance == 0 && textStartPos != -1) {
   191             // leaving a tag
   193 //                // everything up to here was valid text
   192             tagBalance--;
   194 //                strippedBuf += buf.substring(textStartPos, pos);
   193             textStartPos = -1;
   195 //                textStartPos = -1;
   194         } else if (tagBalance == 0 && textStartPos == -1) {
   196 //            }
   195             // first char of text
   197 //            tagBalance++;
   196             textStartPos = pos;
   198 //        } else if (c == '>') {
   197         }
   199 //            // leaving a tag
   198     }
   200 //            tagBalance--;
       
   201 //            textStartPos = -1;
       
   202 //        } else if (tagBalance == 0 && textStartPos == -1) {
       
   203 //            // first char of text
       
   204 //            textStartPos = pos;
       
   205 //        }
       
   206 //    }
       
   207 //    
       
   208 //    // add remaining text - if any
       
   209 //    if (tagBalance == 0 && textStartPos != -1) {
       
   210 //        strippedBuf += buf.substring(textStartPos, pos);
       
   211 //    }
   199     
   212     
   200     // add remaining text - if any
   213 //    return strippedBuf;
   201     if (tagBalance == 0 && textStartPos != -1) {
       
   202         strippedBuf += buf.substring(textStartPos, pos);
       
   203     }
       
   204     
       
   205     return strippedBuf;
       
   206 }
   214 }
   207 
   215 
   208 FeedUpdateBroker.prototype.cancel = function() {
   216 FeedUpdateBroker.prototype.cancel = function() {
   209 	this.cancelled = true;
   217 	this.cancelled = true;
   210 	this.httpReq.abort();
   218 	this.httpReq.abort();
   211 }
   219 }
   212 
   220 
   213 function escapeLtGt(text){
   221 function doEscapeLtGt(text){
   214 	var lt = "&lt;";
   222 	var lt = "&lt;";
   215 	var gt = "&gt;";
   223 	var gt = "&gt;";
   216 	return text.replace(/</g, lt).replace(/>/g, gt);
   224 	return text.replace(/</g, lt).replace(/>/g, gt);
   217 }
   225 }