Ejemplo n.º 1
0
        /// <summary>
        /// Adds individual FHIR Resource Instance files attached to the implementation guide being exported
        /// to the ZIP package
        /// </summary>
        /// <remarks>Uses the mime-type of the file to determine if the attached file is xml or json.</remarks>
        private void AddResourceInstances()
        {
            var parserSettings = new fhir_stu3.Hl7.Fhir.Serialization.ParserSettings();

            parserSettings.AcceptUnknownMembers        = true;
            parserSettings.AllowUnrecognizedEnums      = true;
            parserSettings.DisallowXsiAttributesOnRoot = false;
            var fhirXmlParser  = new FhirXmlParser(parserSettings);
            var fhirJsonParser = new FhirJsonParser(parserSettings);

            // Check that each FHIR resource instance is valid and has the required fields
            foreach (var file in ig.Files)
            {
                var fileData = file.GetLatestData();
                fhir_stu3.Hl7.Fhir.Model.Resource resource = null;
                string resourceContent;
                string fileExtension = "";

                try
                {
                    string fileContent = System.Text.Encoding.UTF8.GetString(fileData.Data);

                    if (file.MimeType == "application/xml" || file.MimeType == "text/xml")
                    {
                        resource      = fhirXmlParser.Parse <fhir_stu3.Hl7.Fhir.Model.Resource>(fileContent);
                        fileExtension = "xml";
                    }
                    else if (file.MimeType == "application/json" || file.MimeType == "binary/octet-stream")
                    {
                        resource      = fhirJsonParser.Parse <fhir_stu3.Hl7.Fhir.Model.Resource>(fileContent);
                        fileExtension = "json";
                    }
                }
                catch
                {
                }

                if (resource == null || string.IsNullOrEmpty(resource.Id))
                {
                    continue;
                }

                try
                {
                    // Convert the resource to the desired format for output
                    resourceContent = this.Serialize(resource);
                    fileExtension   = this.JsonFormat ? "json" : "xml";
                    string fileName = string.Format("resources/{0}/{1}.{2}", resource.ResourceType.ToString().ToLower(), resource.Id, fileExtension);

                    // Add the resource to the zip file
                    this.zip.AddEntry(fileName, resourceContent);
                }
                catch
                {
                    string fileName = string.Format("resources/{0}/{1}.{2}", resource.ResourceType.ToString().ToLower(), resource.Id, fileExtension);
                    this.zip.AddEntry(fileName, fileData.Data);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds examples for the profiles to the ZIP package. Only adds valid samples to the zip package.
        /// </summary>
        /// <remarks>Attempts to use the fhir-net-api to parse the resource as Xml and then as Json. If the sample
        /// cannot be parsed successfully, then it is skipped and not added to the ZIP package.</remarks>
        private void AddExamples()
        {
            // Validate that each of the samples associated with profiles has the required fields
            var templateExamples = (from t in this.templates
                                    join ts in this.tdb.TemplateSamples on t.Id equals ts.TemplateId
                                    select new { Template = t, Sample = ts });

            var parserSettings = new fhir_stu3.Hl7.Fhir.Serialization.ParserSettings();

            parserSettings.AcceptUnknownMembers        = true;
            parserSettings.AllowUnrecognizedEnums      = true;
            parserSettings.DisallowXsiAttributesOnRoot = false;
            var fhirXmlParser  = new FhirXmlParser(parserSettings);
            var fhirJsonParser = new FhirJsonParser(parserSettings);

            foreach (var templateExample in templateExamples)
            {
                fhir_stu3.Hl7.Fhir.Model.Resource resource = null;
                string fileExtension = "";

                DataExamples.StructureDefinition strucDefExamples = new DataExamples.StructureDefinition();

                if (this.dataExamples.StructureDefinitions.ContainsKey(templateExample.Template.Bookmark))
                {
                    strucDefExamples = this.dataExamples.StructureDefinitions[templateExample.Template.Bookmark];
                }
                else
                {
                    this.dataExamples.StructureDefinitions.Add(templateExample.Template.Bookmark, strucDefExamples);
                }

                try
                {
                    resource      = fhirXmlParser.Parse <fhir_stu3.Hl7.Fhir.Model.Resource>(templateExample.Sample.XmlSample);
                    fileExtension = "xml";
                }
                catch
                {
                }

                try
                {
                    if (resource == null)
                    {
                        resource      = fhirJsonParser.Parse <fhir_stu3.Hl7.Fhir.Model.Resource>(templateExample.Sample.XmlSample);
                        fileExtension = "json";
                    }
                }
                catch
                {
                }

                if (resource == null || string.IsNullOrEmpty(resource.Id))
                {
                    continue;
                }

                string fileName = string.Format("resources/{0}/{1}.{2}", resource.ResourceType.ToString().ToLower(), resource.Id, fileExtension);
                this.zip.AddEntry(fileName, templateExample.Sample.XmlSample);

                // Add the sample to the control file
                string keyValue  = string.Format("{0}/{1}", resource.ResourceType.ToString(), resource.Id);
                string baseValue = string.Format("{0}-{1}.html", resource.ResourceType.ToString(), resource.Id);

                this.control.Resources.Add(keyValue, new Models.Control.ResourceReference()
                {
                    //template_base = "instance-template-example.html",
                    ReferenceBase = baseValue
                });

                strucDefExamples.Examples.Add(new DataExamples.Example()
                {
                    Type = resource.ResourceType.ToString(),
                    Id   = resource.Id
                });
            }
        }