using System; using System.Collections.Generic; //using System.Linq; using System.Text; namespace ECOM.ServiceBus { internal class FastMessageEncoder { internal class Proxy { FastMessageEncoder m_Encoder = new FastMessageEncoder (); public Proxy Encode(string Key, T Value) { m_Encoder [Key] = Value; return this; } public override string ToString() { return m_Encoder.ToString (); } static public implicit operator string(Proxy proxy) { return proxy.ToString (); } } public static Proxy Encode(string Key, T Value) { Proxy proxy = new Proxy (); proxy.Encode (Key, Value); return proxy; } public FastMessageEncoder() { m_arMessage = new List (); } public void Set(string Key, string strValue) { StringBuilder Value = new StringBuilder (strValue); Value.Replace ("\\", "\\\\"); Value.Replace ("/", "\\/"); Value.Replace ("\"", "\\\""); StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append ('"').Append (Value).Append ('"'); m_arMessage.Add (build.ToString ()); } public void Set(string Key, int Value) { StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append (Value); m_arMessage.Add (build.ToString ()); } public void Set(string Key, double Value) { StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append (Value); m_arMessage.Add (build.ToString ()); } public void Set(string Key, bool Value) { StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append (Value.ToString ()); m_arMessage.Add (build.ToString ()); } public void Set(string Key, string [] arValue) { StringBuilder tValue = new StringBuilder (); foreach (string strValue in arValue) { StringBuilder Value = new StringBuilder (strValue); Value.Replace ("\\", "\\\\"); Value.Replace ("/", "\\/"); Value.Replace ("\"", "\\\""); if (tValue.Length > 0) { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (',').Append (' '); } else { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (' ').Append (' '); } tValue.Append ('\"').Append (Value).Append ('"'); } StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']'); m_arMessage.Add (build.ToString ()); } public void Set(string Key, List arValue) { StringBuilder tValue = new StringBuilder (); foreach (string strValue in arValue) { StringBuilder Value = new StringBuilder (strValue); Value.Replace ("\\", "\\\\"); Value.Replace ("/", "\\/"); Value.Replace ("\"", "\\\""); if (tValue.Length > 0) { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (',').Append (' '); } else { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (' ').Append (' '); } tValue.Append ('\"').Append (Value).Append ('"'); } StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']'); m_arMessage.Add (build.ToString ()); } public void Set(string Key, List arValue) { StringBuilder tValue = new StringBuilder (); foreach (FastMessageEncoder strValue in arValue) { StringBuilder Value = new StringBuilder (strValue.ToString ()); Value.Replace ("\\", "\\\\"); Value.Replace ("/", "\\/"); Value.Replace ("\"", "\\\""); if (tValue.Length > 0) { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (',').Append (' '); } else { tValue.Append ('\r').Append ('\n').Append (' ').Append (' '); tValue.Append (' ').Append (' '); } tValue.Append ('\"').Append (Value).Append ('"'); } StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append (' ').Append (' '); build.Append ('"').Append (Key).Append ('"'); build.Append (' ').Append (':').Append (' '); build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']'); m_arMessage.Add (build.ToString ()); } public string ToStringOfElement() { StringBuilder build = new StringBuilder (); for (int Index = 0; Index < m_arMessage.Count; Index++) { string msg = m_arMessage [Index]; if (Index > 0) build.Append (','); build.Append (msg); } return build.ToString (); } public override string ToString() { StringBuilder build = new StringBuilder (); build.Append ('\r').Append ('\n').Append ('{'); build.Append (ToStringOfElement ()); build.Append ('\r').Append ('\n').Append ('}').Append ('\r').Append ('\n'); return build.ToString (); } static Type TypeOf_int = typeof (int); static Type TypeOf_uint = typeof (uint); static Type TypeOf_UInt16 = typeof (UInt16); static Type TypeOf_long = typeof (long); static Type TypeOf_ulong = typeof (ulong); static Type TypeOf_double = typeof (double); static Type TypeOf_float = typeof (float); static Type TypeOf_bool = typeof (bool); static Type TypeOf_string = typeof (string); static Type TypeOf_list_string = typeof (List); static Type TypeOf_string_array = typeof (string []); public object this [string Key] { set { Type type = value.GetType (); if (type == TypeOf_int) { int toInt = (int) value; Set (Key, toInt); return; } if (type == TypeOf_uint) { uint toUint = (uint) value; int toInt = (int) toUint; Set (Key, toInt); return; } if (type == TypeOf_UInt16) { UInt16 toUint16 = (UInt16) value; int toInt = (int) toUint16; Set (Key, toInt); return; } if (type == TypeOf_long) { long toLong = (long) value; int toInt = (int) toLong; Set (Key, toInt); return; } if (type == TypeOf_ulong) { ulong toUlong = (ulong) value; int toInt = (int) toUlong; Set (Key, toInt); return; } if (type == TypeOf_double) { double toDouble = (double) value; Set (Key, toDouble); return; } if (type == TypeOf_float) { double toDouble = (float) value; Set (Key, toDouble); return; } if (type == TypeOf_bool) { bool toBool = (bool) value; Set (Key, toBool); return; } if (type == TypeOf_string) { Set (Key, value.ToString ()); return; } if (type == TypeOf_list_string) { List arString = (List) value; Set (Key, arString); return; } if (type == TypeOf_string_array) { string [] arString = (string []) value; Set (Key, arString); return; } throw (new ArgumentException ("Unknown data type of " + type.ToString ())); } } protected List m_arMessage; } }