001    /*
002     * Copyright 1999-2002,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka;
018    
019    /**
020     * This exception is thrown by <em>validators</em> when a validation has failed
021     *
022     * @author Doug Sale
023     * @author dIon Gillard (mainly javadoc)
024     * @version $Revision: 155424 $
025     */
026    public class ValidationException extends Exception {
027        
028        /**
029         * Create a validation exception with a null label and reason
030         */
031        public ValidationException() {
032            this(null, null);
033        }
034    
035        /**
036         * Create a validation exception with the given
037         * label and reason
038         * @param label the user's description of the assertion/test
039         * @param reason the reason it failed
040         */    
041        public ValidationException(String label, String reason) {
042            _label = label;
043            _reason = reason;
044        }
045    
046        /** User's description of the test/assertion */
047        private String _label = null;
048    
049        /** Why the test/assertion failed */
050        private String _reason = null;
051    
052        /**
053         * the message of the exception
054         * @return the message of the exception, built from
055         * the label and reason
056         */    
057        public String getMessage() {
058            StringBuffer buf = new StringBuffer();
059            if (null != getLabel()) {
060                buf.append(getLabel());
061                if (null != getReason()) {
062                    buf.append(": ");
063                }
064            }
065            if (null != getReason()) {
066                buf.append(getReason());
067            }
068            return buf.toString();
069        }
070    
071        /**
072         * User's description of the test/assertion
073         * @return User's description of the test/assertion
074         */
075        public String getLabel() {
076            return _label;
077        }
078    
079        /**
080         * Why the test/assertion failed
081         * @return Why the test/assertion failed
082         */
083        public String getReason() {
084            return _reason;
085        }
086    }