001package org.hl7.fhir.dstu2016may.utils; 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 033 034import java.io.File; 035import java.io.FileInputStream; 036import java.io.FileOutputStream; 037import java.util.ArrayList; 038import java.util.HashMap; 039import java.util.List; 040 041import org.hl7.fhir.dstu2016may.formats.IParser.OutputStyle; 042import org.hl7.fhir.dstu2016may.formats.XmlParser; 043import org.hl7.fhir.dstu2016may.metamodel.Element; 044import org.hl7.fhir.dstu2016may.metamodel.Manager; 045import org.hl7.fhir.dstu2016may.metamodel.Manager.FhirFormat; 046import org.hl7.fhir.dstu2016may.model.Bundle; 047import org.hl7.fhir.dstu2016may.model.StructureMap; 048import org.hl7.fhir.utilities.TextFile; 049import org.hl7.fhir.utilities.Utilities; 050 051public class Transformer { 052 053 private String txServer; 054 private String definitions; 055 private List<String> folders = new ArrayList<String>(); 056 private String source; 057 private String mapUri; 058 private String output; 059 private String message; 060 private StructureMapUtilities scu; 061 062 public String getTxServer() { 063 return txServer; 064 } 065 public void setTxServer(String txServer) { 066 this.txServer = txServer; 067 } 068 public String getDefinitions() { 069 return definitions; 070 } 071 public void setDefinitions(String definitions) { 072 this.definitions = definitions; 073 } 074 public List<String> getFolders() { 075 return folders; 076 } 077 078 public void addFolder(String value) { 079 folders.add(value); 080 } 081 082 public String getSource() { 083 return source; 084 } 085 public void setSource(String source) { 086 this.source = source; 087 } 088 public String getOutput() { 089 return output; 090 } 091 public void setOutput(String output) { 092 this.output = output; 093 } 094 095 096 097 098 099 public String getMapUri() { 100 return mapUri; 101 } 102 public void setMapUri(String mapUri) { 103 this.mapUri = mapUri; 104 } 105 public boolean process() { 106 try { 107 System.out.println(" .. load definitions from "+definitions); 108 IWorkerContext context = SimpleWorkerContext.fromPack(definitions); 109 scu = new StructureMapUtilities(context, new HashMap<String, StructureMap>(), null); 110 111 for (String folder : folders) { 112 System.out.println(" .. load additional definitions from "+folder); 113 ((SimpleWorkerContext) context).loadFromFolder(folder); 114 loadMaps(folder); 115 } 116 System.out.println(" .. load source from "+source); 117 Element e = Manager.parse(context, new FileInputStream(source), FhirFormat.XML); 118 119 Bundle bundle = new Bundle(); 120 StructureMap map = scu.getLibrary().get(mapUri); 121 if (map == null) 122 throw new Error("Unable to find map "+mapUri+" (Known Maps = "+Utilities.listCanonicalUrls(scu.getLibrary().keySet())+")"); 123 scu.transform(null, e, map, bundle); 124 new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(output), bundle); 125 return true; 126 } catch (Exception e) { 127 e.printStackTrace(); 128 message = e.getMessage(); 129 return false; 130 } 131 } 132 133 private void loadMaps(String folder) { 134 for (String f : new File(folder).list()) { 135 try { 136 StructureMap map = scu.parse(TextFile.fileToString(Utilities.path(folder, f))); 137 scu.getLibrary().put(map.getUrl(), map); 138 } catch (Exception e) { 139 } 140 } 141 142 } 143 public String getMessage() { 144 return message; 145 } 146 147 148}