//// /// Copyright (c) 2016 Saúl Piña . /// /// This file is part of xmlrpcwsc. /// /// xmlrpcwsc is free software: you can redistribute it and/or modify /// it under the terms of the GNU Lesser General Public License as published by /// the Free Software Foundation, either version 3 of the License, or /// (at your option) any later version. /// /// xmlrpcwsc is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU Lesser General Public License for more details. /// /// You should have received a copy of the GNU Lesser General Public License /// along with xmlrpcwsc. If not, see . //// using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace XmlRpc { /// /// XML-RPC response /// public class XmlRpcResponse { private bool isFault; private int faultCode; private string faultString; private object value; /// /// Initializes a new instance of the class /// /// Fault code /// Fault string public XmlRpcResponse(int faultCode, string faultString) { this.isFault = true; this.faultCode = faultCode; this.faultString = faultString; this.value = null; } /// /// Initializes a new instance of the class /// /// Fault code /// Fault string /// Value. public XmlRpcResponse(int faultCode, string faultString, object value) { this.isFault = true; this.faultCode = faultCode; this.faultString = faultString; this.value = value; } /// /// Initializes a new instance of the class /// /// Value public XmlRpcResponse(object value) { this.isFault = false; this.faultCode = -1; this.faultString = null; this.value = value; } /// /// Determines whether this instance is fault /// /// true if this instance is fault; otherwise, false public bool IsFault() { return isFault; } /// /// Gets the fault code /// /// The fault code public int GetFaultCode() { return faultCode; } /// /// Gets the fault string /// /// The fault string public string GetFaultString() { return faultString; } /// /// Gets the value /// /// The value public object GetObject() { return value; } /// /// Determines whether this instance is null /// /// true if this instance is null; otherwise, false public bool IsNull() { return GetObject() == null; } /// /// Determines whether this instance is int /// /// true if this instance is int; otherwise, false public bool IsInt() { return GetObject() is int; } /// /// Gets the value int /// /// The value int public int GetInt() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsInt()) return (int)GetObject(); else throw new InvalidCastException("The value is not of type int"); } /// /// Determines whether this instance is boolean /// /// true if this instance is boolean; otherwise, false public bool IsBoolean() { return GetObject() is bool; } /// /// Gets the value boolean /// /// true, if value boolean was gotten, false otherwise public bool GetBoolean() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsBoolean()) return (bool)GetObject(); else throw new InvalidCastException("The value is not of type bool"); } /// /// Determines whether this instance is string /// /// true if this instance is string; otherwise, false public bool IsString() { return GetObject() is string; } /// /// Gets the value string /// /// The value string public string GetString() { if (IsNull()) throw new NullReferenceException("The value is null"); else return ObjectToString(GetObject()); } /// /// Objects to string /// /// The to string /// Value private string ObjectToString(object value) { if (value is List) { StringBuilder stringReturn = new StringBuilder(); stringReturn.Append("["); foreach (object temp in (List) value) { stringReturn.Append(ObjectToString(temp)); stringReturn.Append(", "); } if (((List)value).Count > 0) stringReturn.Remove(stringReturn.Length - 2, 2); stringReturn.Append("]"); return stringReturn.ToString(); } else if (value is Dictionary) { StringBuilder stringReturn = new StringBuilder(); stringReturn.Append("{"); foreach (KeyValuePair temp in (Dictionary) value) { stringReturn.Append(temp.Key); stringReturn.Append(": "); stringReturn.Append(ObjectToString(temp.Value)); stringReturn.Append(", "); } if (((Dictionary)value).Count > 0) stringReturn.Remove(stringReturn.Length - 2, 2); stringReturn.Append("}"); return stringReturn.ToString(); } else if (value is byte[]) { return Convert.ToBase64String((byte[])value); } else { return value.ToString(); } } /// /// Determines whether this instance is double /// /// true if this instance is double; otherwise, false public bool IsDouble() { return GetObject() is double; } /// /// Gets the value double /// /// The value double public double GetDouble() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsDouble()) return (double)GetObject(); else throw new InvalidCastException("The value is not of type double"); } /// /// Determines whether this instance is date time /// /// true if this instance is date time; otherwise, false public bool IsDateTime() { return GetObject() is DateTime; } /// /// Gets the value date time /// /// The value date time public DateTime GetDateTime() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsDateTime()) return (DateTime)GetObject(); else throw new InvalidCastException("The value is not of type DateTime"); } /// /// Determines whether this instance is byte /// /// true if this instance is byte; otherwise, false public bool IsByte() { return GetObject() is byte[]; } /// /// Gets the value byte /// /// The value byte public byte[] GetByte() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsByte()) return (byte[])GetObject(); else throw new InvalidCastException("The value is not of type byte[]"); } /// /// Determines whether this instance is array /// /// true if this instance is array; otherwise, false public bool IsArray() { return GetObject() is List; } /// /// Gets the value array /// /// The value array public List GetArray() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsArray()) return (List)GetObject(); else throw new InvalidCastException("The value is not of type List"); } /// /// Determines whether this instance is struct /// /// true if this instance is struct; otherwise, false public bool IsStruct() { return GetObject() is Dictionary; } /// /// Gets the value struct /// /// The value struct public Dictionary GetStruct() { if (IsNull()) throw new NullReferenceException("The value is null"); else if (IsStruct()) return (Dictionary)GetObject(); else throw new InvalidCastException("The value is not of type Dictionary"); } } }