/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Pure javascript implementation to read xml in RSS format.
* @namespace jsbeans
* @class RssReader
* @constructor
* @param txt {String} the rss xml string.It may be a result of an AJAX call.
* */
jsbeans.RssReader = function(txt) {
this.txt = txt;
this._items = [];
this.size = 0;
var _load = function() {
var xmlDoc = null;
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(txt);
}
catch(e) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(txt, "text/xml");
}
return xmlDoc;
};
this.xmlDocument = _load();
};
/**
* Return the <code>DOMParser</code> or <code>Microsoft.XMLDOM</code> to manage XML.
* @method getDocument
* @return {DOMParser}
* */
jsbeans.RssReader.prototype.getDocument = function() {
return this.xmlDocument;
};
/**
* @method getTitle
* @return {String}
* */
jsbeans.RssReader.prototype.getTitle = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getLink
* @return {String}
* */
jsbeans.RssReader.prototype.getLink = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("link")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getDescription
* @return {String}
* */
jsbeans.RssReader.prototype.getDescription = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("description")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getLanguage
* @return {String}
* */
jsbeans.RssReader.prototype.getLanguage = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("language")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getCopyright
* @return {String}
* */
jsbeans.RssReader.prototype.getCopyright = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("copyright")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* <span class="fixme">FIXME</span>: change return type to Date
* @method getPubDate
* @return {String}
* */
jsbeans.RssReader.prototype.getPubDate = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* <span class="fixme">FIXME</span>: change return type to Date
* @method getLastBuildDate
* @return {String}
* */
jsbeans.RssReader.prototype.getLastBuildDate = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("lastBuildDate")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getTtl
* @return {String}
* */
jsbeans.RssReader.prototype.getTtl = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("ttl")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getImage
* @return {String}
* */
jsbeans.RssReader.prototype.getImage = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getImageTitle
* @return {String}
* */
jsbeans.RssReader.prototype.getImageTitle = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getImageUrl
* @return {String}
* */
jsbeans.RssReader.prototype.getImageUrl = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].getElementsByTagName("url")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getImageLink
* @return {String}
* */
jsbeans.RssReader.prototype.getImageLink = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].getElementsByTagName("link")[0].childNodes[0].nodeValue;
} catch(e) {}
return res;
};
/**
* <span class="fixme">FIXME</span>: change return type to Integer
* @method getImageWidth
* @return {String}
* */
jsbeans.RssReader.prototype.getImageWidth = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].getElementsByTagName("width")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* <span class="fixme">FIXME</span>: change return type to Integer
* @method getImageHeight
* @return {String}
* */
jsbeans.RssReader.prototype.getImageHeight = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("channel")[0].getElementsByTagName("image")[0].getElementsByTagName("height")[0].childNodes[0].nodeValue;
}
catch(e) {}
return res;
};
/**
* @method getItems
* @return {Arrat&tl;DOM>}
* */
jsbeans.RssReader.prototype.getItems = function() {
var res = null;
try {
res = this.getDocument().getElementsByTagName("item");
}
catch(e) {}
return res;
};
/**
* Returns RSS in JSON format
* @method getJSONItems
* @return {Array<JSON>}
* */
jsbeans.RssReader.prototype.getJSONItems = function() {
var res = new Array();
var items = this.xmlDocument.getElementsByTagName("item");
if (items == null || items.length == 0) {
try {
items = this.xmlDocument.getElementsByTagName("channel")[0].getElementsByTagName("item");
}
catch(e) {
return res;
}
}
if (items == null || items.length == 0) {
return res;
}
for (var i = 0, item; item = items[i]; i++) {
try {
var o = {};
try {
o.title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
catch(a) {
o.title = null;
}
try {
o.description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;
}
catch(a) {
o.description = null;
}
try {
o.link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
}
catch(a) {
o.link = null;
}
try {
o.enclosure = item.getElementsByTagName("enclosure")[0].childNodes[0].nodeValue;
}
catch(a) {
o.enclosure = {};
}
try {
o.enclosure.url = item.getElementsByTagName("enclosure")[0].attributes.getNamedItem("url").text;
}
catch(a) {
o.enclosure.url = null;
}
try {
o.enclosure.length = item.getElementsByTagName("enclosure")[0].attributes.getNamedItem("length").text;
}
catch(a) {
o.enclosure.length = null;
}
try {
o.enclosure.type = item.getElementsByTagName("enclosure")[0].attributes.getNamedItem("type").text;
}
catch(a) {
o.enclosure.type = null;
}
try {
o.category = item.getElementsByTagName("category")[0].childNodes[0].nodeValue;
}
catch(a) {
o.category = {};
}
try {
o.category.domain = item.getElementsByTagName("category")[0].attributes.getNamedItem("category").text;
}
catch(b) {
o.category.domain = null;
}
try {
o.pubDate = item.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
}
catch(c) {
o.pubDate = null;
}
try {
o.guid = item.getElementsByTagName("guid")[0].childNodes[0].nodeValue;
} catch(b) {
o.giud = {};
}
try {
o.guid.isPermaLink = item.getElementsByTagName("guid")[0].attributes.getNamedItem("isPermaLink").text;
}
catch(d) {
o.guid.isPermaLink = null;
}
res.push(o);
}
catch(e) {}
}
return res;
};
/**
* Also kmown as <code>innerText</code>. A way to get text within HTML/XML tags.
* @method stripHTML
* @param str {String} a string in HTML or XML format
* @return {String} the text contained in <code class="param">str</code> without tags.
* @static
* */
jsbeans.RssReader.stripHTML = function(str) {
return str.replace(/<(?:.|\s)*?>/g, '');
};