/// <param name="message">an ER7 or XML encoded message to validate
        /// </param>
        /// <param name="isXML">true if XML, false if ER7
        /// </param>
        /// <param name="version">HL7 version (e.g. "2.2") to which the message belongs
        /// </param>
        /// <returns> true if the message is OK
        /// </returns>
        /// <throws>  HL7Exception if there is at least one error and this validator is set to fail on errors </throws>
        public virtual bool validate(System.String message, bool isXML, System.String version)
        {
            IEncodingRule[]     rules   = myContext.getEncodingRules(version, isXML ? "XML" : "ER7");
            ValidationException toThrow = null;
            bool result = true;

            for (int i = 0; i < rules.Length; i++)
            {
                ValidationException[] ex = rules[i].test(message);
                for (int j = 0; j < ex.Length; j++)
                {
                    result = false;
                    ourLog.Error("Invalid message", ex[j]);
                    if (failOnError && toThrow == null)
                    {
                        toThrow = ex[j];
                    }
                }
            }

            if (toThrow != null)
            {
                throw new HL7Exception("Invalid message", toThrow);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Get enconding rules
        /// </summary>
        /// <param name="theVersion"></param>
        /// <param name="theEncoding"></param>
        /// <returns></returns>
        public IEncodingRule[] getEncodingRules(string theVersion, string theEncoding)
        {
            IEncodingRule[] result = new IEncodingRule[0];
            if (originalContext != null)
            {
                result = originalContext.getEncodingRules(theVersion, theEncoding);
            }

            foreach (ISpecificEncodingRule rule in encodingRules)
            {
                // Add the rule if it applies
                bool flag1 = rule.GetVersions().Contains("*") || rule.GetVersions().Contains(theVersion);
                bool flag2 = rule.GetEncodings().Contains("*") || rule.GetEncodings().Contains(theEncoding);
                if (flag1 && flag2)
                {
                    List <IEncodingRule> rules = new List <IEncodingRule>(result);
                    rules.Add(rule);
                    result = rules.ToArray();
                }
            }

            return(result);
        }