现在的位置: 首页 > 综合 > 正文

RssValidator

2012年04月14日 ⁄ 综合 ⁄ 共 13025字 ⁄ 字号 评论关闭

一段Rss文档验证的代码,由于最近要换工作,没时间写了,所以还没有写完。提前发出来,大家分享。

其中关于MIME、cloud和PICS的文档还没有找到(网速现在实在慢的可以-_-),如果谁有这方面的资料希望可以分享。

using System.Xml;

using System;

using System.Xml.XPath;

using System.Collections.Specialized;

using System.Text.RegularExpressions;

namespace SoRss.RssFinder

{

internal class Rss2Validator : RssVaildator

{

private Version _FeedVersion = null;

 

public override Version FeedVersion

{

get { return (_FeedVersion); }

}

 

private void AddFailedToLoadLog(string nameOfElement)

{

AddLog("failed to load " + nameOfElement + " element.");

}

 

protected override void OnValidate(ValidateEventArgs e)

{

bool res = false;

 

XmlDocument document = e.Document;

 

res = DocumentValidate(document);

 

e.Result = res;

}

 

private bool DocumentValidate(XmlDocument document)

{

bool res = false;

 

if (document != null)

{

XmlElement rssElement = document.DocumentElement;

res = RssElementValidate(rssElement);

}

else

{

AddFailedToLoadLog("document");

}

return res;

}

 

private bool RssElementValidate(XmlElement rssElement)

{

bool res = false;

 

if (rssElement != null)

{

XmlAttribute versionAttribute = rssElement.Attributes[NameOfVersionAttributeInRss];

res = VersionAttributeValidate(versionAttribute);

 

if (res)

{

XmlElement channelElement = rssElement[NameOfChannelElementInRss];

res = ChannelElementOfRssValidate(channelElement);

}

}

else

{

AddFailedToLoadLog(NameOfRssElement);

}

return res;

}

 

private bool VersionAttributeValidate(XmlAttribute versionAttribute)

{

bool res = false;

if (versionAttribute != null)

{

string versionString = versionAttribute.InnerText;

res = InnerTextOfVersionValidate(versionString);

}

else

{

AddLog("failed to load " + NameOfRssElement + " element.");

}

return res;

}

 

private bool InnerTextOfVersionValidate(string versionString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(versionString))

{

try

{

_FeedVersion = new Version(versionString);

res = true;

}

catch (OverflowException ex)

{

AddLog(ex.ToString());

}

catch (FormatException ex)

{

AddLog(ex.ToString());

}

catch (ArgumentNullException ex)

{

AddLog(ex.ToString());

}

catch (ArgumentOutOfRangeException ex)

{

AddLog(ex.ToString());

}

catch (ArgumentException ex)

{

AddLog(ex.ToString());

}

 

if (!res)

{

AddLog("failed to format inner text of " + NameOfVersionAttributeInRss + " attribute.");

}

}

else

{

AddLog("failed to load " + NameOfVersionAttributeInRss + " attribute of " + NameOfRssElement + " element.");

}

 

return res;

}

 

private bool ChannelElementOfRssValidate(XmlElement channelElement)

{

bool res = false;

 

if (channelElement != null)

{

res = RequiredElementsOfChannelValidate(channelElement);

 

if (res)

{

res = OptionalElementsOfChannelValidate(channelElement);

}

 

if (res)

{

res = ItemElementsOfChannelValidate(channelElement);

}

}

else

{

AddFailedToLoadLog(NameOfChannelElementInRss);

}

 

return (res);

}

 

private bool ItemElementsOfChannelValidate(XmlElement channelElement)

{

bool res = false;

 

if (channelElement != null)

{

try

{

XmlNodeList itemElements = channelElement.SelectNodes(NameOfItemElementInChannel);

 

if (itemElements.Count > 0)

{

foreach (XmlNode itemElement in itemElements)

{

res = ItemElementOfChannelValidate(itemElement);

if (!res)

{

break;

}

}

}

else

{

AddLog("Found no " + NameOfItemElementInChannel + "element.");

res = true;

}

}

catch (XPathException ex)

{

AddLog(ex.ToString());

res = false;

}

}

else

{

AddFailedToLoadLog(NameOfChannelElementInRss);

}

return (res);

}

 

private bool ItemElementOfChannelValidate(XmlNode itemElement)

{

bool res = false;

 

if (itemElement != null)

{

res = OptionalElementsOfItemValidate(itemElement);

}

else

{

AddFailedToLoadLog(NameOfItemElementInChannel);

}

return (res);

}

 

private bool OptionalElementsOfItemValidate(XmlNode itemElement)

{

bool res = false;

 

if (itemElement != null)

{

XmlElement titleElement = itemElement[NameOfTitleElementInItem];

res = TitleElementOfItemValidate(titleElement);

 

if (res)

{

XmlElement linkElement = itemElement[NameOfLinkElementInItem];

res = LinkElementOfItemValidate(linkElement);

}

 

if (res)

{

XmlElement descriptionElement = itemElement[NameOfDescriptionElementInItem];

res = DescriptionElementOfItemValidate(descriptionElement);

}

 

if (res)

{

XmlElement authorElement = itemElement[NameOfAuthorElementInItem];

res = AuthorElementOfItemValidate(authorElement);

}

 

if (res)

{

XmlElement categoryElement = itemElement[NameOfCategoryElementInItem];

res = CategoryElementOfItemValidate(categoryElement);

}

 

if (res)

{

XmlElement commentsElement = itemElement[NameOfCommentsElementInItem];

res = commentsElementOfItemValidate(commentsElement);

}

 

if (res)

{

XmlElement enclosureElement = itemElement[NameOfEnclosureElementInItem];

res = EnclosureElementOfItemValidate(enclosureElement);

}

 

if (res)

{

XmlElement guidElement = itemElement[NameOfGuidElementInItem];

res = GuidElementOfItemValidate(guidElement);

}

 

if (res)

{

XmlElement pubDateElement = itemElement[NameOfPubDateElementInItem];

res = PubDateElementOfItemValidate(pubDateElement);

}

 

if (res)

{

XmlElement sourceElement = itemElement[NameOfSourceElementInItem];

res = SourceElementOfItemValidate(sourceElement);

}

}

else

{

AddFailedToLoadLog(NameOfItemElementInChannel);

}

 

return (res);

}

 

#region Optional Elements Of Item Validate

private bool SourceElementOfItemValidate(XmlElement sourceElement)

{

bool res = false;

 

if (sourceElement != null)

{

XmlAttribute urlAttribute = sourceElement.Attributes[NameOfUrlAttributeInSourceInItem];

res = UrlAttributeInSourceInItemValidate(urlAttribute);

}

else

{

AddFailedToLoadLog(NameOfSourceElementInItem);

res = true;

}

 

return (res);

}

 

private bool UrlAttributeInSourceInItemValidate(XmlAttribute urlAttribute)

{

bool res = false;

 

if (urlAttribute != null)

{

string urlString = urlAttribute.InnerText;

res = InnerTextOfUrlAttributeInSourceInItem(urlString);

}

else

{

AddFailedToLoadLog(NameOfUrlAttributeInSourceInItem);

}

 

return (res);

}

 

private bool InnerTextOfUrlAttributeInSourceInItem(string urlString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(urlString))

{

if (IsWellFormedUri(urlString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfUrlAttributeInSourceInItem + " attribute in " + NameOfSourceElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfUrlAttributeInSourceInItem + " attribute in " + NameOfSourceElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

}

 

return (res);

}

 

private bool PubDateElementOfItemValidate(XmlElement pubDateElement)

{

bool res = false;

 

if (pubDateElement != null)

{

string pubDateString = pubDateElement.InnerText;

res = InnerTextOfPubDateInItem(pubDateString);

}

else

{

AddFailedToLoadLog(NameOfPubDateElementInItem);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfPubDateInItem(string pubDateString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(pubDateString))

{

if (UTCTimeValidate(pubDateString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfPubDateElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfPubDateElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

res = true;

}

 

return (res);

}

 

private bool GuidElementOfItemValidate(XmlElement guidElement)

{

bool res = false;

 

if (guidElement != null)

{

XmlAttribute isPermaLinkAttribute = guidElement.Attributes[NameOfIsPermaLinkAttributeInGuidInItem];

res = IsPermaLinkAttributeInGuidInItemValidate(isPermaLinkAttribute);

 

if (res)

{

string guidString = guidElement.InnerText;

res = InnerTextOfGuidInItemValidate(guidString);

}

}

else

{

AddFailedToLoadLog(NameOfGuidElementInItem);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfGuidInItemValidate(string guidString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(guidString))

{

if (IsWellFormedUri(guidString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfGuidElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfGuidElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

res = true;

}

 

return (res);

}

 

private bool IsPermaLinkAttributeInGuidInItemValidate(XmlAttribute isPermaLinkAttribute)

{

bool res = false;

 

if (isPermaLinkAttribute != null)

{

string isPermaLinkString = isPermaLinkAttribute.InnerText;

res = InnerTextOfIsPermaLinkInGuidInItemValidate(isPermaLinkString);

}

else

{

AddFailedToLoadLog(NameOfIsPermaLinkAttributeInGuidInItem);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfIsPermaLinkInGuidInItemValidate(string isPermaLinkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(isPermaLinkString))

{

bool temp = false;

if (bool.TryParse(isPermaLinkString, temp))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfIsPermaLinkAttributeInGuidInItem + " attribute of " + NameOfGuidElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfIsPermaLinkAttributeInGuidInItem + " attribute of " + NameOfGuidElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

}

 

return (res);

}

 

private bool EnclosureElementOfItemValidate(XmlElement enclosureElement)

{

bool res = false;

 

if (enclosureElement != null)

{

XmlAttribute urlAttribute = enclosureElement.Attributes[NameOfUrlAttributeInEnclosureInItem];

res = UrlAttributeInEnclosureInItemValidate(urlAttribute);

 

if (res)

{

XmlAttribute lengthAttribute = enclosureElement.Attributes[NameOfLengthAttributeInEnclosureInItem];

res = LengthAttributeInEnclosureInItemValidate(lengthAttribute);

}

 

if (res)

{

XmlAttribute typeAttribute = enclosureElement.Attributes[NameOfTypeAttributeInEnclosureInItem];

res = TypeAttributeInEnclosureInItemValidate(typeAttribute);

}

}

else

{

AddFailedToLoadLog(NameOfEnclosureElementInItem);

res = true;

}

 

return (res);

}

 

#region Required Attributes of Enclosure Element In Item

private bool TypeAttributeInEnclosureInItemValidate(XmlAttribute typeAttribute)

{

throw new Exception("The method or operation is not implemented.");

}

 

private bool LengthAttributeInEnclosureInItemValidate(XmlAttribute lengthAttribute)

{

bool res = false;

 

if (lengthAttribute != null)

{

string lengthString = lengthAttribute.InnerText;

res = InnerTextOfLengthInEnclosureInItemValidate(lengthString);

}

else

{

AddFailedToLoadLog(NameOfLengthAttributeInEnclosureInItem);

}

 

return (res);

}

 

private bool InnerTextOfLengthInEnclosureInItemValidate(string lengthString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(lengthString))

{

if (IsNumeric(lengthString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfLengthAttributeInEnclosureInItem + " attribute in " + NameOfEnclosureElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfLengthAttributeInEnclosureInItem + " attribute in " + NameOfEnclosureElementInItem + " element in " + NameOfItemElementInChannel + " is emtpy.");

res = true;

}

 

return (res);

}

 

private bool UrlAttributeInEnclosureInItemValidate(XmlAttribute urlAttribute)

{

bool res = false;

 

if (urlAttribute != null)

{

string urlString = urlAttribute.InnerText;

res = InnerTextOfUrlAttributeInEnclosureInItemValidate(urlString);

}

else

{

AddFailedToLoadLog(NameOfUrlAttributeInEnclosureInItem);

}

 

return (res);

}

 

private bool InnerTextOfUrlAttributeInEnclosureInItemValidate(string urlString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(res))

{

if (IsWellFormedUri(urlString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfUrlAttributeInEnclosureInItem + " attribute in " + NameOfEnclosureElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfUrlAttributeInEnclosureInItem + " attribute in " + NameOfEnclosureElementInItem + " element in " + NameOfItemElementInChannel + " is emtpy.");

res = true;

}

 

return (res);

}

#endregion

 

private bool commentsElementOfItemValidate(XmlElement commentsElement)

{

if (commentsElement == null)

{

AddFailedToLoadLog(NameOfCommentsElementInItem);

}

return (true);

}

 

private bool CategoryElementOfItemValidate(XmlElement categoryElement)

{

bool res = false;

 

if (categoryElement != null)

{

XmlAttribute domainAttribute = categoryElement.Attributes[NameOfDomainAttributeInCategoryInItem];

if (domainAttribute != null)

{

res = DomainAttributeInCategoryInItemValidate(domainAttribute);

}

else

{

AddFailedToLoadLog(NameOfDomainAttributeInCategoryInItem);

res = true;

}

}

else

{

AddFailedToLoadLog(NameOfCategoryElementInItem);

res = true;

}

 

return (res);

}

 

private bool DomainAttributeInCategoryInItemValidate(XmlAttribute domainAttribute)

{

bool res = false;

 

if (domainAttribute != null)

{

string domainString = domainAttribute.InnerText;

res = IsWellFormedUri(domainString);

}

else

{

AddFailedToLoadLog(NameOfDomainAttributeInCategoryInItem);

res = true;

}

 

return (res);

}

 

private bool AuthorElementOfItemValidate(XmlElement authorElement)

{

if (authorElement == null)

{

AddFailedToLoadLog(NameOfAuthorElementInItem);

}

return (true);

}

 

private bool DescriptionElementOfItemValidate(XmlElement descriptionElement)

{

if (descriptionElement == null)

{

AddFailedToLoadLog(NameOfDescriptionElementInItem);

}

 

return (true);

}

 

private bool LinkElementOfItemValidate(XmlElement linkElement)

{

bool res = false;

 

if (linkElement != null)

{

string linkString = linkElement.InnerText;

res = InnerTextOfLinkInItem(linkString);

}

else

{

AddFailedToLoadLog(NameOfLinkElementInItem);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfLinkInItem(string linkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(linkString))

{

if (IsWellFormedUri(linkString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfLinkElementInItem + " element in " + NameOfItemElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfLinkElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

res = true;

}

 

return (res);

}

 

private bool TitleElementOfItemValidate(XmlElement titleElement)

{

bool res = false;

 

if (titleElement != null)

{

string titleString = titleElement.InnerText;

res = InnerTextOfTitleInItem(titleString);

}

else

{

AddFailedToLoadLog(NameOfItemElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfTitleInItem(string titleString)

{

if (string.IsNullOrEmpty(titleString))

{

AddLog("inner text of " + NameOfTitleElementInItem + " element in " + NameOfItemElementInChannel + " is empty.");

}

 

return (true);

}

#endregion

 

private bool OptionalElementsOfChannelValidate(XmlElement channelElement)

{

bool res = false;

 

if (channelElement != null)

【上篇】
【下篇】

抱歉!评论已关闭.