Example #1
0
        public static List <String> Validate(String Interaction, MemoryStream XmlInstance, Type TypeReference)
        {
            List <String> errorList = new List <string>(10);


            // rewind the stream so we can validate in memory
            XmlInstance.Seek(0, SeekOrigin.Begin);

            // TODO: verify schemas were loaded using ValidationSettings.Schemas.Count
            XmlReader rdr = null;

            try
            {
                Assembly release = TypeReference.Assembly;
                // Find the appropriate schema set to use
                XmlReaderSettings settings    = null;
                object[]          releaseName = release.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
                if (releaseName.Length > 0)
                {
                    settings = GetSettings((releaseName[0] as AssemblyInformationalVersionAttribute).InformationalVersion);
                    if (settings == null)
                    {
                        Assert.Fail("Cannot find validation settings for '{0}'", (releaseName[0] as AssemblyInformationalVersionAttribute).InformationalVersion);
                    }
                }
                else
                {
                    Assert.Fail("This assembly carries no version information with it");
                }
                settings.ValidationType          = ValidationType.Schema;
                settings.ValidationEventHandler += new ValidationEventHandler(delegate(object sender, ValidationEventArgs e)
                {
                    if (e.Severity == XmlSeverityType.Error && !e.Message.Contains("specializationType") && !e.Message.Contains("incomplete") &&
                        !e.Message.Contains("possible elements expected"))     // Usually incomplete is due to the simple type creator not creating the necessary elements
                    {
                        errorList.Add(String.Format("{0} : Validation exception: {1} @ {2},{3}", e.Severity, e.Message, e.Exception.LineNumber, e.Exception.LinePosition));
                    }
                });

                rdr = XmlReader.Create(XmlInstance, settings);

                try
                {
                    while (rdr.Read())
                    {
                        ;
                    }
                }
                catch (XmlException e)
                {
                    Tracer.Trace("Validation exception: " + e.Message);
                    Tracer.Trace("          Line Number: " + e.LineNumber);
                    errorList.Add("Error: " + e.Message + "Line Number: " + e.LineNumber);
                }
            }
            catch (Exception ReaderCreateException)
            {
                Assert.Fail("Exception: " + ReaderCreateException.ToString());
            }


            if (rdr.ReadState == ReadState.EndOfFile)
            {
                //DebugMessage = "\nValidation complete.";
            }
            else if (rdr.ReadState == ReadState.Error)
            {
                errorList.Add("Validation encountered one or more serious errors and could not continue.");
            }
            //System.Diagnostics.Debug.WriteLine(DebugMessage);



            return(errorList);
        }
Example #2
0
        static XmlReaderSettings GetSettings(String release)
        {
            XmlReaderSettings retVal = null;

            if (ValidationSettings.TryGetValue(release, out retVal))
            {
                return(retVal);
            }

            string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(tmpDir);

            Assembly asm = typeof(XMLValidator).Assembly;

            try
            {
                foreach (var item in asm.GetManifestResourceNames())
                {
                    if (!item.EndsWith("zip"))
                    {
                        continue;
                    }

                    string itemRelease = item.Replace("MARC.Everest.Test.Resources.", "").Replace(".zip", "");
                    if (itemRelease != release)
                    {
                        continue;
                    }

                    ZipInputStream zis = null;
                    Tracer.Trace(item);

                    try
                    {
                        zis    = new ZipInputStream(asm.GetManifestResourceStream(item));
                        retVal = new XmlReaderSettings();

                        // Prepare the unzipping operation
                        ZipEntry entry    = null;
                        String   basePath = Path.Combine(tmpDir, item);
                        if (!Directory.Exists(basePath))
                        {
                            Directory.CreateDirectory(basePath);
                        }

                        List <String> files = new List <string>(10);

                        // Unzip the rmim package
                        while ((entry = zis.GetNextEntry()) != null)
                        {
                            if (entry.IsDirectory) // entry is a directory
                            {
                                string dirName = Path.Combine(basePath, entry.Name);
                                if (!Directory.Exists(dirName))
                                {
                                    Directory.CreateDirectory(dirName);
                                }
                            }
                            else if (entry.IsFile) // entry is file, so extract file.
                            {
                                string     fName = Path.Combine(basePath, entry.Name);
                                FileStream fs    = null;
                                try
                                {
                                    fs = File.Create(fName);
                                    byte[] buffer = new byte[2048]; // 2k buffer
                                    int    szRead = 2048;
                                    while (szRead > 0)
                                    {
                                        szRead = zis.Read(buffer, 0, buffer.Length);
                                        if (szRead > 0)
                                        {
                                            fs.Write(buffer, 0, szRead);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (fs != null)
                                    {
                                        fs.Close();
                                    }
                                }

                                if (fName.EndsWith(".xsd"))
                                {
                                    files.Add(fName);
                                }
                            }
                        }

                        foreach (var fName in files)
                        {
                            retVal.Schemas.Add("urn:hl7-org:v3", fName);
                        }

                        retVal.Schemas.ValidationEventHandler += new ValidationEventHandler(Schemas_ValidationEventHandler);
                        retVal.Schemas.Compile();
                        ValidationSettings.Add(release, retVal);
                        Directory.Delete(basePath, true);
                        return(retVal);
                    }
                    catch (Exception e)
                    {
                        //Assert.Fail(e.ToString());
                        return(retVal);
                    }
                    finally
                    {
                        if (zis != null)
                        {
                            zis.Close();
                            zis.Dispose();
                        }
                    }
                }
            }
            finally
            {
                Directory.Delete(tmpDir, true);
                System.GC.Collect();
            }
            return(retVal);
        }
Example #3
0
 static void Schemas_ValidationEventHandler(object sender, ValidationEventArgs e)
 {
     Tracer.Trace(e.Exception.ToString());
 }