001package org.hl7.fhir.convertors.misc; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033import org.hl7.fhir.dstu3.formats.IParser.OutputStyle; 034import org.hl7.fhir.dstu3.formats.XmlParser; 035import org.hl7.fhir.dstu3.model.*; 036import org.hl7.fhir.dstu3.model.CodeSystem.CodeSystemHierarchyMeaning; 037import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent; 038import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; 039import org.hl7.fhir.dstu3.terminologies.CodeSystemUtilities; 040import org.hl7.fhir.exceptions.FHIRFormatError; 041import org.hl7.fhir.utilities.xml.XMLUtil; 042import org.w3c.dom.Document; 043import org.w3c.dom.Element; 044 045import javax.xml.parsers.DocumentBuilder; 046import javax.xml.parsers.DocumentBuilderFactory; 047import java.io.FileInputStream; 048import java.io.FileOutputStream; 049import java.util.ArrayList; 050import java.util.HashMap; 051import java.util.List; 052import java.util.Map; 053 054 055/** 056 * This is defined as a prototype ClaML importer 057 * 058 * @author Grahame 059 */ 060 061public class ICPC2Importer { 062 063 private String sourceFileName; // the ICPC2 ClaML file 064 private String targetFileNameVS; // the value set to produce 065 private String targetFileNameCS; // the value set to produce 066 067 public ICPC2Importer() { 068 super(); 069 } 070 071 public ICPC2Importer(String sourceFileName, String targetFileNameCS, String targetFileNameVS) { 072 super(); 073 this.sourceFileName = sourceFileName; 074 this.targetFileNameCS = targetFileNameCS; 075 this.targetFileNameVS = targetFileNameVS; 076 } 077 078 public static void main(String[] args) { 079 try { 080 ICPC2Importer r = new ICPC2Importer(); 081 r.setSourceFileName("c:\\temp\\ICPC-2e-v5.0.xml"); 082 r.setTargetFileNameCS("C:\\temp\\icpc2.xml"); 083 r.setTargetFileNameVS("C:\\temp\\icpc2-vs.xml"); 084 r.go(); 085 System.out.println("Completed OK"); 086 } catch (Exception e) { 087 e.printStackTrace(); 088 } 089 } 090 091 public String getSourceFileName() { 092 return sourceFileName; 093 } 094 095 public void setSourceFileName(String sourceFileName) { 096 this.sourceFileName = sourceFileName; 097 } 098 099 public String getTargetFileNameCS() { 100 return targetFileNameCS; 101 } 102 103 public void setTargetFileNameCS(String targetFileName) { 104 this.targetFileNameCS = targetFileName; 105 } 106 107 public String getTargetFileNameVS() { 108 return targetFileNameVS; 109 } 110 111 public void setTargetFileNameVS(String targetFileName) { 112 this.targetFileNameVS = targetFileName; 113 } 114 115 public void go() throws Exception { 116 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 117 factory.setNamespaceAware(false); 118 DocumentBuilder builder = factory.newDocumentBuilder(); 119 Document doc = builder.parse(new FileInputStream(sourceFileName)); 120 121 ValueSet vs = new ValueSet(); 122 vs.setUrl("http://hl7.org/fhir/sid/icpc2/vs"); 123 Element title = XMLUtil.getNamedChild(doc.getDocumentElement(), "Title"); 124 vs.setVersion(title.getAttribute("version")); 125 vs.setName(title.getAttribute("name")); 126 vs.setImmutable(true); 127 Element identifier = XMLUtil.getNamedChild(doc.getDocumentElement(), "Identifier"); 128 vs.setPublisher(identifier.getAttribute("authority")); 129 vs.addIdentifier(new Identifier().setValue(identifier.getAttribute("uid"))); 130 List<Element> authors = new ArrayList<Element>(); 131 XMLUtil.getNamedChildren(XMLUtil.getNamedChild(doc.getDocumentElement(), "Authors"), "Author", authors); 132 for (Element a : authors) 133 if (!a.getAttribute("name").contains("+")) 134 vs.addContact().setName(a.getTextContent()); 135 vs.setCopyright("The copyright of ICPC, both in hard copy and in electronic form, is owned by Wonca. See http://www.kith.no/templates/kith_WebPage____1110.aspx"); 136 vs.setStatus(PublicationStatus.ACTIVE); 137 vs.setDateElement(new DateTimeType(title.getAttribute("date"))); 138 139 vs.getCompose().addInclude().setSystem("http://hl7.org/fhir/sid/icpc2"); 140 CodeSystem cs = new CodeSystem(); 141 cs.setUrl("http://hl7.org/fhir/sid/icpc2"); 142 cs.setVersion(title.getAttribute("version")); 143 cs.setName(title.getAttribute("name")); 144 identifier = XMLUtil.getNamedChild(doc.getDocumentElement(), "Identifier"); 145 cs.setPublisher(identifier.getAttribute("authority")); 146 cs.setIdentifier(new Identifier().setValue(identifier.getAttribute("uid"))); 147 cs.setHierarchyMeaning(CodeSystemHierarchyMeaning.CLASSIFIEDWITH); 148 authors = new ArrayList<Element>(); 149 XMLUtil.getNamedChildren(XMLUtil.getNamedChild(doc.getDocumentElement(), "Authors"), "Author", authors); 150 for (Element a : authors) 151 if (!a.getAttribute("name").contains("+")) 152 cs.addContact().setName(a.getTextContent()); 153 cs.setCopyright("The copyright of ICPC, both in hard copy and in electronic form, is owned by Wonca. See http://www.kith.no/templates/kith_WebPage____1110.aspx"); 154 cs.setStatus(PublicationStatus.ACTIVE); 155 cs.setDateElement(new DateTimeType(title.getAttribute("date"))); 156 cs.setValueSet(vs.getUrl()); 157 158 Map<String, ConceptDefinitionComponent> concepts = new HashMap<String, ConceptDefinitionComponent>(); 159 List<Element> classes = new ArrayList<Element>(); 160 XMLUtil.getNamedChildren(doc.getDocumentElement(), "Class", classes); 161 for (Element cls : classes) { 162 processClass(cls, concepts, cs); 163 } 164 165 XmlParser xml = new XmlParser(); 166 xml.setOutputStyle(OutputStyle.PRETTY); 167 xml.compose(new FileOutputStream(targetFileNameVS), vs); 168 xml.compose(new FileOutputStream(targetFileNameCS), cs); 169 } 170 171 private void processClass(Element cls, Map<String, ConceptDefinitionComponent> concepts, CodeSystem define) throws FHIRFormatError { 172 ConceptDefinitionComponent concept = new ConceptDefinitionComponent(); 173 concept.setCode(cls.getAttribute("code")); 174 concept.setDefinition(getRubric(cls, "preferred")); 175 String s = getRubric(cls, "shortTitle"); 176 if (s != null && !s.equals(concept.getDefinition())) 177 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("shortTitle")).setValue(s); 178 s = getRubric(cls, "inclusion"); 179 if (s != null) 180 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("inclusion")).setValue(s); 181 s = getRubric(cls, "exclusion"); 182 if (s != null) 183 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("exclusion")).setValue(s); 184 s = getRubric(cls, "criteria"); 185 if (s != null) 186 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("criteria")).setValue(s); 187 s = getRubric(cls, "consider"); 188 if (s != null) 189 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("consider")).setValue(s); 190 s = getRubric(cls, "note"); 191 if (s != null) 192 concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("note")).setValue(s); 193 194 concepts.put(concept.getCode(), concept); 195 List<Element> children = new ArrayList<Element>(); 196 XMLUtil.getNamedChildren(cls, "SubClass", children); 197 if (children.size() > 0) 198 CodeSystemUtilities.setNotSelectable(define, concept); 199 200 Element parent = XMLUtil.getNamedChild(cls, "SuperClass"); 201 if (parent == null) { 202 define.addConcept(concept); 203 } else { 204 ConceptDefinitionComponent p = concepts.get(parent.getAttribute("code")); 205 p.getConcept().add(concept); 206 } 207 } 208 209 private String getRubric(Element cls, String kind) { 210 List<Element> rubrics = new ArrayList<Element>(); 211 XMLUtil.getNamedChildren(cls, "Rubric", rubrics); 212 for (Element r : rubrics) { 213 if (r.getAttribute("kind").equals(kind)) 214 return XMLUtil.getNamedChild(r, "Label").getTextContent(); 215 } 216 return null; 217 } 218 219}