001package org.hl7.fhir.convertors.loaders.loaderR5; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.util.ArrayList; 006import java.util.List; 007import java.util.UUID; 008 009/* 010 Copyright (c) 2011+, HL7, Inc. 011 All rights reserved. 012 013 Redistribution and use in source and binary forms, with or without modification, 014 are permitted provided that the following conditions are met: 015 016 * Redistributions of source code must retain the above copyright notice, this 017 list of conditions and the following disclaimer. 018 * Redistributions in binary form must reproduce the above copyright notice, 019 this list of conditions and the following disclaimer in the documentation 020 and/or other materials provided with the distribution. 021 * Neither the name of HL7 nor the names of its contributors may be used to 022 endorse or promote products derived from this software without specific 023 prior written permission. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 028 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 029 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 030 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 031 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 032 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 033 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 034 POSSIBILITY OF SUCH DAMAGE. 035 036 */ 037 038 039import org.hl7.fhir.exceptions.FHIRException; 040import org.hl7.fhir.r5.formats.JsonParser; 041import org.hl7.fhir.r5.formats.XmlParser; 042import org.hl7.fhir.r5.model.Bundle; 043import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; 044import org.hl7.fhir.r5.model.Bundle.BundleType; 045import org.hl7.fhir.r5.model.CanonicalResource; 046import org.hl7.fhir.r5.model.CodeSystem; 047import org.hl7.fhir.r5.model.Resource; 048import org.hl7.fhir.r5.model.StructureDefinition; 049import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 050 051public class R5ToR5Loader extends BaseLoaderR5 { 052 053 public R5ToR5Loader(List<String> types, ILoaderKnowledgeProviderR5 lkp) { 054 super(types, lkp); 055 } 056 057 @Override 058 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 059 Resource r5 = null; 060 if (isJson) 061 r5 = new JsonParser().parse(stream); 062 else 063 r5 = new XmlParser().parse(stream); 064 065 Bundle b; 066 if (r5 instanceof Bundle) 067 b = (Bundle) r5; 068 else { 069 b = new Bundle(); 070 b.setId(UUID.randomUUID().toString().toLowerCase()); 071 b.setType(BundleType.COLLECTION); 072 b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null); 073 } 074 if (killPrimitives) { 075 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 076 for (BundleEntryComponent be : b.getEntry()) { 077 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 078 StructureDefinition sd = (StructureDefinition) be.getResource(); 079 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 080 remove.add(be); 081 } 082 } 083 b.getEntry().removeAll(remove); 084 } 085 if (patchUrls) { 086 for (BundleEntryComponent be : b.getEntry()) { 087 if (be.hasResource()) { 088 doPatchUrls(be.getResource()); 089 } 090 } 091 } 092 return b; 093 } 094 095 @Override 096 public Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException { 097 Resource r5 = null; 098 if (isJson) 099 r5 = new JsonParser().parse(stream); 100 else 101 r5 = new XmlParser().parse(stream); 102 setPath(r5); 103 104 if (killPrimitives) { 105 throw new FHIRException("Cannot kill primitives when using deferred loading"); 106 } 107 if (patchUrls) { 108 doPatchUrls(r5); 109 } 110 return r5; 111 } 112 113 @Override 114 public List<CodeSystem> getCodeSystems() { 115 return new ArrayList<>(); 116 } 117 118 @Override 119 protected String versionString() { 120 return "5.0"; 121 } 122 123 124}