Example #1
0
        /// <summary>Returns the HL7 exceptions in the given arraylist in an array </summary>
        private NuGenHL7Exception[] toArray(System.Collections.ArrayList list)
        {
            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return(retVal);
        }
Example #2
0
        /// <summary> Tests whether the given type falls within a maximum length.  </summary>
        /// <returns> null of OK, an HL7Exception otherwise
        /// </returns>
        public virtual NuGenHL7Exception testLength(Genetibase.NuGenHL7.model.Type type, int maxLength)
        {
            NuGenHL7Exception e = null;

            System.String encoded = NuGenPipeParser.encode(type, this.enc);
            if (encoded.Length > maxLength)
            {
                e = new NuGenProfileNotFollowedException("Length of " + encoded.Length + " exceeds maximum of " + maxLength);
            }
            return(e);
        }
Example #3
0
 private void  addTableTestResult(System.Collections.ArrayList exList, System.String profileID, System.String codeSystem, System.String value_Renamed)
 {
     if (codeSystem != null && value_Renamed != null)
     {
         NuGenHL7Exception e = testValueAgainstTable(profileID, codeSystem, value_Renamed);
         if (e != null)
         {
             exList.Add(e);
         }
     }
 }
Example #4
0
        /// <summary> Tests a group against a group section of a profile.</summary>
        public virtual NuGenHL7Exception[] testGroup(Group group, AbstractSegmentContainer profile, System.String profileID)
        {
            System.Collections.ArrayList exList            = new System.Collections.ArrayList(20);
            System.Collections.ArrayList allowedStructures = new System.Collections.ArrayList(20);

            for (int i = 1; i <= profile.Children; i++)
            {
                ProfileStructure struct_Renamed = profile.getChild(i);

                //only test a structure in detail if it isn't X
                if (!struct_Renamed.Usage.ToUpper().Equals("X".ToUpper()))
                {
                    allowedStructures.Add(struct_Renamed.Name);

                    //see which instances have content
                    try
                    {
                        Structure[] instances = group.getAll(struct_Renamed.Name);
                        System.Collections.ArrayList instancesWithContent = new System.Collections.ArrayList(10);
                        for (int j = 0; j < instances.Length; j++)
                        {
                            if (hasContent(instances[j]))
                            {
                                instancesWithContent.Add(instances[j]);
                            }
                        }

                        NuGenHL7Exception ce = testCardinality(instancesWithContent.Count, struct_Renamed.Min, struct_Renamed.Max, struct_Renamed.Usage, struct_Renamed.Name);
                        if (ce != null)
                        {
                            exList.Add(ce);
                        }

                        //test children on instances with content
                        for (int j = 0; j < instancesWithContent.Count; j++)
                        {
                            Structure           s = (Structure)instancesWithContent[j];
                            NuGenHL7Exception[] childExceptions = testStructure(s, struct_Renamed, profileID);
                            addToList(childExceptions, exList);
                        }
                    }
                    catch (NuGenHL7Exception)
                    {
                        exList.Add(new NuGenProfileNotHL7CompliantException(struct_Renamed.Name + " not found in message"));
                    }
                }
            }

            //complain about X structures that have content
            addToList(checkForExtraStructures(group, allowedStructures), exList);

            return(toArray(exList));
        }
Example #5
0
        /// <summary> Checks cardinality and creates an appropriate exception if out
        /// of bounds.  The usage code is needed because if min cardinality
        /// is > 0, the min # of reps is only required if the usage code
        /// is 'R' (see HL7 v2.5 section 2.12.6.4).
        /// </summary>
        /// <param name="reps">the number of reps
        /// </param>
        /// <param name="min">the minimum number of reps
        /// </param>
        /// <param name="max">the maximum number of reps (-1 means *)
        /// </param>
        /// <param name="usage">the usage code
        /// </param>
        /// <param name="name">the name of the repeating structure (used in exception msg)
        /// </param>
        /// <returns> null if cardinality OK, exception otherwise
        /// </returns>
        protected internal virtual NuGenHL7Exception testCardinality(int reps, int min, int max, System.String usage, System.String name)
        {
            NuGenHL7Exception e = null;

            if (reps < min && usage.ToUpper().Equals("R".ToUpper()))
            {
                e = new NuGenProfileNotFollowedException(name + " must have at least " + min + " repetitions (has " + reps + ")");
            }
            else if (max > 0 && reps > max)
            {
                e = new NuGenProfileNotFollowedException(name + " must have no more than " + max + " repetitions (has " + reps + ")");
            }
            return(e);
        }
Example #6
0
        /// <summary> Tests a Type against the corresponding section of a profile.</summary>
        /// <param name="encoded">optional encoded form of type (if you want to specify this -- if null,
        /// default pipe-encoded form is used to check length and constant val)
        /// </param>
        public virtual NuGenHL7Exception[] testType(Genetibase.NuGenHL7.model.Type type, AbstractComponent profile, System.String encoded, System.String profileID)
        {
            System.Collections.ArrayList exList = new System.Collections.ArrayList();
            if (encoded == null)
            {
                encoded = NuGenPipeParser.encode(type, this.enc);
            }

            NuGenHL7Exception ue = testUsage(encoded, profile.Usage, profile.Name);

            if (ue != null)
            {
                exList.Add(ue);
            }

            if (!profile.Usage.Equals("X"))
            {
                //check datatype
                System.String typeClass = type.GetType().FullName;
                if (typeClass.IndexOf("." + profile.Datatype) < 0)
                {
                    typeClass = typeClass.Substring(typeClass.LastIndexOf('.') + 1);
                    exList.Add(new NuGenProfileNotHL7CompliantException("HL7 datatype " + typeClass + " doesn't match profile datatype " + profile.Datatype));
                }

                //check length
                if (encoded.Length > profile.Length)
                {
                    exList.Add(new NuGenProfileNotFollowedException("The type " + profile.Name + " has length " + encoded.Length + " which exceeds max of " + profile.Length));
                }

                //check constant value
                if (profile.ConstantValue != null && profile.ConstantValue.Length > 0)
                {
                    if (!encoded.Equals(profile.ConstantValue))
                    {
                        exList.Add(new NuGenProfileNotFollowedException("'" + encoded + "' doesn't equal constant value of '" + profile.ConstantValue + "'"));
                    }
                }

                NuGenHL7Exception[] te = testTypeAgainstTable(type, profile, profileID);
                for (int i = 0; i < te.Length; i++)
                {
                    exList.Add(te[i]);
                }
            }

            return(this.toArray(exList));
        }
Example #7
0
        private NuGenHL7Exception testValueAgainstTable(System.String profileID, System.String codeSystem, System.String value_Renamed)
        {
            NuGenHL7Exception ret   = null;
            CodeStore         store = ProfileStoreFactory.getCodeStore(profileID, codeSystem);

            if (store == null)
            {
            }
            else
            {
                if (!store.isValidCode(codeSystem, value_Renamed))
                {
                    ret = new NuGenProfileNotFollowedException("Code " + value_Renamed + " not found in table " + codeSystem + ", profile " + profileID);
                }
            }
            return(ret);
        }
Example #8
0
        /// <summary>Tests the message by passing it to all test apps that have been registered
        /// using addTest().
        /// </summary>
        /// <returns> exceptions that describe any identified problems with the message
        /// </returns>
        public override NuGenHL7Exception[] test(Message in_Renamed)
        {
            System.Collections.ArrayList problems = new System.Collections.ArrayList(40);
            for (int i = 0; i < tests.Count; i++)
            {
                NuGenTestApplication app       = (NuGenTestApplication)tests[i];
                NuGenHL7Exception[]  shortList = app.test(in_Renamed);
                for (int j = 0; j < shortList.Length; j++)
                {
                    problems.Add(shortList[i]);
                }
            }

            NuGenHL7Exception[] retVal = new NuGenHL7Exception[problems.Count];
            problems.CopyTo(retVal);

            return(retVal);
        }
Example #9
0
        /// <summary> Tests all remaining messages available from the currrent source.</summary>
        public virtual NuGenHL7Exception[] testAll()
        {
            System.Collections.ArrayList list = new System.Collections.ArrayList();

            System.String message = null;
            while ((message = NextMessage).Length > 0)
            {
                NuGenHL7Exception e = parsesCorrectly(this.context, message);
                if (e != null)
                {
                    list.Add(e);
                }
            }

            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return(retVal);
        }
Example #10
0
        /// <summary> Tests the message against a profile or profiles.  A constant profile
        /// is used if one was provided to the constructor, otherwise any profiles
        /// declared in the message are used.
        /// </summary>
        public override NuGenHL7Exception[] test(Message in_Renamed)
        {
            NuGenValidationException[] errors = rule.test(in_Renamed);

            NuGenHL7Exception[] result = new NuGenHL7Exception[errors.Length];
            for (int i = 0; i < errors.Length; i++)
            {
                System.Exception t = errors[i].InnerException;
                if ((t != null) && (t is NuGenHL7Exception))
                {
                    result[i] = (NuGenHL7Exception)t;
                }
                else
                {
                    result[i] = new NuGenHL7Exception(errors[i]);
                }
            }

            return(result);
        }
Example #11
0
        /// <summary> Checks whether the given message parses correctly with a GenericParser.
        /// Failure indicates that the parsed and re-encoded message is semantically
        /// different than the original, or that the message could not be parsed.  This
        /// may stem from an error in the parser, or from an error in the message.  This
        /// may also arise from unexpected message components (e.g. Z-segments) although
        /// in future HAPI versions these will be parsed as well.
        /// </summary>
        /// <param name="message">an XML or ER7 encoded message string
        /// </param>
        /// <returns> null if it parses correctly, an HL7Exception otherwise
        /// </returns>
        public static NuGenHL7Exception parsesCorrectly(System.String context, System.String message)
        {
            NuGenHL7Exception problem = null;

            try
            {
                Message       m        = parser.parse(message);
                System.String encoding = parser.getEncoding(message);
                System.String result   = parser.encode(m, encoding);
                if (!NuGenEncodedMessageComparator.equivalent(message, result))
                {
                    problem = new NuGenHL7Exception(context + ": Original differs semantically from parsed/encoded message.\r\n-----Original:------------\r\n" + message + " \r\n------ Parsed/Encoded: ----------\r\n" + result + " \r\n-----Original Standardized: ---------\r\n" + NuGenEncodedMessageComparator.standardize(message) + " \r\n---------------------\r\n");
                }
            }
            catch (System.Exception e)
            {
                problem = new NuGenHL7Exception(context + ": " + e.Message + " in message: \r\n-------------\r\n" + message + "\r\n-------------");
                ;
            }
            return(problem);
        }
Example #12
0
        /// <summary> Tests an element against the corresponding usage code.  The element
        /// is required in its encoded form.
        /// </summary>
        /// <param name="encoded">the pipe-encoded message element
        /// </param>
        /// <param name="usage">the usage code (e.g. "CE")
        /// </param>
        /// <param name="name">the name of the element (for use in exception messages)
        /// </param>
        /// <returns>s null if there is no problem, an HL7Exception otherwise
        /// </returns>
        private NuGenHL7Exception testUsage(System.String encoded, System.String usage, System.String name)
        {
            NuGenHL7Exception e = null;

            if (usage.ToUpper().Equals("R".ToUpper()))
            {
                if (encoded.Length == 0)
                {
                    e = new NuGenProfileNotFollowedException("Required element " + name + " is missing");
                }
            }
            else if (usage.ToUpper().Equals("RE".ToUpper()))
            {
                //can't test anything
            }
            else if (usage.ToUpper().Equals("O".ToUpper()))
            {
                //can't test anything
            }
            else if (usage.ToUpper().Equals("C".ToUpper()))
            {
                //can't test anything yet -- wait for condition syntax in v2.6
            }
            else if (usage.ToUpper().Equals("CE".ToUpper()))
            {
                //can't test anything
            }
            else if (usage.ToUpper().Equals("X".ToUpper()))
            {
                if (encoded.Length > 0)
                {
                    e = new NuGenXElementPresentException("Element " + name + " is present but specified as not used (X)");
                }
            }
            else if (usage.ToUpper().Equals("B".ToUpper()))
            {
                //can't test anything
            }
            return(e);
        }
Example #13
0
        /// <summary> Sets the source reader to point to the given file, and tests
        /// all the messages therein (if a directory, processes all contained
        /// files recursively).
        /// </summary>
        public virtual NuGenHL7Exception[] testAll(System.IO.FileInfo source)
        {
            System.Collections.ArrayList list = new System.Collections.ArrayList();
            System.Console.Out.WriteLine("Testing " + source.FullName);
            if (System.IO.Directory.Exists(source.FullName))
            {
                System.IO.FileInfo[] contents = SupportClass.FileSupport.GetFiles(source);
                for (int i = 0; i < contents.Length; i++)
                {
                    NuGenHL7Exception[] exceptions = testAll(contents[i]);
                    for (int j = 0; j < exceptions.Length; j++)
                    {
                        list.Add(exceptions[j]);
                    }
                }
            }
            else if (System.IO.File.Exists(source.FullName))
            {
                System.IO.StreamReader in_Renamed = new System.IO.StreamReader(source.FullName, System.Text.Encoding.Default);
                Source  = in_Renamed;
                Context = source.FullName;
                NuGenHL7Exception[] exceptions = testAll();
                for (int i = 0; i < exceptions.Length; i++)
                {
                    list.Add(exceptions[i]);
                }
            }
            else
            {
                System.Console.Out.WriteLine("Warning: " + source.FullName + " is not a normal file");
            }

            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return(retVal);
        }
Example #14
0
        /// <summary> Tests a segment against a segment section of a profile.</summary>
        public virtual NuGenHL7Exception[] testSegment(Genetibase.NuGenHL7.model.Segment segment, Seg profile, System.String profileID)
        {
            System.Collections.ArrayList exList        = new System.Collections.ArrayList(20);
            System.Collections.ArrayList allowedFields = new System.Collections.ArrayList(20);

            for (int i = 1; i <= profile.Fields; i++)
            {
                Field field = profile.getField(i);

                //only test a field in detail if it isn't X
                if (!field.Usage.ToUpper().Equals("X".ToUpper()))
                {
                    allowedFields.Add((System.Int32)i);

                    //see which instances have content
                    try
                    {
                        Genetibase.NuGenHL7.model.Type[] instances            = segment.getField(i);
                        System.Collections.ArrayList     instancesWithContent = new System.Collections.ArrayList(10);
                        for (int j = 0; j < instances.Length; j++)
                        {
                            if (hasContent(instances[j]))
                            {
                                instancesWithContent.Add(instances[j]);
                            }
                        }

                        NuGenHL7Exception ce = testCardinality(instancesWithContent.Count, field.Min, field.Max, field.Usage, field.Name);
                        if (ce != null)
                        {
                            ce.FieldPosition = i;
                            exList.Add(ce);
                        }

                        //test field instances with content
                        for (int j = 0; j < instancesWithContent.Count; j++)
                        {
                            Genetibase.NuGenHL7.model.Type s = (Genetibase.NuGenHL7.model.Type)instancesWithContent[j];

                            bool escape = true;                             //escape field value when checking length
                            if (profile.Name.ToUpper().Equals("MSH".ToUpper()) && i < 3)
                            {
                                escape = false;
                            }
                            NuGenHL7Exception[] childExceptions = testField(s, field, escape, profileID);
                            for (int k = 0; k < childExceptions.Length; k++)
                            {
                                childExceptions[k].FieldPosition = i;
                            }
                            addToList(childExceptions, exList);
                        }
                    }
                    catch (NuGenHL7Exception)
                    {
                        exList.Add(new NuGenProfileNotHL7CompliantException("Field " + i + " not found in message"));
                    }
                }
            }

            //complain about X fields with content
            this.addToList(checkForExtraFields(segment, allowedFields), exList);

            NuGenHL7Exception[] ret = toArray(exList);
            for (int i = 0; i < ret.Length; i++)
            {
                ret[i].SegmentName = profile.Name;
            }
            return(ret);
        }
		/// <summary> Sets the source reader to point to the given file, and tests
		/// all the messages therein (if a directory, processes all contained
		/// files recursively).
		/// </summary>
		public virtual NuGenHL7Exception[] testAll(System.IO.FileInfo source)
		{
			System.Collections.ArrayList list = new System.Collections.ArrayList();
			System.Console.Out.WriteLine("Testing " + source.FullName);
			if (System.IO.Directory.Exists(source.FullName))
			{
				System.IO.FileInfo[] contents = SupportClass.FileSupport.GetFiles(source);
				for (int i = 0; i < contents.Length; i++)
				{
					NuGenHL7Exception[] exceptions = testAll(contents[i]);
					for (int j = 0; j < exceptions.Length; j++)
					{
						list.Add(exceptions[j]);
					}
				}
			}
			else if (System.IO.File.Exists(source.FullName))
			{
				System.IO.StreamReader in_Renamed = new System.IO.StreamReader(source.FullName, System.Text.Encoding.Default);
				Source = in_Renamed;
				Context = source.FullName;
				NuGenHL7Exception[] exceptions = testAll();
				for (int i = 0; i < exceptions.Length; i++)
				{
					list.Add(exceptions[i]);
				}
			}
			else
			{
				System.Console.Out.WriteLine("Warning: " + source.FullName + " is not a normal file");
			}

            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return retVal;
		}
		/// <summary> Tests all remaining messages available from the currrent source.</summary>
		public virtual NuGenHL7Exception[] testAll()
		{
			System.Collections.ArrayList list = new System.Collections.ArrayList();
			
			System.String message = null;
			while ((message = NextMessage).Length > 0)
			{
				NuGenHL7Exception e = parsesCorrectly(this.context, message);
				if (e != null)
					list.Add(e);
			}

            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return retVal;
		}
		/// <summary> Checks whether the given message parses correctly with a GenericParser.
		/// Failure indicates that the parsed and re-encoded message is semantically
		/// different than the original, or that the message could not be parsed.  This 
		/// may stem from an error in the parser, or from an error in the message.  This 
		/// may also arise from unexpected message components (e.g. Z-segments) although 
		/// in future HAPI versions these will be parsed as well.
		/// </summary>
		/// <param name="message">an XML or ER7 encoded message string
		/// </param>
		/// <returns> null if it parses correctly, an HL7Exception otherwise
		/// </returns>
		public static NuGenHL7Exception parsesCorrectly(System.String context, System.String message)
		{
			NuGenHL7Exception problem = null;
			try
			{
				Message m = parser.parse(message);
				System.String encoding = parser.getEncoding(message);
				System.String result = parser.encode(m, encoding);
				if (!NuGenEncodedMessageComparator.equivalent(message, result))
				{
					problem = new NuGenHL7Exception(context + ": Original differs semantically from parsed/encoded message.\r\n-----Original:------------\r\n" + message + " \r\n------ Parsed/Encoded: ----------\r\n" + result + " \r\n-----Original Standardized: ---------\r\n" + NuGenEncodedMessageComparator.standardize(message) + " \r\n---------------------\r\n");
				}
			}
			catch (System.Exception e)
			{
				problem = new NuGenHL7Exception(context + ": " + e.Message + " in message: \r\n-------------\r\n" + message + "\r\n-------------");
				;
			}
			return problem;
		}
		/// <summary>Appends an array of HL7 exceptions to a list </summary>
		private void  addToList(NuGenHL7Exception[] exceptions, System.Collections.ArrayList list)
		{
			for (int i = 0; i < exceptions.Length; i++)
			{
				list.Add(exceptions[i]);
			}
		}
		/// <summary>Returns the HL7 exceptions in the given arraylist in an array </summary>
		private NuGenHL7Exception[] toArray(System.Collections.ArrayList list)
		{
            NuGenHL7Exception[] retVal = new NuGenHL7Exception[list.Count];
            list.CopyTo(retVal);

            return retVal;
		}