View Javadoc
1   package ca.uhn.fhir.parser;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import ca.uhn.fhir.parser.json.JsonLikeValue.ScalarType;
5   import ca.uhn.fhir.parser.json.JsonLikeValue.ValueType;
6   
7   /*
8    * #%L
9    * HAPI FHIR - Core Library
10   * %%
11   * Copyright (C) 2014 - 2018 University Health Network
12   * %%
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   * 
17   * http://www.apache.org/licenses/LICENSE-2.0
18   * 
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   * #L%
25   */
26  
27  /**
28   * Parser error handler which throws a {@link DataFormatException} any time an
29   * issue is found while parsing.
30   * 
31   * @see IParser#setParserErrorHandler(IParserErrorHandler)
32   * @see FhirContext#setParserErrorHandler(IParserErrorHandler)
33   */
34  public class StrictErrorHandler implements IParserErrorHandler {
35  
36  	@Override
37  	public void containedResourceWithNoId(IParseLocation theLocation) {
38  		throw new DataFormatException("Resource has contained child resource with no ID");
39  	}
40  
41  	@Override
42  	public void incorrectJsonType(IParseLocation theLocation, String theElementName, ValueType theExpected, ScalarType theExpectedScalarType, ValueType theFound, ScalarType theFoundScalarType) {
43  		String message = LenientErrorHandler.createIncorrectJsonTypeMessage(theElementName, theExpected, theExpectedScalarType, theFound, theFoundScalarType);
44  		throw new DataFormatException(message);
45  	}
46  
47  	@Override
48  	public void invalidValue(IParseLocation theLocation, String theValue, String theError) {
49  		throw new DataFormatException("Invalid attribute value \"" + theValue + "\": " + theError);
50  	}
51  
52  	@Override
53  	public void missingRequiredElement(IParseLocation theLocation, String theElementName) {
54  		StringBuilder b = new StringBuilder();
55  		b.append("Resource is missing required element '");
56  		b.append(theElementName);
57  		b.append("'");
58  		if (theLocation != null) {
59  			b.append(" in parent element '");
60  			b.append(theLocation.getParentElementName());
61  			b.append("'");
62  		}
63  		throw new DataFormatException(b.toString());
64  	}
65  
66  	@Override
67  	public void unexpectedRepeatingElement(IParseLocation theLocation, String theElementName) {
68  		throw new DataFormatException("Multiple repetitions of non-repeatable element '" + theElementName + "' found during parse");
69  	}
70  
71  	@Override
72  	public void unknownAttribute(IParseLocation theLocation, String theAttributeName) {
73  		throw new DataFormatException("Unknown attribute '" + theAttributeName + "' found during parse");
74  	}
75  
76  	@Override
77  	public void unknownElement(IParseLocation theLocation, String theElementName) {
78  		throw new DataFormatException("Unknown element '" + theElementName + "' found during parse");
79  	}
80  
81  	@Override
82  	public void unknownReference(IParseLocation theLocation, String theReference) {
83  		throw new DataFormatException("Resource has invalid reference: " + theReference);
84  	}
85  
86  }