/// <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); } } }
public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { bool readXml = false; // Default to reading JSON // If the request content-type was not specified, or the content-type specified is not JSON or XML if (string.IsNullOrEmpty(RequestContentType) || (!ContentType.JSON_CONTENT_HEADERS.Contains(RequestContentType) && !ContentType.XML_CONTENT_HEADERS.Contains(RequestContentType))) { // Check if a _format param was passed, and is an XML content type if (HttpContext.Current != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Params["_format"]) && ContentType.XML_CONTENT_HEADERS.Contains(HttpContext.Current.Request.Params["_format"])) { readXml = true; } } // Otherwise check if the request's content-type is an XML content-type else if (ContentType.XML_CONTENT_HEADERS.Contains(RequestContentType)) { readXml = true; } return(System.Threading.Tasks.Task.Factory.StartNew <object>(() => { try { var body = base.ReadBodyFromStream(readStream, content); if (type == typeof(Resource) || type.IsSubclassOf(typeof(Resource))) { var parserSettings = new fhir_stu3.Hl7.Fhir.Serialization.ParserSettings(); parserSettings.AcceptUnknownMembers = true; parserSettings.AllowUnrecognizedEnums = true; parserSettings.DisallowXsiAttributesOnRoot = false; if (readXml) { var fhirXmlParser = new FhirXmlParser(parserSettings); return fhirXmlParser.Parse <Resource>(body); } else { var fhirJsonParser = new FhirJsonParser(parserSettings); return fhirJsonParser.Parse <Resource>(body); } } else { throw new NotSupportedException(String.Format("Cannot read unsupported type {0} from body", type.Name)); } } catch (FormatException exc) { throw new Exception("Body parsing failed: " + exc.Message); } })); }
public STU3Validator(IObjectRepository tdb) : base(tdb) { var parserSettings = new fhir_stu3.Hl7.Fhir.Serialization.ParserSettings(); parserSettings.AcceptUnknownMembers = true; parserSettings.AllowUnrecognizedEnums = true; parserSettings.DisallowXsiAttributesOnRoot = false; this.fhirXmlParser = new FhirXmlParser(parserSettings); this.fhirJsonParser = new FhirJsonParser(parserSettings); }
public FhirImplementationGuide Convert(ImplementationGuide ig, SummaryType?summaryType = null, bool includeVocabulary = true) { 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); string url = string.Format("ImplementationGuide/{0}", ig.Id); if (!string.IsNullOrEmpty(ig.Identifier) && (ig.Identifier.StartsWith("http://") || ig.Identifier.StartsWith("https://"))) { url = ig.Identifier.TrimEnd('/') + "/" + url.TrimStart('/'); } var fhirImplementationGuide = new FhirImplementationGuide() { Id = ig.Id.ToString(), Name = ig.Name, Url = url }; // Status if (ig.PublishStatus == PublishStatus.GetPublishedStatus(this.tdb)) { fhirImplementationGuide.Status = PublicationStatus.Active; } else if (ig.PublishStatus == PublishStatus.GetRetiredStatus(this.tdb) || ig.PublishStatus == PublishStatus.GetDeprecatedStatus(this.tdb)) { fhirImplementationGuide.Status = PublicationStatus.Retired; } else { fhirImplementationGuide.Status = PublicationStatus.Draft; } if (summaryType == null || summaryType == SummaryType.Data) { // Package FhirImplementationGuide.PackageComponent package = new FhirImplementationGuide.PackageComponent(); package.Name = "Profiles in this Implementation Guide"; fhirImplementationGuide.Package.Add(package); // Page: Create a page for the implementation guide. This is required by the fhir ig publisher fhirImplementationGuide.Page = new FhirImplementationGuide.PageComponent(); fhirImplementationGuide.Page.Kind = FhirImplementationGuide.GuidePageKind.Page; fhirImplementationGuide.Page.Title = ig.GetDisplayName(); fhirImplementationGuide.Page.Source = string.Format("{0}://{1}/IG/View/{2}", this.scheme, this.authority, ig.Id); // Add profiles to the implementation guide List <Template> templates = ig.GetRecursiveTemplates(this.tdb, inferred: false); var profileResources = (from t in templates.OrderBy(y => y.ImpliedTemplateId) select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("StructureDefinition/{0}", t.FhirId()), Display = t.Name } }); package.Resource.AddRange(profileResources); if (includeVocabulary) { // Add value sets to the implementation guide var valueSetsIds = (from t in templates join tc in this.tdb.TemplateConstraints.AsNoTracking() on t.Id equals tc.TemplateId where tc.ValueSetId != null select tc.ValueSetId) .Distinct() .ToList(); var valueSets = (from vs in this.tdb.ValueSets join vsi in valueSetsIds on vs.Id equals vsi select vs).ToList(); var valueSetResources = (from vs in valueSets where vs.GetIdentifier() != null && !vs.GetIdentifier().StartsWith("http://hl7.org/fhir/ValueSet/") // Ignore value sets in the base spec select new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("ValueSet/{0}", vs.GetFhirId()), Display = vs.Name } }); package.Resource.AddRange(valueSetResources); } // Add each of the individual FHIR resources added as files to the IG foreach (var file in ig.Files) { var fileData = file.GetLatestData(); Resource resource = null; try { string fileContent = System.Text.Encoding.UTF8.GetString(fileData.Data); if (file.MimeType == "application/xml" || file.MimeType == "text/xml") { resource = fhirXmlParser.Parse <Resource>(fileContent); } else if (file.MimeType == "application/json") { resource = fhirJsonParser.Parse <Resource>(fileContent); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageFile = new FhirImplementationGuide.ResourceComponent() { Example = false, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, file.FileName) } }; package.Resource.Add(packageFile); } // Add each of the samples generated for the template/profile var templateExamples = (from t in templates join ts in this.tdb.TemplateSamples on t.Id equals ts.TemplateId select new { Template = t, Sample = ts }); foreach (var templateExample in templateExamples) { Resource resource = null; try { resource = fhirXmlParser.Parse <Resource>(templateExample.Sample.XmlSample); } catch { } try { if (resource == null) { resource = fhirJsonParser.Parse <Resource>(templateExample.Sample.XmlSample); } } catch { } if (resource == null || string.IsNullOrEmpty(resource.Id)) { continue; } var packageExample = new FhirImplementationGuide.ResourceComponent() { Example = true, Source = new ResourceReference() { Reference = string.Format("{0}/{1}", resource.ResourceType, resource.Id), Display = GetResourceName(resource, templateExample.Sample.Name) }, ExampleFor = new ResourceReference() { Reference = string.Format("StructureDefinition/{0}", templateExample.Template.Bookmark), Display = templateExample.Template.Name } }; package.Resource.Add(packageExample); } } return(fhirImplementationGuide); }
/// <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 }); } }