001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature;
017:
018: /**
019: * Indicates client class has attempted to create an invalid feature.
020: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/api/src/main/java/org/geotools/feature/IllegalAttributeException.java $
021: */
022: public class IllegalAttributeException extends Exception {
023: private static final long serialVersionUID = -4964013824521988182L;
024:
025: /** The expected attribute type. */
026: private final AttributeType expected;
027:
028: /** The object that does not match the expected type. */
029: private final Object invalid;
030:
031: /**
032: * Constructor with message argument.
033: *
034: * @param message Reason for the exception being thrown
035: */
036: public IllegalAttributeException(String message) {
037: super (message);
038: expected = null;
039: invalid = null;
040: }
041:
042: /**
043: * Constructor that makes the message given the expected and invalid.
044: *
045: * @param expected the expected AttributeType.
046: * @param invalid the attribute that does not validate against expected.
047: */
048: public IllegalAttributeException(AttributeType expected,
049: Object invalid) {
050: this (expected, invalid, null);
051: }
052:
053: /**
054: * Constructor that makes the message given the expected and invalid, along
055: * with the root cause.
056: *
057: * @param expected the expected AttributeType.
058: * @param invalid the attribute that does not validate against expected.
059: * @param cause the root cause of the error.
060: */
061: public IllegalAttributeException(AttributeType expected,
062: Object invalid, Throwable cause) {
063: super (errorMessage(expected, invalid), cause);
064: this .expected = expected;
065: this .invalid = invalid;
066: }
067:
068: public String toString() {
069: if ((expected == null) && (invalid == null)) {
070: return super .toString();
071: }
072:
073: String message = "IllegalAttribute: "
074: + ((expected == null) ? "null" : expected.getType()
075: .getName());
076:
077: message += (" , but got " + ((invalid == null) ? "null"
078: : invalid.getClass().getName()));
079:
080: return message;
081: }
082:
083: /**
084: * Constructs an error message based on expected and invalid.
085: *
086: * @param expected the expected AttributeType.
087: * @param invalid the attribute that does not validate against expected.
088: *
089: * @return an error message reporting the problem.
090: */
091: static String errorMessage(AttributeType expected, Object invalid) {
092: String message = "expected "
093: + ((expected == null) ? "null" : expected.getType()
094: .getName());
095: message += (" , but got " + ((invalid == null) ? "null"
096: : invalid.getClass().getName()));
097:
098: return message;
099: }
100: }
|