001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.design;
029:
030: import java.util.Arrays;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import net.sf.jasperreports.engine.JRException;
035:
036: /**
037: * An exception that contains a list of {@link JRValidationFault report validation faults}.
038: *
039: * @author Lucian Chirita (lucianc@users.sourceforge.net)
040: * @version $Id: JRValidationException.java 1625 2007-03-09 17:21:31Z lucianc $
041: * @see JRVerifier
042: */
043: public class JRValidationException extends JRException {
044:
045: private final Collection faults;
046:
047: /**
048: * Creates a validation exception containing a single fault.
049: *
050: * @param message the validation message
051: * @param source the validation source
052: * @see JRValidationFault
053: */
054: public JRValidationException(String message, Object source) {
055: this (createFault(message, source));
056: }
057:
058: private static JRValidationFault createFault(String message,
059: Object source) {
060: JRValidationFault fault = new JRValidationFault();
061: fault.setMessage(message);
062: fault.setSource(source);
063: return fault;
064: }
065:
066: /**
067: * Creates a validation exception containing a single fault.
068: *
069: * @param fault the fault
070: */
071: public JRValidationException(JRValidationFault fault) {
072: this (Arrays.asList(new JRValidationFault[] { fault }));
073: }
074:
075: /**
076: * Create an exception.
077: *
078: * @param faults a list of {@link JRValidationFault validation faults}
079: */
080: public JRValidationException(Collection faults) {
081: super (appendMessages(faults));
082:
083: this .faults = faults;
084: }
085:
086: /**
087: * Returns the list of {@link JRValidationFault validation faults}.
088: *
089: * @return the list of {@link JRValidationFault JRValidationFault} instances.
090: */
091: public Collection getFaults() {
092: return faults;
093: }
094:
095: protected static String appendMessages(Collection faults) {
096: StringBuffer sbuffer = new StringBuffer();
097: sbuffer.append("Report design not valid : ");
098: int i = 1;
099: for (Iterator it = faults.iterator(); it.hasNext(); i++) {
100: JRValidationFault fault = (JRValidationFault) it.next();
101: sbuffer.append("\n\t " + i + ". " + fault.getMessage());
102: }
103: return sbuffer.toString();
104: }
105: }
|