001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.parser; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.parser.json.BaseJsonLikeValue.ScalarType; 025import ca.uhn.fhir.parser.json.BaseJsonLikeValue.ValueType; 026import ca.uhn.fhir.util.UrlUtil; 027 028/** 029 * Parser error handler which throws a {@link DataFormatException} any time an 030 * issue is found while parsing. 031 * 032 * @see IParser#setParserErrorHandler(IParserErrorHandler) 033 * @see FhirContext#setParserErrorHandler(IParserErrorHandler) 034 */ 035public class StrictErrorHandler extends ParseErrorHandler implements IParserErrorHandler { 036 037 @Override 038 public void containedResourceWithNoId(IParseLocation theLocation) { 039 throw new DataFormatException(Msg.code(1819) + "Resource has contained child resource with no ID"); 040 } 041 042 @Override 043 public void incorrectJsonType(IParseLocation theLocation, String theElementName, ValueType theExpected, ScalarType theExpectedScalarType, ValueType theFound, ScalarType theFoundScalarType) { 044 String message = LenientErrorHandler.createIncorrectJsonTypeMessage(theElementName, theExpected, theExpectedScalarType, theFound, theFoundScalarType); 045 throw new DataFormatException(Msg.code(1820) + message); 046 } 047 048 @Override 049 public void invalidValue(IParseLocation theLocation, String theValue, String theError) { 050 throw new DataFormatException(Msg.code(1821) + describeLocation(theLocation) + "Invalid attribute value \"" + UrlUtil.sanitizeUrlPart(theValue) + "\": " + theError); 051 } 052 053 @Override 054 public void missingRequiredElement(IParseLocation theLocation, String theElementName) { 055 StringBuilder b = new StringBuilder(); 056 b.append("Resource is missing required element '"); 057 b.append(theElementName); 058 b.append("'"); 059 if (theLocation != null) { 060 b.append(" in parent element '"); 061 b.append(theLocation.getParentElementName()); 062 b.append("'"); 063 } 064 throw new DataFormatException(Msg.code(1822) + b.toString()); 065 } 066 067 @Override 068 public void unexpectedRepeatingElement(IParseLocation theLocation, String theElementName) { 069 throw new DataFormatException(Msg.code(1823) + describeLocation(theLocation) + "Multiple repetitions of non-repeatable element '" + theElementName + "' found during parse"); 070 } 071 072 @Override 073 public void unknownAttribute(IParseLocation theLocation, String theAttributeName) { 074 throw new DataFormatException(Msg.code(1824) + describeLocation(theLocation) + "Unknown attribute '" + theAttributeName + "' found during parse"); 075 } 076 077 @Override 078 public void unknownElement(IParseLocation theLocation, String theElementName) { 079 throw new DataFormatException(Msg.code(1825) + describeLocation(theLocation) + "Unknown element '" + theElementName + "' found during parse"); 080 } 081 082 @Override 083 public void unknownReference(IParseLocation theLocation, String theReference) { 084 throw new DataFormatException(Msg.code(1826) + describeLocation(theLocation) + "Resource has invalid reference: " + theReference); 085 } 086 087 @Override 088 public void extensionContainsValueAndNestedExtensions(IParseLocation theLocation) { 089 throw new DataFormatException(Msg.code(1827) + describeLocation(theLocation) + "Extension contains both a value and nested extensions"); 090 } 091 092}