private ValidationDocument GetGeneratedValidationDocument(IObjectRepository tdb, int implementationGuideId) { ImplementationGuide ig = tdb.ImplementationGuides.Single(y => y.Id == implementationGuideId); ImplementationGuideFile vocFile = tdb.ImplementationGuideFiles.FirstOrDefault(y => y.ContentType == ImplementationGuideFile.ContentTypeVocabulary && y.ImplementationGuideId == ig.Id); string vocFilename = "voc.xml"; if (vocFile != null) { vocFilename = vocFile.FileName; } List <Template> templates = ig.GetRecursiveTemplates(tdb); SchematronGenerator schGenerator = new SchematronGenerator(tdb, ig, templates, true, vocFileName: vocFilename); string schContent = schGenerator.Generate(); string schName = Trifolia.Shared.Helper.NormalizeName(ig.Name) + ".sch"; ValidationDocument schDoc = new ValidationDocument() { Content = ASCIIEncoding.UTF8.GetBytes(schContent), ContentType = ImplementationGuideFile.ContentTypeSchematron, MimeType = "text/xml", Name = schName }; return(schDoc); }
private static XmlDocument GenerateDocument(SchematronGenerator generator, out XmlNamespaceManager nsManager) { string schContent = generator.Generate(); XmlDocument doc = new XmlDocument(); doc.LoadXml(schContent); nsManager = new XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("sch", "http://purl.oclc.org/dsdl/schematron"); return(doc); }
public HttpResponseMessage ExportSchematron(SchematronSettingsModel model) { ImplementationGuide ig = this.tdb.ImplementationGuides.Single(y => y.Id == model.ImplementationGuideId); VocabularyOutputType vocOutputType = GetVocOutputType(model.ValueSetOutputFormat); var templates = (from t in this.tdb.Templates where model.TemplateIds.Contains(t.Id) select t).ToList(); string schematronResult = SchematronGenerator.Generate(tdb, ig, model.IncludeCustomSchematron, vocOutputType, model.VocabularyFileName, templates, model.SelectedCategories, model.DefaultSchematron); byte[] data = ASCIIEncoding.UTF8.GetBytes(schematronResult); string fileName = string.Format("{0}.sch", ig.GetDisplayName(true)); return(GetExportResponse(fileName, XML_MIME_TYPE, data)); }
protected override void ProcessRecord() { ImplementationGuide ig = this.tdb.ImplementationGuides.Single(y => y.Id == this.ImplementationGuideId); SimpleSchema schema = ig.ImplementationGuideType.GetSimpleSchema(); IGSettingsManager igSettings = new IGSettingsManager(this.tdb, ig.Id); var templates = ig.GetRecursiveTemplates(this.tdb); string schematron = SchematronGenerator.Generate(this.tdb, ig, true, VocabularyOutputType.Default, this.VocabFileName, templates); if (!string.IsNullOrEmpty(this.OutputFileName)) { File.WriteAllText(this.OutputFileName, schematron); } else { this.WriteObject(schematron); } }
public void ExportCategory1() { List <string> categories = new List <string>(); categories.Add("CAT1"); SchematronGenerator generator = new SchematronGenerator(this.tdb, this.ig, this.tdb.Templates.ToList(), true, categories: categories); XmlNamespaceManager nsManager = null; XmlDocument doc = GenerateDocument(generator, out nsManager); XmlNode errorPattern = doc.DocumentElement.SelectSingleNode("//sch:pattern[@id='p-urn-oid-1.2.3.4-errors']", nsManager); XmlNode warningPattern = doc.DocumentElement.SelectSingleNode("//sch:pattern[@id='p-urn-oid-1.2.3.4-warnings']", nsManager); Assert.IsNotNull(errorPattern); Assert.IsNotNull(warningPattern); // Un-categorized AssertXML.XPathExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-1']", "Did not find assertion for un-categorized constraint"); AssertXML.XPathExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-1'][text()='SHALL contain exactly one [1..1] code (CONF:1-1).']", "Unexpected narrative for uncategorized constraint"); // Category 2 AssertXML.XPathNotExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-3']", "Did not find assertion for category 2 constraint"); AssertXML.XPathNotExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-3'][text()='[CAT2] SHALL contain exactly one [1..1] value (CONF:1-3).']", "Unexpected narrative for category 2 constraint"); // Category 1 AssertXML.XPathExists(warningPattern, nsManager, "sch:rule/sch:assert[@id='a-1-2']", "Did not find assertion for category 2 constraint"); AssertXML.XPathExists(warningPattern, nsManager, "sch:rule/sch:assert[@id='a-1-2'][text()='[CAT1] SHOULD contain zero or one [0..1] value (CONF:1-2).']", "Unexpected narrative for category 1 constraint"); // Category 1 & 2 AssertXML.XPathExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-13']", "Did not find assertion for category 1&2 constraint"); AssertXML.XPathExists(errorPattern, nsManager, "sch:rule/sch:assert[@id='a-1-13'][text()='[CAT1,CAT2] SHALL contain exactly one [1..1] effectiveTime (CONF:1-13).']", "Unexpected narrative in assertion for category 1&2 constraint"); // First branch (REFR) AssertXML.XPathExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-errors']/sch:assert[@id='a-1-4']", "Did not find assertion for branch 1 root in main template pattern"); AssertXML.XPathExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-4-branch-4-errors']/sch:assert[@id='a-1-6-branch-4']", "Did not find child constraint's assertion for branch 1"); // Second branch (SUBJ) AssertXML.XPathExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-errors']/sch:assert[@id='a-1-7']", "Did not find assertion for branch 2 root in main template pattern"); AssertXML.XPathExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-7-branch-7-errors']/sch:assert[@id='a-1-9-branch-7']", "Did not find child constraint's assertion for branch 2"); // Third branch (XXXX) AssertXML.XPathNotExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-errors']/sch:assert[@id='a-1-10']", "Did not find assertion for branch 3 root in main template pattern"); AssertXML.XPathNotExists(errorPattern, nsManager, "sch:rule[@id='r-urn-oid-1.2.3.4-10-branch-10-errors']/sch:assert[@id='a-1-12-branch-10']", "Did not find child constraint's assertion for branch 3"); }
public HttpResponseMessage Export(ExportSettingsModel model) { ImplementationGuide ig = this.tdb.ImplementationGuides.SingleOrDefault(y => y.Id == model.ImplementationGuideId); if (model.TemplateIds == null) { model.TemplateIds = ig.GetRecursiveTemplates(this.tdb, categories: model.SelectedCategories.ToArray()).Select(y => y.Id).ToList(); } List <Template> templates = this.tdb.Templates.Where(y => model.TemplateIds.Contains(y.Id)).ToList(); SimpleSchema schema = SimplifiedSchemaContext.GetSimplifiedSchema(HttpContext.Current.Application, ig.ImplementationGuideType); IIGTypePlugin igTypePlugin = IGTypePluginFactory.GetPlugin(ig.ImplementationGuideType); IGSettingsManager igSettings = new IGSettingsManager(this.tdb, model.ImplementationGuideId); bool isCDA = ig.ImplementationGuideType.SchemaURI == "urn:hl7-org:v3"; string fileName; byte[] export; string contentType = null; switch (model.ExportFormat) { case ExportFormats.FHIR_Build_Package: case ExportFormats.FHIR_Bundle: case ExportFormats.Native_XML: case ExportFormats.Templates_DSTU_XML: string fileExtension = model.ReturnJson ? "json" : "xml"; contentType = model.ReturnJson ? JSON_MIME_TYPE : XML_MIME_TYPE; if (model.ExportFormat == ExportFormats.FHIR_Build_Package) { fileExtension = "zip"; contentType = ZIP_MIME_TYPE; } fileName = string.Format("{0}.{1}", ig.GetDisplayName(true), fileExtension); var pluginExporter = igTypePlugin.GetExporter(); export = pluginExporter.Export(this.tdb, schema, model.ExportFormat, igSettings, model.SelectedCategories, templates, model.IncludeVocabulary, model.ReturnJson); break; case ExportFormats.Snapshot_JSON: ImplementationGuideController ctrl = new ImplementationGuideController(this.tdb); var dataModel = ctrl.GetViewData(model.ImplementationGuideId, null, null, true); // Serialize the data to JSON var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); jsonSerializer.MaxJsonLength = Int32.MaxValue; export = System.Text.Encoding.UTF8.GetBytes(jsonSerializer.Serialize(dataModel)); // Set the filename to JSON fileName = string.Format("{0}.json", ig.GetDisplayName(true)); contentType = JSON_MIME_TYPE; break; case ExportFormats.Microsoft_Word_DOCX: ImplementationGuideGenerator generator = new ImplementationGuideGenerator(this.tdb, model.ImplementationGuideId, model.TemplateIds); fileName = string.Format("{0}.docx", ig.GetDisplayName(true)); ExportSettings lConfig = new ExportSettings(); lConfig.Use(c => { c.GenerateTemplateConstraintTable = model.TemplateTables == TemplateTableOptions.ConstraintOverview || model.TemplateTables == TemplateTableOptions.Both; c.GenerateTemplateContextTable = model.TemplateTables == TemplateTableOptions.Context || model.TemplateTables == TemplateTableOptions.Both; c.GenerateDocTemplateListTable = model.DocumentTables == DocumentTableOptions.List || model.DocumentTables == DocumentTableOptions.Both; c.GenerateDocContainmentTable = model.DocumentTables == DocumentTableOptions.Containment || model.DocumentTables == DocumentTableOptions.Both; c.AlphaHierarchicalOrder = model.TemplateSortOrder == TemplateSortOrderOptions.AlphaHierarchically; c.DefaultValueSetMaxMembers = model.ValueSetTables ? model.MaximumValueSetMembers : 0; c.GenerateValueSetAppendix = model.ValueSetAppendix; c.IncludeXmlSamples = model.IncludeXmlSample; c.IncludeChangeList = model.IncludeChangeList; c.IncludeTemplateStatus = model.IncludeTemplateStatus; c.IncludeNotes = model.IncludeNotes; c.IncludeVolume1 = model.IncludeVolume1; c.SelectedCategories = model.SelectedCategories; c.GenerateRequiredAndOptionalSectionsTable = model.GenerateRequiredAndOptionalSectionsTable; c.DocumentTemplateTypeId = model.DocumentTemplateTypeId; c.SectionTemplateTypeId = model.SectionTemplateTypeId; }); if (model.ValueSetOid != null && model.ValueSetOid.Count > 0) { Dictionary <string, int> valueSetMemberMaximums = new Dictionary <string, int>(); for (int i = 0; i < model.ValueSetOid.Count; i++) { if (valueSetMemberMaximums.ContainsKey(model.ValueSetOid[i])) { continue; } valueSetMemberMaximums.Add(model.ValueSetOid[i], model.ValueSetMaxMembers[i]); } lConfig.ValueSetMaxMembers = valueSetMemberMaximums; } generator.BuildImplementationGuide(lConfig, ig.ImplementationGuideType.GetPlugin()); export = generator.GetDocument(); contentType = DOCX_MIME_TYPE; break; case ExportFormats.Schematron_SCH: string schematronResult = SchematronGenerator.Generate(tdb, ig, model.IncludeCustomSchematron, VocabularyOutputType.Default, model.VocabularyFileName, templates, model.SelectedCategories, model.DefaultSchematron); export = ASCIIEncoding.UTF8.GetBytes(schematronResult); fileName = string.Format("{0}.sch", ig.GetDisplayName(true)); contentType = XML_MIME_TYPE; if (model.IncludeVocabulary) { using (ZipFile zip = new ZipFile()) { zip.AddEntry(fileName, export); NativeTerminologyExporter nativeTermExporter = new NativeTerminologyExporter(this.tdb); byte[] vocData = nativeTermExporter.GetExport(ig.Id, model.MaximumValueSetMembers, this.GetExportEncoding(model)); string vocFileName = string.Format("{0}", model.VocabularyFileName); //Ensuring the extension is present in case input doesn't have it if (vocFileName.IndexOf(".xml") == -1) { vocFileName += ".xml"; } zip.AddEntry(vocFileName, vocData); using (MemoryStream ms = new MemoryStream()) { zip.Save(ms); fileName = string.Format("{0}.zip", ig.GetDisplayName(true)); contentType = ZIP_MIME_TYPE; export = ms.ToArray(); } } } break; case ExportFormats.Vocabulary_XLSX: fileName = string.Format("{0}.xlsx", ig.GetDisplayName(true)); contentType = XLSX_MIME_TYPE; ExcelExporter excelExporter = new ExcelExporter(this.tdb); export = excelExporter.GetSpreadsheet(ig.Id, model.MaximumValueSetMembers); break; case ExportFormats.Vocbulary_Single_SVS_XML: fileName = string.Format("{0}_SingleSVS.xml", ig.GetDisplayName(true)); contentType = XML_MIME_TYPE; SingleSVSExporter ssvsExporter = new SingleSVSExporter(this.tdb); export = ssvsExporter.GetExport(ig.Id, model.MaximumValueSetMembers, this.GetExportEncoding(model)); break; case ExportFormats.Vocabulary_Multiple_SVS_XML: fileName = string.Format("{0}_MultipleSVS.xml", ig.GetDisplayName(true)); contentType = XML_MIME_TYPE; MultipleSVSExporter msvsExporter = new MultipleSVSExporter(this.tdb); export = msvsExporter.GetExport(ig.Id, model.MaximumValueSetMembers, this.GetExportEncoding(model)); break; case ExportFormats.Vocabulary_Native_XML: fileName = string.Format("{0}_MultipleSVS.xml", ig.GetDisplayName(true)); contentType = XML_MIME_TYPE; NativeTerminologyExporter nativeExporter = new NativeTerminologyExporter(this.tdb); export = nativeExporter.GetExport(ig.Id, model.MaximumValueSetMembers, this.GetExportEncoding(model)); break; case ExportFormats.Web_HTML: // Get the data from the API controller HtmlExporter exporter = new HtmlExporter(this.tdb); var templateIds = templates.Select(y => y.Id).ToArray(); var htmlDataModel = exporter.GetExportData(ig.Id, null, templateIds, model.IncludeInferred); IGController igController = new IGController(this.tdb); var downloadPackage = igController.GetDownloadPackage(ig, JsonConvert.SerializeObject(htmlDataModel)); export = downloadPackage.Content; fileName = downloadPackage.FileName; contentType = ZIP_MIME_TYPE; break; case ExportFormats.Vocbulary_Bundle_FHIR_XML: ValueSetExporter vsExporter = new ValueSetExporter(this.tdb); var bundle = vsExporter.GetImplementationGuideValueSets(ig); if (model.ReturnJson) { export = fhir_stu3.Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToJsonBytes(bundle); fileName = string.Format("{0}_fhir_voc.json", ig.GetDisplayName(true)); contentType = JSON_MIME_TYPE; } else { export = fhir_stu3.Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXmlBytes(bundle); fileName = string.Format("{0}_fhir_voc.xml", ig.GetDisplayName(true)); contentType = XML_MIME_TYPE; } break; default: throw new NotSupportedException(); } return(GetExportResponse(fileName, contentType, export)); }