Gavin.Lv.

唯刀百辟 唯心不易

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一段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)

{

XmlElement languageElement = channelElement[NameOfLanguageElementInChannel];

res = LanguageElementOfChannelValidate(languageElement);

 

if (res)

{

XmlElement copyrightElement = channelElement[NameOfCopyrightElementInChannel];

res = CopyrightElementOfChannelValidate(copyrightElement);

}

 

if (res)

{

XmlElement managingEditorElement = channelElement[NameOfManagingEditorElementInChannel];

res = ManagingEditorElementOfChannelValidate(managingEditorElement);

}

 

if (res)

{

XmlElement webMasterElement = channelElement[NameOfWebMasterElementInChannel];

res = WebMasterElementOfChannelValidate(webMasterElement);

}

 

if (res)

{

XmlElement pubDateElement = channelElement[NameOfPubDateElementInChannel];

res = PubDateElementOfChannelValidate(pubDateElement);

}

 

if (res)

{

XmlElement lastBuildDateElement = channelElement[NameOfLastBuildDateElementInChannel];

res = LastBuildDateElementOfChannelValidate(lastBuildDateElement);

}

 

if (res)

{

XmlElement categoryElement = channelElement[NameOfCategoryElementInChannel];

res = CategoryElementOfChannelValidate(categoryElement);

}

 

if (res)

{

XmlElement generatorElement = channelElement[NameOfGeneratorElementInChannel];

res = GeneratorElementOfChannelValidate(generatorElement);

}

 

if (res)

{

XmlElement docsElement = channelElement[NameOfDocsElementInChannel];

res = DocsElementOfChannelValidate(docsElement);

}

 

if (res)

{

XmlElement cloudElement = channelElement[NameOfCloudElementInChannel];

res = CloudElementOfChannelValidate(cloudElement);

}

 

if (res)

{

XmlElement ttlElement = channelElement[NameOfTtlElementInChannel];

res = TtlElementOfChannelValidate(ttlElement);

}

 

if (res)

{

XmlElement imageElement = channelElement[NameOfImageElementInChannel];

res = ImageElementOfChannelValidate(imageElement);

}

 

if (res)

{

XmlElement ratingElement = channelElement[NameOfRatingElementInChannel];

res = RatingElementOfChannelValidate(ratingElement);

}

 

if (res)

{

XmlElement textInputElement = channelElement[NameOfTextInputElementInChannel];

res = TextInputElementOfChannelValidate(textInputElement);

}

 

if (res)

{

XmlElement skipHoursElement = channelElement[NameOfSkipHoursElementInChannel];

res = SkipHoursElementOfChannelValidate(skipHoursElement);

}

 

if (res)

{

XmlElement skipDaysElement = channelElement[NameOfSkipDaysElementInChannel];

res = SkipDaysElementOfChannelValidate(skipDaysElement);

}

}

else

{

AddFailedToLoadLog(NameOfChannelElementInRss);

}

return (res);

}

 

#region Optional Elements Of Channel Validate

private bool SkipDaysElementOfChannelValidate(XmlElement skipDaysElement)

{

bool res = false;

 

if (skipDaysElement != null)

{

string skipDaysString = skipDaysElement.InnerText;

res = InnerTextOfSkipDaysValidate(skipDaysString);

}

else

{

AddFailedToLoadLog(NameOfSkipDaysElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfSkipDaysValidate(string skipDaysString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(skipDaysString))

{

if (IsNumeric(skipDaysString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfSkipDaysElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfSkipDaysElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool SkipHoursElementOfChannelValidate(XmlElement skipHoursElement)

{

bool res = false;

 

if (skipHoursElement != null)

{

string skipHoursString = skipHoursElement.InnerText;

res = InnerTextOfSkipHoursValidate(skipHoursString);

}

else

{

AddFailedToLoadLog(NameOfSkipHoursElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfSkipHoursValidate(string skipHoursString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(skipHoursString))

{

if (IsNumeric(skipHoursString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfSkipHoursElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfSkipHoursElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool TextInputElementOfChannelValidate(XmlElement textInputElement)

{

bool res = false;

 

if (textInputElement != null)

{

XmlElement titleElement = textInputElement[NameOfTitleElementInTextInput];

res = TitleElementOfTextInputValidate(titleElement);

 

if (res)

{

XmlElement descriptionElement = textInputElement[NameOfDescriptionElementInTextInput];

res = DescriptionElementOfTextInputValidate(descriptionElement);

}

 

if (res)

{

XmlElement nameElement = textInputElement[NameOfNameElementInTextInput];

res = NameElementOfTextInputValidate(nameElement);

}

 

if (res)

{

XmlElement linkElement = textInputElement[NameOfLinkElementInTextInput];

res = LinkElementOfTextInputValidate(linkElement);

}

}

else

{

AddFailedToLoadLog(NameOfTextInputElementInChannel);

res = true;

}

 

return (res);

}

 

#region Required Elements Of TextInput Validate

private bool LinkElementOfTextInputValidate(XmlElement linkElement)

{

bool res = false;

 

if (linkElement != null)

{

string linkString = linkElement.InnerText;

res = InnerTextOfLinkInTextInput(linkString);

}

else

{

AddFailedToLoadLog(NameOfLinkElementInTextInput);

}

 

return (res);

}

 

private bool InnerTextOfLinkInTextInput(string linkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(linkString))

{

if (IsWellFormedUri(linkString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfLinkElementInTextInput + " element in " + NameOfTextInputElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfLinkElementInTextInput + " element in " + NameOfTextInputElementInChannel + " is empty.");

res = true;

}

 

return (res);

}

 

private bool NameElementOfTextInputValidate(XmlElement nameElement)

{

bool res = false;

 

if (nameElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfNameElementInTextInput);

}

 

return (res);

}

 

private bool DescriptionElementOfTextInputValidate(XmlElement descriptionElement)

{

bool res = false;

 

if (descriptionElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfDescriptionElementInTextInput);

}

 

return (res);

}

 

private bool TitleElementOfTextInputValidate(XmlElement titleElement)

{

bool res = false;

 

if (titleElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfTitleElementInTextInput);

}

 

return (res);

}

#endregion

 

private bool RatingElementOfChannelValidate(XmlElement ratingElement)

{

bool res = false;

 

if (ratingElement != null)

{

string ratingString = ratingElement.InnerText;

res = InnerTextOfRatingValidate(ratingString);

}

else

{

AddFailedToLoadLog(NameOfRatingElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfRatingValidate(string ratingString)

{

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

}

 

private bool ImageElementOfChannelValidate(XmlElement imageElement)

{

bool res = false;

 

if (imageElement != null)

{

res = RequiredElementsOfImageValidate(imageElement);

 

if (res)

{

res = OptionalElementsOfImageValidate(imageElement);

}

}

else

{

AddFailedToLoadLog(NameOfImageElementInChannel);

}

return (res);

}

 

private bool OptionalElementsOfImageValidate(XmlElement imageElement)

{

bool res = false;

 

if (imageElement != null)

{

XmlAttribute widthElement = imageElement[NameOfWidthElementInImage];

res = WidthElementOfImageValidate(widthElement);

 

if (res)

{

XmlAttribute heightElement = imageElement[NameOfHeightElementInImage];

res = HeightElementOfImageValidate(heightElement);

}

}

else

{

AddFailedToLoadLog(NameOfImageElementInChannel);

}

 

return (res);

}

 

#region Optional Elements Of Image Validate

private bool HeightElementOfImageValidate(XmlAttribute heightElement)

{

bool res = false;

 

if (heightElement != null)

{

string heightString = heightElement.InnerText;

res = InnerTextOfHeightInImageValidate(heightString);

}

else

{

AddFailedToLoadLog(NameOfHeightElementInImage);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfHeightInImageValidate(string heightString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(heightString))

{

if (IsNumeric(heightString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfHeightElementInImage + " element in " + NameOfImageElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfHeightElementInImage + " element in " + NameOfImageElementInChannel + " is empty.");

}

 

return (res);

}

 

private bool WidthElementOfImageValidate(XmlAttribute widthElement)

{

bool res = false;

 

if (widthElement != null)

{

string widthString = widthElement.InnerText;

res = InnerTextOfWidthInImageValidate(widthString);

}

else

{

AddFailedToLoadLog(NameOfWidthElementInImage);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfWidthInImageValidate(string widthString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(widthString))

{

if (IsNumeric(widthString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfWidthElementInImage + " element in " + NameOfImageElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfWidthElementInImage + " element in " + NameOfImageElementInChannel + " is empty.");

}

 

return (res);

}

#endregion

 

private bool RequiredElementsOfImageValidate(XmlElement imageElement)

{

bool res = false;

 

if (imageElement != null)

{

XmlElement urlElement = imageElement[NameOfUrlElementInImage];

res = UrlElementOfImageValidate(urlElement);

 

if (res)

{

XmlElement titleElement = imageElement[NameOfTitleElementInImage];

res = TitleElementOfImageValidate(titleElement);

}

 

if (res)

{

XmlElement linkElement = imageElement[NameOfLinkElementInImage];

res = LinkElementOfImageValidate(linkElement);

}

}

else

{

AddFailedToLoadLog(NameOfImageElementInChannel);

}

 

return (res);

}

 

#region Required Elements Of Image Validate

private bool LinkElementOfImageValidate(XmlElement linkElement)

{

bool res = false;

 

if (linkElement != null)

{

string linkString = linkElement.InnerText;

res = InnerTextOfLinkInImageValidate(linkString);

}

else

{

AddFailedToLoadLog(NameOfLinkElementInImage);

}

 

return (res);

}

 

private bool InnerTextOfLinkInImageValidate(string linkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(linkString))

{

if (IsWellFormedUri(linkString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfLinkElementInImage + " element in " + NameOfImageElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfLinkElementInImage + " element in " + NameOfImageElementInChannel + " is empty.");

}

 

return (res);

}

 

private bool TitleElementOfImageValidate(XmlElement titleElement)

{

bool res = false;

 

if (titleElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfTitleElementInImage);

}

 

return (res);

}

 

private bool UrlElementOfImageValidate(XmlElement urlElement)

{

bool res = false;

 

if (urlElement != null)

{

string urlString = urlElement.InnerText;

res = InnerTextOfUrlInImage(urlString);

}

else

{

AddFailedToLoadLog(NameOfUrlElementInImage);

}

 

return (res);

}

 

private bool InnerTextOfUrlInImage(string urlString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(urlString))

{

if (IsWellFormedUri(urlString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfUrlElementInImage + " element in " + NameOfImageElementInChannel + ".");

}

}

else

{

AddLog("inner text of " + NameOfUrlElementInImage + " element in " + NameOfImageElementInChannel + " is empty.");

}

 

return (res);

}

#endregion

 

private bool TtlElementOfChannelValidate(XmlElement ttlElement)

{

bool res = false;

 

if (ttlElement != null)

{

string ttlString = ttlElement.InnerText;

res = InnerTextOfTtlElementValidate(ttlString);

}

else

{

AddFailedToLoadLog(NameOfTtlElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfTtlElementValidate(string ttlString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(ttlString))

{

if (IsNumeric(ttlString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfTtlElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfTtlElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool IsNumeric(string value)

{

bool res = false;

 

int temp = 0;

res = int.TryParse(value, temp);

 

return (res);

}

 

private bool CloudElementOfChannelValidate(XmlElement cloudElement)

{

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

}

 

private bool DocsElementOfChannelValidate(XmlElement docsElement)

{

bool res = false;

 

if (docsElement != null)

{

string docsString = docsElement.InnerText;

res = InnerTextOfDocsValidate(docsString);

}

else

{

AddFailedToLoadLog(NameOfDocsElementInChannel);

res = true;

}

return (res);

}

 

private bool InnerTextOfDocsValidate(string docsString)

{

bool res = false;

 

if (!string.IsInterned(docsString))

{

if (Uri.IsWellFormedUriString(docsString, UriKind.RelativeOrAbsolute))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfDocsElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfDocsElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool GeneratorElementOfChannelValidate(XmlElement generatorElement)

{

if (generatorElement == null)

{

AddFailedToLoadLog(NameOfGeneratorElementInChannel);

}

return (true);

}

 

private bool CategoryElementOfChannelValidate(XmlElement categoryElement)

{

if (categoryElement == null)

{

AddFailedToLoadLog(NameOfCategoryElementInChannel);

}

return (true);

}

 

private bool LastBuildDateElementOfChannelValidate(XmlElement lastBuildDateElement)

{

bool res = false;

 

if (lastBuildDateElement != null)

{

string lastBuildDateString = lastBuildDateElement.InnerText;

res = InnertTextOfLastBuidDate(lastBuildDateString);

}

else

{

AddFailedToLoadLog(NameOfLastBuildDateElementInChannel);

}

 

return (res);

}

 

private bool InnertTextOfLastBuidDate(string lastBuildDateString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(pubDateString))

{

if (UTCTimeValidate(pubDateString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfLastBuildDateElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfLastBuildDateElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool PubDateElementOfChannelValidate(XmlElement pubDateElement)

{

bool res = false;

 

if (pubDateElement != null)

{

string pubDateString = pubDateElement.InnerText;

res = InnerTextOfPubDateInChannel(pubDateString);

}

else

{

AddFailedToLoadLog(NameOfPubDateElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfPubDateInChannel(string pubDateString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(pubDateString))

{

if (UTCTimeValidate(pubDateString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfPubDateElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfPubDateElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool UTCTimeValidate(string pubDateString)

{

bool res = false;

 

DateTime pubDate = null;

try

{

pubDate = DateTime.Parse(pubDateString);

}

catch (FormatException ex)

{

AddLog(ex.ToString());

res = false;

}

catch (ArgumentNullException ex)

{

AddLog(ex.ToString());

res = false;

}

catch (ArgumentException ex)

{

AddLog(ex.ToString());

res = false;

}

 

if (pubDate != null)

{

res = (pubDate.Kind == DateTimeKind.Utc);

}

return res;

}

 

private bool WebMasterElementOfChannelValidate(XmlElement webMasterElement)

{

bool res = false;

 

if (webMasterElement != null)

{

string webMasterString = webMasterElement.InnerText;

res = InnerTextOfWebMasterValidate(webMasterString);

}

else

{

AddFailedToLoadLog(NameOfWebMasterElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfWebMasterValidate(string webMasterString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(webMasterString))

{

if (Regex.IsMatch(webMasterString, EmailRegexString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfWebMasterElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfWebMasterElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool ManagingEditorElementOfChannelValidate(XmlElement managingEditorElement)

{

bool res = false;

 

if (managingEditorElement != null)

{

string managingEditorString = managingEditorElement.InnerText;

res = InnerTextOfManagingEditorValidate(managingEditorString);

}

else

{

AddFailedToLoadLog(NameOfManagingEditorElementInChannel);

res = true;

}

 

return (res);

}

 

private bool InnerTextOfManagingEditorValidate(string managingEditorString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(managingEditorString))

{

if (Regex.IsMatch(managingEditorString, EmailRegexString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfManagingEditorElementInChannel + " element in " + NameOfChannelElementInRss + ".");

}

}

else

{

AddLog("inner text of " + NameOfManagingEditorElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

 

private bool CopyrightElementOfChannelValidate(XmlElement copyrightElement)

{

if (copyrightElement == null)

{

AddFailedToLoadLog(NameOfCopyrightElementInChannel);

}

 

return (true);

}

 

private bool LanguageElementOfChannelValidate(XmlElement languageElement)

{

bool res = false;

 

if (languageElement != null)

{

string languageString = languageElement.InnerText;

res = InnerTextOfLanguateValidate(languageString);

}

else

{

AddFailedToLoadLog(NameOfLanguageElementInChannel);

}

 

return (res);

}

 

private bool InnerTextOfLanguateValidate(string languageString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(languageString))

{

string[] allowValues = new string[] {"af","sq ","eu","be","bg","ca","zh-cn","zh-tw","hr",

"cs","da","nl","nl-be","nl-nl","en","en-au","en-bz","en-ca","en-ie","en-jm","en-nz",

"en-ph","en-za","en-tt","en-gb","en-us","en-zw","et","fo","fi","fr","fr-be","fr-ca",

"fr-fr","fr-lu","fr-mc","fr-ch","gl","gd","de","de-at","de-de","de-li","de-lu","de-ch",

"el","haw","hu","is","in","ga","it","it-it","it-ch","ja","ko","mk","no","pl","pt",

"pt-br","pt-pt","ro","ro-mo","ro-ro","ru","ru-mo","ru-ru","sr","sk","sl","es","es-ar",

"es-bo","es-cl","es-co","es-cr","es-do","es-ec","es-sv","es-gt","es-hn","es-mx",

"es-ni","es-pa","es-py","es-pe","es-pr","es-es","es-uy","es-ve","sv","sv-fi",

"sv-se","tr","uk"};

StringCollection allowValueCollection = new StringCollection();

allowValueCollection.AddRange(allowValues);

 

if (allowValueCollection.Contains(languageString))

{

res = true;

}

else

{

AddLog("inner text of " + NameOfLanguageElementInChannel + " element in " + NameOfChannelElementInRss + " is not allowed.");

}

}

else

{

AddLog("inner text of " + NameOfLanguageElementInChannel + " element in " + NameOfChannelElementInRss + " is empty.");

res = true;

}

 

return (res);

}

#endregion

 

private bool RequiredElementsOfChannelValidate(XmlElement channelElement)

{

bool res = false;

 

if (channelElement != null)

{

XmlElement titleElement = channelElement[NameOfTitleElementInChannel];

res = TitleElementOfChannelValidate(titleElement);

 

if (res)

{

XmlElement linkElement = channelElement[NameOfLinkElementInChannel];

res = LinkElementOfChannelValidate(linkElement);

}

 

if (res)

{

XmlElement descriptionElement = channelElement[NameOfDescriptionElementInChannel];

res = DescriptionElementOfChannelValidate(descriptionElement);

}

}

else

{

AddFailedToLoadLog(NameOfChannelElementInRss);

}

return (res);

}

 

#region Required Element Of Channel Validate

private bool DescriptionElementOfChannelValidate(XmlElement descriptionElement)

{

bool res = false;

 

if (descriptionElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfDescriptionElementInChannel);

}

 

return (res);

}

 

private bool LinkElementOfChannelValidate(XmlElement linkElement)

{

bool res = false;

 

if (linkElement != null)

{

string linkString = linkElement.InnerText;

res = InnerTextOfLinkInChannel(linkString);

}

else

{

AddFailedToLoadLog(NameOfLinkElementInChannel);

}

 

return (res);

}

 

private bool InnerTextOfLinkInChannel(string linkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(linkString))

{

if (IsWellFormedUri(linkString))

{

res = true;

}

else

{

AddLog("failed to format inner text of " + NameOfChannelElementInRss + " element.");

}

}

else

{

AddLog("inner text of " + NameOfChannelElementInRss + " element is empty.");

res = true;

}

 

return (res);

}

 

private bool IsWellFormedUri(string linkString)

{

bool res = false;

 

if (!string.IsNullOrEmpty(linkString))

{

res = Uri.IsWellFormedUriString(linkString, UriKind.RelativeOrAbsolute);

}

else

{

res = true;

}

 

return (res);

}

 

private bool TitleElementOfChannelValidate(XmlElement titleElement)

{

bool res = false;

 

if (titleElement != null)

{

res = true;

}

else

{

AddFailedToLoadLog(NameOfTitleElementInChannel);

}

 

return (res);

}

#endregion

}

}

posted on 2007-01-09 09:23  Gavin.Lv.  阅读(627)  评论(0)    收藏  举报