Esempio n. 1
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));
        }
Esempio n. 2
0
 /// <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(Type destinationField, System.String data, EncodingCharacters encodingCharacters)
 {
     System.String[] components = split(data, System.Convert.ToString(encodingCharacters.ComponentSeparator));
     for (int i = 0; i < components.Length; i++)
     {
         System.String[] subcomponents = split(components[i], System.Convert.ToString(encodingCharacters.SubcomponentSeparator));
         for (int j = 0; j < subcomponents.Length; j++)
         {
             System.String val = subcomponents[j];
             if (val != null)
             {
                 val = Escape.unescape(val, encodingCharacters);
             }
             Terser.getPrimitive(destinationField, i + 1, j + 1).Value = val;
         }
     }
 }
Esempio n. 3
0
 /// <summary> Constructs escape sequences using the given escape character - this should only
 /// be called by getEscapeCharacter(), which will cache the results for subsequent use.
 /// </summary>
 //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
 private static System.Collections.Hashtable makeEscapeSequences(EncodingCharacters ec)
 {
     //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
     System.Collections.Hashtable seqs = new System.Collections.Hashtable();
     char[] codes  = new char[] { 'F', 'S', 'T', 'R', 'E' };
     char[] values = new char[] { ec.FieldSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator, ec.RepetitionSeparator, ec.EscapeCharacter };
     for (int i = 0; i < codes.Length; i++)
     {
         System.Text.StringBuilder seq = new System.Text.StringBuilder();
         seq.Append(ec.EscapeCharacter);
         seq.Append(codes[i]);
         seq.Append(ec.EscapeCharacter);
         seqs[seq.ToString()] = System.Convert.ToString(values[i]);
     }
     seqs["\\X000d\\"] = System.Convert.ToString('\r');
     return(seqs);
 }
Esempio n. 4
0
 /// <summary> Encodes the given Type, using the given encoding characters.
 /// It is assumed that the Type represents a complete field rather than a component.
 /// </summary>
 public static System.String encode(Type source, EncodingCharacters encodingChars)
 {
     System.Text.StringBuilder field = new System.Text.StringBuilder();
     for (int i = 1; i <= Terser.numComponents(source); i++)
     {
         System.Text.StringBuilder comp = new System.Text.StringBuilder();
         for (int j = 1; j <= Terser.numSubComponents(source, i); j++)
         {
             Primitive 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));
     //return encode(source, encodingChars, false);
 }
Esempio n. 5
0
        public static System.String escape(System.String text, EncodingCharacters encChars)
        {
            //First, take all special characters and replace them with something that is garbled
            for (int i = 0; i < SPECIAL_ENCODING_VALUES.Length; i++)
            {
                string specialValues = SPECIAL_ENCODING_VALUES[i];
                text = text.Replace(specialValues, EncodeSpecialCharacters(i.ToString()));
            }
            //Encode each escape character
            System.Collections.Hashtable esc    = getEscapeSequences(encChars);
            System.Text.StringBuilder    result = new System.Text.StringBuilder();
            SupportClass.SetSupport      keys   = new SupportClass.HashSetSupport(esc.Keys);
            System.String escChar  = System.Convert.ToString(encChars.EscapeCharacter);
            int           position = 0;

            while (position < text.Length)
            {
                System.Collections.IEnumerator it = keys.GetEnumerator();
                bool isReplaced = false;
                while (it.MoveNext() && !isReplaced)
                {
                    System.String seq = (System.String)it.Current;
                    System.String val = (System.String)esc[seq];
                    if (text.Substring(position, 1).Equals(val))
                    {
                        result.Append(seq);
                        isReplaced = true;
                    }
                }
                if (!isReplaced)
                {
                    result.Append(text.Substring(position, 1));
                }
                position++;
            }

            //Replace each garbled entry with the correct special value
            for (int i = 0; i < SPECIAL_ENCODING_VALUES.Length; i++)
            {
                string specialValues = SPECIAL_ENCODING_VALUES[i];
                result.Replace(EncodeSpecialCharacters(i.ToString()), specialValues);
            }
            return(result.ToString());
        }
Esempio n. 6
0
        public static System.String escape(System.String text, EncodingCharacters encChars)
        {
            //First, take all special characters and replace them with something that is garbled
            for(int i=0;i<SPECIAL_ENCODING_VALUES.Length;i++)
            {
                string specialValues = SPECIAL_ENCODING_VALUES[i];
                text = text.Replace(specialValues, EncodeSpecialCharacters(i.ToString()));
            }
            //Encode each escape character
                        System.Collections.Hashtable esc = getEscapeSequences(encChars);
            System.Text.StringBuilder result = new System.Text.StringBuilder();
            SupportClass.SetSupport keys = new SupportClass.HashSetSupport(esc.Keys);
            System.String escChar = System.Convert.ToString(encChars.EscapeCharacter);
            int position = 0;
            while (position < text.Length)
            {
                System.Collections.IEnumerator it = keys.GetEnumerator();
                bool isReplaced = false;
                while (it.MoveNext() && !isReplaced)
                {
                    System.String seq = (System.String) it.Current;
                    System.String val = (System.String) esc[seq];
                    if (text.Substring(position, 1).Equals(val))
                    {
                        result.Append(seq);
                        isReplaced = true;
                    }
                }
                if (!isReplaced)
                {
                    result.Append(text.Substring(position, 1));
                }
                position++;
            }

            //Replace each garbled entry with the correct special value
            for(int i=0;i<SPECIAL_ENCODING_VALUES.Length;i++)
            {
                string specialValues = SPECIAL_ENCODING_VALUES[i];
                result.Replace(EncodeSpecialCharacters(i.ToString()), specialValues);
            }
            return result.ToString();
        }
 /// <seealso cref="java.lang.Object.equals">
 /// </seealso>
 public override bool Equals(System.Object o)
 {
     if (o is EncodingCharacters)
     {
         EncodingCharacters other = (EncodingCharacters)o;
         if (this.FieldSeparator == other.FieldSeparator && this.ComponentSeparator == other.ComponentSeparator && this.EscapeCharacter == other.EscapeCharacter && this.RepetitionSeparator == other.RepetitionSeparator && this.SubcomponentSeparator == other.SubcomponentSeparator)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Esempio n. 8
0
        /// <summary> Applies escape sequences so that the given text can be safely transmitted
        /// in a delimited message.  A double escape character (e.g. \\) in the given
        /// text is not itself escaped (e.g. \E\\E\) but is instead translated into a
        /// single escape character for transmission (e.g. \).  This allows you to
        /// add escape sequences not handled by this method (e.g. to send \.br\ across
        /// the wire you would set the text of a field to \\.br\\).
        /// </summary>

        /*
         * public static String escape_old(String text, EncodingCharacters encChars) {
         * String result = text;
         * HashMap esc = getEscapeSequences(encChars);
         * Set keys = esc.keySet();
         * Iterator it = keys.iterator();
         *
         * //need to do the escape for the escape character first, and skip it in the loop
         * result = escapeEscapeCharacters(result, encChars);
         * String escChar = String.valueOf(encChars.getEscapeCharacter());
         * while (it.hasNext()) {
         * String seq = (String)it.next();
         * String val = (String)esc.get(seq);
         * if (!val.equals(escChar)) result = replace(result, val, seq); //don't escape the escape character here
         * }
         * return result;
         * }
         */

        /// <summary> Removes escape sequences, replacing them with the text they represent.  </summary>

        /*
         * public static String unescape_old(String text, EncodingCharacters encChars) {
         * String result = text;
         * HashMap esc = getEscapeSequences(encChars);
         * Set keys = esc.keySet();
         * Iterator it = keys.iterator();
         * while (it.hasNext()) {
         * String seq = (String)it.next();
         * String val = (String)esc.get(seq);
         * result = replace(result, seq, val);
         * }
         * return result;
         * }
         */

        /// <summary> Replaces single escape characters with the escape sequence, and double escape characters
        /// with single escape characters.
        /// </summary>

        /*
         * private static String escapeEscapeCharacters(String text, EncodingCharacters encChars) {
         * String result = text;
         * StringBuffer escCharSeq = new StringBuffer();
         * escCharSeq.append(encChars.getEscapeCharacter());
         * escCharSeq.append('E');
         * escCharSeq.append(encChars.getEscapeCharacter());
         * String escChar = String.valueOf(encChars.getEscapeCharacter());
         * result = replace(result, escChar, escCharSeq.toString());
         * result = replace(result, escCharSeq.toString() + escCharSeq.toString(), escChar);
         * return result;
         * }
         */

        /// <summary> Replaces all occurences of the string "replace" with the string "with", in
        /// the string "originalText".
        /// </summary>

        /*
         * private static String replace(String originalText, String replace, String with) {
         * StringBuffer result = new StringBuffer();
         * int replaceLength = replace.length();
         * boolean done = false;
         * int cursor = 0;
         * while (!done) {
         * int nextPosition = originalText.indexOf(replace, cursor);
         * if (nextPosition < 0) {
         * done = true;
         * result.append(originalText.substring(cursor));
         * break;
         * }
         * result.append(originalText.substring(cursor, nextPosition));
         * result.append(with);
         * cursor = nextPosition + replaceLength;
         * }
         * return result.toString();
         * }
         */

        /// <summary> Returns a HashTable with escape sequences as keys, and corresponding
        /// Strings as values.
        /// </summary>
        //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
        private static System.Collections.Hashtable getEscapeSequences(EncodingCharacters encChars)
        {
            //escape sequence strings must be assembled using the given escape character

            //see if this has already been done for this set of encoding characters
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable escapeSequences = null;
            //UPGRADE_TODO: Method 'java.util.HashMap.get' was converted to 'System.Collections.Hashtable.Item' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMapget_javalangObject'"
            System.Object o = variousEncChars[encChars];
            if (o == null)
            {
                //this means we haven't got the sequences for these encoding characters yet - let's make them
                escapeSequences           = makeEscapeSequences(encChars);
                variousEncChars[encChars] = escapeSequences;
            }
            else
            {
                //we already have escape sequences for these encoding characters
                //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
                escapeSequences = (System.Collections.Hashtable)o;
            }
            return(escapeSequences);
        }
Esempio n. 9
0
        public static System.String unescape(System.String text, EncodingCharacters encChars)
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder();
            int textLength = text.Length;

            System.Collections.Hashtable esc  = getEscapeSequences(encChars);
            SupportClass.SetSupport      keys = new SupportClass.HashSetSupport(esc.Keys);
            System.String escChar             = System.Convert.ToString(encChars.EscapeCharacter);
            int           position            = 0;

            while (position < textLength)
            {
                System.Collections.IEnumerator it = keys.GetEnumerator();
                bool isReplaced = false;
                while (it.MoveNext() && !isReplaced)
                {
                    System.String seq       = (System.String)it.Current;
                    System.String val       = (System.String)esc[seq];
                    int           seqLength = seq.Length;
                    if (position + seqLength <= textLength)
                    {
                        if (text.Substring(position, (position + seqLength) - (position)).Equals(seq))
                        {
                            result.Append(val);
                            isReplaced = true;
                            position   = position + seq.Length;
                        }
                    }
                }
                if (!isReplaced)
                {
                    result.Append(text.Substring(position, ((position + 1)) - (position)));
                    position++;
                }
            }
            return(result.ToString());
        }
Esempio n. 10
0
        /// <summary>copies contents of "other" </summary>
        public EncodingCharacters(EncodingCharacters other)
        {
            this.fieldSep = other.FieldSeparator;

            this.encChars = new char[4];

            this.encChars[0] = other.ComponentSeparator;

            this.encChars[1] = other.RepetitionSeparator;

            this.encChars[2] = other.EscapeCharacter;

            this.encChars[3] = other.SubcomponentSeparator;
        }
Esempio n. 11
0
 /// <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(Type destinationField, System.String data, EncodingCharacters encodingCharacters)
 {
     System.String[] components = split(data, System.Convert.ToString(encodingCharacters.ComponentSeparator));
     for (int i = 0; i < components.Length; i++)
     {
         System.String[] subcomponents = split(components[i], System.Convert.ToString(encodingCharacters.SubcomponentSeparator));
         for (int j = 0; j < subcomponents.Length; j++)
         {
             System.String val = subcomponents[j];
             if (val != null)
             {
                 val = Escape.unescape(val, encodingCharacters);
             }
             Terser.getPrimitive(destinationField, i + 1, j + 1).Value = val;
         }
     }
 }
Esempio n. 12
0
 /// <summary>Returns the component or subcomponent separator from the given encoding characters. </summary>
 private static char getSeparator(bool subComponents, EncodingCharacters encodingChars)
 {
     char separator;
     if (subComponents)
     {
         separator = encodingChars.SubcomponentSeparator;
     }
     else
     {
         separator = encodingChars.ComponentSeparator;
     }
     return separator;
 }
Esempio n. 13
0
 private static System.String encodePrimitive(Primitive p, EncodingCharacters encodingChars)
 {
     System.String val = ((Primitive) p).Value;
     if (val == null)
     {
         val = "";
     }
     else
     {
         val = Escape.escape(val, encodingChars);
     }
     return val;
 }
Esempio n. 14
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);
        }
Esempio n. 15
0
        public static void Main(System.String[] args)
        {
            System.String testString = "foo$r$this is $ $p$test$r$r$ string";
            //System.out.println(testString);
            //System.out.println(replace(testString, "$r$", "***"));
            //System.out.println(replace(testString, "$", "+"));

            //test speed gain with cache
            int n = 100000;
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable seqs;
            EncodingCharacters ec = new EncodingCharacters('|', "^~\\&");
            //warm up the JIT
            for (int i = 0; i < n; i++)
            {
                seqs = makeEscapeSequences(ec);
            }
            for (int i = 0; i < n; i++)
            {
                seqs = getEscapeSequences(ec);
            }
            //time
            long start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
            for (int i = 0; i < n; i++)
            {
                seqs = makeEscapeSequences(ec);
            }
            System.Console.Out.WriteLine("Time to make " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start));
            start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
            for (int i = 0; i < n; i++)
            {
                seqs = getEscapeSequences(ec);
            }
            System.Console.Out.WriteLine("Time to get " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start));
            start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
            for (int i = 0; i < n; i++)
            {
                seqs = makeEscapeSequences(ec);
            }
            System.Console.Out.WriteLine("Time to make " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start));

            //test escape:
            testString = "this | is ^ a field \\T\\ with & some ~ bad stuff \\T\\";
            System.Console.Out.WriteLine("Original:  " + testString);
            System.String escaped = escape(testString, ec);
            System.Console.Out.WriteLine("Escaped:   " + escaped);
            System.Console.Out.WriteLine("Unescaped: " + unescape(escaped, ec));
        }
Esempio n. 16
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);
                    }
                }
            }
        }
Esempio n. 17
0
        /// <summary> Returns given group serialized as a pipe-encoded string - this method is called
        /// by encode(Message source, String encoding).
        /// </summary>
        public static System.String encode(Group source, EncodingCharacters encodingChars)
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder();

            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)
                    {
                        result.Append(encode((Group) reps[rep], encodingChars));
                    }
                    else
                    {
                        System.String segString = encode((Segment) reps[rep], encodingChars);
                        if (segString.Length >= 4)
                        {
                            result.Append(segString);
                            result.Append('\r');
                        }
                    }
                }
            }
            return result.ToString();
        }
Esempio n. 18
0
 /// <summary> Encodes the given Type, using the given encoding characters. 
 /// It is assumed that the Type represents a complete field rather than a component.
 /// </summary>
 public static System.String encode(Type source, EncodingCharacters encodingChars)
 {
     System.Text.StringBuilder field = new System.Text.StringBuilder();
     for (int i = 1; i <= Terser.numComponents(source); i++)
     {
         System.Text.StringBuilder comp = new System.Text.StringBuilder();
         for (int j = 1; j <= Terser.numSubComponents(source, i); j++)
         {
             Primitive 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);
     //return encode(source, encodingChars, false);
 }
Esempio n. 19
0
 public static System.String unescape(System.String text, EncodingCharacters encChars)
 {
     System.Text.StringBuilder result = new System.Text.StringBuilder();
     int textLength = text.Length;
     System.Collections.Hashtable esc = getEscapeSequences(encChars);
     SupportClass.SetSupport keys = new SupportClass.HashSetSupport(esc.Keys);
     System.String escChar = System.Convert.ToString(encChars.EscapeCharacter);
     int position = 0;
     while (position < textLength)
     {
         System.Collections.IEnumerator it = keys.GetEnumerator();
         bool isReplaced = false;
         while (it.MoveNext() && !isReplaced)
         {
             System.String seq = (System.String) it.Current;
             System.String val = (System.String) esc[seq];
             int seqLength = seq.Length;
             if (position + seqLength <= textLength)
             {
                 if (text.Substring(position, (position + seqLength) - (position)).Equals(seq))
                 {
                     result.Append(val);
                     isReplaced = true;
                     position = position + seq.Length;
                 }
             }
         }
         if (!isReplaced)
         {
             result.Append(text.Substring(position, ((position + 1)) - (position)));
             position++;
         }
     }
     return result.ToString();
 }
Esempio n. 20
0
        /// <summary> Applies escape sequences so that the given text can be safely transmitted 
        /// in a delimited message.  A double escape character (e.g. \\) in the given 
        /// text is not itself escaped (e.g. \E\\E\) but is instead translated into a 
        /// single escape character for transmission (e.g. \).  This allows you to 
        /// add escape sequences not handled by this method (e.g. to send \.br\ across
        /// the wire you would set the text of a field to \\.br\\). 
        /// </summary>
        /*
        public static String escape_old(String text, EncodingCharacters encChars) {
        String result = text;
        HashMap esc = getEscapeSequences(encChars);
        Set keys = esc.keySet();
        Iterator it = keys.iterator();

        //need to do the escape for the escape character first, and skip it in the loop
        result = escapeEscapeCharacters(result, encChars);
        String escChar = String.valueOf(encChars.getEscapeCharacter());
        while (it.hasNext()) {
        String seq = (String)it.next();
        String val = (String)esc.get(seq);
        if (!val.equals(escChar)) result = replace(result, val, seq); //don't escape the escape character here
        }
        return result;
        }
        */
        /// <summary> Removes escape sequences, replacing them with the text they represent.  </summary>
        /*
        public static String unescape_old(String text, EncodingCharacters encChars) {
        String result = text;
        HashMap esc = getEscapeSequences(encChars);
        Set keys = esc.keySet();
        Iterator it = keys.iterator();
        while (it.hasNext()) {
        String seq = (String)it.next();
        String val = (String)esc.get(seq);
        result = replace(result, seq, val);
        }
        return result;
        }
        */
        /// <summary> Replaces single escape characters with the escape sequence, and double escape characters 
        /// with single escape characters. 
        /// </summary>
        /*
        private static String escapeEscapeCharacters(String text, EncodingCharacters encChars) {
        String result = text;
        StringBuffer escCharSeq = new StringBuffer();
        escCharSeq.append(encChars.getEscapeCharacter());
        escCharSeq.append('E');
        escCharSeq.append(encChars.getEscapeCharacter());
        String escChar = String.valueOf(encChars.getEscapeCharacter());
        result = replace(result, escChar, escCharSeq.toString());
        result = replace(result, escCharSeq.toString() + escCharSeq.toString(), escChar);
        return result;
        }
        */
        /// <summary> Replaces all occurences of the string "replace" with the string "with", in 
        /// the string "originalText". 
        /// </summary>
        /*
        private static String replace(String originalText, String replace, String with) {
        StringBuffer result = new StringBuffer();
        int replaceLength = replace.length();
        boolean done = false;
        int cursor = 0;
        while (!done) {
        int nextPosition = originalText.indexOf(replace, cursor);
        if (nextPosition < 0) {
        done = true;
        result.append(originalText.substring(cursor));
        break;
        }
        result.append(originalText.substring(cursor, nextPosition));
        result.append(with);
        cursor = nextPosition + replaceLength;
        }
        return result.toString();
        }
        */
        /// <summary> Returns a HashTable with escape sequences as keys, and corresponding 
        /// Strings as values.  
        /// </summary>
        //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
        private static System.Collections.Hashtable getEscapeSequences(EncodingCharacters encChars)
        {
            //escape sequence strings must be assembled using the given escape character

            //see if this has already been done for this set of encoding characters
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable escapeSequences = null;
            //UPGRADE_TODO: Method 'java.util.HashMap.get' was converted to 'System.Collections.Hashtable.Item' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMapget_javalangObject'"
            System.Object o = variousEncChars[encChars];
            if (o == null)
            {
                //this means we haven't got the sequences for these encoding characters yet - let's make them
                escapeSequences = makeEscapeSequences(encChars);
                variousEncChars[encChars] = escapeSequences;
            }
            else
            {
                //we already have escape sequences for these encoding characters
                //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
                escapeSequences = (System.Collections.Hashtable) o;
            }
            return escapeSequences;
        }
Esempio n. 21
0
 /// <summary> Constructs escape sequences using the given escape character - this should only 
 /// be called by getEscapeCharacter(), which will cache the results for subsequent use.
 /// </summary>
 //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
 private static System.Collections.Hashtable makeEscapeSequences(EncodingCharacters ec)
 {
     //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
     System.Collections.Hashtable seqs = new System.Collections.Hashtable();
     char[] codes = new char[]{'F', 'S', 'T', 'R', 'E'};
     char[] values = new char[]{ec.FieldSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator, ec.RepetitionSeparator, ec.EscapeCharacter};
     for (int i = 0; i < codes.Length; i++)
     {
         System.Text.StringBuilder seq = new System.Text.StringBuilder();
         seq.Append(ec.EscapeCharacter);
         seq.Append(codes[i]);
         seq.Append(ec.EscapeCharacter);
         seqs[seq.ToString()] = System.Convert.ToString(values[i]);
     }
     seqs["\\X000d\\"] = System.Convert.ToString('\r');
     return seqs;
 }
Esempio n. 22
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);
            }
        }
Esempio n. 23
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;
        }
Esempio n. 24
0
        /// <summary> Encodes the given Type, using the given encoding characters. 
        /// It is assumed that the Type represents a complete field rather than a component.
        /// </summary>
        protected virtual Control encode(Type source, EncodingCharacters encodingChars)
        {
            if(source is ca.uhn.hl7v2.model.Composite)
            {
                TableCell td;
                TableRow tr;
                Table tbl = new Table();
                tbl.Attributes["border"]=_componentBorder.ToString();
                tbl.CellSpacing = _componentCellSpacing;
                tbl.CellPadding = _componentCellPadding;
                for (int i = 1; i <= Terser.numComponents(source); i++)
                {

                    for (int j = 1; j <= Terser.numSubComponents(source, i); j++)
                    {
                        tr = new TableRow();
                        Primitive p = Terser.getPrimitive(source, i, j);
                        Type type = (Type)p;
                        td = new TableCell();
                        if(_cssComponentField!=null)
                            td.CssClass = _cssComponentField;
                        string desc = "&nbsp;";
                        if(type.Description!=null && type.Description.Trim().Length>0)
                            desc = type.Description;
                        if(j>1)
                            desc = "&nbsp;&nbsp;&nbsp;-" + desc;

                        td.Text = desc;
                        tr.Cells.Add(td);
                        string val = "&nbsp;";
                        if(p.Value!=null && p.Value.Trim().Length>0)
                        {
                            val = p.Value;
                            val = val.Replace(@"\.br\","<BR>");
                        }
                        td = new TableCell();
                        td.Text = val;
                        tr.Cells.Add(td);
                        tbl.Rows.Add(tr);
                    }

                }
                return tbl;
            }
            else
            {
                Primitive p = Terser.getPrimitive(source,1,1);
                string val = "&nbsp;";

                if(p.Value!=null && p.Value.Trim().Length>0)
                {
                    val = p.Value;
                    val = val.Replace(@"\.br\","<BR>");
                }
                return new LiteralControl(val);
            }
        }
Esempio n. 25
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);
        }
Esempio n. 26
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);
                }
            }
        }
Esempio n. 27
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);
            }
        }