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