Coverage Report - org.geotoolkit.image.io.InvalidImageStoreException
 
Classes in this File Line Coverage Branch Coverage Complexity
InvalidImageStoreException
0 %
0/31
0 %
0/28
5,25
 
 1  
 /*
 2  
  *    Geotoolkit.org - An Open Source Java GIS Toolkit
 3  
  *    http://www.geotoolkit.org
 4  
  *
 5  
  *    (C) 2010-2012, Open Source Geospatial Foundation (OSGeo)
 6  
  *    (C) 2010-2012, Geomatys
 7  
  *
 8  
  *    This library is free software; you can redistribute it and/or
 9  
  *    modify it under the terms of the GNU Lesser General Public
 10  
  *    License as published by the Free Software Foundation;
 11  
  *    version 2.1 of the License.
 12  
  *
 13  
  *    This library is distributed in the hope that it will be useful,
 14  
  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16  
  *    Lesser General Public License for more details.
 17  
  */
 18  
 package org.geotoolkit.image.io;
 19  
 
 20  
 import java.io.File;
 21  
 import java.util.Arrays;
 22  
 import javax.imageio.IIOException;
 23  
 
 24  
 import org.geotoolkit.resources.Errors;
 25  
 import org.apache.sis.util.resources.IndexedResourceBundle;
 26  
 import org.geotoolkit.internal.image.io.Formats;
 27  
 import org.geotoolkit.internal.io.IOUtilities;
 28  
 import org.geotoolkit.util.converter.Classes;
 29  
 
 30  
 
 31  
 /**
 32  
  * Thrown if the {@linkplain javax.imageio.ImageReader#setInput(Object, boolean, boolean) input
 33  
  * given to a reader} or the {@linkplain javax.imageio.ImageWriter#setOutput(Object) output
 34  
  * given to a writer} is invalid. This exception is thrown for errors that can not be detected
 35  
  * at {@code setInput(...)} or {@code setOutput(...)} invocation time, but are rather detected
 36  
  * when the first read or write operation is attempted.
 37  
  * <p>
 38  
  * This exception is used for programmatic errors only. It shall not be used for corrupted files
 39  
  * or connection problems. This exception is used for example by {@link ImageReaderAdapter} and
 40  
  * {@link ImageWriterAdapter} because they defer the initialization of their underlying image
 41  
  * reader or writer until first needed.
 42  
  *
 43  
  * @author Martin Desruisseaux (Geomatys)
 44  
  * @version 3.10
 45  
  *
 46  
  * @since 3.08
 47  
  * @module
 48  
  */
 49  
 public class InvalidImageStoreException extends IIOException {
 50  
     /**
 51  
      * Serial version for compatibility with different versions.
 52  
      */
 53  
     private static final long serialVersionUID = 7306875489752707142L;
 54  
 
 55  
     /**
 56  
      * Constructs a new exception with the specified detail message.
 57  
      * The detail message is saved for later retrieval by the {@link #getMessage()} method.
 58  
      *
 59  
      * @param message The details message.
 60  
      */
 61  
     public InvalidImageStoreException(final String message) {
 62  0
         super(message);
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Constructs a new exception with the specified detail message and cause.
 67  
      * The cause is saved for later retrieval by the {@link #getCause()} method.
 68  
      *
 69  
      * @param message The details message.
 70  
      * @param cause The cause.
 71  
      */
 72  
     public InvalidImageStoreException(final String message, final Throwable cause) {
 73  0
         super(message, cause);
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Builds an error message for an input of unsupported type.
 78  
      *
 79  
      * @param  resources The resource bundle to use for formatting the message, or {@code null} if unknown.
 80  
      * @param  io        The actual input or output object.
 81  
      * @param  expected  The expected input or output types.
 82  
      * @param  write     {@code true} for building a message for write operation, or {@code false}
 83  
      *                   for read operation.
 84  
      *
 85  
      * @since 3.10
 86  
      */
 87  
     InvalidImageStoreException(final IndexedResourceBundle resources, final Object io,
 88  
             final Class<?>[] expected, final boolean write)
 89  
     {
 90  0
         super(unknownType(resources, io, expected, write));
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super()
 95  
      * call in constructors").
 96  
      */
 97  
     private static String unknownType(final IndexedResourceBundle resources, final Object io,
 98  
             final Class<?>[] expected, final boolean write)
 99  
     {
 100  
         /*
 101  
          * In the particular case where the input or output object is a file,
 102  
          * check if the file (read mode) and the parent directory (read/write
 103  
          * mode) exists.
 104  
          */
 105  0
         if (io instanceof File) {
 106  0
             File file = (File) io;
 107  0
             if (write) {
 108  0
                 file = file.getParentFile();
 109  0
                 if (file != null && !file.isDirectory()) {
 110  0
                     return resources.getString(Errors.Keys.NOT_A_DIRECTORY_1, file);
 111  
                 }
 112  0
             } else if (!file.exists()) {
 113  0
                 int key = Errors.Keys.FILE_DOES_NOT_EXIST_1;
 114  0
                 final File parent = file.getParentFile();
 115  0
                 if (parent != null && !parent.isDirectory()) {
 116  0
                     key = Errors.Keys.NOT_A_DIRECTORY_1;
 117  0
                     file = parent;
 118  
                 }
 119  0
                 return resources.getString(key, file);
 120  
             }
 121  
         }
 122  
         /*
 123  
          * If the given input or output was supposed to be a supported type, format an error
 124  
          * message saying that we can't read or write to that input or output object.
 125  
          */
 126  0
         if (expected != null) {
 127  0
             for (final Class<?> e : expected) {
 128  0
                 if (e.isInstance(io)) {
 129  0
                     return resources.getString(
 130  
                             write ? Errors.Keys.CANT_WRITE_FILE_1 : Errors.Keys.CANT_READ_FILE_1,
 131  
                             IOUtilities.name(io));
 132  
                 }
 133  
             }
 134  
         }
 135  
         /*
 136  
          * If the input or output object is not a supported type, format an error
 137  
          * message with the list of expected types.
 138  
          */
 139  0
         String message = resources.getString(Errors.Keys.UNKNOWN_TYPE_1, Classes.getShortClassName(io));
 140  0
         if (expected != null && expected.length != 0) {
 141  0
             String[] names = new String[expected.length];
 142  0
             for (int i=0; i<expected.length; i++) {
 143  0
                 names[i] = Classes.getShortName(expected[i]);
 144  
             }
 145  0
             names = Formats.simplify(names);
 146  0
             message = message + ' ' + resources.getString(
 147  
                     Errors.Keys.EXPECTED_ONE_OF_1, Arrays.toString(names));
 148  
         }
 149  0
         return message;
 150  
     }
 151  
 }