/// <summary> /// Creates a new message of the given type. If there is a template /// for the message type, then it is used to set all the values in the /// new message (the values will be copied from the original messages, /// not referred to directly, to avoid affecting the templates if a value /// in a message created this way is modified). If the factory has an /// ITraceGenerator set, it uses it to assign a new trace number as a /// NUMERIC value of length 6 in field 11; if AssignDate is true, /// then the current DateTime is stored in field 7 as a DATE10 type. /// </summary> /// <param name="type"></param> /// <returns></returns> public IsoMessage NewMessage(int type) { IsoMessage m = new IsoMessage(_isoHeaders[type]) { Type = type, Etx = Etx }; IsoMessage templ = _typeTemplates[type]; if (templ != null) { for (int i = 2; i < 128; i++) { if (templ.HasField(i)) { m.SetField(i, (IsoValue)templ.GetField(i).Clone()); } } } if (TraceGenerator != null) { m.SetValue(11, TraceGenerator.NextTrace(), IsoType.NUMERIC, 6); } if (AssignDate) { m.SetValue(7, DateTime.Now, IsoType.DATE10, 10); } return(m); }
/// <summary> /// Creates a MessageFactory and configures it from the XML file read /// from the absolute path specified. /// </summary> /// <param name="filename">An absolute path to the XML configuration file.</param> /// <returns>A new configured MessageFactory.</returns> public static MessageFactory CreateFromFile(string filename) { XmlDocument xml = new XmlDocument(); xml.Load(filename); MessageFactory m = new MessageFactory(); foreach (XmlNode node in xml.DocumentElement.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { if ("header".Equals(node.Name)) { string type = node.Attributes["type"].Value; string header = node.ChildNodes[0].Value; m.SetIsoHeader(Convert.ToInt16(type, 16), header); } else if ("template".Equals(node.Name)) { int type = Convert.ToInt16(node.Attributes["type"].Value, 16); IsoMessage templ = new IsoMessage(); templ.Type = type; foreach (XmlNode field in node.ChildNodes) { if (field.NodeType == XmlNodeType.Element && "field".Equals(field.Name)) { int num = Convert.ToInt16(field.Attributes["num"].Value); string ftype = field.Attributes["type"].Value; string length = field.Attributes["length"] == null ? "0" : field.Attributes["length"].Value; string val = field.ChildNodes[0].Value; //Ir creando mensaje con esto templ.SetValue(num, val, (IsoType)Enum.Parse(typeof(IsoType), ftype), Convert.ToInt16(length)); } } m.AddMessageTemplate(templ); } else if ("parse".Equals(node.Name)) { int type = Convert.ToInt16(node.Attributes["type"].Value, 16); Dictionary <int, FieldParseInfo> guide = new Dictionary <int, FieldParseInfo>(); foreach (XmlNode field in node.ChildNodes) { if (field.NodeType == XmlNodeType.Element && "field".Equals(field.Name)) { string num = field.Attributes["num"].Value; string ftype = field.Attributes["type"].Value; XmlAttribute lenAttr = field.Attributes["length"]; int len = 0; if (lenAttr != null) { len = Convert.ToInt16(lenAttr.Value); } //TODO ir creando guia con esto FieldParseInfo fpi = new FieldParseInfo((IsoType)Enum.Parse(typeof(IsoType), ftype), len); guide[Convert.ToInt16(num)] = fpi; } } m.SetParseDictionary(type, guide); } else if ("assign-date".Equals(node.Name)) { string val = node.Attributes["value"].Value; m.AssignDate = "true".Equals(val); } } } return(m); }