JD2022-TU1/main/tools/extern/Bloomberg/Code/Libraries/Bloomberg.Message/Writer/AttributeWriter.cs

73 lines
2 KiB
C#

using System;
using System.Text;
using Bloomberg.Message.Components;
using Bloomberg.Message.Utils;
namespace Bloomberg.Message.Writer
{
public class AttributeWriter : SimpleWriter
{
public bool EnableValidation { get; set; }
public string Errors { get; set; }
public AttributeWriter(DataSender sender) : base(sender)
{
EnableValidation = false;
Errors = string.Empty;
}
protected override uint StartMarker()
{
return MessageConstants.AttributeStartMarker;
}
protected override uint EndMarker()
{
return MessageConstants.AttributeEndMarker;
}
public void WriteValue(string value)
{
WriteData(MessageConstants.MessageEncoding.GetBytes(""));
WriteData(MessageConstants.MessageEncoding.GetBytes(value));
}
public void WriteKeyValue(string key, string value)
{
if (!ValidateKey(key))
return;
WriteData(MessageConstants.MessageEncoding.GetBytes(key));
WriteData(MessageConstants.MessageEncoding.GetBytes(value));
}
private void WriteData(byte[] data)
{
if (data != null)
{
Sender.SendOnStream(BitConverter.GetBytes((uint)data.Length));
Sender.SendOnStream(data);
}
}
protected bool ValidateKey(string key)
{
if (!EnableValidation)
return true;
if (key == null)
{
Errors += "AttributeWriter::ValidateKey : NULL passed for attribute name!\n";
return false;
}
if (key.StartsWith("#"))
{
Errors += $"AttributeWriter::ValidateKey : {key} is an invalid attribute name! '#' is a reserved prefix.\n";
return false;
}
return true;
}
}
}