001package ca.uhn.fhir.parser; 002 003import ca.uhn.fhir.context.FhirContext; 004import ca.uhn.fhir.parser.json.JsonLikeValue.ScalarType; 005import ca.uhn.fhir.parser.json.JsonLikeValue.ValueType; 006import ca.uhn.fhir.util.UrlUtil; 007 008/* 009 * #%L 010 * HAPI FHIR - Core Library 011 * %% 012 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 013 * %% 014 * Licensed under the Apache License, Version 2.0 (the "License"); 015 * you may not use this file except in compliance with the License. 016 * You may obtain a copy of the License at 017 * 018 * http://www.apache.org/licenses/LICENSE-2.0 019 * 020 * Unless required by applicable law or agreed to in writing, software 021 * distributed under the License is distributed on an "AS IS" BASIS, 022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 023 * See the License for the specific language governing permissions and 024 * limitations under the License. 025 * #L% 026 */ 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 BaseErrorHandler implements IParserErrorHandler { 036 037 @Override 038 public void containedResourceWithNoId(IParseLocation theLocation) { 039 throw new DataFormatException("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(message); 046 } 047 048 @Override 049 public void invalidValue(IParseLocation theLocation, String theValue, String theError) { 050 throw new DataFormatException(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(b.toString()); 065 } 066 067 @Override 068 public void unexpectedRepeatingElement(IParseLocation theLocation, String theElementName) { 069 throw new DataFormatException(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(describeLocation(theLocation) + "Unknown attribute '" + theAttributeName + "' found during parse"); 075 } 076 077 @Override 078 public void unknownElement(IParseLocation theLocation, String theElementName) { 079 throw new DataFormatException(describeLocation(theLocation) + "Unknown element '" + theElementName + "' found during parse"); 080 } 081 082 @Override 083 public void unknownReference(IParseLocation theLocation, String theReference) { 084 throw new DataFormatException(describeLocation(theLocation) + "Resource has invalid reference: " + theReference); 085 } 086 087 @Override 088 public void extensionContainsValueAndNestedExtensions(IParseLocation theLocation) { 089 throw new DataFormatException(describeLocation(theLocation) + "Extension contains both a value and nested extensions"); 090 } 091 092}