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: * Utility class which should never instanciate itself.
067: */
068: private FileUtil() {
069: super ();
070: }
071:
072: /**
073: * Concatenates a file path with the file name. If
074: * necessary it adds a File.separator between the path
075: * and file name. For example "/home" or "/home/" and "clemens" both
076: * become "/home/clemens".<p>
077: *
078: * This method is inspired from the FrIJDE project out
079: * of the gCollins.File.FileTools class.<p>
080: *
081: * FrIJDE Homepage:
082: * http://amber.wpi.edu/~thethe/Document/Besiex/Java/FrIJDE/
083: *
084: * @param sPath_ a directory path. Is not allowed to be null.
085: * @param sFile_ the base name of a file.
086: *
087: * @return sPath_ if sFile_ is empty.
088: */
089: private static String concatPath(String sPath_, String sFile_) {
090: Util.panicIf(sPath_ == null);
091: //System.out.println("ccl.util.FileUtil.concatPath(..).sPath_: --->" + sPath_ + "<---");
092: //System.out.println("ccl.util.FileUtil.concatPath(..).sFile_: " + sFile_);
093:
094: String sRetVal = sPath_;
095:
096: if (!Util.isEmpty(sFile_)) {
097: if (sPath_.length() > 0 && !sPath_.endsWith(File.separator)) {
098: sRetVal += File.separator;
099: }
100:
101: sRetVal += sFile_;
102: }
103:
104: return sRetVal;
105: }
106:
107: /**
108: * Reads a File and returns the content in a String.
109: * CRLF -> LF conversion takes place. This is a convenience method so you don't
110: * need to bother creating a file reader object and closing it after it has
111: * been used.
112: *
113: * @param sFileName_ the name of the file to read.
114: *
115: * @return a string with the content of the file but without
116: * any CR characters.
117: *
118: * @throws FileNotFoundException if file does not exist.
119: * @throws IOException if any file operation fails.
120: */
121: public static String readFile(String sFileName_)
122: throws IOException, FileNotFoundException {
123: StringBuffer sFileContent = new StringBuffer(100000);
124:
125: try {
126: FileReader frIni = new FileReader(sFileName_);
127: if (frIni != null) {
128: BufferedReader brIni = new BufferedReader(frIni);
129: if (brIni != null) {
130: while (brIni.ready()) {
131: String sLine = brIni.readLine();
132: if (sLine == null) {
133: break;
134: }
135: sFileContent.append(sLine).append('\n');
136: }
137: brIni.close();
138: }
139: frIni.close();
140: }
141: } catch (FileNotFoundException fileNotFoundException) {
142: throw new FileNotFoundException("No such file: '"
143: + sFileName_ + "'");
144: }
145:
146: return sFileContent.toString();
147: }
148:
149: /**
150: * @return It's the canonical path of sFileName_.
151: */
152: private static String getAbsoluteFileName(String sFileName_) {
153: String sRetVal = null;
154:
155: try {
156: File pFile = new File(sFileName_);
157: sRetVal = pFile.getCanonicalPath();
158: } catch (Exception e) {
159: return null;
160: }
161:
162: return sRetVal;
163: }
164:
165: /**
166: * This method returns an absolute (canonical)
167: * file name. The difference to getAbsoluteFileName
168: * is that this method uses the system property
169: * "user.dir" instead of the native system's current
170: * directory. This way you get a chance of changing
171: * the current directory inside Java and let your
172: * program reflect that change.
173: */
174: public static String normalizeFileName(String sFile) {
175: return normalizeFileName(sFile, (String) System.getProperties()
176: .get("user.dir"));
177: }
178:
179: /**
180: * This method returns an absolute (canonical)
181: * file name. The difference to getAbsoluteFileName
182: * is that this method uses the system property
183: * sUserDir instead of the native system's current
184: * directory. This way you get a chance of changing
185: * the current directory inside Java and let your
186: * program reflect that change.
187: */
188: private static String normalizeFileName(String sFile,
189: String sUserDir) {
190: sFile = sFile.trim();
191: if (Util.isEmpty(sFile) || sFile.equals(".")) {
192: sFile = sUserDir;
193: } else if (!FileUtil.isAbsolute(sFile)) {
194: sFile = FileUtil.concatPath(sUserDir, sFile);
195: }
196: sFile = FileUtil.getAbsoluteFileName(sFile);
197:
198: return sFile;
199: }
200:
201: /**
202: * Tests if the file represented by this File object is an absolute
203: * pathname. The definition of an absolute pathname is system
204: * dependent. For example, on UNIX, a pathname is absolute if its first
205: * character is the separator character. On Windows
206: * platforms, a pathname is absolute if its first character is an
207: * ASCII '\' or '/', or if it begins with a letter followed by a colon.
208: */
209: private static boolean isAbsolute(String sFileName_) {
210: return new File(sFileName_).isAbsolute();
211: }
212:
213: }
|