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