001 /* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is the reusable ccl java library 015 * (http://www.kclee.com/clemens/java/ccl/). 016 * 017 * The Initial Developer of the Original Code is 018 * Chr. Clemens Lee. 019 * Portions created by Chr. Clemens Lee are Copyright (C) 2002 020 * Chr. Clemens Lee. All Rights Reserved. 021 * 022 * Contributor(s): Chr. Clemens Lee <clemens@kclee.com> 023 * 024 * Alternatively, the contents of this file may be used under the terms of 025 * either the GNU General Public License Version 2 or later (the "GPL"), or 026 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 027 * in which case the provisions of the GPL or the LGPL are applicable instead 028 * of those above. If you wish to allow use of your version of this file only 029 * under the terms of either the GPL or the LGPL, and not to allow others to 030 * use your version of this file under the terms of the MPL, indicate your 031 * decision by deleting the provisions above and replace them with the notice 032 * and other provisions required by the GPL or the LGPL. If you do not delete 033 * the provisions above, a recipient may use your version of this file under 034 * the terms of any one of the MPL, the GPL or the LGPL. 035 * 036 * ***** END LICENSE BLOCK ***** */ 037 038 package net.sourceforge.cobertura.javancss; 039 040 import java.io.BufferedReader; 041 import java.io.File; 042 import java.io.FileNotFoundException; 043 import java.io.FileReader; 044 import java.io.IOException; 045 046 /** 047 * Utility class for file operations.<p> 048 * 049 * Simple but most commonly used methods of this class are:<br> 050 * - {@link #readFile(java.lang.String) readFile}<br> 051 * - {@link #concatPath(java.lang.String, java.lang.String) concatPath}<br> 052 * 053 * Other less frequently used but still handy methods are:<br> 054 * - {@link #normalizeFileName(java.lang.String) normalizeFileName} to take the current user directory into account via the 'user.dir' system property<br> 055 * 056 * @version $Id: FileUtil.java 384 2006-03-17 20:10:49Z thekingant $ 057 * @author <a href="http://www.kclee.com/clemens/"> 058 * Chr. Clemens Lee</a> 059 * <<a href="mailto:clemens@kclee.com"> 060 * clemens@kclee.com 061 * </a>> 062 */ 063 public class FileUtil 064 { 065 066 /** 067 * Utility class which should never instanciate itself. 068 */ 069 private FileUtil() 070 { 071 super(); 072 } 073 074 /** 075 * Concatenates a file path with the file name. If 076 * necessary it adds a File.separator between the path 077 * and file name. For example "/home" or "/home/" and "clemens" both 078 * become "/home/clemens".<p> 079 * 080 * This method is inspired from the FrIJDE project out 081 * of the gCollins.File.FileTools class.<p> 082 * 083 * FrIJDE Homepage: 084 * http://amber.wpi.edu/~thethe/Document/Besiex/Java/FrIJDE/ 085 * 086 * @param sPath_ a directory path. Is not allowed to be null. 087 * @param sFile_ the base name of a file. 088 * 089 * @return sPath_ if sFile_ is empty. 090 */ 091 private static String concatPath(String sPath_, String sFile_) 092 { 093 Util.panicIf(sPath_ == null); 094 //System.out.println("ccl.util.FileUtil.concatPath(..).sPath_: --->" + sPath_ + "<---"); 095 //System.out.println("ccl.util.FileUtil.concatPath(..).sFile_: " + sFile_); 096 097 String sRetVal = sPath_; 098 099 if (!Util.isEmpty(sFile_)) 100 { 101 if (sPath_.length() > 0 && !sPath_.endsWith(File.separator)) 102 { 103 sRetVal += File.separator; 104 } 105 106 sRetVal += sFile_; 107 } 108 109 return sRetVal; 110 } 111 112 /** 113 * Reads a File and returns the content in a String. 114 * CRLF -> LF conversion takes place. This is a convenience method so you don't 115 * need to bother creating a file reader object and closing it after it has 116 * been used. 117 * 118 * @param sFileName_ the name of the file to read. 119 * 120 * @return a string with the content of the file but without 121 * any CR characters. 122 * 123 * @throws FileNotFoundException if file does not exist. 124 * @throws IOException if any file operation fails. 125 */ 126 public static String readFile(String sFileName_) throws IOException, FileNotFoundException 127 { 128 StringBuffer sFileContent = new StringBuffer(100000); 129 130 try 131 { 132 FileReader frIni = new FileReader(sFileName_); 133 if (frIni != null) 134 { 135 BufferedReader brIni = new BufferedReader(frIni); 136 if (brIni != null) 137 { 138 while (brIni.ready()) 139 { 140 String sLine = brIni.readLine(); 141 if (sLine == null) 142 { 143 break; 144 } 145 sFileContent.append(sLine).append('\n'); 146 } 147 brIni.close(); 148 } 149 frIni.close(); 150 } 151 } 152 catch (FileNotFoundException fileNotFoundException) 153 { 154 throw new FileNotFoundException("No such file: '" + sFileName_ + "'"); 155 } 156 157 return sFileContent.toString(); 158 } 159 160 /** 161 * @return It's the canonical path of sFileName_. 162 */ 163 private static String getAbsoluteFileName(String sFileName_) 164 { 165 String sRetVal = null; 166 167 try 168 { 169 File pFile = new File(sFileName_); 170 sRetVal = pFile.getCanonicalPath(); 171 } 172 catch (Exception e) 173 { 174 return null; 175 } 176 177 return sRetVal; 178 } 179 180 /** 181 * This method returns an absolute (canonical) 182 * file name. The difference to getAbsoluteFileName 183 * is that this method uses the system property 184 * "user.dir" instead of the native system's current 185 * directory. This way you get a chance of changing 186 * the current directory inside Java and let your 187 * program reflect that change. 188 */ 189 public static String normalizeFileName(String sFile) 190 { 191 return normalizeFileName(sFile, (String)System.getProperties().get("user.dir")); 192 } 193 194 /** 195 * This method returns an absolute (canonical) 196 * file name. The difference to getAbsoluteFileName 197 * is that this method uses the system property 198 * sUserDir instead of the native system's current 199 * directory. This way you get a chance of changing 200 * the current directory inside Java and let your 201 * program reflect that change. 202 */ 203 private static String normalizeFileName(String sFile, String sUserDir) 204 { 205 sFile = sFile.trim(); 206 if (Util.isEmpty(sFile) || sFile.equals(".")) 207 { 208 sFile = sUserDir; 209 } 210 else if (!FileUtil.isAbsolute(sFile)) 211 { 212 sFile = FileUtil.concatPath(sUserDir, sFile); 213 } 214 sFile = FileUtil.getAbsoluteFileName(sFile); 215 216 return sFile; 217 } 218 219 /** 220 * Tests if the file represented by this File object is an absolute 221 * pathname. The definition of an absolute pathname is system 222 * dependent. For example, on UNIX, a pathname is absolute if its first 223 * character is the separator character. On Windows 224 * platforms, a pathname is absolute if its first character is an 225 * ASCII '\' or '/', or if it begins with a letter followed by a colon. 226 */ 227 private static boolean isAbsolute(String sFileName_) 228 { 229 return new File(sFileName_).isAbsolute(); 230 } 231 232 }