using System;
using System.Xml;
using System.Collections;

/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon
 * 
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 */

namespace FeedExpress
{
	/// 
	/// Summary description for RDFReader.
	/// 
	public class RDFReader : IRssReader
	{
		private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		public RDFReader() {
		}

		public bool canRead(XmlDocument document) {
			XmlNode versionAttr = document.DocumentElement.Attributes.GetNamedItem("version");
			if (versionAttr == null) {
				// Check the namespace
				if (document.DocumentElement.NamespaceURI == "http://www.w3.org/1999/02/22-rdf-syntax-ns#") { 
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}

		public IDictionary readFeeds(XmlDocument document) {
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
			nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
			nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");
			nsmgr.AddNamespace("admin", "http://webns.net/mvcb/");
			nsmgr.AddNamespace("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			nsmgr.AddNamespace(String.Empty, "http://purl.org/rss/1.0/");
			nsmgr.AddNamespace("__defns", "http://purl.org/rss/1.0/");


			XmlNodeList itemList = document.DocumentElement.SelectNodes("//__defns:item", nsmgr);
			IDictionary returnValue = new Hashtable(itemList.Count);
			foreach (XmlNode item in itemList) {
				FeedEntry entry = new FeedEntry();
				entry.title = XmlUtils.getNodeText(item, "__defns:title", nsmgr);
				entry.description = XmlUtils.getNodeText(item, "__defns:description", nsmgr);
				entry.link = XmlUtils.getNodeText(item, "__defns:link", nsmgr);
				entry.publicationDate = XmlUtils.getNodeDateTime(item , "dc:date", nsmgr);
				entry.subject = XmlUtils.getNodeText(item , "dc:subject", nsmgr);
				entry.creator = XmlUtils.getNodeText(item , "dc:creator", nsmgr);
				// Since the format doesn't contain a Guid, create one using the link
				entry.rssGuid = new FeedEntry.RssGuid(entry.link, true);
				returnValue.Add(entry.link, entry);
			}
			return returnValue;
		}

		public ChannelInformation readInformation(XmlDocument document) {
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
			nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
			nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");
			nsmgr.AddNamespace("admin", "http://webns.net/mvcb/");
			nsmgr.AddNamespace("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			nsmgr.AddNamespace(String.Empty, "http://purl.org/rss/1.0/");
			nsmgr.AddNamespace("__defns", "http://purl.org/rss/1.0/");


			ChannelInformation chInfo = new ChannelInformation();
			XmlElement root = (XmlElement)document.DocumentElement.SelectSingleNode("//__defns:channel", nsmgr);
			chInfo.title = XmlUtils.getNodeText(root, "__defns:title", nsmgr);
			chInfo.link = XmlUtils.getNodeText(root, "__defns:link", nsmgr);
			chInfo.description = XmlUtils.getNodeText(root, "__defns:description", nsmgr);
			chInfo.language = XmlUtils.getNodeText(root, "__defns:language", nsmgr);
			chInfo.creator = XmlUtils.getNodeText(root, "__defns:webMaster", nsmgr);
			chInfo.date = XmlUtils.getNodeDateTime(root, "__defns:pubDate", nsmgr);
			
			XmlNode xmlNode = root.SelectSingleNode("admin:generatorAgent", nsmgr);
			chInfo.generatorAgent = XmlUtils.getAttributeValue(xmlNode, "resource", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");

			chInfo.updatePeriod = ChannelInformation.UpdatePeriodEnum.Hour;
			chInfo.updateFrequency = 1;
			chInfo.updateBase = DateTime.Now;

			chInfo.cssStyle = HTMLUtils.readChannelHTMLCSS(chInfo.link);
			return chInfo;
		}

	}
}
Close Window