001package org.hl7.fhir.r4.elementmodel; 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 032import java.io.IOException; 033import java.io.InputStream; 034import java.io.OutputStream; 035import java.util.List; 036 037import org.hl7.fhir.exceptions.DefinitionException; 038import org.hl7.fhir.exceptions.FHIRException; 039import org.hl7.fhir.exceptions.FHIRFormatError; 040import org.hl7.fhir.r4.context.IWorkerContext; 041import org.hl7.fhir.r4.formats.FormatUtilities; 042import org.hl7.fhir.r4.formats.IParser.OutputStyle; 043import org.hl7.fhir.r4.model.StructureDefinition; 044import org.hl7.fhir.r4.model.StructureDefinition.TypeDerivationRule; 045import org.hl7.fhir.r4.utils.ToolingExtensions; 046import org.hl7.fhir.utilities.Utilities; 047import org.hl7.fhir.utilities.validation.ValidationMessage; 048import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity; 049import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType; 050import org.hl7.fhir.utilities.validation.ValidationMessage.Source; 051 052public abstract class ParserBase { 053 054 public interface ILinkResolver { 055 String resolveType(String type); 056 057 String resolveProperty(Property property); 058 059 String resolvePage(String string); 060 } 061 062 public enum ValidationPolicy { 063 NONE, QUICK, EVERYTHING 064 } 065 066 public boolean isPrimitive(String code) { 067 return Utilities.existsInList(code, "boolean", "integer", "string", "decimal", "uri", "base64Binary", "instant", 068 "date", "dateTime", "time", "code", "oid", "id", "markdown", "unsignedInt", "positiveInt", "xhtml", "url", 069 "canonical"); 070 071// StructureDefinition sd = context.fetchTypeDefinition(code); 072// return sd != null && sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE; 073 } 074 075 protected IWorkerContext context; 076 protected ValidationPolicy policy; 077 protected List<ValidationMessage> errors; 078 protected ILinkResolver linkResolver; 079 protected boolean showDecorations; 080 081 public ParserBase(IWorkerContext context) { 082 super(); 083 this.context = context; 084 policy = ValidationPolicy.NONE; 085 } 086 087 public void setupValidation(ValidationPolicy policy, List<ValidationMessage> errors) { 088 this.policy = policy; 089 this.errors = errors; 090 } 091 092 public abstract Element parse(InputStream stream) 093 throws IOException, FHIRFormatError, DefinitionException, FHIRException; 094 095 public abstract void compose(Element e, OutputStream destination, OutputStyle style, String base) 096 throws FHIRException, IOException; 097 098 public void logError(int line, int col, String path, IssueType type, String message, IssueSeverity level) 099 throws FHIRFormatError { 100 if (policy == ValidationPolicy.EVERYTHING) { 101 ValidationMessage msg = new ValidationMessage(Source.InstanceValidator, type, line, col, path, message, level); 102 errors.add(msg); 103 } else if (level == IssueSeverity.FATAL || (level == IssueSeverity.ERROR && policy == ValidationPolicy.QUICK)) 104 throw new FHIRFormatError(message + String.format(" at line %d col %d", line, col)); 105 } 106 107 protected StructureDefinition getDefinition(int line, int col, String ns, String name) throws FHIRFormatError { 108 if (ns == null) { 109 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no namespace)", 110 IssueSeverity.FATAL); 111 return null; 112 } 113 if (name == null) { 114 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no name)", 115 IssueSeverity.FATAL); 116 return null; 117 } 118 for (StructureDefinition sd : context.allStructures()) { 119 if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION 120 && !sd.getUrl().startsWith("http://hl7.org/fhir/StructureDefinition/de-")) { 121 if (name.equals(sd.getType()) && (ns == null || ns.equals(FormatUtilities.FHIR_NS)) && !ToolingExtensions 122 .hasExtension(sd, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) 123 return sd; 124 String sns = ToolingExtensions.readStringExtension(sd, 125 "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"); 126 if (name.equals(sd.getType()) && ns != null && ns.equals(sns)) 127 return sd; 128 } 129 } 130 logError(line, col, name, IssueType.STRUCTURE, 131 "This does not appear to be a FHIR resource (unknown namespace/name '" + ns + "::" + name + "')", 132 IssueSeverity.FATAL); 133 return null; 134 } 135 136 protected StructureDefinition getDefinition(int line, int col, String name) throws FHIRFormatError { 137 if (name == null) { 138 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no name)", 139 IssueSeverity.FATAL); 140 return null; 141 } 142 // first pass: only look at base definitions 143 for (StructureDefinition sd : context.getStructures()) { 144 if (sd.getUrl().equals("http://hl7.org/fhir/StructureDefinition/" + name)) { 145 context.generateSnapshot(sd); 146 return sd; 147 } 148 } 149 for (StructureDefinition sd : context.getStructures()) { 150 if (name.equals(sd.getType()) && sd.getDerivation() == TypeDerivationRule.SPECIALIZATION) { 151 context.generateSnapshot(sd); 152 return sd; 153 } 154 } 155 logError(line, col, name, IssueType.STRUCTURE, 156 "This does not appear to be a FHIR resource (unknown name '" + name + "')", IssueSeverity.FATAL); 157 return null; 158 } 159 160 public ILinkResolver getLinkResolver() { 161 return linkResolver; 162 } 163 164 public ParserBase setLinkResolver(ILinkResolver linkResolver) { 165 this.linkResolver = linkResolver; 166 return this; 167 } 168 169 public boolean isShowDecorations() { 170 return showDecorations; 171 } 172 173 public void setShowDecorations(boolean showDecorations) { 174 this.showDecorations = showDecorations; 175 } 176 177}