//============================================================ // 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 //------------------------------------------------------------ XmlRpcScalarValue value = obj as XmlRpcScalarValue; if (value != null) { int result = String.Compare(this.ToString(), value.ToString(), StringComparison.Ordinal); 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> /// Saves the current <see cref="XmlRpcScalarValue"/> 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("value"); if (this.ValueType != XmlRpcScalarValueType.None) { writer.WriteStartElement(XmlRpcClient.ScalarTypeAsString(this.ValueType)); writer.WriteString(XmlRpcScalarValue.ValueAsString(this.ValueType, this.Value)); writer.WriteEndElement(); } else { writer.WriteString(this.Value != null ? this.Value.ToString() : String.Empty); } writer.WriteEndElement(); }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source) /// <summary> /// Loads this <see cref="XmlRpcScalarValue"/> 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="XmlRpcScalarValue"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// <para>This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="XmlRpcScalarValue"/>.</para> /// </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 scalar parameter information //------------------------------------------------------------ if (source.HasChildren) { if (source.MoveToFirstChild()) { XmlRpcScalarValueType type = XmlRpcScalarValueType.None; if (String.Compare(source.Name, "i4", StringComparison.OrdinalIgnoreCase) == 0) { // Framework prefers the <int> designator for integers, so this handles when the <i4> designator is utilized. type = XmlRpcScalarValueType.Integer; } else { type = XmlRpcClient.ScalarTypeByName(source.Name); } if (type != XmlRpcScalarValueType.None) { this.ValueType = type; if (!String.IsNullOrEmpty(source.Value)) { this.Value = XmlRpcScalarValue.StringAsValue(type, source.Value); } wasLoaded = true; } } } else { if (!String.IsNullOrEmpty(source.Value)) { this.Value = source.Value; wasLoaded = true; } } return(wasLoaded); }
/// <summary> /// Saves the current <see cref="XmlRpcScalarValue"/> 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) { Guard.ArgumentNotNull(writer, "writer"); writer.WriteStartElement("value"); if (this.ValueType != XmlRpcScalarValueType.None) { writer.WriteStartElement(XmlRpcClient.ScalarTypeAsString(this.ValueType)); writer.WriteString(XmlRpcScalarValue.ValueAsString(this.ValueType, this.Value)); writer.WriteEndElement(); } else { writer.WriteString(this.Value != null ? this.Value.ToString() : String.Empty); } writer.WriteEndElement(); }
/// <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 (obj == null) { return(1); } XmlRpcScalarValue value = obj as XmlRpcScalarValue; if (value != null) { int result = String.Compare(this.ToString(), value.ToString(), StringComparison.Ordinal); 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> /// Constructs a new <see cref="IXmlRpcValue"/> object from the specified <see cref="XPathNavigator"/>. /// Parameters specify the XML data source and the variable where the new <see cref="IXmlRpcValue"/> object is returned. /// </summary> /// <param name="source">A <see cref="XPathNavigator"/> that represents the XML data source to be parsed.</param> /// <param name="value"> /// When this method returns, contains an object that represents the <see cref="IXmlRpcValue"/> specified by the <paramref name="source"/>, or <b>null</b> if the conversion failed. /// This parameter is passed uninitialized. /// </param> /// <returns> /// <b>true</b> if <paramref name="source"/> was converted successfully; otherwise, <b>false</b>. /// This operation returns <b>false</b> if the <paramref name="source"/> parameter is a null reference (Nothing in Visual Basic), /// or represents XML data that is not in the expected format. /// </returns> /// <remarks> /// The <paramref name="source"/> is expected to represent an XML-RPC <b>value</b> node. /// </remarks> public static bool TryParseValue(XPathNavigator source, out IXmlRpcValue value) { if (source == null || String.Compare(source.Name, "value", StringComparison.OrdinalIgnoreCase) != 0) { value = null; return(false); } if (source.HasChildren) { XPathNavigator navigator = source.CreateNavigator(); if (navigator.MoveToFirstChild()) { if (String.Compare(navigator.Name, "i4", StringComparison.OrdinalIgnoreCase) == 0) { int scalar; if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return(true); } } else if (String.Compare(navigator.Name, "int", StringComparison.OrdinalIgnoreCase) == 0) { int scalar; if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return(true); } } else if (String.Compare(navigator.Name, "boolean", StringComparison.OrdinalIgnoreCase) == 0) { bool scalar; if (XmlRpcClient.TryParseBoolean(navigator.Value, out scalar)) { value = new XmlRpcScalarValue(scalar); return(true); } } else if (String.Compare(navigator.Name, "string", StringComparison.OrdinalIgnoreCase) == 0) { value = new XmlRpcScalarValue(navigator.Value); return(true); } else if (String.Compare(navigator.Name, "double", StringComparison.OrdinalIgnoreCase) == 0) { double scalar; if (Double.TryParse(navigator.Value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return(true); } } else if (String.Compare(navigator.Name, "dateTime.iso8601", StringComparison.OrdinalIgnoreCase) == 0) { DateTime scalar; if (SyndicationDateTimeUtility.TryParseRfc3339DateTime(navigator.Value, out scalar)) { value = new XmlRpcScalarValue(scalar); return(true); } } else if (String.Compare(navigator.Name, "base64", StringComparison.OrdinalIgnoreCase) == 0) { if (!String.IsNullOrEmpty(navigator.Value)) { try { byte[] data = Convert.FromBase64String(navigator.Value); value = new XmlRpcScalarValue(data); return(true); } catch (FormatException) { value = null; return(false); } } } else if (String.Compare(navigator.Name, "struct", StringComparison.OrdinalIgnoreCase) == 0) { XmlRpcStructureValue structure = new XmlRpcStructureValue(); if (structure.Load(source)) { value = structure; return(true); } } else if (String.Compare(navigator.Name, "array", StringComparison.OrdinalIgnoreCase) == 0) { XmlRpcArrayValue array = new XmlRpcArrayValue(); if (array.Load(source)) { value = array; return(true); } } } } else if (!String.IsNullOrEmpty(source.Value)) { value = new XmlRpcScalarValue(source.Value); return(true); } value = null; return(false); }
/// <summary> /// Constructs a new <see cref="IXmlRpcValue"/> object from the specified <see cref="XPathNavigator"/>. /// Parameters specify the XML data source and the variable where the new <see cref="IXmlRpcValue"/> object is returned. /// </summary> /// <param name="source">A <see cref="XPathNavigator"/> that represents the XML data source to be parsed.</param> /// <param name="value"> /// When this method returns, contains an object that represents the <see cref="IXmlRpcValue"/> specified by the <paramref name="source"/>, or <b>null</b> if the conversion failed. /// This parameter is passed uninitialized. /// </param> /// <returns> /// <b>true</b> if <paramref name="source"/> was converted successfully; otherwise, <b>false</b>. /// This operation returns <b>false</b> if the <paramref name="source"/> parameter is a null reference (Nothing in Visual Basic), /// or represents XML data that is not in the expected format. /// </returns> /// <remarks> /// The <paramref name="source"/> is expected to represent an XML-RPC <b>value</b> node. /// </remarks> public static bool TryParseValue(XPathNavigator source, out IXmlRpcValue value) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ if (source == null || String.Compare(source.Name, "value", StringComparison.OrdinalIgnoreCase) != 0) { value = null; return false; } //------------------------------------------------------------ // Attempt to perform conversion //------------------------------------------------------------ if(source.HasChildren) { XPathNavigator navigator = source.CreateNavigator(); if (navigator.MoveToFirstChild()) { if (String.Compare(navigator.Name, "i4", StringComparison.OrdinalIgnoreCase) == 0) { int scalar; if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return true; } } else if (String.Compare(navigator.Name, "int", StringComparison.OrdinalIgnoreCase) == 0) { int scalar; if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return true; } } else if (String.Compare(navigator.Name, "boolean", StringComparison.OrdinalIgnoreCase) == 0) { bool scalar; if (XmlRpcClient.TryParseBoolean(navigator.Value, out scalar)) { value = new XmlRpcScalarValue(scalar); return true; } } else if (String.Compare(navigator.Name, "string", StringComparison.OrdinalIgnoreCase) == 0) { value = new XmlRpcScalarValue(navigator.Value); return true; } else if (String.Compare(navigator.Name, "double", StringComparison.OrdinalIgnoreCase) == 0) { double scalar; if (Double.TryParse(navigator.Value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out scalar)) { value = new XmlRpcScalarValue(scalar); return true; } } else if (String.Compare(navigator.Name, "dateTime.iso8601", StringComparison.OrdinalIgnoreCase) == 0) { DateTime scalar; if (SyndicationDateTimeUtility.TryParseRfc3339DateTime(navigator.Value, out scalar)) { value = new XmlRpcScalarValue(scalar); return true; } } else if (String.Compare(navigator.Name, "base64", StringComparison.OrdinalIgnoreCase) == 0) { if(!String.IsNullOrEmpty(navigator.Value)) { try { byte[] data = Convert.FromBase64String(navigator.Value); value = new XmlRpcScalarValue(data); return true; } catch(FormatException) { value = null; return false; } } } else if (String.Compare(navigator.Name, "struct", StringComparison.OrdinalIgnoreCase) == 0) { XmlRpcStructureValue structure = new XmlRpcStructureValue(); if (structure.Load(source)) { value = structure; return true; } } else if (String.Compare(navigator.Name, "array", StringComparison.OrdinalIgnoreCase) == 0) { XmlRpcArrayValue array = new XmlRpcArrayValue(); if (array.Load(source)) { value = array; return true; } } } } else if (!String.IsNullOrEmpty(source.Value)) { value = new XmlRpcScalarValue(source.Value); return true; } //------------------------------------------------------------ // Conversion failed, so set result as null and return false //------------------------------------------------------------ value = null; return false; }