001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: *
005: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
006: * (C) 2004, Institut de Recherche pour le Développement
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: */
018: package org.geotools.referencing.wkt;
019:
020: // J2SE dependencies
021: import java.lang.reflect.Modifier;
022:
023: // Geotools dependencies
024: import org.geotools.resources.Utilities;
025: import org.geotools.resources.i18n.ErrorKeys;
026: import org.geotools.resources.i18n.Errors;
027:
028: /**
029: * Thrown by {@link Formattable#toWKT} when an object can't be formatted as WKT.
030: * A formatting may fails because an object is too complex for the WKT format
031: * capability (for example an {@linkplain org.geotools.referencing.crs.DefaultEngineeringCRS
032: * engineering CRS} with different unit for each axis), or because only some specific
033: * implementations can be formatted as WKT.
034: *
035: * @since 2.0
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/wkt/UnformattableObjectException.java $
037: * @version $Id: UnformattableObjectException.java 24701 2007-03-07 22:25:42Z desruisseaux $
038: * @author Martin Desruisseaux
039: *
040: * @see Formatter#setInvalidWKT
041: */
042: public class UnformattableObjectException extends
043: UnsupportedOperationException {
044: /**
045: * The type of the object that can't be formatted.
046: */
047: private final Class unformattable;
048:
049: /**
050: * Constructs an exception with the specified detail message.
051: *
052: * @param message The detail message.
053: *
054: * @deprecated Replaced by {@link #UnformattableObjectException(String, Class)}.
055: */
056: public UnformattableObjectException(final String message) {
057: super (message);
058: unformattable = Object.class;
059: }
060:
061: /**
062: * Constructs an exception with the specified detail message.
063: *
064: * @param message The detail message. If {@code null}, a default message will be created.
065: * @param unformattable The type of the object that can't be formatted.
066: *
067: * @since 2.4
068: */
069: public UnformattableObjectException(final String message,
070: final Class unformattable) {
071: super (message);
072: this .unformattable = unformattable;
073: }
074:
075: /**
076: * Returns the type of the object that can't be formatted. This is often an OpenGIS
077: * interface rather than the implementation class. For example if a engineering CRS
078: * uses different unit for each axis, then this method may return
079: * <code>{@linkplain org.opengis.referencing.crs.CoordinateReferenceSystem}.class</code>.
080: * It doesn't mean that no CRS can be formatted; only that a particular instance of it
081: * can't. Other possible classes are {@link org.opengis.referencing.datum.ImageDatum},
082: * {@link org.opengis.referencing.crs.ProjectedCRS}, <cite>etc</cite>.
083: *
084: * @since 2.4
085: */
086: public Class getUnformattableClass() {
087: return unformattable;
088: }
089:
090: /**
091: * Returns the detail message. A default message is formatted
092: * if none was specified at construction time.
093: */
094: public String getMessage() {
095: String message = super .getMessage();
096: if (message == null) {
097: Class c = unformattable;
098: while (!Modifier.isPublic(c.getModifiers())) {
099: final Class candidate = c.getSuperclass();
100: if (candidate == null) {
101: break;
102: }
103: c = candidate;
104: }
105: return Errors.format(ErrorKeys.INVALID_WKT_FORMAT_$1,
106: Utilities.getShortName(c));
107: }
108: return message;
109: }
110: }
|