001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "DefaultValidationExceptionHandler.java".  Description: 
010"Basic implementation of a ValidationExceptionHandler." 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132012.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2.validation;
027
028import ca.uhn.hl7v2.DefaultHapiContext;
029import ca.uhn.hl7v2.HapiContext;
030
031/**
032 * Simple implementation of a handler that just tracks if
033 * {@link #error(ValidationException)} has been called at least once.
034 * 
035 * @author Christian Ohr
036 */
037public class DefaultValidationExceptionHandler extends AbstractValidationExceptionHandler<Boolean> 
038                     implements ValidationExceptionHandlerFactory<Boolean> {
039
040        private boolean result = true;
041
042        public DefaultValidationExceptionHandler() {
043        super(new DefaultHapiContext());
044    }
045        
046    public DefaultValidationExceptionHandler(HapiContext context) {
047        super(context);
048    }
049
050    /**
051         * If the validation process encounters a violation, this method is called.
052         * 
053         * @param exception a {@link ValidationException} of error severity.
054         */
055        public void error(final ValidationException exception) {
056                result = false;
057        }
058
059        /**
060         * Called after the validation process. Should return an overall boolean validation result.
061         * 
062         * @return the overall assessment of the validation process. This method should usually return
063         *         <code>false</code> if {@link #onExceptions(ValidationException[])} has been
064         *         called at least once.
065         * @see {@link DefaultValidationExceptionHandler}
066         */
067        public Boolean result() {
068                return result;
069        }
070
071    public boolean hasFailed() {
072        return !result;
073    }
074
075        public ValidationExceptionHandler<Boolean> getNewInstance(HapiContext theContext) {
076                return new DefaultValidationExceptionHandler(theContext);
077        }
078        
079        
080}