001package ca.uhn.fhir.parser;
002
003import ca.uhn.fhir.context.FhirContext;
004
005/*
006 * #%L
007 * HAPI FHIR - Core Library
008 * %%
009 * Copyright (C) 2014 - 2016 University Health Network
010 * %%
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 *      http://www.apache.org/licenses/LICENSE-2.0
016 * 
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 * #L%
023 */
024
025/**
026 * The default error handler, which logs issues but does not abort parsing
027 * 
028 * @see IParser#setParserErrorHandler(IParserErrorHandler)
029 * @see FhirContext#setParserErrorHandler(IParserErrorHandler)
030 */
031public class LenientErrorHandler implements IParserErrorHandler {
032
033        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LenientErrorHandler.class);
034        private boolean myLogErrors;
035
036        /**
037         * Constructor which configures this handler to log all errors
038         */
039        public LenientErrorHandler() {
040                myLogErrors = true;
041        }
042
043        /**
044         * Constructor
045         * 
046         * @param theLogErrors Should errors be logged?
047         * @since 1.2
048         */
049        public LenientErrorHandler(boolean theLogErrors) {
050                myLogErrors = theLogErrors;
051        }
052
053        @Override
054        public void unknownElement(IParseLocation theLocation, String theElementName) {
055                if (myLogErrors) {
056                        ourLog.warn("Unknown element '{}' found while parsing", theElementName);
057                }
058        }
059
060        @Override
061        public void unknownAttribute(IParseLocation theLocation, String theElementName) {
062                if (myLogErrors) {
063                        ourLog.warn("Unknown attribute '{}' found while parsing", theElementName);
064                }
065        }
066
067        @Override
068        public void unexpectedRepeatingElement(IParseLocation theLocation, String theElementName) {
069                if (myLogErrors) {
070                        ourLog.warn("Multiple repetitions of non-repeatable element '{}' found while parsing", theElementName);
071                }
072        }
073
074}