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.HL7Exception;
030import ca.uhn.hl7v2.HapiContext;
031
032/**
033 * Simple implementation of a handler that just tracks if
034 * {@link #error(ValidationException)} has been called at least once.
035 * 
036 * @author Christian Ohr
037 */
038public class DefaultValidationExceptionHandler extends AbstractValidationExceptionHandler<Boolean> 
039                     implements ValidationExceptionHandlerFactory<Boolean> {
040
041        private boolean result = true;
042
043        public DefaultValidationExceptionHandler() {
044        super(new DefaultHapiContext());
045    }
046        
047    public DefaultValidationExceptionHandler(HapiContext context) {
048        super(context);
049    }
050
051    /**
052         * If the validation process encounters a violation, this method is called.
053         * 
054         * @param exception a {@link ValidationException} of error severity.
055         */
056        public void error(final ValidationException exception) {
057                result = false;
058        }
059
060        /**
061         * Called after the validation process. Should return an overall boolean validation result.
062         * 
063         * @return the overall assessment of the validation process. This method should usually return
064         *         <code>false</code> if {@link #onExceptions(ValidationException[])} has been
065         *         called at least once.
066         * @throws HL7Exception
067         * 
068         * @see {@link DefaultValidationExceptionHandler}
069         */
070        public Boolean result() throws HL7Exception {
071                return result;
072        }
073
074    public boolean hasFailed() {
075        return !result;
076    }
077
078        public ValidationExceptionHandler<Boolean> getNewInstance(HapiContext theContext) {
079                return new DefaultValidationExceptionHandler(theContext);
080        }
081        
082        
083}