//============================================================ // ICOMPARABLE IMPLEMENTATION //============================================================ #region CompareTo(object obj) /// <summary> /// Compares the current instance with another object of the same type. /// </summary> /// <param name="obj">An object to compare with this instance.</param> /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns> /// <exception cref="ArgumentException">The <paramref name="obj"/> is not the expected <see cref="Type"/>.</exception> public int CompareTo(object obj) { //------------------------------------------------------------ // If target is a null reference, instance is greater //------------------------------------------------------------ if (obj == null) { return(1); } //------------------------------------------------------------ // Determine comparison result using property state of objects //------------------------------------------------------------ AtomPublishingControlSyndicationExtension value = obj as AtomPublishingControlSyndicationExtension; if (value != null) { int result = String.Compare(this.Description, value.Description, StringComparison.OrdinalIgnoreCase); result = result | Uri.Compare(this.Documentation, value.Documentation, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); result = result | String.Compare(this.Name, value.Name, StringComparison.OrdinalIgnoreCase); result = result | this.Version.CompareTo(value.Version); result = result | String.Compare(this.XmlNamespace, value.XmlNamespace, StringComparison.Ordinal); result = result | String.Compare(this.XmlPrefix, value.XmlPrefix, StringComparison.Ordinal); result = result | AtomPublishingUtility.CompareCommonObjectAttributes(this.Context, value.Context); result = result | this.Context.IsDraft.CompareTo(value.Context.IsDraft); return(result); } else { throw new ArgumentException(String.Format(null, "obj is not of type {0}, type was found to be '{1}'.", this.GetType().FullName, obj.GetType().FullName), "obj"); } }
/// <summary> /// Initializes the syndication extension context using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <b>XPathNavigator</b> used to load this <see cref="AtomPublishingControlSyndicationExtensionContext"/>.</param> /// <param name="manager">The <see cref="XmlNamespaceManager"/> object used to resolve prefixed syndication extension elements and attributes.</param> /// <returns><b>true</b> if the <see cref="AtomPublishingControlSyndicationExtensionContext"/> was able to be initialized using the supplied <paramref name="source"/>; otherwise <b>false</b>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="manager"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source, XmlNamespaceManager manager) { bool wasLoaded = false; Guard.ArgumentNotNull(source, "source"); Guard.ArgumentNotNull(manager, "manager"); if (AtomPublishingUtility.FillCommonObjectAttributes(this, source)) { wasLoaded = true; } if (source.HasChildren) { XPathNavigator draftNavigator = source.SelectSingleNode("app:draft", manager); if (draftNavigator != null && !String.IsNullOrEmpty(draftNavigator.Value)) { if (String.Compare(draftNavigator.Value, "yes", StringComparison.OrdinalIgnoreCase) == 0) { this.IsDraft = true; wasLoaded = true; } else if (String.Compare(draftNavigator.Value, "no", StringComparison.OrdinalIgnoreCase) == 0) { this.IsDraft = false; wasLoaded = true; } else { this.IsDraft = false; } } } return(wasLoaded); }
/// <summary> /// Writes the current context to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <b>XmlWriter</b> to which you want to write the current context.</param> /// <param name="xmlNamespace">The XML namespace used to qualify prefixed syndication extension elements and attributes.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is an empty string.</exception> public void WriteTo(XmlWriter writer, string xmlNamespace) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNullOrEmptyString(xmlNamespace, "xmlNamespace"); //------------------------------------------------------------ // Write current extension details to the writer //------------------------------------------------------------ writer.WriteStartElement("control", xmlNamespace); AtomPublishingUtility.WriteCommonObjectAttributes(this, writer); if (this.IsDraft) { writer.WriteElementString("draft", xmlNamespace, "yes"); } //------------------------------------------------------------ // Write the syndication extensions of the current instance //------------------------------------------------------------ SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
/// <summary> /// Modifies the <see cref="IAtomPublishingCommonObjectAttributes"/> to match the data source. /// </summary> /// <param name="target">The object that implements the <see cref="IAtomPublishingCommonObjectAttributes"/> interface to be filled.</param> /// <param name="source">The <see cref="XPathNavigator"/> to extract Atom common attribute information from.</param> /// <returns><b>true</b> if the <paramref name="target"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="target"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public static bool FillCommonObjectAttributes(IAtomPublishingCommonObjectAttributes target, XPathNavigator source) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(target, "target"); Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Initialize XML namespace resolver //------------------------------------------------------------ XmlNamespaceManager manager = AtomPublishingUtility.CreateNamespaceManager(source.NameTable); //------------------------------------------------------------ // Attempt to extract xml:base attribute information //------------------------------------------------------------ string xmlBaseAttribute = source.GetAttribute("base", manager.LookupNamespace("xml")); if (!String.IsNullOrEmpty(xmlBaseAttribute)) { Uri baseUri; if (Uri.TryCreate(xmlBaseAttribute, UriKind.RelativeOrAbsolute, out baseUri)) { target.BaseUri = baseUri; wasLoaded = true; } } //------------------------------------------------------------ // Attempt to extract xml:lang attribute information //------------------------------------------------------------ string xmlLangAttribute = source.GetAttribute("lang", manager.LookupNamespace("xml")); if (!String.IsNullOrEmpty(xmlLangAttribute)) { try { CultureInfo language = new CultureInfo(source.XmlLang); target.Language = language; wasLoaded = true; } catch (ArgumentException) { System.Diagnostics.Trace.TraceWarning("Unable to determine CultureInfo with a name of {0}.", source.XmlLang); } } return(wasLoaded); }
/// <summary> /// Writes the current context to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <b>XmlWriter</b> to which you want to write the current context.</param> /// <param name="xmlNamespace">The XML namespace used to qualify prefixed syndication extension elements and attributes.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is an empty string.</exception> public void WriteTo(XmlWriter writer, string xmlNamespace) { Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNullOrEmptyString(xmlNamespace, "xmlNamespace"); writer.WriteStartElement("control", xmlNamespace); AtomPublishingUtility.WriteCommonObjectAttributes(this, writer); if (this.IsDraft) { writer.WriteElementString("draft", xmlNamespace, "yes"); } SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source, XmlNamespaceManager manager) /// <summary> /// Initializes the syndication extension context using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <b>XPathNavigator</b> used to load this <see cref="AtomPublishingControlSyndicationExtensionContext"/>.</param> /// <param name="manager">The <see cref="XmlNamespaceManager"/> object used to resolve prefixed syndication extension elements and attributes.</param> /// <returns><b>true</b> if the <see cref="AtomPublishingControlSyndicationExtensionContext"/> was able to be initialized using the supplied <paramref name="source"/>; otherwise <b>false</b>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="manager"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source, XmlNamespaceManager manager) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); Guard.ArgumentNotNull(manager, "manager"); //------------------------------------------------------------ // Attempt to extract common attributes information //------------------------------------------------------------ if (AtomPublishingUtility.FillCommonObjectAttributes(this, source)) { wasLoaded = true; } //------------------------------------------------------------ // Attempt to extract syndication extension information //------------------------------------------------------------ if (source.HasChildren) { XPathNavigator draftNavigator = source.SelectSingleNode("app:draft", manager); if (draftNavigator != null && !String.IsNullOrEmpty(draftNavigator.Value)) { if (String.Compare(draftNavigator.Value, "yes", StringComparison.OrdinalIgnoreCase) == 0) { this.IsDraft = true; wasLoaded = true; } else if (String.Compare(draftNavigator.Value, "no", StringComparison.OrdinalIgnoreCase) == 0) { this.IsDraft = false; wasLoaded = true; } else { this.IsDraft = false; } } } return(wasLoaded); }