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