001 /**
002 * $Id: IOExceptionHelper.java,v 1.4 2010/09/06 09:19:11 oboehm Exp $
003 *
004 * Copyright (c) 2009 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 10.06.2009 by oliver (ob@aosd.de)
019 */
020 package patterntesting.exception.io;
021
022 import java.io.*;
023
024 /**
025 * @author <a href="boehm@javatux.de">oliver</a>
026 * @since 10.06.2009
027 */
028 public final class IOExceptionHelper {
029
030 /** No need to instantiate it (utility class). */
031 private IOExceptionHelper() {}
032
033 /**
034 * In contrast to the original IOException the file name will be
035 * appear alway as absolute file name.
036 * The cause of the given IOException is ignored for the moment bcause
037 * the constructor IOException(String s, Throwable t) is not available
038 * in Java 5.
039 *
040 * @param ioe the original IOException
041 * @param file the file
042 * @return a better IOException
043 */
044 public static IOException getBetterIOException(final IOException ioe, final File file) {
045 File parent = file.getAbsoluteFile().getParentFile();
046 if (parent.exists()) {
047 if (file.isAbsolute()) {
048 return ioe;
049 } else {
050 String msg = ioe.getMessage() + ": " + file.getAbsolutePath();
051 return new IOException(msg);
052 }
053 } else {
054 return getDirNotFoundException(ioe, parent);
055 }
056 }
057
058 /**
059 * @param ioe the original IOException
060 * @param filename which caused the IOExeption
061 * @return a better IOException
062 */
063 public static IOException getBetterIOException(final IOException ioe,
064 final String filename) {
065 return getBetterIOException(ioe, new File(filename));
066 }
067
068 /**
069 * If the cause of a FileNotFoundException is a missing directory you will
070 * get a DirNotFoundException as return value.
071 *
072 * @param ioe the original FileNotFoundException
073 * @param file the cause of the FileNotFoundException
074 * @return a FileNotFoundException or a DirNotFoundException
075 */
076 public static FileNotFoundException getBetterFileNotFoundException(
077 final FileNotFoundException ioe, final File file) {
078 File parent = file.getAbsoluteFile().getParentFile();
079 if (!parent.exists()) {
080 return getDirNotFoundException(ioe, parent);
081 }
082 if (file.isAbsolute()) {
083 return ioe;
084 } else {
085 String msg = ioe.getMessage().substring(file.toString().length());
086 return new FileNotFoundException(file.getAbsolutePath() + msg);
087 }
088 }
089
090 /**
091 * At the moment this is only a simple constructor call. But when we
092 * switch to Java6 we will evaluate the cause of the given IOException.
093 *
094 * @param ioe the IOException with the cause
095 * @param dir the directory
096 * @return a DirNotFoundException
097 */
098 public static DirNotFoundException getDirNotFoundException(final IOException ioe,
099 final File dir) {
100 return new DirNotFoundException(dir.getAbsolutePath()
101 + " (No such directory)");
102 }
103
104 }