001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage.io;
018:
019: // J2SE dependencies
020: import javax.imageio.IIOException;
021:
022: /**
023: * The base class for error related to grid coverage's metadata.
024: * This exception is thrown by the helper class {@link MetadataBuilder}.
025: *
026: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/coverage/io/MetadataException.java $
027: * @version $Id: MetadataException.java 25699 2007-05-31 15:55:07Z desruisseaux $
028: * @author Martin Desruisseaux
029: *
030: * @since 2.2
031: */
032: public class MetadataException extends IIOException {
033: /**
034: * Serial number for interoperability with different versions.
035: */
036: private static final long serialVersionUID = -3146379152278866037L;
037:
038: /**
039: * The key for the faulty metadata, or {@code null} if none.
040: */
041: private final MetadataBuilder.Key key;
042:
043: /**
044: * The alias used for the metadata, or {@code null} if none.
045: */
046: private final String alias;
047:
048: /**
049: * Constructs an exception with the specified message and no key.
050: *
051: * @param message The message, or {@code null} if none.
052: */
053: public MetadataException(final String message) {
054: super (message);
055: key = null;
056: alias = null;
057: }
058:
059: /**
060: * Constructs an exception with the specified message and exception as its cause.
061: *
062: * @param message The message, or {@code null} if none.
063: * @param cause The cause for this exception.
064: */
065: public MetadataException(final String message, final Throwable cause) {
066: super (message, cause);
067: key = null;
068: alias = null;
069: }
070:
071: /**
072: * Constructs an exception with the specified message. This exception is usually raised because
073: * no value was defined for the key {@code key}, or the value was ambiguous.
074: *
075: * @param message The message, or {@code null} if none.
076: * @param key The metadata key which was the cause for this exception, or {@code null} if
077: * none. This is a format neutral key, for example {@link MetadataBuilder#DATUM}.
078: * @param alias The alias used for for the key {@code key}, or {@code null} if none. This is
079: * usually the name used in the external file parsed.
080: */
081: public MetadataException(final String message,
082: final MetadataBuilder.Key key, final String alias) {
083: super (message);
084: this .key = key;
085: this .alias = alias;
086: }
087:
088: /**
089: * Constructs an exception from the specified cause.
090: *
091: * @param cause The cause for this exception.
092: * @param key The metadata key which was the cause for this exception, or {@code null} if
093: * none. This is a format neutral key, for example {@link MetadataBuilder#DATUM}.
094: * @param alias The alias used for for the key {@code key}, or {@code null} if none. This is
095: * usually the name used in the external file parsed.
096: */
097: public MetadataException(final Exception cause,
098: final MetadataBuilder.Key key, final String alias) {
099: super (cause.getLocalizedMessage(), cause);
100: this .key = key;
101: this .alias = alias;
102: }
103:
104: /**
105: * Returns the metadata key which has raised this exception. This exception has usually
106: * been raised because no value was defined for this key, or the value was ambiguous.
107: *
108: * @return The metadata key, or {@code null} if none.
109: */
110: public MetadataBuilder.Key getMetadataKey() {
111: return key;
112: }
113:
114: /**
115: * Returns the alias used for the key {@link #getMetadataKey}. This is usually the name
116: * used in the external file to be parsed. The alias is format-dependent, while the key
117: * (as returned by {@link #getMetadataKey}) if format neutral.
118: *
119: * @return The alias, or {@code null} if none.
120: */
121: public String getMetadataAlias() {
122: return alias;
123: }
124:
125: /**
126: * Returns a string representation of this exception. This implementation is similar to
127: * {@link Throwable#toString()}, except that the string will includes key and alias names
128: * if they are defined. The localized message, if any, may be written on the next line.
129: * Example:
130: *
131: * <blockquote><pre>
132: * org.geotools.coverage.io.MissingMetadataException(key="YMaximum", alias="ULY"):
133: * Aucune valeur n'est définie pour la propriété "ULY".
134: * </pre></blockquote>
135: */
136: public String toString() {
137: final MetadataBuilder.Key key = getMetadataKey();
138: final String alias = getMetadataAlias();
139: if (key == null && alias == null) {
140: return super .toString();
141: }
142: final StringBuffer buffer = new StringBuffer(getClass()
143: .getName());
144: buffer.append('[');
145: if (key != null) {
146: buffer.append("key=\"").append(key).append('"');
147: if (alias != null) {
148: buffer.append(", ");
149: }
150: }
151: if (alias != null) {
152: buffer.append("alias=\"").append(alias).append('"');
153: }
154: buffer.append(']');
155: final String message = getLocalizedMessage();
156: if (message != null) {
157: buffer.append(':').append(
158: System.getProperty("line.separator", "\n")).append(
159: message);
160: }
161: return buffer.toString();
162: }
163: }
|