Exemplo n.º 1
0
        /// <summary> Formats a Message object into an html table object using the given
        /// </summary>
        /// <throws>  HL7Exception if the data fields in the message do not permit encoding </throws>
        /// <summary>      (e.g. required fields are null)
        /// </summary>
        /// <throws>  EncodingNotSupportedException if the requested encoding is not </throws>
        /// <summary>      supported by this parser.
        /// </summary>
        public Table buildTable(Message source)
        {
            //get encoding characters ...
            Segment msh = (Segment)source.get_Renamed("MSH");

            System.String fieldSepString = Terser.Get(msh, 1, 0, 1, 1);

            if (fieldSepString == null)
            {
                throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing");
            }

            char fieldSep = '|';

            if (fieldSepString != null && fieldSepString.Length > 0)
            {
                fieldSep = fieldSepString[0];
            }

            System.String encCharString = Terser.Get(msh, 2, 0, 1, 1);

            EncodingCharacters en = new EncodingCharacters(fieldSep, encCharString);

            //pass down to group encoding method which will operate recursively on children ...
            Table tbl = new Table();

            encode((Group)source, en, tbl);
            return(tbl);
        }
Exemplo n.º 2
0
        /// <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(Segment 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);
                Type[]        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);
                        }
                        //UPGRADE_TODO: Class 'org.w3c.dom.DOMException' was converted to 'System.Exceptiont' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                        catch (System.Exception e)
                        {
                            throw new HL7Exception("DOMException encoding Segment: ", HL7Exception.APPLICATION_INTERNAL_ERROR, e);
                        }
                        hasValue = true;
                    }
                }
            }
            return(hasValue);
        }
Exemplo n.º 3
0
        /// <summary> Formats a Message object into an HL7 message string using this parser's
        /// default encoding ("VB").
        /// </summary>
        /// <throws>  HL7Exception if the data fields in the message do not permit encoding </throws>
        /// <summary>      (e.g. required fields are null)
        /// </summary>
        protected internal override System.String doEncode(Message source)
        {
            //get encoding characters ...
            Segment msh = (Segment)source.get_Renamed("MSH");

            System.String fieldSepString = Terser.Get(msh, 1, 0, 1, 1);

            if (fieldSepString == null)
            {
                throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing");
            }

            char fieldSep = '|';

            if (fieldSepString != null && fieldSepString.Length > 0)
            {
                fieldSep = fieldSepString[0];
            }

            System.String encCharString = Terser.Get(msh, 2, 0, 1, 1);

            if (encCharString == null)
            {
                throw new HL7Exception("Can't encode message: MSH-2 (encoding characters) is missing");
            }

            if (encCharString.Length != 4)
            {
                throw new HL7Exception("Encoding characters '" + encCharString + "' invalid -- must be 4 characters", HL7Exception.DATA_TYPE_ERROR);
            }
            EncodingCharacters en = new EncodingCharacters(fieldSep, encCharString);

            //pass down to group encoding method which will operate recursively on children ...
            return(encode((Group)source, en));
        }
Exemplo n.º 4
0
 private void  parseReps(Segment segmentObject, System.Xml.XmlElement segmentElement, System.String fieldName, int fieldNum)
 {
     System.Xml.XmlNodeList reps = segmentElement.GetElementsByTagName(fieldName);
     for (int i = 0; i < reps.Count; i++)
     {
         parse(segmentObject.getField(fieldNum, i), (System.Xml.XmlElement)reps.Item(i));
     }
 }
Exemplo n.º 5
0
        public static System.String encode(Segment source, EncodingCharacters encodingChars)
        {
            System.Text.StringBuilder result = new System.Text.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
                {
                    Type[] reps = source.getField(i);
                    for (int j = 0; j < reps.Length; j++)
                    {
                        System.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));
        }
Exemplo n.º 6
0
        /// <summary> <p>Returns a minimal amount of data from a message string, including only the
        /// data needed to send a response to the remote system.  This includes the
        /// following fields:
        /// <ul><li>field separator</li>
        /// <li>encoding characters</li>
        /// <li>processing ID</li>
        /// <li>message control ID</li></ul>
        /// This method is intended for use when there is an error parsing a message,
        /// (so the Message object is unavailable) but an error message must be sent
        /// back to the remote system including some of the information in the inbound
        /// message.  This method parses only that required information, hopefully
        /// avoiding the condition that caused the original error.</p>
        /// </summary>
        public override Segment getCriticalResponseData(System.String message)
        {
            System.String version      = getVersion(message);
            Segment       criticalData = Parser.makeControlMSH(version, Factory);

            Terser.Set(criticalData, 1, 0, 1, 1, parseLeaf(message, "MSH.1", 0));
            Terser.Set(criticalData, 2, 0, 1, 1, parseLeaf(message, "MSH.2", 0));
            Terser.Set(criticalData, 10, 0, 1, 1, parseLeaf(message, "MSH.10", 0));
            System.String procID = parseLeaf(message, "MSH.11", 0);
            if (procID == null || procID.Length == 0)
            {
                procID = parseLeaf(message, "PT.1", message.IndexOf("MSH.11"));
                //this field is a composite in later versions
            }
            Terser.Set(criticalData, 11, 0, 1, 1, procID);

            return(criticalData);
        }
Exemplo n.º 7
0
        /// <summary> Creates a version-specific MSH object and returns it as a version-independent
        /// MSH interface.
        /// throws HL7Exception if there is a problem, e.g. invalid version, code not available
        /// for given version.
        /// </summary>
        public static Segment makeControlMSH(System.String version, ModelClassFactory factory)
        {
            Segment msh = null;

            try
            {
                Message dummy = (Message)GenericMessage.getGenericMessageClass(version).GetConstructor(new System.Type[] { typeof(ModelClassFactory) }).Invoke(new System.Object[] { factory });

                System.Type[]   constructorParamTypes = new System.Type[] { typeof(Group), typeof(ModelClassFactory) };
                System.Object[] constructorParamArgs  = new System.Object[] { dummy, factory };
                System.Type     c = factory.getSegmentClass("MSH", version);
                System.Reflection.ConstructorInfo constructor = c.GetConstructor(constructorParamTypes);
                msh = (Segment)constructor.Invoke(constructorParamArgs);
            }
            catch (System.Exception e)
            {
                throw new HL7Exception("Couldn't create MSH for version " + version + " (does your classpath include this version?) ... ", HL7Exception.APPLICATION_INTERNAL_ERROR, e);
            }
            return(msh);
        }
Exemplo n.º 8
0
        /// <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.getStructureName().Equals("ERR"))
            {
                throw new HL7Exception("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(errorSegment, 1, rep, 1, 1, this.SegmentName);
            }

            if (this.SegmentRepetition >= 0)
            {
                Terser.Set(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition));
            }

            if (this.FieldPosition >= 0)
            {
                Terser.Set(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition));
            }

            Terser.Set(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode));
            Terser.Set(errorSegment, 1, rep, 4, 3, "hl70357");
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            Terser.Set(errorSegment, 1, rep, 4, 5, this.Message);

            //try to get error condition text
            try
            {
                System.String desc = TableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode));
                Terser.Set(errorSegment, 1, rep, 4, 2, desc);
            }
            catch (LookupException e)
            {
                ourLog.debug("Warning: LookupException getting error condition text (are we connected to a TableRepository?)", e);
            }
        }
Exemplo n.º 9
0
        /// <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)
        {
            //UPGRADE_TODO: Class 'java.util.HashSet' was converted to 'SupportClass.HashSetSupport' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashSet'"
            SupportClass.HashSetSupport done = new SupportClass.HashSetSupport();

            //        for (int i = 1; i <= segmentObject.numFields(); i++) {
            //            String elementName = makeElementName(segmentObject, i);
            //            done.add(elementName);
            //            parseReps(segmentObject, segmentElement, elementName, i);
            //        }

            //UPGRADE_TODO: Method 'org.w3c.dom.Node.getChildNodes' was converted to 'System.Xml.XmlNode.ChildNodes' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
            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
                    {
                        log.debug("Child of segment " + segmentObject.getStructureName() + " doesn't look like a field: " + elementName);
                    }
                }
            }

            //set data type of OBX-5
            if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0)
            {
                Varies.fixOBX5(segmentObject, Factory);
            }
        }
Exemplo n.º 10
0
 /// <summary> Returns given group serialized as a pipe-encoded string - this method is called
 /// by encode(Message source, String encoding).
 /// </summary>
 protected virtual void encode(Group source, EncodingCharacters encodingChars, Table tbl)
 {
     System.String[] names = source.Names;
     for (int i = 0; i < names.Length; i++)
     {
         Structure[] reps = source.getAll(names[i]);
         for (int rep = 0; rep < reps.Length; rep++)
         {
             if (reps[rep] is Group)
             {
                 Group g = (Group)reps[rep];
                 encode(g, encodingChars, tbl);
             }
             else
             {
                 Segment segment = (Segment)reps[rep];
                 encode(segment, encodingChars, tbl);
             }
         }
     }
 }
Exemplo n.º 11
0
 /// <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(Segment 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);
         Type[] 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);
                 }
                     //UPGRADE_TODO: Class 'org.w3c.dom.DOMException' was converted to 'System.Exceptiont' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                 catch (System.Exception e)
                 {
                     throw new HL7Exception("DOMException encoding Segment: ", HL7Exception.APPLICATION_INTERNAL_ERROR, e);
                 }
                 hasValue = true;
             }
         }
     }
     return hasValue;
 }
Exemplo n.º 12
0
        /// <summary> Returns the expected XML element name for the given child of a message constituent
        /// of the given class (the class should be a Composite or Segment class).
        /// </summary>

        /*private String makeElementName(Class c, int child) {
         * String longClassName = c.getName();
         * String shortClassName = longClassName.substring(longClassName.lastIndexOf('.') + 1, longClassName.length());
         * if (shortClassName.startsWith("Valid")) {
         * shortClassName = shortClassName.substring(5, shortClassName.length());
         * }
         * return shortClassName + "." + child;
         * }*/

        /// <summary>Returns the expected XML element name for the given child of the given Segment </summary>
        private System.String makeElementName(Segment s, int child)
        {
            return(s.getStructureName() + "." + child);
        }
Exemplo n.º 13
0
        /// <summary> <p>Returns a minimal amount of data from a message string, including only the
        /// data needed to send a response to the remote system.  This includes the
        /// following fields:
        /// <ul><li>field separator</li>
        /// <li>encoding characters</li>
        /// <li>processing ID</li>
        /// <li>message control ID</li></ul>
        /// This method is intended for use when there is an error parsing a message,
        /// (so the Message object is unavailable) but an error message must be sent
        /// back to the remote system including some of the information in the inbound
        /// message.  This method parses only that required information, hopefully
        /// avoiding the condition that caused the original error.  The other
        /// fields in the returned MSH segment are empty.</p>
        /// </summary>
        public override Segment getCriticalResponseData(System.String message)
        {
            //try to get MSH segment
            int locStartMSH = message.IndexOf("MSH");

            if (locStartMSH < 0)
            {
                throw new HL7Exception("Couldn't find MSH segment in message: " + message, HL7Exception.SEGMENT_SEQUENCE_ERROR);
            }
            //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"
            int locEndMSH = message.IndexOf('\r', locStartMSH + 1);

            if (locEndMSH < 0)
            {
                locEndMSH = message.Length;
            }
            System.String mshString = message.Substring(locStartMSH, (locEndMSH) - (locStartMSH));

            //find out what the field separator is
            char fieldSep = mshString[3];

            //get field array
            System.String[] fields = split(mshString, System.Convert.ToString(fieldSep));

            Segment msh = null;

            try
            {
                //parse required fields
                System.String   encChars      = fields[1];
                char            compSep       = encChars[0];
                System.String   messControlID = fields[9];
                System.String[] procIDComps   = split(fields[10], System.Convert.ToString(compSep));

                //fill MSH segment
                System.String version = "2.4";                 //default
                try
                {
                    version = this.getVersion(message);
                }
                catch (System.Exception)
                {
                    /* use the default */
                }

                msh = Parser.makeControlMSH(version, Factory);
                Terser.Set(msh, 1, 0, 1, 1, System.Convert.ToString(fieldSep));
                Terser.Set(msh, 2, 0, 1, 1, encChars);
                Terser.Set(msh, 10, 0, 1, 1, messControlID);
                Terser.Set(msh, 11, 0, 1, 1, procIDComps[0]);
            }
            catch (System.Exception e)
            {
                SupportClass.WriteStackTrace(e, Console.Error);
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Class.getName' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                throw new HL7Exception("Can't parse critical fields from MSH segment (" + e.GetType().FullName + ": " + e.Message + "): " + mshString, HL7Exception.REQUIRED_FIELD_MISSING);
            }

            return(msh);
        }
Exemplo n.º 14
0
        /// <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.getStructureName().Equals("ERR"))
                throw new HL7Exception("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(errorSegment, 1, rep, 1, 1, this.SegmentName);

            if (this.SegmentRepetition >= 0)
                Terser.Set(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition));

            if (this.FieldPosition >= 0)
                Terser.Set(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition));

            Terser.Set(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode));
            Terser.Set(errorSegment, 1, rep, 4, 3, "hl70357");
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            Terser.Set(errorSegment, 1, rep, 4, 5, this.Message);

            //try to get error condition text
            try
            {
                System.String desc = TableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode));
                Terser.Set(errorSegment, 1, rep, 4, 2, desc);
            }
            catch (LookupException e)
            {
                ourLog.debug("Warning: LookupException getting error condition text (are we connected to a TableRepository?)", e);
            }
        }
Exemplo n.º 15
0
        protected virtual void encode(Segment source, EncodingCharacters encodingChars, Table tbl)
        {
            TableRow tr;
            TableCell td;
            tr = new TableRow();
            td = new TableCell();
            td.ColumnSpan = 3;
            if(_cssSegmentHeader!=null && _cssSegmentHeader.Trim().Length>0)
            {
                tr.CssClass=_cssSegmentHeader;
            }
            td.Text = source.getStructureName();
            tr.Cells.Add(td);
            tbl.Rows.Add(tr);

            //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
                {
                    tr = new TableRow();

                    td = new TableCell();
                    td.Text = i.ToString();
                    tr.Cells.Add(td);

                    Type[] reps = source.getField(i);
                    string description = source.getFieldDescription(i);
                    td = new TableCell();
                    if(_cssSegmentField!=null)
                        td.CssClass = _cssSegmentField;
                    if(description!=null && description.Trim().Length>0)
                        td.Text = description;
                    else
                        td.Text ="&nbsp;";

                    tr.Cells.Add(td);

                    td = new TableCell();
                    if(reps.Length==0)
                        td.Text = "&nbsp;";
                    else
                    {
                        for (int j = 0; j < reps.Length; j++)
                        {
                            td.Controls.Add(encode(reps[j], encodingChars));
                            if(j<reps.Length-1)
                                td.Controls.Add(new LiteralControl("<hr size='2'>"));
                        }
                    }
                    tr.Cells.Add(td);
                    tbl.Rows.Add(tr);
                }
                catch (HL7Exception e)
                {
                    log.error("Error while encoding segment: ", e);
                }
            }
        }
Exemplo n.º 16
0
 private void parseReps(Segment segmentObject, System.Xml.XmlElement segmentElement, System.String fieldName, int fieldNum)
 {
     System.Xml.XmlNodeList reps = segmentElement.GetElementsByTagName(fieldName);
     for (int i = 0; i < reps.Count; i++)
     {
         parse(segmentObject.getField(fieldNum, i), (System.Xml.XmlElement) reps.Item(i));
     }
 }
Exemplo n.º 17
0
 /// <summary> Returns the expected XML element name for the given child of a message constituent 
 /// of the given class (the class should be a Composite or Segment class). 
 /// </summary>
 /*private String makeElementName(Class c, int child) {
 String longClassName = c.getName();
 String shortClassName = longClassName.substring(longClassName.lastIndexOf('.') + 1, longClassName.length());
 if (shortClassName.startsWith("Valid")) {
 shortClassName = shortClassName.substring(5, shortClassName.length());
 }
 return shortClassName + "." + child;
 }*/
 /// <summary>Returns the expected XML element name for the given child of the given Segment </summary>
 private System.String makeElementName(Segment s, int child)
 {
     return s.getStructureName() + "." + child;
 }
Exemplo n.º 18
0
        protected virtual void encode(Segment source, EncodingCharacters encodingChars, Table tbl)
        {
            TableRow  tr;
            TableCell td;

            tr            = new TableRow();
            td            = new TableCell();
            td.ColumnSpan = 3;
            if (_cssSegmentHeader != null && _cssSegmentHeader.Trim().Length > 0)
            {
                tr.CssClass = _cssSegmentHeader;
            }
            td.Text = source.getStructureName();
            tr.Cells.Add(td);
            tbl.Rows.Add(tr);


            //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
                {
                    tr = new TableRow();

                    td      = new TableCell();
                    td.Text = i.ToString();
                    tr.Cells.Add(td);

                    Type[] reps        = source.getField(i);
                    string description = source.getFieldDescription(i);
                    td = new TableCell();
                    if (_cssSegmentField != null)
                    {
                        td.CssClass = _cssSegmentField;
                    }
                    if (description != null && description.Trim().Length > 0)
                    {
                        td.Text = description;
                    }
                    else
                    {
                        td.Text = "&nbsp;";
                    }

                    tr.Cells.Add(td);

                    td = new TableCell();
                    if (reps.Length == 0)
                    {
                        td.Text = "&nbsp;";
                    }
                    else
                    {
                        for (int j = 0; j < reps.Length; j++)
                        {
                            td.Controls.Add(encode(reps[j], encodingChars));
                            if (j < reps.Length - 1)
                            {
                                td.Controls.Add(new LiteralControl("<hr size='2'>"));
                            }
                        }
                    }
                    tr.Cells.Add(td);
                    tbl.Rows.Add(tr);
                }
                catch (HL7Exception e)
                {
                    log.error("Error while encoding segment: ", e);
                }
            }
        }
Exemplo n.º 19
0
        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
            {
                PipeParser         parser      = new PipeParser();
                System.IO.FileInfo messageFile = new System.IO.FileInfo(args[0]);
                long fileLength = SupportClass.FileLength(messageFile);
                //UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                System.IO.StreamReader r = new System.IO.StreamReader(messageFile.FullName, System.Text.Encoding.Default);
                char[] cbuf = new char[(int)fileLength];
                //UPGRADE_TODO: Method 'java.io.Reader.read' was converted to 'System.IO.StreamReader.Read' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioReaderread_char[]'"
                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);

                ca.uhn.hl7v2.parser.XMLParser 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
                            //UPGRADE_TODO: Class 'javax.xml.parsers.DocumentBuilder' was converted to 'System.Xml.XmlDocument' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaxxmlparsersDocumentBuilder'"
                            //UPGRADE_ISSUE: Method 'javax.xml.parsers.DocumentBuilderFactory.newInstance' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaxxmlparsersDocumentBuilderFactory'"
                            //DocumentBuilderFactory.newInstance();

                            System.Xml.XmlDocument docBuilder = new System.Xml.XmlDocument();
                            //UPGRADE_TODO: Class 'javax.xml.parsers.DocumentBuilder' was converted to 'System.Xml.XmlDocument' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaxxmlparsersDocumentBuilder'"
                            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" + doc.OuterXml);

                            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);
                            //UPGRADE_TODO: Class 'javax.xml.parsers.DocumentBuilder' was converted to 'System.Xml.XmlDocument' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaxxmlparsersDocumentBuilder'"
                            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();
                            System.Xml.XmlTextWriter ser  = new System.Xml.XmlTextWriter(out2);

                            //System.Xml.XmlWriter ser = System.Xml.XmlWriter.Create(out2);
                            doc.WriteTo(ser);
                            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);
            }
        }
Exemplo n.º 20
0
        /// <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, 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, 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));
                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 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);
                        log.debug(statusMessage.ToString());
                        //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 (HL7Exception e)
                    {
                        //set the field location and throw again ...
                        e.FieldPosition = i;
                        e.SegmentRepetition = MessageIterator.getIndex(destination.ParentGroup, destination).rep;
                        e.SegmentName = destination.getStructureName();
                        throw e;
                    }
                }
            }

            //set data type of OBX-5
            if (destination.GetType().FullName.IndexOf("OBX") >= 0)
            {
                Varies.fixOBX5(destination, Factory);
            }
        }
Exemplo n.º 21
0
        public static System.String encode(Segment source, EncodingCharacters encodingChars)
        {
            System.Text.StringBuilder result = new System.Text.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
                {
                    Type[] reps = source.getField(i);
                    for (int j = 0; j < reps.Length; j++)
                    {
                        System.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);
        }
Exemplo n.º 22
0
        /// <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)
        {
            //UPGRADE_TODO: Class 'java.util.HashSet' was converted to 'SupportClass.HashSetSupport' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashSet'"
            SupportClass.HashSetSupport done = new SupportClass.HashSetSupport();

            //        for (int i = 1; i <= segmentObject.numFields(); i++) {
            //            String elementName = makeElementName(segmentObject, i);
            //            done.add(elementName);
            //            parseReps(segmentObject, segmentElement, elementName, i);
            //        }

            //UPGRADE_TODO: Method 'org.w3c.dom.Node.getChildNodes' was converted to 'System.Xml.XmlNode.ChildNodes' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
            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
                    {
                        log.debug("Child of segment " + segmentObject.getStructureName() + " doesn't look like a field: " + elementName);
                    }
                }
            }

            //set data type of OBX-5
            if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0)
            {
                Varies.fixOBX5(segmentObject, Factory);
            }
        }
Exemplo n.º 23
0
        /// <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, 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, 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));
                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 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);
                        log.debug(statusMessage.ToString());
                        //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 (HL7Exception e)
                    {
                        //set the field location and throw again ...
                        e.FieldPosition     = i;
                        e.SegmentRepetition = MessageIterator.getIndex(destination.ParentGroup, destination).rep;
                        e.SegmentName       = destination.getStructureName();
                        throw e;
                    }
                }
            }

            //set data type of OBX-5
            if (destination.GetType().FullName.IndexOf("OBX") >= 0)
            {
                Varies.fixOBX5(destination, Factory);
            }
        }