/// <summary> /// Saves the current <see cref="AtomTextConstruct"/> to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to which you want to save.</param> /// <param name="elementName">The local name of the text construct being written.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="elementName"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="elementName"/> is an empty string.</exception> public void WriteTo(XmlWriter writer, string elementName) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNullOrEmptyString(elementName, "elementName"); //------------------------------------------------------------ // Write XML representation of the current instance //------------------------------------------------------------ writer.WriteStartElement(elementName, AtomUtility.AtomNamespace); AtomUtility.WriteCommonObjectAttributes(this, writer); if (this.TextType == AtomTextConstructType.Xhtml && String.IsNullOrEmpty(writer.LookupPrefix(AtomUtility.XhtmlNamespace))) { writer.WriteAttributeString("xmlns", "xhtml", null, AtomUtility.XhtmlNamespace); } if (this.TextType != AtomTextConstructType.None) { writer.WriteAttributeString("type", AtomTextConstruct.ConstructTypeAsString(this.TextType)); } if (this.TextType == AtomTextConstructType.Xhtml) { writer.WriteStartElement("div", AtomUtility.XhtmlNamespace); writer.WriteString(this.Content); writer.WriteEndElement(); } else { writer.WriteString(this.Content); } //------------------------------------------------------------ // Write the syndication extensions of the current instance //------------------------------------------------------------ SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
/// <summary> /// Saves the current <see cref="AtomIcon"/> to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to which you want to save.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> public void WriteTo(XmlWriter writer) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); //------------------------------------------------------------ // Write XML representation of the current instance //------------------------------------------------------------ writer.WriteStartElement("icon", AtomUtility.AtomNamespace); AtomUtility.WriteCommonObjectAttributes(this, writer); writer.WriteString(this.Uri != null ? this.Uri.ToString() : String.Empty); //------------------------------------------------------------ // Write the syndication extensions of the current instance //------------------------------------------------------------ SyndicationExtensionAdapter.WriteExtensionsTo(this.Extensions, writer); writer.WriteEndElement(); }
//============================================================ // 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 //------------------------------------------------------------ AtomLink value = obj as AtomLink; if (value != null) { int result = this.Length.CompareTo(value.Length); result = result | String.Compare(this.ContentType, value.ContentType, StringComparison.OrdinalIgnoreCase); result = result | String.Compare(this.Relation, value.Relation, StringComparison.OrdinalIgnoreCase); string sourceLanguageName = this.ContentLanguage != null ? this.ContentLanguage.Name : String.Empty; string targetLanguageName = value.ContentLanguage != null ? value.ContentLanguage.Name : String.Empty; result = result | String.Compare(sourceLanguageName, targetLanguageName, StringComparison.OrdinalIgnoreCase); result = result | String.Compare(this.Title, value.Title, StringComparison.OrdinalIgnoreCase); result = result | Uri.Compare(this.Uri, value.Uri, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); result = result | AtomUtility.CompareCommonObjectAttributes(this, value); 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"); } }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source) /// <summary> /// Loads this <see cref="AtomContent"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <returns><b>true</b> if the <see cref="AtomContent"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="AtomContent"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Initialize XML namespace resolver //------------------------------------------------------------ XmlNamespaceManager manager = AtomUtility.CreateNamespaceManager(source.NameTable); //------------------------------------------------------------ // Attempt to extract common attributes information //------------------------------------------------------------ if (AtomUtility.FillCommonObjectAttributes(this, source)) { wasLoaded = true; } //------------------------------------------------------------ // Attempt to extract syndication information //------------------------------------------------------------ if (source.HasAttributes) { string typeAttribute = source.GetAttribute("type", String.Empty); string sourceAttribute = source.GetAttribute("src", String.Empty); if (!String.IsNullOrEmpty(typeAttribute)) { this.ContentType = typeAttribute; wasLoaded = true; } if (!String.IsNullOrEmpty(sourceAttribute)) { Uri src; if (Uri.TryCreate(sourceAttribute, UriKind.RelativeOrAbsolute, out src)) { this.Source = src; wasLoaded = true; } } } if (String.Compare(this.ContentType, "xhtml", StringComparison.OrdinalIgnoreCase) == 0) { XPathNavigator xhtmlDivNavigator = source.SelectSingleNode("xhtml:div", manager); if (xhtmlDivNavigator != null && !String.IsNullOrEmpty(xhtmlDivNavigator.Value)) { this.Content = xhtmlDivNavigator.InnerXml; wasLoaded = true; } } else if (!String.IsNullOrEmpty(source.Value)) { this.Content = source.Value; wasLoaded = true; } return(wasLoaded); }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source) /// <summary> /// Loads this <see cref="AtomTextConstruct"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <returns><b>true</b> if the <see cref="AtomTextConstruct"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="AtomTextConstruct"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Initialize XML namespace resolver //------------------------------------------------------------ XmlNamespaceManager manager = AtomUtility.CreateNamespaceManager(source.NameTable); //------------------------------------------------------------ // Attempt to extract common attributes information //------------------------------------------------------------ if (AtomUtility.FillCommonObjectAttributes(this, source)) { wasLoaded = true; } //------------------------------------------------------------ // Attempt to extract syndication information //------------------------------------------------------------ if (source.HasAttributes) { string typeAttribute = source.GetAttribute("type", String.Empty); if (!String.IsNullOrEmpty(typeAttribute)) { AtomTextConstructType type = AtomTextConstruct.ConstructTypeByName(typeAttribute); if (type != AtomTextConstructType.None) { this.TextType = type; wasLoaded = true; } } } if (this.TextType == AtomTextConstructType.Xhtml) { XPathNavigator xhtmlDivNavigator = source.SelectSingleNode("xhtml:div", manager); if (xhtmlDivNavigator != null && !String.IsNullOrEmpty(xhtmlDivNavigator.Value)) { this.Content = xhtmlDivNavigator.Value; wasLoaded = true; } } else if (this.TextType == AtomTextConstructType.Html && !String.IsNullOrEmpty(source.InnerXml)) { this.Content = source.InnerXml; wasLoaded = true; } else if (!String.IsNullOrEmpty(source.Value)) { this.Content = source.Value; wasLoaded = true; } return(wasLoaded); }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source) /// <summary> /// Loads this <see cref="AtomLink"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <returns><b>true</b> if the <see cref="AtomLink"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="AtomLink"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Attempt to extract common attributes information //------------------------------------------------------------ if (AtomUtility.FillCommonObjectAttributes(this, source)) { wasLoaded = true; } //------------------------------------------------------------ // Attempt to extract syndication information //------------------------------------------------------------ if (source.HasAttributes) { string hrefAttribute = source.GetAttribute("href", String.Empty); string relAttribute = source.GetAttribute("rel", String.Empty); string typeAttribute = source.GetAttribute("type", String.Empty); string hreflangAttribute = source.GetAttribute("hreflang", String.Empty); string titleAttribute = source.GetAttribute("title", String.Empty); string lengthAttribute = source.GetAttribute("length", String.Empty); if (!String.IsNullOrEmpty(hrefAttribute)) { Uri href; if (Uri.TryCreate(hrefAttribute, UriKind.RelativeOrAbsolute, out href)) { this.Uri = href; wasLoaded = true; } } if (!String.IsNullOrEmpty(relAttribute)) { this.Relation = relAttribute; wasLoaded = true; } if (!String.IsNullOrEmpty(typeAttribute)) { this.ContentType = typeAttribute; wasLoaded = true; } if (!String.IsNullOrEmpty(hreflangAttribute)) { try { CultureInfo language = new CultureInfo(hreflangAttribute); this.ContentLanguage = language; wasLoaded = true; } catch (ArgumentException) { System.Diagnostics.Trace.TraceWarning("AtomLink unable to determine CultureInfo with a name of {0}.", source.XmlLang); } } if (!String.IsNullOrEmpty(titleAttribute)) { this.Title = titleAttribute; wasLoaded = true; } if (!String.IsNullOrEmpty(lengthAttribute)) { long length; if (Int64.TryParse(lengthAttribute, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out length)) { this.Length = length; wasLoaded = true; } } } return(wasLoaded); }