001package org.hl7.fhir.dstu2016may.metamodel; 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.InputStream; 035import java.io.OutputStream; 036import java.util.ArrayList; 037import java.util.List; 038 039import org.hl7.fhir.dstu2016may.formats.FormatUtilities; 040import org.hl7.fhir.dstu2016may.formats.IParser.OutputStyle; 041import org.hl7.fhir.dstu2016may.model.ElementDefinition; 042import org.hl7.fhir.dstu2016may.model.ElementDefinition.PropertyRepresentation; 043import org.hl7.fhir.dstu2016may.model.ElementDefinition.TypeRefComponent; 044import org.hl7.fhir.dstu2016may.model.StructureDefinition; 045import org.hl7.fhir.dstu2016may.utils.IWorkerContext; 046import org.hl7.fhir.dstu2016may.utils.ProfileUtilities; 047import org.hl7.fhir.dstu2016may.utils.ToolingExtensions; 048import org.hl7.fhir.exceptions.DefinitionException; 049import org.hl7.fhir.exceptions.FHIRFormatError; 050import org.hl7.fhir.utilities.Utilities; 051import org.hl7.fhir.utilities.validation.ValidationMessage; 052import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity; 053import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType; 054import org.hl7.fhir.utilities.validation.ValidationMessage.Source; 055 056public abstract class ParserBase { 057 058 interface IErrorNotifier { 059 060 } 061 public enum ValidationPolicy { NONE, QUICK, EVERYTHING } 062 063 public static boolean isPrimitive(String code) { 064 return Utilities.existsInList(code, 065 "xhtml", "boolean", "integer", "string", "decimal", "uri", "base64Binary", "instant", "date", "dateTime", 066 "time", "code", "oid", "id", "markdown", "unsignedInt", "positiveInt", "xhtml", "base64Binary"); 067 } 068 069 protected IWorkerContext context; 070 protected ValidationPolicy policy; 071 protected List<ValidationMessage> errors; 072 073 public ParserBase(IWorkerContext context) { 074 super(); 075 this.context = context; 076 policy = ValidationPolicy.NONE; 077 } 078 079 public void setupValidation(ValidationPolicy policy, List<ValidationMessage> errors) { 080 this.policy = policy; 081 this.errors = errors; 082 } 083 084 public abstract Element parse(InputStream stream) throws Exception; 085 086 public abstract void compose(Element e, OutputStream destination, OutputStyle style, String base) throws Exception; 087 088 089 public void logError(int line, int col, String path, IssueType type, String message, IssueSeverity level) throws FHIRFormatError { 090 if (policy == ValidationPolicy.EVERYTHING) { 091 ValidationMessage msg = new ValidationMessage(Source.InstanceValidator, type, line, col, path, message, level); 092 errors.add(msg); 093 } else if (level == IssueSeverity.FATAL || (level == IssueSeverity.ERROR && policy == ValidationPolicy.QUICK)) 094 throw new FHIRFormatError(message+String.format(" at line %d col %d", line, col)); 095 } 096 097 098 protected StructureDefinition getDefinition(int line, int col, String ns, String name) throws FHIRFormatError { 099 if (ns == null) { 100 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no namespace)", IssueSeverity.FATAL); 101 return null; 102 } 103 if (name == null) { 104 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no name)", IssueSeverity.FATAL); 105 return null; 106 } 107 for (StructureDefinition sd : context.allStructures()) { 108 if (name.equals(sd.getIdElement().getIdPart())) { 109 if((ns == null || ns.equals(FormatUtilities.FHIR_NS)) && !ToolingExtensions.hasExtension(sd, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) 110 return sd; 111 String sns = ToolingExtensions.readStringExtension(sd, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"); 112 if (ns != null && ns.equals(sns)) 113 return sd; 114 } 115 } 116 logError(line, col, name, IssueType.STRUCTURE, "This does not appear to be a FHIR resource (unknown namespace/name '"+ns+"::"+name+"')", IssueSeverity.FATAL); 117 return null; 118 } 119 120 protected StructureDefinition getDefinition(int line, int col, String name) throws FHIRFormatError { 121 if (name == null) { 122 logError(line, col, name, IssueType.STRUCTURE, "This cannot be parsed as a FHIR object (no name)", IssueSeverity.FATAL); 123 return null; 124 } 125 for (StructureDefinition sd : context.allStructures()) { 126 if (name.equals(sd.getIdElement().getIdPart())) { 127 return sd; 128 } 129 } 130 logError(line, col, name, IssueType.STRUCTURE, "This does not appear to be a FHIR resource (unknown name '"+name+"')", IssueSeverity.FATAL); 131 return null; 132 } 133 134 135 protected List<Property> getChildProperties(Property property, String elementName, String statedType) throws DefinitionException { 136 ElementDefinition ed = property.getDefinition(); 137 StructureDefinition sd = property.getStructure(); 138 List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed); 139 if (children.isEmpty()) { 140 // ok, find the right definitions 141 String t = null; 142 if (ed.getType().size() == 1) 143 t = ed.getType().get(0).getCode(); 144 else if (ed.getType().size() == 0) 145 throw new Error("types == 0, and no children found"); 146 else { 147 t = ed.getType().get(0).getCode(); 148 boolean all = true; 149 for (TypeRefComponent tr : ed.getType()) { 150 if (!tr.getCode().equals(t)) { 151 all = false; 152 break; 153 } 154 } 155 if (!all) { 156 // ok, it's polymorphic 157 if (ed.hasRepresentation(PropertyRepresentation.TYPEATTR)) { 158 t = statedType; 159 if (t == null && ToolingExtensions.hasExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaultype")) 160 t = ToolingExtensions.readStringExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaultype"); 161 boolean ok = false; 162 for (TypeRefComponent tr : ed.getType()) 163 if (tr.getCode().equals(t)) 164 ok = true; 165 if (!ok) 166 throw new DefinitionException("Type '"+t+"' is not an acceptable type for '"+elementName+"' on property "+property.getDefinition().getPath()); 167 168 } else { 169 t = elementName.substring(tail(ed.getPath()).length() - 3); 170 if (isPrimitive(lowFirst(t))) 171 t = lowFirst(t); 172 } 173 } 174 } 175 if (!"xhtml".equals(t)) { 176 sd = context.fetchTypeDefinition(t); 177 if (sd == null) 178 throw new DefinitionException("Unable to find class '"+t+"' for name '"+elementName+"' on property "+property.getDefinition().getPath()); 179 children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0)); 180 } 181 } 182 List<Property> properties = new ArrayList<Property>(); 183 for (ElementDefinition child : children) { 184 properties.add(new Property(context, child, sd)); 185 } 186 return properties; 187 } 188 189 private String lowFirst(String t) { 190 return t.substring(0, 1).toLowerCase()+t.substring(1); 191 } 192 193 private String tail(String path) { 194 return path.contains(".") ? path.substring(path.lastIndexOf(".")+1) : path; 195 } 196 197}