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