public void GetPrimitive_SubComponentIsLessThen0_ArgumentOutOfRangeException() { var type = suiS12.MSH.MessageType; Assert.Throws <ArgumentOutOfRangeException>( () => Terser.GetPrimitive(type, 0, -1)); }
/// <summary> /// Returns given <see cref="IType"/> as a pipe-encoded string, using the given encoding characters. /// <para> /// It is assumed that the Type represents a complete field rather than a component. /// </para> /// </summary> /// <param name="source">An <see cref="IType"/> object from which to construct an encoded string.</param> /// <param name="encodingChars">Encoding characters to be used.</param> /// <returns>The encoded type.</returns> /// <exception cref="HL7Exception">Thrown if the data fields in the group 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(IType source, EncodingCharacters encodingChars) { if (source is Varies) { var varies = (Varies)source; if (varies.Data != null) { source = varies.Data; } } var field = new StringBuilder(); for (var i = 1; i <= Terser.NumComponents(source); i++) { var comp = new StringBuilder(); for (var j = 1; j <= Terser.NumSubComponents(source, i); j++) { var p = Terser.GetPrimitive(source, i, j); comp.Append(EncodePrimitive(p, encodingChars)); comp.Append(encodingChars.SubcomponentSeparator); } field.Append(StripExtraDelimiters(comp.ToString(), encodingChars.SubcomponentSeparator)); field.Append(encodingChars.ComponentSeparator); } return(StripExtraDelimiters(field.ToString(), encodingChars.ComponentSeparator)); }
/// <summary> Fills a field with values from an unparsed string representing the field. </summary> /// <param name="destinationField">the field Type. /// </param> /// <param name="data">the field string (including all components and subcomponents; not including field delimiters). /// </param> /// <param name="encodingCharacters">the encoding characters used in the message. /// </param> private static void Parse(IType destinationField, string data, EncodingCharacters encodingCharacters) { var components = Split(data, Convert.ToString(encodingCharacters.ComponentSeparator)); for (var i = 0; i < components.Length; i++) { var subcomponents = Split(components[i], Convert.ToString(encodingCharacters.SubcomponentSeparator)); for (var j = 0; j < subcomponents.Length; j++) { var val = subcomponents[j]; if (val != null) { val = Escape.UnescapeText(val, encodingCharacters); } Terser.GetPrimitive(destinationField, i + 1, j + 1).Value = val; } } }
/// <summary> /// Parses a segment string and populates the given Segment object. /// <para> /// Unexpected fields are added as Varies' at the end of the segment. /// </para> /// </summary> /// <param name="destination">Segment to parse the segment string into.</param> /// <param name="segment">Encoded segment.</param> /// <param name="encodingChars">Encoding characters to be used.</param> /// <param name="repetition">The repetition number of this segment within its group.</param> /// <param name="parserOptions">Contains configuration that will be applied when parsing.</param> /// <exception cref="HL7Exception"> /// If the given string does not contain the given segment or if the string is not encoded properly. /// </exception> public virtual void Parse(ISegment destination, string segment, EncodingCharacters encodingChars, int repetition, ParserOptions parserOptions) { parserOptions = parserOptions ?? DefaultParserOptions; var 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)); } var fields = Split(segment, Convert.ToString(encodingChars.FieldSeparator)); for (var i = 1; i < fields.Length; i++) { var 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 ... var isMSH2 = IsDelimDefSegment(destination.GetStructureName()) && i + fieldOffset == 2; if (isMSH2) { reps = new string[1]; reps[0] = fields[i]; } for (var j = 0; j < reps.Length; j++) { try { var statusMessage = $"Parsing field {i + fieldOffset} repetition {j}"; Log.Debug(statusMessage); var field = destination.GetField(i + fieldOffset, j); if (isMSH2) { Terser.GetPrimitive(field, 1, 1).Value = reps[j]; } else { Parse(field, reps[j], encodingChars); } } catch (HL7Exception e) { // set the field location and throw again ... e.FieldPosition = i; if (repetition > 1) { e.SegmentRepetition = repetition; } e.SegmentName = destination.GetStructureName(); throw; } } } // set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.FixOBX5(destination, Factory, parserOptions); } }
public void GetPrimitive_TypeIsNull_ThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>( () => Terser.GetPrimitive(null, 0, 0)); }