/// <summary> /// Populates the given Element with data from the given Segment, by inserting Elements /// corresponding to the Segment's fields, their components, etc. Returns true if there is at /// least one data value in the segment. /// </summary> /// /// <exception cref="HL7Exception"> Thrown when a HL 7 error condition occurs. </exception> /// /// <param name="segmentObject"> The segment object. </param> /// <param name="segmentElement"> Element describing the segment. </param> /// /// <returns> true if it succeeds, false if it fails. </returns> public virtual bool Encode(ISegment segmentObject, System.Xml.XmlElement segmentElement) { bool hasValue = false; int n = segmentObject.NumFields(); for (int i = 1; i <= n; i++) { System.String name = MakeElementName(segmentObject, i); IType[] reps = segmentObject.GetField(i); for (int j = 0; j < reps.Length; j++) { System.Xml.XmlElement newNode = segmentElement.OwnerDocument.CreateElement(name); bool componentHasValue = Encode(reps[j], newNode); if (componentHasValue) { try { segmentElement.AppendChild(newNode); } catch (System.Exception e) { throw new HL7Exception( "DOMException encoding Segment: ", HL7Exception.APPLICATION_INTERNAL_ERROR, e); } hasValue = true; } } } return(hasValue); }
/// <summary> Populates the given Element with data from the given Segment, by inserting /// Elements corresponding to the Segment's fields, their components, etc. Returns /// true if there is at least one data value in the segment. /// </summary> public virtual bool Encode(ISegment segmentObject, XmlElement segmentElement) { var hasValue = false; var n = segmentObject.NumFields(); for (var i = 1; i <= n; i++) { var name = MakeElementName(segmentObject, i); var reps = segmentObject.GetField(i); for (var j = 0; j < reps.Length; j++) { var newNode = segmentElement.OwnerDocument.CreateElement(name); var componentHasValue = Encode(reps[j], newNode); if (componentHasValue) { try { segmentElement.AppendChild(newNode); } catch (Exception e) { throw new HL7Exception("DOMException encoding Segment: ", ErrorCode.APPLICATION_INTERNAL_ERROR, e); } hasValue = true; } } } return(hasValue); }
/// <summary> Copies contents from the source segment to the destination segment. This /// method calls copy(Type, Type) on each repetition of each field (see additional /// behavioural description there). An attempt is made to copy each repetition of /// each field in the source segment, regardless of whether the corresponding /// destination field is repeating or even exists. /// </summary> /// <param name="from">the segment from which data are copied /// </param> /// <param name="to">the segment into which data are copied /// </param> public static void copy(ISegment from, ISegment to) { int n = from.NumFields(); for (int i = 1; i <= n; i++) { IType[] reps = from.GetField(i); for (int j = 0; j < reps.Length; j++) { copy(reps[j], to.GetField(i, j)); } } }
/// <summary> Copies contents from the source segment to the destination segment. This /// method calls copy(Type, Type) on each repetition of each field (see additional /// behavioural description there). An attempt is made to copy each repetition of /// each field in the source segment, regardless of whether the corresponding /// destination field is repeating or even exists. /// </summary> /// <param name="from">the segment from which data are copied. /// </param> /// <param name="to">the segment into which data are copied. /// </param> public static void Copy(ISegment from, ISegment to) { var n = from.NumFields(); for (var i = 1; i <= n; i++) { var reps = from.GetField(i); for (var j = 0; j < reps.Length; j++) { Copy(reps[j], to.GetField(i, j)); } } }
/// <summary> /// Formats a <see cref="IMessage"/> object into an HL7 message string using the given encoding. /// </summary> /// <param name="source">An <see cref="IMessage"/> object from which to construct an encoded message string.</param> /// <param name="encodingChars">Encoding characters to be used.</param> /// <returns>The encoded message.</returns> /// <exception cref="HL7Exception">Thrown if the data fields in the message do not permit encoding (e.g. required fields are null).</exception> /// <exception cref="EncodingNotSupportedException">Thrown if the requested encoding is not supported by this parser.</exception> public static string Encode(ISegment source, EncodingCharacters encodingChars) { var result = new StringBuilder(); result.Append(source.GetStructureName()); result.Append(encodingChars.FieldSeparator); // start at field 2 for MSH segment because field 1 is the field delimiter var startAt = 1; if (IsDelimDefSegment(source.GetStructureName())) { startAt = 2; } // loop through fields; for every field delimit any repetitions and add field delimiter after ... var numFields = source.NumFields(); for (var i = startAt; i <= numFields; i++) { try { var reps = source.GetField(i); for (var j = 0; j < reps.Length; j++) { var fieldText = Encode(reps[j], encodingChars); // if this is MSH-2, then it shouldn't be escaped, so un-escape it again if (IsDelimDefSegment(source.GetStructureName()) && i == 2) { fieldText = Escape.UnescapeText(fieldText, encodingChars); } result.Append(fieldText); if (j < reps.Length - 1) { result.Append(encodingChars.RepetitionSeparator); } } } catch (HL7Exception e) { Log.Error("Error while encoding segment: ", e); } result.Append(encodingChars.FieldSeparator); } // strip trailing delimiters ... return(StripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator)); }
public static String Encode(ISegment source, EncodingCharacters encodingChars) { StringBuilder result = new StringBuilder(); result.Append(source.GetStructureName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (IsDelimDefSegment(source.GetStructureName())) startAt = 2; //loop through fields; for every field delimit any repetitions and add field delimiter after ... int numFields = source.NumFields(); for (int i = startAt; i <= numFields; i++) { try { IType[] reps = source.GetField(i); for (int j = 0; j < reps.Length; j++) { String fieldText = Encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (IsDelimDefSegment(source.GetStructureName()) && i == 2) fieldText = Escape.unescape(fieldText, encodingChars); result.Append(fieldText); if (j < reps.Length - 1) result.Append(encodingChars.RepetitionSeparator); } } catch (HL7Exception e) { log.Error("Error while encoding segment: ", e); } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return StripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator); }
/// <summary> Parses a segment string and populates the given Segment object. Unexpected fields are /// added as Varies' at the end of the segment. /// /// </summary> /// <throws> HL7Exception if the given string does not contain the </throws> /// <summary> given segment or if the string is not encoded properly /// </summary> public virtual void Parse(ISegment destination, String segment, EncodingCharacters encodingChars) { int fieldOffset = 0; if (IsDelimDefSegment(destination.GetStructureName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.Set(destination, 1, 0, 1, 1, Convert.ToString(encodingChars.FieldSeparator)); } String[] fields = Split(segment, Convert.ToString(encodingChars.FieldSeparator)); for (int i = 1; i < fields.Length; i++) { String[] reps = Split(fields[i], Convert.ToString(encodingChars.RepetitionSeparator)); if (log.DebugEnabled) { log.Debug(reps.Length + "reps delimited by: " + encodingChars.RepetitionSeparator); } //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = IsDelimDefSegment(destination.GetStructureName()) && i + fieldOffset == 2; if (isMSH2) { reps = new String[1]; reps[0] = fields[i]; } for (int j = 0; j < reps.Length; j++) { try { StringBuilder statusMessage = new StringBuilder("Parsing field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); log.Debug(statusMessage.ToString()); if (i > 0 && i <= destination.NumFields() && j < destination.GetMaxCardinality(i)) { IType field = destination.GetField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { Parse(field, reps[j], encodingChars); } } else { StringBuilder errorMessage = new StringBuilder("Unexpected repitition for field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); statusMessage.Append(", ignoring extra repititions."); log.Debug(statusMessage.ToString()); } } catch (HL7Exception e) { //set the field location and throw again ... e.FieldPosition = i + fieldOffset; e.SegmentRepetition = MessageIterator.getIndex(destination.ParentStructure, destination).rep; e.SegmentName = destination.GetStructureName(); throw; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }
/// <summary> Populates the given Element with data from the given Segment, by inserting /// Elements corresponding to the Segment's fields, their components, etc. Returns /// true if there is at least one data value in the segment. /// </summary> public virtual bool Encode(ISegment segmentObject, XmlElement segmentElement) { bool hasValue = false; int n = segmentObject.NumFields(); for (int i = 1; i <= n; i++) { String name = MakeElementName(segmentObject, i); IType[] reps = segmentObject.GetField(i); for (int j = 0; j < reps.Length; j++) { XmlElement newNode = segmentElement.OwnerDocument.CreateElement(name); bool componentHasValue = Encode(reps[j], newNode); if (componentHasValue) { try { segmentElement.AppendChild(newNode); } catch (Exception e) { throw new HL7Exception("DOMException encoding Segment: ", HL7Exception.APPLICATION_INTERNAL_ERROR, e); } hasValue = true; } } } return hasValue; }