/// <summary> Populates the given Segment object with data from the given XML Element.</summary> /// <throws> HL7Exception if the XML Element does not have the correct name and structure </throws> /// <summary> for the given Segment, or if there is an error while setting individual field values. /// </summary> public virtual void parse(Segment segmentObject, System.Xml.XmlElement segmentElement) { SupportClass.HashSetSupport done = new SupportClass.HashSetSupport(); System.Xml.XmlNodeList all = segmentElement.ChildNodes; for (int i = 0; i < all.Count; i++) { System.String elementName = all.Item(i).Name; if (System.Convert.ToInt16(all.Item(i).NodeType) == (short)System.Xml.XmlNodeType.Element && !done.Contains(elementName)) { done.Add(elementName); int index = elementName.IndexOf('.'); if (index >= 0 && elementName.Length > index) { //properly formatted element System.String fieldNumString = elementName.Substring(index + 1); int fieldNum = System.Int32.Parse(fieldNumString); parseReps(segmentObject, segmentElement, elementName, fieldNum); } else { } } } //set data type of OBX-5 if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(segmentObject, Factory); } }
/// <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(Segment destination, System.String segment, NuGenEncodingCharacters encodingChars) { int fieldOffset = 0; if (isDelimDefSegment(destination.getName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.set_Renamed(destination, 1, 0, 1, 1, System.Convert.ToString(encodingChars.FieldSeparator)); } System.String[] fields = split(segment, System.Convert.ToString(encodingChars.FieldSeparator)); //destination.setName(fields[0]); for (int i = 1; i < fields.Length; i++) { System.String[] reps = split(fields[i], System.Convert.ToString(encodingChars.RepetitionSeparator)); //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = isDelimDefSegment(destination.getName()) && i + fieldOffset == 2; if (isMSH2) { reps = new System.String[1]; reps[0] = fields[i]; } for (int j = 0; j < reps.Length; j++) { try { System.Text.StringBuilder statusMessage = new System.Text.StringBuilder("Parsing field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); //parse(destination.getField(i + fieldOffset, j), reps[j], encodingChars, false); Type field = destination.getField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { parse(field, reps[j], encodingChars); } } catch (NuGenHL7Exception e) { //set the field location and throw again ... e.FieldPosition = i; e.SegmentRepetition = MessageIterator.getIndex(destination.Parent, destination).rep; e.SegmentName = destination.getName(); throw e; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }
/// <summary> Populates the given error segment with information from this Exception.</summary> public virtual void populate(Segment errorSegment) { //make sure it's an ERR if (!errorSegment.getName().Equals("ERR")) { throw new NuGenHL7Exception("Can only populate an ERR segment with an exception -- got: " + errorSegment.GetType().FullName); } int rep = errorSegment.getField(1).Length; //append after existing reps if (this.SegmentName != null) { Terser.set_Renamed(errorSegment, 1, rep, 1, 1, this.SegmentName); } if (this.SegmentRepetition >= 0) { Terser.set_Renamed(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition)); } if (this.FieldPosition >= 0) { Terser.set_Renamed(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition)); } Terser.set_Renamed(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 3, "hl70357"); Terser.set_Renamed(errorSegment, 1, rep, 4, 5, this.Message); //try to get error condition text try { System.String desc = NuGenTableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 2, desc); } catch (NuGenLookupException) { } }
/// <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(Segment destination, System.String segment, NuGenEncodingCharacters encodingChars) { int fieldOffset = 0; if (isDelimDefSegment(destination.getName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.set_Renamed(destination, 1, 0, 1, 1, System.Convert.ToString(encodingChars.FieldSeparator)); } System.String[] fields = split(segment, System.Convert.ToString(encodingChars.FieldSeparator)); //destination.setName(fields[0]); for (int i = 1; i < fields.Length; i++) { System.String[] reps = split(fields[i], System.Convert.ToString(encodingChars.RepetitionSeparator)); //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = isDelimDefSegment(destination.getName()) && i + fieldOffset == 2; if (isMSH2) { reps = new System.String[1]; reps[0] = fields[i]; } for (int j = 0; j < reps.Length; j++) { try { System.Text.StringBuilder statusMessage = new System.Text.StringBuilder("Parsing field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); //parse(destination.getField(i + fieldOffset, j), reps[j], encodingChars, false); Type field = destination.getField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { parse(field, reps[j], encodingChars); } } catch (NuGenHL7Exception e) { //set the field location and throw again ... e.FieldPosition = i; e.SegmentRepetition = MessageIterator.getIndex(destination.Parent, destination).rep; e.SegmentName = destination.getName(); throw e; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }
/// <summary> Populates the given error segment with information from this Exception.</summary> public virtual void populate(Segment errorSegment) { //make sure it's an ERR if (!errorSegment.getName().Equals("ERR")) throw new NuGenHL7Exception("Can only populate an ERR segment with an exception -- got: " + errorSegment.GetType().FullName); int rep = errorSegment.getField(1).Length; //append after existing reps if (this.SegmentName != null) Terser.set_Renamed(errorSegment, 1, rep, 1, 1, this.SegmentName); if (this.SegmentRepetition >= 0) Terser.set_Renamed(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition)); if (this.FieldPosition >= 0) Terser.set_Renamed(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition)); Terser.set_Renamed(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 3, "hl70357"); Terser.set_Renamed(errorSegment, 1, rep, 4, 5, this.Message); //try to get error condition text try { System.String desc = NuGenTableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 2, desc); } catch (NuGenLookupException) { } }
public static void Main(System.String[] args) { if (args.Length != 1) { System.Console.Out.WriteLine("Usage: XMLParser pipe_encoded_file"); System.Environment.Exit(1); } //read and parse message from file try { NuGenPipeParser parser = new NuGenPipeParser(); System.IO.FileInfo messageFile = new System.IO.FileInfo(args[0]); long fileLength = SupportClass.FileLength(messageFile); System.IO.StreamReader r = new System.IO.StreamReader(messageFile.FullName, System.Text.Encoding.Default); char[] cbuf = new char[(int)fileLength]; System.Console.Out.WriteLine("Reading message file ... " + r.Read((System.Char[])cbuf, 0, cbuf.Length) + " of " + fileLength + " chars"); r.Close(); System.String messString = System.Convert.ToString(cbuf); Message mess = parser.parse(messString); System.Console.Out.WriteLine("Got message of type " + mess.GetType().FullName); Genetibase.NuGenHL7.parser.NuGenXMLParser xp = new AnonymousClassXMLParser(); //loop through segment children of message, encode, print to console System.String[] structNames = mess.Names; for (int i = 0; i < structNames.Length; i++) { Structure[] reps = mess.getAll(structNames[i]); for (int j = 0; j < reps.Length; j++) { if (typeof(Segment).IsAssignableFrom(reps[j].GetType())) { //ignore groups System.Xml.XmlDocument docBuilder = new System.Xml.XmlDocument(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); //new doc for each segment System.Xml.XmlElement root = doc.CreateElement(reps[j].GetType().FullName); doc.AppendChild(root); xp.encode((Segment)reps[j], root); System.IO.StringWriter out_Renamed = new System.IO.StringWriter(); System.Console.Out.WriteLine("Segment " + reps[j].GetType().FullName + ": \r\n" + out_Renamed.ToString()); System.Type[] segmentConstructTypes = new System.Type[] { typeof(Message) }; System.Object[] segmentConstructArgs = new System.Object[] { null }; Segment s = (Segment)reps[j].GetType().GetConstructor(segmentConstructTypes).Invoke(segmentConstructArgs); xp.parse(s, root); System.Xml.XmlDocument doc2 = new System.Xml.XmlDocument(); System.Xml.XmlElement root2 = doc2.CreateElement(s.GetType().FullName); doc2.AppendChild(root2); xp.encode(s, root2); System.IO.StringWriter out2 = new System.IO.StringWriter(); if (out2.ToString().Equals(out_Renamed.ToString())) { System.Console.Out.WriteLine("Re-encode OK"); } else { System.Console.Out.WriteLine("Warning: XML different after parse and re-encode: \r\n" + out2.ToString()); } } } } } catch (System.Exception e) { SupportClass.WriteStackTrace(e, Console.Error); } }
/// <summary> Populates the given Segment object with data from the given XML Element.</summary> /// <throws> HL7Exception if the XML Element does not have the correct name and structure </throws> /// <summary> for the given Segment, or if there is an error while setting individual field values. /// </summary> public virtual void parse(Segment segmentObject, System.Xml.XmlElement segmentElement) { SupportClass.HashSetSupport done = new SupportClass.HashSetSupport(); System.Xml.XmlNodeList all = segmentElement.ChildNodes; for (int i = 0; i < all.Count; i++) { System.String elementName = all.Item(i).Name; if (System.Convert.ToInt16(all.Item(i).NodeType) == (short) System.Xml.XmlNodeType.Element && !done.Contains(elementName)) { done.Add(elementName); int index = elementName.IndexOf('.'); if (index >= 0 && elementName.Length > index) { //properly formatted element System.String fieldNumString = elementName.Substring(index + 1); int fieldNum = System.Int32.Parse(fieldNumString); parseReps(segmentObject, segmentElement, elementName, fieldNum); } else { } } } //set data type of OBX-5 if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(segmentObject, Factory); } }